@@ -43,6 +43,7 @@ package protocol
43
43
44
44
import (
45
45
"bytes"
46
+ "path/filepath"
46
47
"sync"
47
48
48
49
"github.com/cloudwego/hertz/internal/bytesconv"
@@ -318,9 +319,9 @@ func (u *URI) SetHostBytes(host []byte) {
318
319
//
319
320
// Examples:
320
321
//
321
- // * For /foo/bar/baz.html path returns baz.html.
322
- // * For /foo/bar/ returns empty byte slice.
323
- // * For /foobar.js returns foobar.js.
322
+ // - For /foo/bar/baz.html path returns baz.html.
323
+ // - For /foo/bar/ returns empty byte slice.
324
+ // - For /foobar.js returns foobar.js.
324
325
func (u * URI ) LastPathSegment () []byte {
325
326
path := u .Path ()
326
327
n := bytes .LastIndexByte (path , '/' )
@@ -334,14 +335,14 @@ func (u *URI) LastPathSegment() []byte {
334
335
//
335
336
// The following newURI types are accepted:
336
337
//
337
- // * Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
338
- // uri is replaced by newURI.
339
- // * Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
340
- // the original scheme is preserved.
341
- // * Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
342
- // of the original uri is replaced.
343
- // * Relative path, i.e. xx?yy=abc . In this case the original RequestURI
344
- // is updated according to the new relative path.
338
+ // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
339
+ // uri is replaced by newURI.
340
+ // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
341
+ // the original scheme is preserved.
342
+ // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
343
+ // of the original uri is replaced.
344
+ // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
345
+ // is updated according to the new relative path.
345
346
func (u * URI ) Update (newURI string ) {
346
347
u .UpdateBytes (bytesconv .S2b (newURI ))
347
348
}
@@ -350,14 +351,14 @@ func (u *URI) Update(newURI string) {
350
351
//
351
352
// The following newURI types are accepted:
352
353
//
353
- // * Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
354
- // uri is replaced by newURI.
355
- // * Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
356
- // the original scheme is preserved.
357
- // * Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
358
- // of the original uri is replaced.
359
- // * Relative path, i.e. xx?yy=abc . In this case the original RequestURI
360
- // is updated according to the new relative path.
354
+ // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
355
+ // uri is replaced by newURI.
356
+ // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
357
+ // the original scheme is preserved.
358
+ // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
359
+ // of the original uri is replaced.
360
+ // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
361
+ // is updated according to the new relative path.
361
362
func (u * URI ) UpdateBytes (newURI []byte ) {
362
363
u .requestURI = u .updateBytes (newURI , u .requestURI )
363
364
}
@@ -484,6 +485,18 @@ func normalizePath(dst, src []byte) []byte {
484
485
dst = addLeadingSlash (dst , src )
485
486
dst = decodeArgAppendNoPlus (dst , src )
486
487
488
+ // Windows server need to replace all backslashes with
489
+ // forward slashes to avoid path traversal attacks.
490
+ if filepath .Separator == '\\' {
491
+ for {
492
+ n := bytes .IndexByte (dst , '\\' )
493
+ if n < 0 {
494
+ break
495
+ }
496
+ dst [n ] = '/'
497
+ }
498
+ }
499
+
487
500
// remove duplicate slashes
488
501
b := dst
489
502
bSize := len (b )
0 commit comments