Skip to content

caddyhttp: fix path_regexp (MatchPathRE) Windows backslash bypass - #7858

Merged
mholt merged 2 commits into
caddyserver:masterfrom
thientd:fix/pathre-windows-backslash
Jul 10, 2026
Merged

mholt merged 2 commits into
caddyserver:masterfrom
thientd:fix/pathre-windows-backslash

Conversation

@thientd

@thientd thientd commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

MatchPath was fixed in #7763 to normalize Windows path separators, but its sibling MatchPathRE (the path_regexp matcher) was missed. On Windows a guard like path_regexp ^/private/ is therefore bypassed by GET /private\secret.txt (or /private%5csecret.txt, which decodes to the same r.URL.Path), while the OS still resolves the file, so file_server ends up serving the protected content.

The Windows normalization is now a single shared helper, normalizeWindowsPath, called by both MatchPath and MatchPathRE (no duplicated logic). It converts \ to / and trims trailing dots/spaces per path component — not only at the end of the whole path — leaving ./.. for cleanPath to resolve. This also closes the same per-component trailing-dot/space gap that remained in the path matcher after #7763.

Verified on native Windows 11 (arm64) with a path_regexp ^/private/ guard in front of file_server:

  • before: GET /private\secret.txt, GET /private%5csecret.txt, and GET /private./secret.txt return 200 and leak the file
  • after: all return 403, while GET /private/... and public paths are unaffected
  • TestPathMatcherWindows and TestPathREMatcherWindows fail on the unpatched matchers and pass with the fix; go vet clean; cross-compiles for windows/amd64 and windows/arm64

Refs #5613, #4407, #7763. Addresses GHSA-c293-7rw2-3gwv (incomplete fix of CVE-2026-52844).

Assistance Disclosure

AI (Anthropic Claude) was used substantially for this PR: it helped identify the missed path_regexp call-site, drafted the normalizeWindowsPath helper and the regression tests, and ran the dynamic verification on native Windows. I directed the work, reviewed all changes, and verified correctness (Windows 11 arm64: all path/path_regexp Windows tests pass; go vet clean; cross-compiles for windows/amd64 and windows/arm64).

@CLAassistant

CLAassistant commented Jul 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@steadytao steadytao 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.

I think this still needs one more pass before merge. The same Windows normalisation should probably be shared by path and path_regexp and it should handle trailing dots/spaces per path component, not only at the end of the whole path.

Could you also add the required AI disclosure to your PR body.

@thientd
thientd requested a review from steadytao July 8, 2026 07:41
… path matcher

Apply the same Windows path normalization to MatchPathRE that MatchPath already had, and factor it into a shared normalizeWindowsPath helper so both matchers use one implementation.

Also strip trailing dots and spaces per path component (not only at the end of the whole path), matching how Windows resolves paths; this fixes the same gap in the path matcher too. Adds regression tests for both matchers.

See caddyserver#5613.
@thientd
thientd force-pushed the fix/pathre-windows-backslash branch from 65d3a9f to b0db936 Compare July 8, 2026 13:34
@thientd

thientd commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review — all three points are addressed in the latest push:

  1. Shared normalization. The Windows path normalization is now a single
    normalizeWindowsPath() helper in matchers.go, called by both MatchPath
    and MatchPathRE, so there's no duplicated logic and future path matchers
    can reuse it.

  2. Per-component trailing dots/spaces. Good catch — the old
    strings.TrimRight(reqPath, ". ") only trimmed the end of the whole path.
    The helper now splits on / and trims trailing dots/spaces from every
    component (leaving ./.. for cleanPath to resolve), so ^/private/ also
    covers /private./secret and /private /secret. This closes the same
    per-component gap in the path matcher too.

  3. AI disclosure added to the PR body.

Tests: added per-component cases to TestPathMatcherWindows and a new
TestPathREMatcherWindows (literal \, %5c/%5C, per-component dot/space).
Verified on native Windows 11 (arm64) — all pass; go vet clean; cross-compiles
for windows/amd64 and windows/arm64.

@steadytao steadytao 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, the decoded-path cases are addressed, but the escaped-path branch still bypasses the new per-component normalisation whenever a matcher contains %. MatchPath switches to EscapedPath() and only normalises backslash separators there so a pattern such as /private%2f* can still miss /private.%5csecret.txt.

Please apply equivalent trailing-dot/space normalisation in escaped space and add a regression test with a %-containing matcher.

The MatchPath escaped-path branch (taken when a matcher pattern contains
'%') only folded backslash separators via windowsEscapedPathSeparatorRepl;
it skipped the per-component trailing dot/space normalization now applied
on the decoded branch. On Windows this left a bypass: a matcher such as
`path /private%2f*` could be evaded by GET /private.%5csecret.txt, since
`private.` and `private` resolve to the same directory on NTFS.

Add normalizeWindowsEscapedPath, which trims trailing dots and spaces —
literal ("." / " ") and percent-encoded ("%2e" / "%20") — from every
component in raw/escaped space, splitting on both '/' and encoded '%2f'
separators while preserving them, and leaving "." / ".." for CleanPath.
Regression tests cover the literal and percent-encoded variants against a
'%'-containing matcher.
@thientd

thientd commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — addressed in fac502c.

The escaped-path branch (the one taken when a matcher pattern contains %) now runs the same per-component trailing dot/space normalization as the decoded branch, via a new normalizeWindowsEscapedPath helper called right after the backslash-separator fold:

It walks the escaped path splitting on both literal / and encoded %2f/%2F separators, and trims each component's trailing dots and spaces — literal (. / space) and percent-encoded (%2e / %20) — while preserving the separators so the escaped-space comparison still lines up. . and .. segments are left intact for CleanPath to resolve.
So your example /private.%5csecret.txt (and the %2e / %20 encoded variants) now normalizes to /private%2fsecret.txt, which is correctly caught by /private%2f* instead of slipping past it.
Added regression tests in TestPathMatcherWindows using a %-containing matcher, covering the literal trailing dot, encoded dot (%2e), and encoded space (%20) cases.

@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.

I don't know much about Windows but overall I think the change LGTM, I am going by the additional tests 😅

@mholt
mholt enabled auto-merge (squash) July 10, 2026 17:25

@steadytao steadytao 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.

LGTM.

@mholt
mholt merged commit c6180a0 into caddyserver:master Jul 10, 2026
26 checks passed
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.

5 participants