Skip to content

caddyhttp: Sanitize the path before evaluating path matchers - #4407

Merged
mholt merged 1 commit into
masterfrom
path-matching-safety
Nov 8, 2021
Merged

mholt merged 1 commit into
masterfrom
path-matching-safety

Conversation

@francislavoie

@francislavoie francislavoie commented Nov 8, 2021 •

Copy link
Copy Markdown
Member

I found an issue where it's possible to bypass path matchers by crafting a request with a path that doubles up slashes, or uses URL-encoded characters to smuggle the request past the path matcher.

This becomes a security issue, because auth gates using basicauth can be bypassed 😬

The solution is to unescape (convert URL-encoded characters) then clean the path, before matching.

This could be a breaking change if exact behaviour of the matcher is required (without cleaning the path). In that case, then it's possible to use the expression matcher to do a direct string comparison or regexp on the {http.request.uri.path} placeholder (or simply {path} in the Caddyfile). For example:

# Simple prefix match
expression `{path}.startsWith("/foo")`

# Complex regexp match
expression `{path}.matches("^/foo.*$")`

Example of an exploitable situation:

example.com {
	root * /srv

	basicauth /secret-files* {
		bob <hash>
	}

	file_server
}

It's expected that the path matcher /secret-files* will always require bob to be authenticated before they're let through to the file_server. But a request like //secret-files/uh-oh.txt would punch through unmatched, reaching file_server. The file_server handler does clean the path though (collapsing the doubled-up slashes), so it would serve /srv/secret-files/uh-oh.txt back to the client.

@francislavoie francislavoie added the bug 🐞 Something isn't working label Nov 8, 2021
@francislavoie francislavoie added this to the v2.4.6 milestone Nov 8, 2021
@francislavoie
francislavoie requested a review from mholt November 8, 2021 20:28

@mholt mholt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for finding and fixing this. I'm hoping this won't break legitimate use cases, but I'm glad there's a workaround. Will release this today.

@mholt
mholt merged commit e7457b4 into master Nov 8, 2021
@francislavoie
francislavoie deleted the path-matching-safety branch November 8, 2021 20:45
francislavoie added a commit that referenced this pull request Dec 30, 2021
This is a followup to #4407, in response to a report on the forums: https://caddy.community/t/php-fastcgi-phishing-redirection/14542

Turns out that doing `TrimRight` to remove trailing dots, _before_ cleaning the path, will cause double-dots at the end of the path to not be cleaned away as they should. We should instead remove the dots _after_ cleaning.
francislavoie added a commit that referenced this pull request Dec 30, 2021
This is a followup to #4407, in response to a report on the forums: https://caddy.community/t/php-fastcgi-phishing-redirection/14542

Turns out that doing `TrimRight` to remove trailing dots, _before_ cleaning the path, will cause double-dots at the end of the path to not be cleaned away as they should. We should instead remove the dots _after_ cleaning.
@caddyserver caddyserver deleted a comment Jan 6, 2022
@caddyserver caddyserver deleted a comment Jan 6, 2022
nordstern pushed a commit to uptimerobot/caddy that referenced this pull request Jan 24, 2022
This is a followup to caddyserver#4407, in response to a report on the forums: https://caddy.community/t/php-fastcgi-phishing-redirection/14542

Turns out that doing `TrimRight` to remove trailing dots, _before_ cleaning the path, will cause double-dots at the end of the path to not be cleaned away as they should. We should instead remove the dots _after_ cleaning.
mholt pushed a commit that referenced this pull request Aug 15, 2026
…versal (#7941)

* caddyhttp: match url_pattern against decoded, cleaned path

The url_pattern matcher evaluated the raw, percent-encoded request URI
while path-consuming handlers resolve the decoded, cleaned r.URL.Path.
An encoded-slash payload such as "..%2f" stayed a single opaque segment
for the WHATWG URLPattern parser, so "/public/..%2fadmin/secret" matched
"/public/*" while handlers decoded it to "/admin/secret", bypassing any
route-level access control built with url_pattern.

Match the same path model handlers resolve: decode the path, normalize
and clean it (mirroring the path matcher and #4407), then re-encode
through url.URL to a canonical escaped form before running the pattern.
The go-urlpattern library is spec-correct; the fix is in the integration.

* build(deps): bump github.com/dunglas/go-urlpattern to v1.0.0

Moves off the pseudo-version to the first tagged release.
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.

2 participants