Skip to content
Source
Sign inSign up
TanStack/router

fix: Backslash incorrectly decoded (#3962) #5534

ClosedDreierF(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/5534/headopened

Summary by CodeRabbit

  • Bug Fixes

    • Pathname parsing now preserves percent-encoded sequences (e.g., encoded backslashes) within path segments instead of decoding them, preventing unintended changes to parameters.
  • Tests

    • Added coverage to verify encoded characters in path parameters remain intact during parsing.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core

    2. Walkthrough

      Removes per-segment in-path URI decoding for percent-encoded sequences in regular pathname segments; segments are now left as-is. Adds a test ensuring encoded backslashes in path parameters are preserved.

      Changes

      Cohort / File(s)Summary
      Path decoding change
      packages/router-core/src/path.ts
      Removed logic that split percent-encoded sequences (e.g., %25-delimited) and decoded each pathname segment; partToMatch is now preserved without per-segment decodeURI decoding.
      Test: preserve encoded backslash
      packages/router-core/tests/path.test.ts
      Added a test asserting parsing of /foo%5Cbar preserves the %5C (backslash) in the resulting path segment.

      Sequence Diagram(s)

      sequenceDiagram
        autonumber
        participant Caller
        participant parsePathname
        participant PathLogic
      
        Caller->>parsePathname: parse raw pathname (e.g. "/foo%5Cbar")
        parsePathname->>PathLogic: identify partToMatch (regular segment)
        alt Previous behavior
          PathLogic->>PathLogic: split on "%25" and decode segments (decodeURI)
          PathLogic->>parsePathname: rejoin decoded segments
        else New behavior
          PathLogic-->>parsePathname: return partToMatch unchanged
        end
        parsePathname-->>Caller: return parsed segments (preserve %5C)

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~10 minutes

      • TanStack/router#5169: Modifies pathname-segment decoding logic in packages/router-core/src/path.ts, closely related to this change.

      Poem

      🐰 Along the paths I nimbly hop, Encoded bars I let them stop, %5C stays where it belongs, No extra decode, no wrongs, Tests sing softly — hop, hop, hop! 🥕

      Pre-merge checks and finishing touches

      ✅ Passed checks (3 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title Check✅ PassedThe pull request title "fix: Backslash incorrectly decoded (#3962)" is clearly and directly related to the main changes in the changeset. The code modification removes in-path decoding for percent-encoded sequences (particularly affecting %5C, which represents backslashes), and the test addition validates that backslashes in paths like '/foo%5Cbar' are preserved without decoding. The title is concise, specific, and uses a standard commit convention that clearly conveys the primary fix being implemented. Someone reviewing the repository history would immediately understand that this PR addresses backslash handling in path parsing.
      Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
      ✨ Finishing touches
      • 📝 Generate docstrings
        🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Post copyable unit tests in a comment

      📜 Recent review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 4490a45747021619c0dbc0f117855c9bc1f005cf and e675ee2cdc350d25c32dfb57f18973c060a2aa85.

      📒 Files selected for processing (2)
      • packages/router-core/src/path.ts (1 hunks)
      • packages/router-core/tests/path.test.ts (1 hunks)
      🚧 Files skipped from review as they are similar to previous changes (2)
      • packages/router-core/src/path.ts
      • packages/router-core/tests/path.test.ts

      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
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    3. Actionable comments posted: 0

      🧹 Nitpick comments (1)
      packages/router-core/tests/path.test.ts (1)

      348-353: Consider adding a complementary matching test.

      The test correctly verifies that backslashes and percent signs in parameters are properly encoded during path interpolation. However, given that the PR fixes incorrect decoding behavior (per the title), consider adding a matching test that verifies the reverse operation works correctly.

      Add this test in the matchPathname → path param(s) matching section to verify bidirectional correctness:

      {
        name: 'should correctly decode backslashes and percent-encoded sequences in path parameters',
        input: '/b/test%5Bs%5C%252F.%5C%252Fparameter%5D',
        matchingOptions: {
          to: '/b/$postId',
        },
        expectedMatchedParams: { 
          postId: 'test[s\\%2F.\\%2Fparameter]' 
        },
      },

      This ensures that:

      1. The encoded path can be matched against the route pattern
      2. The parameter is correctly decoded back to the original value
      3. The fix for incorrect decoding is validated in both directions
      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 4693e0e3d4340238a79f26a2a9b102b4e670ea2a and 4490a45747021619c0dbc0f117855c9bc1f005cf.

      📒 Files selected for processing (2)
      • packages/router-core/src/path.ts (1 hunks)
      • packages/router-core/tests/path.test.ts (1 hunks)
      🧰 Additional context used
      📓 Path-based instructions (2)
      **/*.{ts,tsx}

      📄 CodeRabbit inference engine (AGENTS.md)

      Use TypeScript in strict mode with extensive type safety across the codebase

      Files:

      • packages/router-core/tests/path.test.ts
      • packages/router-core/src/path.ts
      packages/router-core/**

      📄 CodeRabbit inference engine (AGENTS.md)

      Keep framework-agnostic core router logic in packages/router-core/

      Files:

      • packages/router-core/tests/path.test.ts
      • packages/router-core/src/path.ts
      🔇 Additional comments (1)
      packages/router-core/src/path.ts (1)

      348-352: LGTM: Pathname segments now correctly preserved without parsing-layer decoding.

      Removing the URI decoding logic from baseParsePathname is the correct fix. The git history confirms this commit ("fix: Backslash incorrectly decoded #3962") addresses a double-decoding bug.

      Verification shows:

      • No parsePathname tests pass encoded strings and expect decoding (no % in test inputs)
      • decodeURIComponent is already in place in matchByPath (lines 688, 692, 744, 783) for boundary decoding
      • The three-layer architecture is intact: parse (preserve as-is) → match (decode at boundaries) → interpolate (encode at boundaries)
      • This change aligns with the correct encoding/decoding pattern
    4. Thanks for the this.

      When replicating the test on its own into main, without the accompanied fix, I'm expecting the test to fail. Unfortunately, this passes already without the above changes.

      We should have a failing test to which the applied changes resolve the issue. This is to ensure that we both understand the need for the change and to ensure future updates don't inadvertently break the functionality without the test giving a false positive.

    5. 🤦‍♂️ Good catch @nlynzaad! The new test fails without the change.

    6. 🤖 Nx Cloud AI Fix Eligible

      An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

      To disable these notifications, a workspace admin can disable them [in workspace settings.](https://cloud.nx.app/orgs/6412ca9d1c251d000efa21ba/workspaces/6442f7de8226a6291646a813/settings#selfHealingCISettings?utm_source=pull-request&utm_medium=comment)

      View your CI Pipeline Execution ↗ for commit e675ee2cdc350d25c32dfb57f18973c060a2aa85

      CommandStatusDurationResult
      nx affected --targets=test:eslint,test:unit,tes...❌ Failed6m 36sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 27sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-10-25 04:26:58 UTC

    7. More templates

      @tanstack/arktype-adapter

      npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5534

      @tanstack/directive-functions-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5534

      @tanstack/eslint-plugin-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5534

      @tanstack/history

      npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5534

      @tanstack/nitro-v2-vite-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5534

      @tanstack/react-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5534

      @tanstack/react-router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5534

      @tanstack/react-router-ssr-query

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5534

      @tanstack/react-start

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5534

      @tanstack/react-start-client

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5534

      @tanstack/react-start-server

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5534

      @tanstack/router-cli

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5534

      @tanstack/router-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5534

      @tanstack/router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5534

      @tanstack/router-devtools-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5534

      @tanstack/router-generator

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5534

      @tanstack/router-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5534

      @tanstack/router-ssr-query-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5534

      @tanstack/router-utils

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5534

      @tanstack/router-vite-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5534

      @tanstack/server-functions-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5534

      @tanstack/solid-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5534

      @tanstack/solid-router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5534

      @tanstack/solid-start

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5534

      @tanstack/solid-start-client

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5534

      @tanstack/solid-start-server

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5534

      @tanstack/start-client-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5534

      @tanstack/start-plugin-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5534

      @tanstack/start-server-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5534

      @tanstack/start-static-server-functions

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5534

      @tanstack/start-storage-context

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5534

      @tanstack/valibot-adapter

      npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5534

      @tanstack/virtual-file-routes

      npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5534

      @tanstack/zod-adapter

      npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5534

      commit: e675ee2

    8. @DreierF thanks again for this. it helped a lot to understand the problem and the road to a solution. unfortunately, we need to decode the path segments and should just exclude the backslash similar to how percentage signs are currently handled.

      I've been able to wire up a decoding function that is able to handle these requirements as well as any further additions we might need to specifically ignore.

      I'm closing this PR in favour of #5663

Sign in to join this conversation.