react-router icon indicating copy to clipboard operation
react-router copied to clipboard

[Bug]: Wildcard in route path incorrectly decodes the uri fragment.

Open mrxdst opened this issue 3 years ago • 2 comments

What version of React Router are you using?

6.0.1

Steps to Reproduce

https://stackblitz.com/edit/github-tm3ka5?file=src/App.tsx

Expected Behavior

We encode 2 uri components and expect to decode 2 uri components.

Actual Behavior

We incorrectly end up with 4 uri components

mrxdst avatar Nov 06 '21 14:11 mrxdst

Hi @mrxdst , I see the behavior is the same with the a tag

image

htdangkhoa avatar Nov 06 '21 19:11 htdangkhoa

I believe I've found the issue.

In the matchPath function.

Change the following:

memo[paramName] = safelyDecodeURIComponent(
  captureGroups[index] || "",
  paramName
);

To:

if (paramName === "*") {
  memo[paramName] = safelyDecodeURI(
    captureGroups[index] || "",
    paramName
  );
} else {
  memo[paramName] = safelyDecodeURIComponent(
    captureGroups[index] || "",
    paramName
  );
}

The value of the * param might not be a single uri component. Therefor decodeURI should be used instead.

mrxdst avatar Nov 08 '21 08:11 mrxdst

useParams always gives back decoded values, so if you're doing additional /-delimiting within the splat route you'll need to implement your own logic to parse that out correctly. useLocation().pathname will contain the encoded pathname so you can do you manual decoding from there as well.

brophdawg11 avatar Jan 24 '23 17:01 brophdawg11