Redirect path captures double-encode percent-encoded reserved characters

Open Beginner friendly
#622 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript
Domain
backend

Research direction

Start in src/middleware/redirects.ts and follow one captured segment through the three hops the report names: decodeURI(rawPath), compileDestination(params) and the encodeURI(rawDestPath) call in formatDestination. Run the supplied repro.js against a local build to confirm the four %25XX outputs, then check the existing redirect middleware tests for the pattern used to assert Location headers. Done means reserved escapes like %40 and %2B survive into Location unchanged while plain spaces still encode as %20.

Written by the indexing model from the issue text.

Description

When a redirect destination uses captured path segments (such as :id or regex :1), percent-encoded reserved characters in the matched segment (%40, %2B, %2F, %3F, %23, %26, %3A, %3D, %2C, %3B, %24) are double-encoded into %25XX in the Location header.

Reproduction

Save as repro.js:

const http = require("node:http");
const superstatic = require("superstatic").server;

const app = superstatic({
  port: 0,
  host: "127.0.0.1",
  config: {
    redirects: [{ source: "/u/:id", destination: "/users/:id" }],
  },
});

const server = app.listen(async () => {
  const { port } = server.address();
  for (const path of [
    "/u/a%20b",
    "/u/user%40example.com",
    "/u/c%2B%2B",
    "/u/a%3Fb",
  ]) {
    await new Promise((resolve, reject) => {
      http.get({ host: "127.0.0.1", port, path }, (res) => {
        res.resume();
        console.log(`${path} -> ${res.statusCode} ${res.headers.location}`);
        resolve();
      }).on("error", reject);
    });
  }
  server.close();
});

Run:

npm install [email protected]
node repro.js
Actual output
/u/a%20b -> 301 /users/a%20b
/u/user%40example.com -> 301 /users/user%2540example.com
/u/c%2B%2B -> 301 /users/c%252B%252B
/u/a%3Fb -> 301 /users/a%253Fb
Expected output
/u/a%20b -> 301 /users/a%20b
/u/user%40example.com -> 301 /users/user%40example.com
/u/c%2B%2B -> 301 /users/c%2B%2B
/u/a%3Fb -> 301 /users/a%3Fb
Versions
  • superstatic: 11.1.0 and main (a239edf)
  • Node.js: v26.13.0

In src/middleware/redirects.ts, decodeURI(rawPath) leaves URI-reserved escapes (; / ? : @ & = + $ , #) percent-encoded, decodeURIComponent(this.compileDestination(params)) keeps them as %XX, and encodeURI(rawDestPath) in formatDestination encodes % again to %25.

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
1.1k
Forks
90
Avg merge
3d 19h
Merged PRs (30d)
13

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/superstatic

All issues in firebase/superstatic

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.