Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileserver: Fix file matcher with empty try_files #4147

Merged
merged 2 commits into from
May 4, 2021

Conversation

francislavoie
Copy link
Member

Fixes #4146

If TryFiles is empty, we fill it with r.URL.Path. In this case, this is /. Then later, in prepareFilePath(), we run the replacer (which turns {path} into / at that point) but file remains the original value (and the placeholder is still the placeholder there).

So then strings.HasSuffix(file, "/") will be false for the placeholder, but true for the empty TryFiles codepath, because file was / due to being set to the actual request value beforehand.

This means that suffix becomes // in that case, so after sanitizedPathJoin, it becomes ./, so strictFileExists's strings.HasSuffix(file, separator) codepath will return true.

I think we should change the m.TryFiles == nil codepath to m.TryFiles = []string{"{http.request.uri.path}"} for consistency. (And maybe consider hoisting this to Provision cause there's no point doing this on every request). I don't think this "optimization" of directly using r.URL.Path is so valuable, cause it causes this edgecase with directories.

Fixes #4146

If `TryFiles` is empty, we fill it with `r.URL.Path`. In this case, this is `/`. Then later, in `prepareFilePath()`, we run the replacer (which turns `{path}` into `/` at that point) but `file` remains the original value (and the placeholder is still the placeholder there).

So then `strings.HasSuffix(file, "/")` will be `false` for the placeholder, but `true` for the empty `TryFiles` codepath, because `file` was `/` due to being set to the actual request value beforehand.

This means that `suffix` becomes `//` in that case, so after `sanitizedPathJoin`, it becomes `./`, so `strictFileExists`'s `strings.HasSuffix(file, separator)` codepath will return true.

I think we should change the `m.TryFiles == nil` codepath to `m.TryFiles = []string{"{http.request.uri.path}"}` for consistency. (And maybe consider hoisting this to `Provision` cause there's no point doing this on every request). I don't think this "optimization" of directly using `r.URL.Path` is so valuable, cause it causes this edgecase with directories.
@francislavoie francislavoie added the bug 🐞 Something isn't working label May 4, 2021
@francislavoie francislavoie added this to the v2.4.0 milestone May 4, 2021
@francislavoie francislavoie requested a review from mholt May 4, 2021 03:47
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! What a weird edge case. Just a minor suggestion on the comment.

modules/caddyhttp/fileserver/matcher.go Show resolved Hide resolved
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
@francislavoie francislavoie requested a review from mholt May 4, 2021 14:48
@mholt mholt merged commit 74f5d66 into master May 4, 2021
@mholt mholt deleted the file-matcher-empty-try-files branch May 4, 2021 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

file matcher should not match /
2 participants