Skip to content

Commit

Permalink
Fix parameter injection in useRoute and useSearch.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Jan 10, 2024
1 parent a3c62bf commit 49d63ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/wouter/src/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const stripQm = (str) => (str[0] === "?" ? str.slice(1) : str);
*/
export const unescape = (str) => {
try {
return decodeURIComponent(str);
return decodeURI(str);
} catch (_e) {
// fail-safe mode: if string can't be decoded do nothing
return str;
Expand Down
7 changes: 7 additions & 0 deletions packages/wouter/test/use-route.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ it("supports other characters in segments", () => {
});
});

it("ignores escaped slashes", () => {
assertRoute("/:param/bar", "/foo%2Fbar/bar", { param: "foo%2Fbar" });
assertRoute("/:param", "/foo%2Fbar%D1%81%D0%B0%D0%BD%D1%8F", {
param: "foo%2Fbarсаня",
});
});

it("reacts to pattern updates", () => {
const { result, rerender } = renderHook(
({ pattern }: { pattern: string }) => useRoute(pattern),
Expand Down
10 changes: 10 additions & 0 deletions packages/wouter/test/use-search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ it("unescapes search string", () => {
act(() => navigate("/?вопрос=как дела?"));
expect(searchResult.current).toBe("вопрос=как дела?");
});

it("is safe against parameter injection", () => {
history.replaceState(null, "", "/?search=foo%26parameter_injection%3Dbar");
const { result } = renderHook(() => useSearch());

const searchParams = new URLSearchParams(result.current);
const query = Object.fromEntries(searchParams.entries());

expect(query).toEqual({ search: "foo&parameter_injection=bar" });
});

0 comments on commit 49d63ee

Please sign in to comment.