Skip to content

fix: apply root fallback before query string when rewritePrefix is empty - #504

Merged
Tony133 merged 2 commits into
fastify:mainfrom
abdul-karim-mia:fix/rewrite-prefix-empty-query
Dec 3, 2026
Merged

Tony133 merged 2 commits into
fastify:mainfrom
abdul-karim-mia:fix/rewrite-prefix-empty-query

Conversation

@abdul-karim-mia

Copy link
Copy Markdown
Contributor

What

Fixes #503

With rewritePrefix: '' (and a prefix such as /api or /:tenant/api), requesting the prefix root with a query string (GET /api?a=1) rewrote dest to '' and then appended ?a=1 before evaluating dest || '/'. Because '?a=1' is non-empty, fromParametersWithRewritePrefix returned '?a=1', leaving url.split('?', 1)[0] in handler as '' so reply.from('') fell back to forwarding the unstripped /api?a=1 (and custom handlers received dest = '').

Changes

  • index.js (fromParametersWithRewritePrefix): Normalize an empty rewritten path (if (!dest) dest = '/') before appending ?${qs.stringify(queryParams)}, so root requests with query strings resolve to '/?a=1' in reply.fromParameters() and dest = '/' in both the default handler and custom handler / upgrade paths.
  • test/test.js: Added regression tests covering GET /api, GET /api/?a=1, GET /api?a=1 (including reply.fromParameters() and custom handler dest), plus parameterized prefix (/:tenant/api) with rewritePrefix: ''.

Verification

  • Confirmed both new tests failed on main prior to the fix (GET /api?a=1 reached upstream as /api?a=1 and /:tenant/api?a=1 returned 404).
  • Ran npm test (eslint, borp -C --check-coverage, tstyche):
    • eslint: 0 errors
    • borp -C --check-coverage: 200/200 tests passed, 100% statements/branches/functions/lines
    • tstyche: 44/44 assertions passed

@mcollina
mcollina requested a balanced review from Copilot December 3, 2026 15:59

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the fix that normalizes empty rewritten paths before appending query strings.

No open issues 🎉

What changed in this PR

Fixes #503 by ensuring empty rewritten paths become / before query strings are appended, preventing the proxy from forwarding the original prefix.

Changes:

  • Moves the root-path fallback before query-string serialization.
  • Adds regression coverage for literal and parameterized prefixes, reply.fromParameters(), and custom handlers.
File Description
test/​test.js Adds regression tests for empty rewrite prefixes and root requests.
index.js Normalizes empty rewritten paths before appending query strings.

Review effort: Balanced

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

@Tony133
Tony133 merged commit ba18787 into fastify:main Dec 3, 2026
51 of 53 checks passed
@abdul-karim-mia
abdul-karim-mia deleted the fix/rewrite-prefix-empty-query branch December 3, 2026 18:17
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.

rewritePrefix: '' forwards the prefix to the upstream when the prefix root is requested with a query string (regression in 11.4.2)

4 participants