fix(web): keep legacy hash links on this site (CodeQL js/xss)

The SvelteKit 3 fallback for a /#/ path that matches no route passed the
hash straight to location.replace(): CodeQL code-scanning alert #25,
client-side XSS, and an open redirect in practice - `#//other.example/x`
resolved to another site once goto rejected it.

The hash is now resolved against the page's origin and ignored when it
names another one, goto gets that URL, and an unmatched path goes to the
404 page at its fixed address (/error/404). Nothing from the hash reaches
location.* any more. Tests cover both off-site forms (`#//host`,
`#/\host`) and the 404 fallback; checked in a browser on `npm run dev`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5.5 committed 2026-10-26 14:42:59 +07:00
1 parent 0c454fad2c
commit f42a854b66
4 files changed
+38 -19

No files matched your search

+2 -1
View File
@@ -16,7 +16,8 @@ All notable changes to this awesome noob repo will be documented here.
- The service worker is wired into the build by `web/build/pwa.ts`, on
vite-plugin-pwa: `@vite-pwa/sveltekit` never supported SvelteKit 3.
- An old `/#/…` link to a page that no longer exists now opens the "Page not
found" view; SvelteKit 3 refuses to navigate to it in place.
found" page (`/error/404`); SvelteKit 3 refuses to navigate to it in place.
One that names another site (`/#//other.example/…`) is ignored.
- SvelteKit now asks the site for its version (`/_app/version.json`, a few
bytes) when a tab regains focus and once an hour, where it used to ask only
after a navigation failed. A newer deploy still changes nothing until a
+7 -4
View File
@@ -729,10 +729,13 @@ scraping pipeline (`scripts/`) driven by GitHub Actions.
HashRouter-era `/#/…` URLs still arrive - from released 1.4.x builds, and from
old links to the GitHub Pages site, whose tombstone redirect forwards the
fragment - and `upgradeLegacyHashUrl()` upgrades them on load with
`goto(…, {replace: true})`, falling back to `location.replace` for a path
that matches no route (kit 3's `goto` rejects it). Do not remove it, and do
not reduce it to a `history.replaceState`: that fixed the address bar and
left the dashboard rendered.
`goto(…, {replace: true})`; a path that matches no route (kit 3's `goto`
rejects it) goes to `/error/404`. The hash is resolved against the page's
origin and ignored if it names another (`#//other.example`), and nothing
from it may reach `location.*` - CodeQL flagged exactly that as XSS and an
open redirect. Do not remove it, and do not reduce it to a
`history.replaceState`: that fixed the address bar and left the dashboard
rendered.
- **`web/static/_headers` sets the browser cache, by path.** Hashed output
(`/_app/immutable/*`, `/workbox-*`) is a year, immutable; the art and the
manifest, which keep their names, are a day fresh plus a week of
+20 -9
View File
@@ -5,12 +5,11 @@ vi.mock("$app/navigation", () => ({ goto }));
import { upgradeLegacyHashUrl } from "./legacy-url";
const ORIGIN = "https://free-steam-games.win";
/** A page loaded at `/` + hash, as a 1.4.x link or the old Pages tombstone sends it. */
function loadWithHash(hash: string) {
const replace = vi.fn();
vi.stubGlobal("window", { location: { hash } });
vi.stubGlobal("location", { hash, replace });
return replace;
vi.stubGlobal("window", { location: { hash, origin: ORIGIN } });
}
describe("upgradeLegacyHashUrl", () => {
@@ -38,13 +37,25 @@ describe("upgradeLegacyHashUrl", () => {
goto.mockResolvedValue(undefined);
loadWithHash(hash);
upgradeLegacyHashUrl();
expect(goto).toHaveBeenCalledWith(path, { replace: true, reset: false });
expect(goto).toHaveBeenCalledOnce();
const [url, options] = goto.mock.calls[0];
expect(String(url)).toBe(`${ORIGIN}${path}`);
expect(options).toEqual({ replace: true, reset: false });
});
it("loads a path that matches no route natively, where it gets the 404 view", async () => {
goto.mockRejectedValue(new Error("Cannot find a route for /no-such-page"));
const replace = loadWithHash("#/no-such-page");
it.each(["#//other.example/games/730", "#/\\other.example/games/730"])(
"never leaves the site for %s",
(hash) => {
loadWithHash(hash);
upgradeLegacyHashUrl();
expect(goto).not.toHaveBeenCalled();
},
);
it("sends a path that matches no route to the 404 page", async () => {
goto.mockRejectedValueOnce(new Error("Cannot find a route for /no-such-page")).mockResolvedValue(undefined);
loadWithHash("#/no-such-page");
upgradeLegacyHashUrl();
await vi.waitFor(() => expect(replace).toHaveBeenCalledWith("/no-such-page"));
await vi.waitFor(() => expect(goto).toHaveBeenLastCalledWith("/error/404", { replace: true }));
});
});
+9 -5
View File
@@ -27,8 +27,13 @@ import { goto } from "$app/navigation";
export function upgradeLegacyHashUrl(): void {
if (typeof window === "undefined") return;
const { hash } = window.location;
const { hash, origin } = window.location;
if (!hash.startsWith("#/")) return;
// The hash is anyone's to write. Resolved against this origin and checked
// against it, because `#//other.example/…` (or `#/\other.example`) names
// another site, and an old link is never a reason to leave this one.
const url = new URL(hash.slice(1), origin);
if (url.origin !== origin) return;
// goto, not history.replaceState. By the time this runs the router has
// already rendered the document it was given - "/", the dashboard - and a
// bare replaceState changed the address bar while leaving the dashboard on
@@ -36,8 +41,7 @@ export function upgradeLegacyHashUrl(): void {
// out of history, so Back does not bounce through it.
//
// goto rejects a path that matches no route (SvelteKit 3; SvelteKit 2 loaded
// it natively). A real load of that path gets the layout's 404 view, as it
// always did, and cannot come back here: the hash is gone.
const target = hash.slice(1) || "/";
goto(target, { replace: true, reset: false }).catch(() => location.replace(target));
// it natively). Such a link gets the 404 page at its fixed address: nothing
// from the hash ever reaches a navigation that bypasses the router.
void goto(url, { replace: true, reset: false }).catch(() => goto("/error/404", { replace: true }));
}