0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-30 22:34:15 -05:00

File matcher enforces trailing-slash convention to match dirs/files

This commit is contained in:
Matthew Holt 2019-09-06 13:32:02 -06:00
parent 4bd9496525
commit 97ace2a39e
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -19,6 +19,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strings"
"time" "time"
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
@ -149,7 +150,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
for _, f := range m.TryFiles { for _, f := range m.TryFiles {
suffix := path.Clean(repl.ReplaceAll(f, "")) suffix := path.Clean(repl.ReplaceAll(f, ""))
fullpath := sanitizedPathJoin(root, suffix) fullpath := sanitizedPathJoin(root, suffix)
if fileExists(fullpath) { if strictFileExists(fullpath) {
return suffix, fullpath, true return suffix, fullpath, true
} }
} }
@ -207,22 +208,33 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
return return
} }
// fileExists returns true if file exists, // strictFileExists returns true if file exists
// false if it doesn't, or false if there // and matches the convention of the given file
// was any other error. // path. If the path ends in a forward slash,
func fileExists(file string) bool { // the file must also be a directory; if it does
_, err := os.Stat(file) // NOT end in a forward slash, the file must NOT
if err == nil { // be a directory.
return true func strictFileExists(file string) bool {
} else if os.IsNotExist(err) { stat, err := os.Stat(file)
return false if err != nil {
} else { // in reality, this can be any error
// we don't know if it exists, // such as permission or even obscure
// so assume it doesn't, since // ones like "is not a directory" (when
// there must have been some // trying to stat a file within a file);
// other error anyway // in those cases we can't be sure if
// the file exists, so we just treat any
// error as if it does not exist; see
// https://stackoverflow.com/a/12518877/1048862
return false return false
} }
if strings.HasSuffix(file, "/") {
// by convention, file paths ending
// in a slash must be a directory
return stat.IsDir()
}
// by convention, file paths NOT ending
// in a slash must NOT be a directory
return !stat.IsDir()
} }
const ( const (