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
+3 -2
View File
@@ -640,6 +640,7 @@ class NoPartialsError extends Error {}
*/
export async function applyPartials(res: Response): Promise<void> {
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<void> {
// 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<string, { export: string; url: string }>;
signals: string | null;
@@ -669,7 +670,7 @@ export async function applyPartials(res: Response): Promise<void> {
);
}
const stateDom = doc.getElementById("__FRSH_STATE")?.textContent;
const stateDom = doc.getElementById(`__FRSH_STATE_${uuid}`)?.textContent;
let state: SerializedState = [[], []];
// Load all dependencies