fix: __FRSH_STATE potentially being overwritten by user code (#2256)

As outlined in https://github.com/denoland/fresh/issues/2254 it was
possible to overwrite the script tag that is used by Fresh to pass
island props from the server to the client. This could be done by
injecting raw HTML via `dangerouslySetInnerHTML` and simply using the
same id that Fresh was using.

With this PR we generate a unique id per render that cannot be guessed
anymore. It's composed of `__FRSH_STATE_<uuid>`
This commit is contained in:
Marvin Hagemeister
2024-01-15 19:41:12 +01:00
committed by GitHub
parent 8583394481
commit 9f81806c6e
15 changed files with 117 additions and 14 deletions
+16 -1
View File
@@ -949,7 +949,7 @@ Deno.test("Adds nonce to inline scripts", async () => {
await withFakeServe("./tests/fixture/main.ts", async (server) => {
const doc = await server.getHtml(`/nonce_inline`);
const stateScript = doc.querySelector("#__FRSH_STATE")!;
const stateScript = doc.querySelector("[id^=__FRSH_STATE]")!;
const nonce = stateScript.getAttribute("nonce")!;
const el = doc.querySelector("#inline-script")!;
@@ -1220,3 +1220,18 @@ Deno.test("empty string fallback for optional params", async () => {
assertEquals(data, { path: "foo", version: "" });
});
});
// See https://github.com/denoland/fresh/issues/2254
Deno.test("should not be able to override __FRSH_STATE", async () => {
await withPageName("./tests/fixture/main.ts", async (page, address) => {
let didError = false;
page.on("pageerror", (ev) => {
didError = true;
console.log(ev);
});
await page.goto(`${address}/spoof_state`);
await page.waitForSelector(".raw_ready");
assert(!didError);
});
});