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:
@@ -74,6 +74,7 @@ import * as $route_groups_bar_boof_index from "./routes/route-groups/(bar)/boof/
|
||||
import * as $route_groups_foo_layout from "./routes/route-groups/(foo)/_layout.tsx";
|
||||
import * as $route_groups_foo_index from "./routes/route-groups/(foo)/index.tsx";
|
||||
import * as $signal_shared from "./routes/signal_shared.tsx";
|
||||
import * as $spoof_state from "./routes/spoof_state.tsx";
|
||||
import * as $state_in_props_middleware from "./routes/state-in-props/_middleware.ts";
|
||||
import * as $state_in_props_index from "./routes/state-in-props/index.tsx";
|
||||
import * as $state_middleware_middleware from "./routes/state-middleware/_middleware.ts";
|
||||
@@ -85,6 +86,7 @@ import * as $std from "./routes/std.tsx";
|
||||
import * as $umlaut_äöüß from "./routes/umlaut-äöüß.tsx";
|
||||
import * as $wildcard from "./routes/wildcard.tsx";
|
||||
import * as $Counter from "./islands/Counter.tsx";
|
||||
import * as $DangerousIsland from "./islands/DangerousIsland.tsx";
|
||||
import * as $Foo_Bar from "./islands/Foo.Bar.tsx";
|
||||
import * as $FormIsland from "./islands/FormIsland.tsx";
|
||||
import * as $Greeter from "./islands/Greeter.tsx";
|
||||
@@ -194,6 +196,7 @@ const manifest = {
|
||||
"./routes/route-groups/(foo)/_layout.tsx": $route_groups_foo_layout,
|
||||
"./routes/route-groups/(foo)/index.tsx": $route_groups_foo_index,
|
||||
"./routes/signal_shared.tsx": $signal_shared,
|
||||
"./routes/spoof_state.tsx": $spoof_state,
|
||||
"./routes/state-in-props/_middleware.ts": $state_in_props_middleware,
|
||||
"./routes/state-in-props/index.tsx": $state_in_props_index,
|
||||
"./routes/state-middleware/_middleware.ts": $state_middleware_middleware,
|
||||
@@ -208,6 +211,7 @@ const manifest = {
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $Counter,
|
||||
"./islands/DangerousIsland.tsx": $DangerousIsland,
|
||||
"./islands/Foo.Bar.tsx": $Foo_Bar,
|
||||
"./islands/FormIsland.tsx": $FormIsland,
|
||||
"./islands/Greeter.tsx": $Greeter,
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
|
||||
export default function RawIsland(props: { raw: string }) {
|
||||
const [css, set] = useState("");
|
||||
useEffect(() => {
|
||||
set("raw_ready");
|
||||
}, []);
|
||||
|
||||
return <div class={css} dangerouslySetInnerHTML={{ __html: props.raw }} />;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import DangerousIsland from "../islands/DangerousIsland.tsx";
|
||||
|
||||
export default function SerializePrototype() {
|
||||
return <DangerousIsland raw={`<h1 id="__FRSH_STATE">{.invalid.json}</h1>`} />;
|
||||
}
|
||||
@@ -117,9 +117,12 @@ import * as $relative_link_index from "./routes/relative_link/index.tsx";
|
||||
import * as $scroll_restoration_index from "./routes/scroll_restoration/index.tsx";
|
||||
import * as $scroll_restoration_injected from "./routes/scroll_restoration/injected.tsx";
|
||||
import * as $scroll_restoration_update from "./routes/scroll_restoration/update.tsx";
|
||||
import * as $spoof_state_index from "./routes/spoof_state/index.tsx";
|
||||
import * as $spoof_state_partial from "./routes/spoof_state/partial.tsx";
|
||||
import * as $Counter from "./islands/Counter.tsx";
|
||||
import * as $CounterA from "./islands/CounterA.tsx";
|
||||
import * as $CounterB from "./islands/CounterB.tsx";
|
||||
import * as $DangerousIsland from "./islands/DangerousIsland.tsx";
|
||||
import * as $Fader from "./islands/Fader.tsx";
|
||||
import * as $InvalidSlot from "./islands/InvalidSlot.tsx";
|
||||
import * as $KeyExplorer from "./islands/KeyExplorer.tsx";
|
||||
@@ -262,11 +265,14 @@ const manifest = {
|
||||
"./routes/scroll_restoration/index.tsx": $scroll_restoration_index,
|
||||
"./routes/scroll_restoration/injected.tsx": $scroll_restoration_injected,
|
||||
"./routes/scroll_restoration/update.tsx": $scroll_restoration_update,
|
||||
"./routes/spoof_state/index.tsx": $spoof_state_index,
|
||||
"./routes/spoof_state/partial.tsx": $spoof_state_partial,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $Counter,
|
||||
"./islands/CounterA.tsx": $CounterA,
|
||||
"./islands/CounterB.tsx": $CounterB,
|
||||
"./islands/DangerousIsland.tsx": $DangerousIsland,
|
||||
"./islands/Fader.tsx": $Fader,
|
||||
"./islands/InvalidSlot.tsx": $InvalidSlot,
|
||||
"./islands/KeyExplorer.tsx": $KeyExplorer,
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
|
||||
export default function DangerousIsland(props: { raw: string }) {
|
||||
const [css, set] = useState("");
|
||||
useEffect(() => {
|
||||
set("raw_ready");
|
||||
}, []);
|
||||
|
||||
return <div class={css} dangerouslySetInnerHTML={{ __html: props.raw }} />;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
export default function SerializePrototype() {
|
||||
return (
|
||||
<div>
|
||||
<Partial name="content">
|
||||
<p>initial</p>
|
||||
</Partial>
|
||||
<a href="/spoof_state/partial">Update</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
import DangerousIsland from "../../islands/DangerousIsland.tsx";
|
||||
|
||||
export default function Res() {
|
||||
return (
|
||||
<Partial name="content">
|
||||
<DangerousIsland raw={`<h1 id="__FRSH_STATE">{.invalid.json}</h1>`} />
|
||||
<p class="done">partial</p>
|
||||
</Partial>
|
||||
);
|
||||
}
|
||||
+16
-1
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1569,3 +1569,23 @@ Deno.test("render partial without title", async () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
// See https://github.com/denoland/fresh/issues/2254
|
||||
Deno.test("should not be able to override __FRSH_STATE", async () => {
|
||||
await withPageName(
|
||||
"./tests/fixture_partials/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.click("a");
|
||||
await page.waitForSelector(".raw_ready");
|
||||
|
||||
assert(!didError);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -18,8 +18,8 @@ Deno.test("doesn't leak data across renderers", async () => {
|
||||
const resp = await handler(req);
|
||||
const doc = parseHtml(await resp.text());
|
||||
|
||||
assertSelector(doc, "#__FRSH_STATE");
|
||||
const text = doc.querySelector("#__FRSH_STATE")?.textContent!;
|
||||
assertSelector(doc, "[id^=__FRSH_STATE]");
|
||||
const text = doc.querySelector("[id^=__FRSH_STATE]")?.textContent!;
|
||||
const json = JSON.parse(text);
|
||||
assertEquals(json, { "v": [[{ "site": name }], []] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user