Skip to content

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

Open
simonchrz wants to merge 1 commit into
git-pkgs:mainfrom
simonchrz:fix/double-encoded-path
Open

simonchrz wants to merge 1 commit 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, literal percent sequences that do not form escapes, and Composer archive paths continue to work as expected.

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 rejects qualifying double-escaped paths with HTTP 400. Composer archive paths are exempt. Tests cover 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 contacting upstream, while listed single-escaped paths, a non-escape percent sequence, and the Composer path continue to 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 path handling covered by this guard.

Suggested labels: bug

Suggested reviewers: andrew, pinguinfuss

Merge Risk: Minimal · up to 88448

The change rejects package paths that were percent-escaped twice with HTTP 400, closing a gap where a path could be checked under one name and fetched upstream under another. Single escapes, bare percent signs and Composer archives continue to work. No actionable merge 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. The middleware rejects decoded paths that still contain a percent escape with HTTP 400, and the PR objectives …
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. The middleware rejects decoded paths that still contain a percent escape with HTTP 400, and the PR objectives describe the same double-escape guard. The title is specific and concise. It does not name the 400 response or the Composer exemption, which is acceptable 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: 8b1824f9-ccbd-4dcf-a775-2ce301076d56
Commits

Reviewing files that changed from the base of the PR and between 81fc611 and 884484f.

Files selected for processing: 3

  • internal/server/double_escape_test.go
  • internal/server/middleware.go
  • internal/server/server.go

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

@simonchrz simonchrz added the bug Something isn't working label Nov 27, 2026
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.

1 participant