Repository navigation
Conversation
steadytao
left a comment
There was a problem hiding this comment.
LGTM. The dedicated suffix scan correctly mirrors the prefix semantics while preserving literal escaped-pattern matching and the regression coverage looks sufficient.
The focused fix and its fail-before coverage still look good, and the existing approval remains persuasive. The branch is now conflicting with |
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>
2bd7b6f to
c681f6a
Compare
|
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.
|
- 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.
- 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.
What
StripPathSuffix(uri strip_suffix/handle_pathin the Caddyfile) is documented to behave likeStripPathPrefix: the suffix is matched in normalized (unescaped) space, except where the pattern itself uses a%xxescape. 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%xxescape, which defeatstrimPathPrefix's escape detection (it expects%to precede the two hex digits). So a decoded pattern silently failed to match a percent-encoded path.Demonstration
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
trimPathSuffixthat iterates from the ends of both strings and applies the same escape-aware, case-insensitive comparison astrimPathPrefix. An escape in the pattern is still compared literally, so%2fsuffixcontinues to require the path to contain that exact escape (covered by an added negative test).Present since #4948, which introduced both the escape-aware
trimPathPrefixand 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.