Skip to content

Refuse package paths that were percent-escaped twice - #833

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
simonchrz:fix/double-encoded-path
Nov 29, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
simonchrz:fix/double-encoded-path

Conversation

@simonchrz

@simonchrz simonchrz commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Most handlers parse the request path decoded once and append that same string to the upstream URL. A path escaped twice is decoded once more by the upstream, so the proxy checks one name and fetches another:

  • GET /gem/gems/rails-7.0.%2530.gem is checked as version 7.0.%30, which no denylist entry names and no index lists, and is fetched as rails-7.0.0.gem.
  • GET /pypi/packages/…/requests-2.31.0.tar.g%257a has no known extension, so no name is parsed and the checks are skipped.

That gets a denied version past the denylist, and for the ecosystems that allow a version their index doesn't list, a version in cooldown too. The file is cached under the made-up version, so no real cache entry is overwritten.

rejectQueryInPath and rejectDotSegments already close the same gap for ?, #, ; and dot segments.

Change

rejectDoubleEscapedPath returns 400 when the decoded path still holds a percent-escape (% and two hex digits). It runs on the same routes as rejectQueryInPath, with the same Composer archive exemption. The generic handler is not included: it forwards the escaped path as sent.

A path escaped once is unaffected (%40types%2Fnode, libstdc%2B%2B6), and so is a % that doesn't start an escape. A file whose real name contains an escape could not be fetched before either, since the upstream URL decoded it.

Tests

  • TestProtocolHandlersRejectDoubleEscapedPath sends such a path to gem, hex, pypi, maven, go, cran, conda, nuget, conan, debian, rpm, apk, npm, cargo, helm and the container routes through the mounted router: 400, and no upstream request. It fails without the guard.
  • TestDoubleEscapeGuardKeepsSingleEscapes keeps single escapes, a bare % and Composer archives passing.
  • go test ./internal/server/ ./internal/handler/ and golangci-lint run ./internal/server/ pass.

Summary by CodeRabbit

  • Bug Fixes
    • Requests containing paths with double-escaped characters now receive a 400 Bad Request instead of being forwarded upstream.
    • Valid single-escaped paths and percent signs that do not begin a valid escape continue to work as expected.
    • Composer archive paths remain accessible, including paths that would otherwise be rejected by the double-escape check.

Handlers check the path decoded once and append the same string to the upstream URL, where the upstream decodes it again. rails-7.0.%2530.gem was checked as version 7.0.%30 and fetched as 7.0.0, past the denylist and cooldown. Such paths now get 400 before any handler runs.
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The generic route now applies a middleware guard that rejects paths containing a valid percent escape, except Composer archive paths. Tests check rejected paths, allowed paths, and upstream requests.

Changes: Double-escaped path handling

Layer / File(s) Summary
Path escape detection
internal/server/middleware.go
The middleware rejects non-Composer paths whose decoded path contains a percent sign followed by two hexadecimal digits. Helpers detect valid escapes and hexadecimal characters.
Route integration and path coverage
internal/server/server.go, internal/server/double_escape_test.go
The generic route applies the middleware. Tests verify that double-escaped paths receive HTTP 400 without an upstream request, while single-escaped paths, a percent sign that does not begin an escape, and the listed Composer path reach the next handler.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • git-pkgs/proxy#772: Changes how the generic handler forwards escaped paths, which is relevant to the escaped-path handling covered by this guard.

Suggested reviewers: andrew, pinguinfuss

Merge Risk: Minimal · up to 61b36

The change rejects double-escaped package paths with a 400 before the proxy's denylist, index and cooldown checks, closing a bypass. Legitimate single-escaped requests and Composer archives are unaffected, and no merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Refuse package paths that were percent-escaped twice" matches the main change: rejectDoubleEscapedPath now returns HTTP 400 when a decoded request path still contains a percent sign followe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Refuse package paths that were percent-escaped twice" matches the main change: rejectDoubleEscapedPath now returns HTTP 400 when a decoded request path still contains a percent sign followed by two hex digits. It is specific and descriptive, and it names the core behavior. It does not cover the Composer exemption or the tests, which is expected for a title.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ada3ac17-b1c5-4277-9db9-1bc99a2339c7
Commits

Reviewing files that changed from the base of the PR and between 884484f and 61b36cf.

Files selected for processing: 1

  • internal/server/double_escape_test.go
Files with no reviewable changes: 1

  • internal/server/double_escape_test.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@simonchrz simonchrz added the bug Something isn't working label Nov 27, 2026
@andrew
andrew merged commit 6eed90e into git-pkgs:main Nov 29, 2026
9 checks passed
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