lukeed / lukeed/polka

Polka interprets percent-encoded slashes the same as regular slashes

Open
#142 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

has fix
Dominant language
JavaScript
Stars
5.6k
Forks
176
PR merge metrics
No merged PRs in 30d

Description

I have an app with a query endpoint like /query/:input and sometimes the input can be something like 1/7.

I would have a url like /query/1%20%2F%207 (percent encoded 1 / 7) and end up getting a 404 page. I added a console.log call and it turned out this was being decoded into /query/1 / 7 in request.url. Express doesn't have this issue, for comparison.

I was using the next version from NPM because that was the default in the Sapper template.

Repro

const polka = require("polka");

polka()
  .use((req, res, next) => {
    console.log("url", req.url);
    next();
  })
  .get("/query/:input", (req, res) => {
    res.end(
      JSON.stringify({
        url: req.url,
        input: req.params.input,
      })
    );
  })
  .listen(3000, (err) => {
    if (err) throw err;
    console.log("> Running on localhost:3000");
  });
$ curl http://localhost:3000/query/1%20%2F%207
Expected behavior / Express behavior

200 OK with body:

{
  "url": "/query/1%20%2F%207",
  "input": "1 / 7"
}
Actual behavior (5.2.0)

200 OK with body:

{
  "url": "/query/1%20%2F%207",
  "input": "1%20%2F%207"
}

(input is urlencoded when it should be decoded)

Actual behavior (next)

Server prints url /query/1 / 7.

Curl sees 404 Not Found.

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Start with the provided Polka reproduction and run the curl request against the /query/:input route. Trace how the request URL is decoded before routing, then verify that the encoded slash stays in req.url, input becomes "1 / 7", and the request returns 200 rather than 404.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.