Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize double slashes in incoming locations #11924

Open
wants to merge 1 commit into
base: v6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/selfish-fishes-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Normalize double slashes in incoming locations
35 changes: 35 additions & 0 deletions packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7542,6 +7542,41 @@ function testDomRouter(
});
});
});

if (name === "<DataBrowserRouter>") {
describe("DataBrowserRouter-only tests", () => {
it("is defensive against double slash URLs in window.location", async () => {
let testWindow = getWindow("/a///b");
let router = createTestRouter(
[
{
path: "*",
Component() {
return <Link to="/page">Go to Page</Link>;
},
},
{
path: "/page",
Component() {
return <h1>Worked!</h1>;
},
},
],
{
window: testWindow,
}
);
render(<RouterProvider router={router} />);
expect(testWindow.location.pathname).toBe("/a///b");
expect(router.state.location.pathname).toBe("/a/b");

fireEvent.click(screen.getByText("Go to Page"));
await waitFor(() => screen.getByText("Worked!"));
expect(testWindow.location.pathname).toBe("/page");
expect(router.state.location.pathname).toBe("/page");
});
});
}
}

function getWindowImpl(initialUrl: string, isHash = false): Window {
Expand Down
16 changes: 13 additions & 3 deletions packages/router/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ function getHistoryState(location: Location, index: number): HistoryState {
};
}

function normalizeMultipleSlashes(path: string) {
return path.replace(/\/{2,}/g, "/");
}

/**
* Creates a Location object with a unique key from the given Path
*/
Expand All @@ -536,11 +540,17 @@ export function createLocation(
state: any = null,
key?: string
): Readonly<Location> {
let toLocation = typeof to === "string" ? parsePath(to) : to;
if (toLocation.pathname) {
toLocation.pathname = normalizeMultipleSlashes(toLocation.pathname);
}
let location: Readonly<Location> = {
pathname: typeof current === "string" ? current : current.pathname,
pathname: normalizeMultipleSlashes(
typeof current === "string" ? current : current.pathname
),
search: "",
hash: "",
...(typeof to === "string" ? parsePath(to) : to),
...toLocation,
Comment on lines +543 to +553
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if we think this is too widespread. There is a very targeted fix we could put in createURL that just fixes scenarios we expect would throw. By doing it here are a bit more robust and effectively sanitize when we create Location's for the router. We do then expose Location's back out to the user so it might be a concern of that feeling "breaking" to mess with incoming paths in a different manner...

state,
// TODO: This could be cleaned up. push/replace should probably just take
// full Locations now and avoid the need to run through this flow at all
Expand Down Expand Up @@ -586,7 +596,7 @@ export function parsePath(path: string): Partial<Path> {
}

if (path) {
parsedPath.pathname = path;
parsedPath.pathname = normalizeMultipleSlashes(path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ export function createRouter(init: RouterInit): Router {
// Create a GET request for the loaders
request = createClientSideRequest(
init.history,
request.url,
createPath(new URL(request.url)),
request.signal
);
}
Expand Down