Skip to content

rewrite: fix strip_path_suffix ignoring percent-encoding - #7877

Merged
steadytao merged 1 commit into
caddyserver:masterfrom
TowyTowy:fix/strip-path-suffix-percent-encoding
Aug 28, 2026
Merged

steadytao merged 1 commit into
caddyserver:masterfrom
TowyTowy:fix/strip-path-suffix-percent-encoding

Conversation

@TowyTowy

Copy link
Copy Markdown
Contributor

What

StripPathSuffix (uri strip_suffix / handle_path in the Caddyfile) is documented to behave like StripPathPrefix: the suffix is matched in normalized (unescaped) space, except where the pattern itself uses a %xx escape. That normalized comparison never actually happened for suffixes.

Suffix stripping was implemented as reverse(trimPathPrefix(reverse(path), reverse(suffix))). Reversing the strings moves the % to the end of each %xx escape, which defeats trimPathPrefix's escape detection (it expects % to precede the two hex digits). So a decoded pattern silently failed to match a percent-encoded path.

Demonstration

operation path expected before after
strip_prefix /a/b/c /a%2Fb/c/d /d /d ✓ /d ✓
strip_suffix /b/c /a/b%2Fc /a /a/b%2Fc ✗ /a ✓
strip_suffix bc /a%62c /a /a%62c ✗ /a ✓

Prefix and suffix share identical doc comments but produced different results.

Fix

Replace the reverse trick with a dedicated trimPathSuffix that iterates from the ends of both strings and applies the same escape-aware, case-insensitive comparison as trimPathPrefix. An escape in the pattern is still compared literally, so %2fsuffix continues to require the path to contain that exact escape (covered by an added negative test).

Present since #4948, which introduced both the escape-aware trimPathPrefix and the reverse-based suffix trimming.

Testing

Added three cases to TestRewrite (two fail before the fix, the negative guard passes both ways); go test ./modules/caddyhttp/... passes, gofmt/vet clean.


Disclosure: this bug was found by a code audit of the rewrite handlers, and the fix and tests were prepared with the assistance of an LLM (Claude). The diagnosis was verified by a human via fail-before/pass-after runs.

@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. The dedicated suffix scan correctly mirrors the prefix semantics while preserving literal escaped-pattern matching and the regression coverage looks sufficient.

@mholt

mholt commented Aug 25, 2026

Copy link
Copy Markdown
Member

This is Matt's Codex agent, GPT-5.6 Sol, replying on his behalf.

The focused fix and its fail-before coverage still look good, and the existing approval remains persuasive. The branch is now conflicting with master, so it cannot be merged as-is. Francis, would you please refresh the branch and rerun the focused rewrite tests? Assuming the conflict resolution does not materially change the implementation and CI is green, this should be ready to merge.

StripPathSuffix is documented to behave like StripPathPrefix: the suffix
is matched in normalized (unescaped) space except where the pattern uses
an escape sequence. But suffix stripping was implemented as

    reverse(trimPathPrefix(reverse(escapedPath), reverse(suffix)))

Reversing the strings moves the '%' to the *end* of each "%xx" escape,
which defeats trimPathPrefix's escape detection (it expects '%' to
precede the two hex digits). As a result the escape-aware, normalized
comparison never happened for suffixes: a decoded pattern failed to
match a percent-encoded path.

Concretely, StripPathPrefix "/a/b/c" strips "/a%2Fb/c/d" to "/d", but the
mirror StripPathSuffix "/b/c" left "/a/b%2Fc" untouched instead of
producing "/a"; likewise StripPathSuffix "bc" did not strip "/a%62c".
This has been the behavior since caddyserver#4948, which introduced both the
escape-aware trimPathPrefix and the reverse-based suffix trimming.

Replace the reverse trick with a dedicated trimPathSuffix that iterates
from the ends of both strings and applies the same escape-aware,
case-insensitive comparison as trimPathPrefix. An escape in the pattern
itself is still compared literally, so "%2fsuffix" continues to require
the path to contain that exact escape.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@TowyTowy
TowyTowy force-pushed the fix/strip-path-suffix-percent-encoding branch from 2bd7b6f to c681f6a Compare August 27, 2026 22:09
@TowyTowy

Copy link
Copy Markdown
Contributor Author

Refreshed on master (36 commits) and pushed. The rebase was clean, so the implementation is unchanged.

For what it's worth, GitHub was reporting the branch as mergeable rather than conflicting, so the conflict may have been transient or a misread, but it's rebased either way.

go test ./modules/caddyhttp/rewrite/... is green on the new base, and TestRewrite still fails without the fix and passes with it.

@steadytao
steadytao merged commit 7bf1b90 into caddyserver:master Aug 28, 2026
24 checks passed
@steadytao steadytao added the bug 🐞 Something isn't working label Aug 28, 2026
@steadytao steadytao added this to the v2.11.5 milestone Aug 28, 2026
mohammed90 added a commit that referenced this pull request Aug 30, 2026
- reverseproxy: TestHostActiveHealthCounters and TestHostResetHealth
  tested Host.activeHealth*/countHealth*/resetHealth methods, which
  master #7916 moved to Upstream.
- rewrite: rewrite_utils_test.go tested the reverse() helper, which
  master #7877 removed as part of a strip_path_suffix bug fix.
- root: Config.cancelFunc changed from context.CancelFunc to
  context.CancelCauseFunc upstream; pass nil cause in the test cleanup.
mohammed90 added a commit that referenced this pull request Oct 19, 2026
- reverseproxy: TestHostActiveHealthCounters and TestHostResetHealth
  tested Host.activeHealth*/countHealth*/resetHealth methods, which
  master #7916 moved to Upstream.
- rewrite: rewrite_utils_test.go tested the reverse() helper, which
  master #7877 removed as part of a strip_path_suffix bug fix.
- root: Config.cancelFunc changed from context.CancelFunc to
  context.CancelCauseFunc upstream; pass nil cause in the test cleanup.
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.

3 participants