Skip to content

@astrojs/node: trailing-slash redirect drops part of a query string that contains a second ? #17962

Closed

Description

@Chy-Zaber-Bin-Zahid

Astro Info

Astro                    v7.3.2
Node                     v24.14.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  @astrojs/node v11.1.5 (mode: standalone)
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response (server-side redirect, browser independent)

Describe the Bug

When the Node adapter's static handler issues a trailing-slash redirect, it rebuilds the Location header from fullUrl.split('?'), which keeps only the part of the query before the second ?. Any query string that itself contains a ? (a return URL, an OAuth state, a search URL) is silently truncated.

With trailingSlash: 'never':

GET /one/?redirect=/x?y=1&z=2
HTTP/1.1 301 Moved Permanently
Location: /one?redirect=/x

With trailingSlash: 'always', GET /one?redirect=/x?y=1&z=2 redirects to /one/?redirect=/x.

The splitting happens in packages/integrations/node/src/serve-static.ts (const [urlPath, urlQuery] = fullUrl.split('?')). RFC 3986 allows ? inside the query component, and the redirect is only meant to normalize the trailing slash.

I have a fix with regression tests in #17961.

What's the expected result?

The redirect keeps the query string intact:

Location: /one?redirect=/x?y=1&z=2

Link to Minimal Reproducible Example

https://github.com/Chy-Zaber-Bin-Zahid/astro-node-trailing-slash-query-repro

npm install && npm run build && npm start &
curl -si 'http://localhost:4321/one/?redirect=/x?y=1&z=2' | grep -i location

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. added
    triage: in progress
    on Sep 10, 2026
  2. astro-factory commented on Sep 10, 2026

    @astro-factory
    Contributor

    Triage complete

    • Prepare the workspace: ready
    • Install dependencies: complete
    • Build the project: complete
    • Reproduce the issue: confirmed
    • Diagnose the cause: high confidence
    • Verify the diagnosis: bug
    • Attempt a fix: verified
    • Publish the result: set triage: fix pending

    • Reproduced: Yes
    • Exploration: Yes — View branch
    • Unit Test: Yes — packages/integrations/node/test/trailing-slash.test.ts (2 new test cases)
    • Priority: P4: important. This silently corrupts redirect URLs containing ? in the query string (e.g. OAuth callbacks, return URLs), violating RFC 3986 §3.4. Affects both trailingSlash: 'never' and trailingSlash: 'always' in standalone mode.

    The root cause is in packages/integrations/node/src/serve-static.ts at line 60: fullUrl.split('?') splits on every ? in the URL, but array destructuring only keeps the first two elements. When the query string itself contains a ? (e.g. /one/?redirect=/x?y=1&z=2), everything after the second ? is silently discarded, producing a truncated Location header like /one?redirect=/x.

    I found a potential fix for this issue. The fix replaces split('?') with indexOf('?') to split only on the first ?, preserving the full query string. All 36 trailing-slash tests pass (34 existing + 2 new regression tests).

    Try this fix

    You can test this fix right now without waiting for a release:

    # @astrojs/node
    npm i https://pkg.pr.new/@astrojs/node@75e7d13

    If this fixes your issue, please leave a comment letting us know (for example
    "confirmed, this fixes it"). A pull request will then be opened to get it merged.

    Full Triage Report

    Issue Title

    @astrojs/node: trailing-slash redirect drops part of a query string that contains a second ?

    Issue Description

    When the Node adapter's static handler issues a trailing-slash redirect, it rebuilds the Location header from fullUrl.split('?'), which keeps only the part of the query before the second ?. Any query string that itself contains a ? (a return URL, an OAuth state, a search URL) is silently truncated.

    RFC 3986 allows ? inside the query component, and the redirect is only meant to normalize the trailing slash.

    Environment

    • Astro v7.3.2
    • Node v24.14.0
    • @astrojs/node v11.1.5 (mode: standalone)
    • output: static

    Bug Location

    File: packages/integrations/node/src/serve-static.ts, line 60:

    const [urlPath, urlQuery] = fullUrl.split('?');

    When fullUrl is /one/?redirect=/x?y=1&z=2, split('?') produces ['/one/', 'redirect=/x', 'y=1&z=2']. Only the first two elements are destructured, so urlQuery becomes just 'redirect=/x' and 'y=1&z=2' is silently discarded.

    This urlQuery is then used in the trailingSlash: 'never' and trailingSlash: 'always' redirect branches:

    • Line 98 (never): pathname = urlPath.slice(0, -1) + (urlQuery ? '?' + urlQuery : '');
    • Line 112 (always): pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : '');

    Reproduction

    Steps

    1. Use the existing trailing-slash test fixture from the node integration package
    2. Build with trailingSlash: 'never' and start standalone server
    3. Send raw HTTP request: GET /one/?redirect=/x?y=1&z=2
    4. Inspect the Location header in the 301 response

    Actual Result

    • trailingSlash: 'never': GET /one/?redirect=/x?y=1&z=2 → Location: /one?redirect=/x (truncated!)
    • trailingSlash: 'always': GET /one?redirect=/x?y=1&z=2 → Location: /one/?redirect=/x (truncated!)

    Expected Result

    • trailingSlash: 'never': Location: /one?redirect=/x?y=1&z=2 (full query preserved)
    • trailingSlash: 'always': Location: /one/?redirect=/x?y=1&z=2 (full query preserved)

    Root Cause

    String.prototype.split('?') splits on every ? in the string. Array destructuring assigns only the first two elements, so any query content after a second ? is silently dropped. The split('?') was introduced in commit f61248895 ("Adapter enhancements", PR #9661, Jan 2024) and has been present since trailing-slash redirect support was first implemented.

    Fix

    Replaced fullUrl.split('?') with fullUrl.indexOf('?') to split only on the first ?:

    -			const [urlPath, urlQuery] = fullUrl.split('?');
    +			const qIndex = fullUrl.indexOf('?');
    +			const urlPath = qIndex >= 0 ? fullUrl.slice(0, qIndex) : fullUrl;
    +			const urlQuery = qIndex >= 0 ? fullUrl.slice(qIndex + 1) : '';

    Added two regression tests in trailing-slash.test.ts using rawRequest (to avoid fetch URL normalization):

    • Always > Without base: verifies GET /one?redirect=/x?y=1&z=2 → Location: /one/?redirect=/x?y=1&z=2
    • Never > Without base: verifies GET /one/?redirect=/x?y=1&z=2 → Location: /one?redirect=/x?y=1&z=2

    All 36 tests pass (34 existing + 2 new), no regressions.

    Changeset: .changeset/tricky-sheep-relate.md — @astrojs/node: patch

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

  3. added
    - P4: important
    pkg: node
    triage: fix pending
    and removed
    triage: in progress
    on Sep 10, 2026
  4. self-assigned this
    on Sep 10, 2026
  5. matthewp commented on Sep 10, 2026

    @matthewp
    Contributor

    Bot, I can confirm this fixes it

  6. added
    triage: fix verified
    and removed
    triage: fix pending
    on Sep 10, 2026
  7. astro-factory commented on Sep 10, 2026

    @astro-factory
    Contributor

    The fix has been verified! A pull request has been created: #17964

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- P4: importantpkg: nodetriage: fix verified

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions