@@ -161,7 +161,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
161
161
filesToHide := fsrv .transformHidePaths (repl )
162
162
163
163
root := repl .ReplaceAll (fsrv .Root , "." )
164
- filename := sanitizedPathJoin (root , r .URL .Path )
164
+ filename := caddyhttp . SanitizedPathJoin (root , r .URL .Path )
165
165
166
166
fsrv .logger .Debug ("sanitized path join" ,
167
167
zap .String ("site_root" , root ),
@@ -185,7 +185,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
185
185
var implicitIndexFile bool
186
186
if info .IsDir () && len (fsrv .IndexNames ) > 0 {
187
187
for _ , indexPage := range fsrv .IndexNames {
188
- indexPath := sanitizedPathJoin (filename , indexPage )
188
+ indexPath := caddyhttp . SanitizedPathJoin (filename , indexPage )
189
189
if fileHidden (indexPath , filesToHide ) {
190
190
// pretend this file doesn't exist
191
191
fsrv .logger .Debug ("hiding index file" ,
@@ -435,42 +435,6 @@ func (fsrv *FileServer) transformHidePaths(repl *caddy.Replacer) []string {
435
435
return hide
436
436
}
437
437
438
- // sanitizedPathJoin performs filepath.Join(root, reqPath) that
439
- // is safe against directory traversal attacks. It uses logic
440
- // similar to that in the Go standard library, specifically
441
- // in the implementation of http.Dir. The root is assumed to
442
- // be a trusted path, but reqPath is not.
443
- func sanitizedPathJoin (root , reqPath string ) string {
444
- // TODO: Caddy 1 uses this:
445
- // prevent absolute path access on Windows, e.g. http://localhost:5000/C:\Windows\notepad.exe
446
- // if runtime.GOOS == "windows" && len(reqPath) > 0 && filepath.IsAbs(reqPath[1:]) {
447
- // TODO.
448
- // }
449
-
450
- // TODO: whereas std lib's http.Dir.Open() uses this:
451
- // if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
452
- // return nil, errors.New("http: invalid character in file path")
453
- // }
454
-
455
- // TODO: see https://play.golang.org/p/oh77BiVQFti for another thing to consider
456
-
457
- if root == "" {
458
- root = "."
459
- }
460
-
461
- path := filepath .Join (root , filepath .Clean ("/" + reqPath ))
462
-
463
- // filepath.Join also cleans the path, and cleaning strips
464
- // the trailing slash, so we need to re-add it afterwards.
465
- // if the length is 1, then it's a path to the root,
466
- // and that should return ".", so we don't append the separator.
467
- if strings .HasSuffix (reqPath , "/" ) && len (reqPath ) > 1 {
468
- path += separator
469
- }
470
-
471
- return path
472
- }
473
-
474
438
// fileHidden returns true if filename is hidden according to the hide list.
475
439
// filename must be a relative or absolute file system path, not a request
476
440
// URI path. It is expected that all the paths in the hide list are absolute
0 commit comments