FORGE-290: keep encoded & in route handler query strings #1043

Merged
gmackie merged 1 commit from FORGE-290-query-decode into main 2026-11-02 04:32:44 +00:00
Owner

Fixes FORGE-290: https://tasks.gmac.io/gmacko/issue/FORGE-290

Cause

@opennextjs/aws 4.1.0 (used by @opennextjs/cloudflare 1.20.2) set a route handler's req.url to the path plus convertToQueryString(internalEvent.query). That query object is already decoded, and convertToQueryString joined it without re-encoding, so a value's %26 became a parameter separator (dist/core/requestHandler.js, setInitialURL override). Pages read invokeQuery, so they were unaffected. The middleware was not involved: it passes the request through unchanged.

Effect: /api/auth/login?redirect=<authorize URL> stored only /api/mcp/oauth/authorize?response_type=code, so a signed-out sign-in from an MCP client (T3 Code) ended on Invalid client_id.

Fix

This backports the upstream fix from @opennextjs/aws 4.1.9 as a pnpm patch (patches/@opennextjs__aws@4.1.0.patch):

  • req.url = initialURL.pathname + initialURL.search keeps the raw query string.
  • convertToQueryString encodes through URLSearchParams, which also covers rewrites and i18n.

Upgrading instead isn't practical within this bug's scope: @opennextjs/cloudflare 1.20.10+ (which uses aws 4.1.9+) requires next >= 16.3.8, and we're on 16.3.0. Drop the patch when OpenNext is upgraded.

Tests

  • apps/web/src/__tests__/opennext-query.test.ts reads the installed OpenNext files and fails if the patch stops applying. It passes with the patch and fails without it, checked locally.
  • scripts/preview-smoke.mjs sends both production repros to the PR preview's built Worker: the login redirect cookie keeps %26y%3D2, and the authorize return path keeps state=a%26b.

🤖 Generated with Claude Code

Fixes FORGE-290: https://tasks.gmac.io/gmacko/issue/FORGE-290 ## Cause `@opennextjs/aws` 4.1.0 (used by `@opennextjs/cloudflare` 1.20.2) set a route handler's `req.url` to the path plus `convertToQueryString(internalEvent.query)`. That query object is already decoded, and `convertToQueryString` joined it without re-encoding, so a value's `%26` became a parameter separator (`dist/core/requestHandler.js`, `setInitialURL` override). Pages read `invokeQuery`, so they were unaffected. The middleware was not involved: it passes the request through unchanged. Effect: `/api/auth/login?redirect=<authorize URL>` stored only `/api/mcp/oauth/authorize?response_type=code`, so a signed-out sign-in from an MCP client (T3 Code) ended on `Invalid client_id`. ## Fix This backports the upstream fix from `@opennextjs/aws` 4.1.9 as a pnpm patch (`patches/@opennextjs__aws@4.1.0.patch`): - `req.url = initialURL.pathname + initialURL.search` keeps the raw query string. - `convertToQueryString` encodes through `URLSearchParams`, which also covers rewrites and i18n. Upgrading instead isn't practical within this bug's scope: `@opennextjs/cloudflare` 1.20.10+ (which uses aws 4.1.9+) requires `next >= 16.3.8`, and we're on 16.3.0. Drop the patch when OpenNext is upgraded. ## Tests - `apps/web/src/__tests__/opennext-query.test.ts` reads the installed OpenNext files and fails if the patch stops applying. It passes with the patch and fails without it, checked locally. - `scripts/preview-smoke.mjs` sends both production repros to the PR preview's built Worker: the login redirect cookie keeps `%26y%3D2`, and the authorize return path keeps `state=a%26b`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-290: keep encoded & in route handler query strings
All checks were successful
CI / superseded (pull_request) Successful in 0s
CI / storybook (pull_request) Successful in 1m22s
CI / gitleaks (pull_request) Successful in 12s
CI / web-build (pull_request) Successful in 2m25s
CI / checks (pull_request) Successful in 1m46s
CI / unit-api (pull_request) Successful in 48s
CI / database-d1 (pull_request) Has been skipped
Registry deps hash / pnpm-deps-hash (pull_request) Successful in 4m1s
CI / unit-web (pull_request) Successful in 2m20s
CI / go (pull_request) Successful in 3m27s
CI / database (pull_request) Successful in 4m57s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 35s
Preview / deploy (pull_request) Has been skipped
Preview / e2e (pull_request) Has been skipped
Preview / release (pull_request) Has been skipped
Preview / cleanup (pull_request) Successful in 24s
232aad45a3
@opennextjs/aws 4.1.0 set a route handler's req.url to the path plus a query
string rebuilt from the already-decoded query, without re-encoding it. A
value's %26 became a separator, so /api/auth/login?redirect=<authorize URL>
saved only /api/mcp/oauth/authorize?response_type=code, and a signed-out MCP
sign-in ended on "Invalid client_id". Pages were unaffected; the middleware
was not involved.

Backport the upstream fix from @opennextjs/aws 4.1.9 as a pnpm patch: req.url
keeps the raw query string, and convertToQueryString encodes with
URLSearchParams. Upgrading instead needs @opennextjs/cloudflare 1.20.10+,
which requires next >= 16.3.8 (we are on 16.3.0).

Regression tests: a unit test on the installed (patched) OpenNext files, and
two preview smoke checks that send both production repros to the built Worker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Worker Preview: https://pr-1043.previews.forgegraf.com (updates on every push; behind Cloudflare Access; shared previews database, the banner shows which PR owns it)

<!-- forgegraph-preview --> Worker Preview: https://pr-1043.previews.forgegraf.com (updates on every push; behind Cloudflare Access; shared previews database, the banner shows which PR owns it)
Sign in to join this conversation.
No description provided.