Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Be defensive against leading double slashes in static router `encodeLocation()`

- Prevents `TypeError: Invalid URL` during SSR in `<ServerRouter>`, `<StaticRouterProvider>`, and `<StaticRouter>` when encoding locations with leading double slashes (such as query-only or hash-only `<NavLink>`s on double-slash request URLs)
72 changes: 72 additions & 0 deletions packages/react-router/__tests__/dom/data-static-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { StaticHandlerContext } from "../../index";
import {
Form,
Link,
NavLink,
Outlet,
useLoaderData,
useLocation,
Expand Down Expand Up @@ -1299,6 +1300,77 @@ describe("A <StaticRouterProvider>", () => {
);
});

it("is defensive against leading double slashes in request URLs", async () => {
let routes = [
{
path: "*",
element: (
<>
<NavLink to="?tab=1">Query link</NavLink>
<NavLink to="#section">Hash link</NavLink>
</>
),
},
];
let { query } = createStaticHandler(routes);

for (let pathname of ["//", "//about"]) {
let context = (await query(
new Request(`http://localhost${pathname}`),
)) as StaticHandlerContext;
let router = createStaticRouter(routes, context);

let html = ReactDOMServer.renderToStaticMarkup(
<React.StrictMode>
<StaticRouterProvider
router={router}
context={context}
hydrate={false}
/>
</React.StrictMode>,
);

expect(html).toBe(
`<a aria-current="page" class="active" href="${pathname}?tab=1" data-discover="true">Query link</a>` +
`<a aria-current="page" class="active" href="${pathname}#section" data-discover="true">Hash link</a>`,
);
expect(router.encodeLocation(pathname)).toEqual({
pathname,
search: "",
hash: "",
});
}
});

it("encodes mixed leading separators in router.encodeLocation", async () => {
let routes = [{ path: "/", element: <h1>Home</h1> }];
let context = (await createStaticHandler(routes).query(
new Request("http://localhost/"),
)) as StaticHandlerContext;
let router = createStaticRouter(routes, context);

expect(router.encodeLocation("//about?tab=1#top")).toEqual({
pathname: "//about",
search: "?tab=1",
hash: "#top",
});
expect(router.encodeLocation("\\\\about")).toEqual({
pathname: "//about",
search: "",
hash: "",
});
expect(router.encodeLocation("/\\about")).toEqual({
pathname: "//about",
search: "",
hash: "",
});
expect(router.encodeLocation("\\/about")).toEqual({
pathname: "//about",
search: "",
hash: "",
});
});

describe("boundary tracking", () => {
it("tracks the deepest boundary during render", async () => {
let routes = [
Expand Down
21 changes: 20 additions & 1 deletion packages/react-router/__tests__/dom/static-location-test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import * as ReactDOMServer from "react-dom/server.edge";
import { Routes, Route, StaticRouter, useLocation } from "../../index";
import { NavLink, Routes, Route, StaticRouter, useLocation } from "../../index";

describe("A <StaticRouter>", () => {
describe("with a string location prop", () => {
Expand Down Expand Up @@ -82,4 +82,23 @@ describe("A <StaticRouter>", () => {
});
});
});

it("is defensive against leading double slashes in location", () => {
for (let pathname of ["//", "//about"]) {
let html = ReactDOMServer.renderToStaticMarkup(
<StaticRouter location={pathname}>
<Routes>
<Route
path="*"
element={<NavLink to="?tab=1">Query link</NavLink>}
/>
</Routes>
</StaticRouter>,
);

expect(html).toBe(
`<a aria-current="page" class="active" href="${pathname}?tab=1" data-discover="true">Query link</a>`,
);
}
});
});
10 changes: 6 additions & 4 deletions packages/react-router/lib/dom/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
convertRoutesToDataRoutes,
isRouteErrorResponse,
} from "../router/utils";
import { ABSOLUTE_URL_REGEX } from "../router/url";
import { PROTOCOL_RELATIVE_URL_REGEX } from "../router/url";
import { DataRoutes, Router } from "../components";
import {
DataRouterContext,
Expand Down Expand Up @@ -499,14 +499,16 @@ function createHref(to: To) {
}

function encodeLocation(to: To): Path {
let base = "http://localhost";
let href = typeof to === "string" ? to : createPath(to);
// Treating this as a full URL will strip any trailing spaces so we need to
// pre-encode them since they might be part of a matching splat param from
// an ancestor route
href = href.replace(/ $/, "%20");
let encoded = ABSOLUTE_URL_REGEX.test(href)
? new URL(href)
: new URL(href, "http://localhost");
if (PROTOCOL_RELATIVE_URL_REGEX.test(href)) {
href = base + href;
}
let encoded = new URL(href, base);
return {
pathname: encoded.pathname,
search: encoded.search,
Expand Down