From 9f81806c6eef1071623e877f0df041392f112644 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 15 Jan 2024 19:41:12 +0100 Subject: [PATCH] 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_` --- src/runtime/entrypoints/main.ts | 5 +++-- src/server/context.ts | 7 ++++--- src/server/render.ts | 5 +++-- src/server/rendering/fresh_tags.tsx | 12 +++++++---- src/server/rendering/state.ts | 3 +++ tests/fixture/fresh.gen.ts | 4 ++++ tests/fixture/islands/DangerousIsland.tsx | 10 ++++++++++ tests/fixture/routes/spoof_state.tsx | 5 +++++ tests/fixture_partials/fresh.gen.ts | 6 ++++++ .../islands/DangerousIsland.tsx | 10 ++++++++++ .../routes/spoof_state/index.tsx | 12 +++++++++++ .../routes/spoof_state/partial.tsx | 11 ++++++++++ tests/main_test.ts | 17 +++++++++++++++- tests/partials_test.ts | 20 +++++++++++++++++++ tests/render_test.ts | 4 ++-- 15 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 tests/fixture/islands/DangerousIsland.tsx create mode 100644 tests/fixture/routes/spoof_state.tsx create mode 100644 tests/fixture_partials/islands/DangerousIsland.tsx create mode 100644 tests/fixture_partials/routes/spoof_state/index.tsx create mode 100644 tests/fixture_partials/routes/spoof_state/partial.tsx diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index de401bfb..8cf9e411 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -640,6 +640,7 @@ class NoPartialsError extends Error {} */ export async function applyPartials(res: Response): Promise { const contentType = res.headers.get("Content-Type"); + const uuid = res.headers.get("X-Fresh-UUID"); if (contentType !== "text/html; charset=utf-8") { throw new Error(partialErrorMessage); } @@ -652,7 +653,7 @@ export async function applyPartials(res: Response): Promise { // Preload all islands because they need to be available synchronously // for rendering later const islands: IslandRegistry = {}; - const dataRaw = doc.getElementById("__FRSH_PARTIAL_DATA")!; + const dataRaw = doc.getElementById(`__FRSH_PARTIAL_DATA_${uuid}`)!; let data: { islands: Record; signals: string | null; @@ -669,7 +670,7 @@ export async function applyPartials(res: Response): Promise { ); } - const stateDom = doc.getElementById("__FRSH_STATE")?.textContent; + const stateDom = doc.getElementById(`__FRSH_STATE_${uuid}`)?.textContent; let state: SerializedState = [[], []]; // Load all dependencies diff --git a/src/server/context.ts b/src/server/context.ts index aafdc355..f76fc97a 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -669,7 +669,7 @@ const createRenderNotFound = ( return async (req, ctx) => { const notFound = extractResult.notFound; if (!notFound.component) { - return sendResponse(["Not found.", undefined], { + return sendResponse(["Not found.", "", undefined], { status: STATUS_CODE.NotFound, isDev: dev, statusText: undefined, @@ -771,7 +771,7 @@ function collectEntrypoints( } function sendResponse( - resp: [string, ContentSecurityPolicy | undefined], + resp: [string, string, ContentSecurityPolicy | undefined], options: { status: number; statusText: string | undefined; @@ -779,11 +779,12 @@ function sendResponse( isDev: boolean; }, ) { + const [body, uuid, csp] = resp; const headers: Record = { "content-type": "text/html; charset=utf-8", + "x-fresh-uuid": uuid, }; - const [body, csp] = resp; if (csp) { if (options.isDev) { csp.directives.connectSrc = [ diff --git a/src/server/render.ts b/src/server/render.ts index 71d8d778..86a269c4 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -122,7 +122,7 @@ export function checkAsyncComponent( */ export async function render( opts: RenderOptions, -): Promise<[string, ContentSecurityPolicy | undefined] | Response> { +): Promise<[string, string, ContentSecurityPolicy | undefined] | Response> { const component = opts.route.component; // Only inherit layouts up to the nearest root layout. @@ -242,6 +242,7 @@ export async function render( // ensures that each render request is associated with the same // data. const renderState = new RenderState( + crypto.randomUUID(), { url, route: opts.route.pattern, @@ -393,5 +394,5 @@ export async function render( moduleScripts: result.moduleScripts, lang: ctx.lang, }); - return [html, csp]; + return [html, renderState.renderUuid, csp]; } diff --git a/src/server/rendering/fresh_tags.tsx b/src/server/rendering/fresh_tags.tsx index bf0f41cf..baad2a7d 100644 --- a/src/server/rendering/fresh_tags.tsx +++ b/src/server/rendering/fresh_tags.tsx @@ -72,16 +72,20 @@ export function renderFreshTags( // The inline script that will hydrate the page. let script = ""; - // Serialize the state into the `; + ``; hasSignals = res.hasSignals; requiresDeserializer = res.requiresDeserializer; @@ -94,7 +98,7 @@ export function renderFreshTags( const url = addImport("signals.js"); script += `import { signal } from "${url}";`; } - script += `const ST = document.getElementById("__FRSH_STATE").textContent;`; + script += `const ST = document.getElementById("${stateId}").textContent;`; script += `const STATE = `; if (res.requiresDeserializer) { if (res.hasSignals) { @@ -166,7 +170,7 @@ export function renderFreshTags( ); const nonce = renderState.csp ? ` nonce="${renderState.getNonce()}` : ""; opts.bodyHtml += - ``; + ``; } if (script !== "") { opts.bodyHtml += diff --git a/src/server/rendering/state.ts b/src/server/rendering/state.ts index 2143c66e..82e3a922 100644 --- a/src/server/rendering/state.ts +++ b/src/server/rendering/state.ts @@ -16,6 +16,7 @@ export interface RenderStateRouteOptions { } export class RenderState { + readonly renderUuid: string; // deno-lint-ignore no-explicit-any componentStack: any[]; renderingUserTemplate = false; @@ -48,12 +49,14 @@ export class RenderState { basePath: string; constructor( + renderUuid: string, routeOptions: RenderStateRouteOptions, // deno-lint-ignore no-explicit-any componentStack: any[], csp?: ContentSecurityPolicy, error?: unknown, ) { + this.renderUuid = renderUuid; this.routeOptions = routeOptions; this.csp = csp; this.componentStack = componentStack; diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index bc2a6804..e60cd48d 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -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, diff --git a/tests/fixture/islands/DangerousIsland.tsx b/tests/fixture/islands/DangerousIsland.tsx new file mode 100644 index 00000000..287c481e --- /dev/null +++ b/tests/fixture/islands/DangerousIsland.tsx @@ -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
; +} diff --git a/tests/fixture/routes/spoof_state.tsx b/tests/fixture/routes/spoof_state.tsx new file mode 100644 index 00000000..0399ca21 --- /dev/null +++ b/tests/fixture/routes/spoof_state.tsx @@ -0,0 +1,5 @@ +import DangerousIsland from "../islands/DangerousIsland.tsx"; + +export default function SerializePrototype() { + return {.invalid.json}`} />; +} diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index d97abc56..ec5922e4 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -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, diff --git a/tests/fixture_partials/islands/DangerousIsland.tsx b/tests/fixture_partials/islands/DangerousIsland.tsx new file mode 100644 index 00000000..d2fd6cc6 --- /dev/null +++ b/tests/fixture_partials/islands/DangerousIsland.tsx @@ -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
; +} diff --git a/tests/fixture_partials/routes/spoof_state/index.tsx b/tests/fixture_partials/routes/spoof_state/index.tsx new file mode 100644 index 00000000..0ee8e71b --- /dev/null +++ b/tests/fixture_partials/routes/spoof_state/index.tsx @@ -0,0 +1,12 @@ +import { Partial } from "$fresh/runtime.ts"; + +export default function SerializePrototype() { + return ( +
+ +

initial

+
+ Update +
+ ); +} diff --git a/tests/fixture_partials/routes/spoof_state/partial.tsx b/tests/fixture_partials/routes/spoof_state/partial.tsx new file mode 100644 index 00000000..8884e759 --- /dev/null +++ b/tests/fixture_partials/routes/spoof_state/partial.tsx @@ -0,0 +1,11 @@ +import { Partial } from "$fresh/runtime.ts"; +import DangerousIsland from "../../islands/DangerousIsland.tsx"; + +export default function Res() { + return ( + + {.invalid.json}`} /> +

partial

+
+ ); +} diff --git a/tests/main_test.ts b/tests/main_test.ts index 6be9bdf5..252ac46b 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -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); + }); +}); diff --git a/tests/partials_test.ts b/tests/partials_test.ts index 8d6f03f0..d1d34d09 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -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); + }, + ); +}); diff --git a/tests/render_test.ts b/tests/render_test.ts index 067a7459..66356b65 100644 --- a/tests/render_test.ts +++ b/tests/render_test.ts @@ -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 }], []] }); }