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:
@@ -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<string, string> = {
|
||||
"content-type": "text/html; charset=utf-8",
|
||||
"x-fresh-uuid": uuid,
|
||||
};
|
||||
|
||||
const [body, csp] = resp;
|
||||
if (csp) {
|
||||
if (options.isDev) {
|
||||
csp.directives.connectSrc = [
|
||||
|
||||
@@ -122,7 +122,7 @@ export function checkAsyncComponent<T>(
|
||||
*/
|
||||
export async function render<Data>(
|
||||
opts: RenderOptions<Data>,
|
||||
): 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<Data>(
|
||||
// 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<Data>(
|
||||
moduleScripts: result.moduleScripts,
|
||||
lang: ctx.lang,
|
||||
});
|
||||
return [html, csp];
|
||||
return [html, renderState.renderUuid, csp];
|
||||
}
|
||||
|
||||
@@ -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 +=
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user