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

caddyhttp: properly sanitize requests for root path #6360

Merged
merged 2 commits into from
Jun 2, 2024

Conversation

willnorris
Copy link
Contributor

@willnorris willnorris commented Jun 1, 2024

SanitizePathJoin protects against directory traversal attacks by checking for requests whose URL path look like they are trying to request something other than a local file, and returns the root directory in those cases.

The method is also careful to ensure that requests which contain a trailing slash include a trailing slash in the returned value. However, for requests that contain only a slash (requests for the root path), the IsLocal check returns early before the matching trailing slash is re-added.

This change updates SanitizePathJoin to only perform the filepath.IsLocal check if the cleaned request URL path is non-empty.


This change also updates the existing SanitizePathJoin tests to use filepath.FromSlash rather than filepath.Join. This makes the expected value a little easier to read, but also has the advantage of not being processed by filepath.Clean like filepath.Join is. This means that the exact expect value will be compared, not the result of first cleaning it.

Fixes #6352

@willnorris willnorris force-pushed the will/sanitize branch 2 times, most recently from 14d047f to 6b7b155 Compare June 1, 2024 21:17
SanitizePathJoin protects against directory traversal attacks by
checking for requests whose URL path look like they are trying to
request something other than a local file, and returns the root
directory in those cases.

The method is also careful to ensure that requests which contain a
trailing slash include a trailing slash in the returned value.  However,
for requests that contain only a slash (requests for the root path), the
IsLocal check returns early before the matching trailing slash is
re-added.

This change updates SanitizePathJoin to only perform the
filepath.IsLocal check if the cleaned request URL path is non-empty.

---

This change also updates the existing SanitizePathJoin tests to use
filepath.FromSlash rather than filepath.Join. This makes the expected
value a little easier to read, but also has the advantage of not being
processed by filepath.Clean like filepath.Join is. This means that the
exact expect value will be compared, not the result of first cleaning
it.

Fixes caddyserver#6352
@willnorris
Copy link
Contributor Author

okay, after a little bit of test cleanup, I think this is ready to go now.

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, I scanned this a few times and wondered how you handled the Windows cases but I see you didn't change those 👍

This is quite an elegant and simple fix. I appreciate that! Thanks for the patch :)

@mholt mholt enabled auto-merge (squash) June 2, 2024 03:36
@mholt mholt merged commit f8a2c60 into caddyserver:master Jun 2, 2024
23 checks passed
@willnorris willnorris deleted the will/sanitize branch June 2, 2024 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

the change to SanitizedPathJoin in v2.8.x can cause routing to break in certain configurations
2 participants