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
+8 -4
View File
@@ -72,16 +72,20 @@ export function renderFreshTags(
// The inline script that will hydrate the page.
let script = "";
// Serialize the state into the <script id=__FRSH_STATE> tag and generate the
// Serialize the state into the <script id="__FRSH_STATE-<uuid>"> tag and generate the
// inline script to deserialize it. This script starts by deserializing the
// state in the tag. This potentially requires importing @preact/signals.
let hasSignals = false;
let requiresDeserializer = false;
if (state[0].length > 0 || state[1].length > 0) {
// Careful: This must be unique per render to avoid injected content
// via `dangerouslySetInnerHTML` being able to overwrite our state.
const stateId = `__FRSH_STATE_${renderState.renderUuid}`;
const res = serialize(state);
const escapedState = htmlEscapeJsonString(res.serialized);
opts.bodyHtml +=
`<script id="__FRSH_STATE" type="application/json" nonce="${renderState.getNonce()}">${escapedState}</script>`;
`<script id="${stateId}" type="application/json" nonce="${renderState.getNonce()}">${escapedState}</script>`;
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 +=
`<script id="__FRSH_PARTIAL_DATA" type="application/json"${nonce}">${escapedData}</script>`;
`<script id="__FRSH_PARTIAL_DATA_${renderState.renderUuid}" type="application/json"${nonce}">${escapedData}</script>`;
}
if (script !== "") {
opts.bodyHtml +=
+3
View File
@@ -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;