From 8416078b6a537baa3d41cb069edb355671305563 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 30 Jun 2025 16:31:58 +0200 Subject: [PATCH] fix(core): improve island props serialization (#3071) --- src/runtime/server/preact_hooks.tsx | 10 ++-- src/utils.ts | 13 +++++ src/utils_test.ts | 43 ++++++++++++++- tests/fixtures_islands/EscapeIsland.tsx | 5 +- tests/islands_test.tsx | 71 ++++++++++++++++++++++++- 5 files changed, 135 insertions(+), 7 deletions(-) diff --git a/src/runtime/server/preact_hooks.tsx b/src/runtime/server/preact_hooks.tsx index 1e85b493..d31a9e01 100644 --- a/src/runtime/server/preact_hooks.tsx +++ b/src/runtime/server/preact_hooks.tsx @@ -32,6 +32,7 @@ import { import { escape as escapeHtml } from "@std/html"; import { HttpError } from "../../error.ts"; import { getCodeFrame } from "../../dev/middlewares/error_overlay/code_frame.tsx"; +import { escapeScript } from "../../utils.ts"; const enum OptionsType { ATTR = "attr", @@ -455,7 +456,9 @@ function FreshRuntimeScript() { id={`__FRSH_STATE_${partialId}`} type="application/json" // deno-lint-ignore react-no-danger - dangerouslySetInnerHTML={{ __html: JSON.stringify(json) }} + dangerouslySetInnerHTML={{ + __html: escapeScript(JSON.stringify(json), { json: true }), + }} /> ); } else { @@ -476,8 +479,9 @@ function FreshRuntimeScript() { .join(",") + "}"; - const serializedProps = JSON.stringify( - stringify(islandProps, stringifiers), + const serializedProps = escapeScript( + JSON.stringify(stringify(islandProps, stringifiers)), + { json: true }, ); const runtimeUrl = `${basePath}/_fresh/js/${BUILD_ID}/fresh-runtime.js`; diff --git a/src/utils.ts b/src/utils.ts index 1a5321c8..a98f8e36 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -53,3 +53,16 @@ export function pathToExportName(filePath: string): string { const regex = /^[^a-z_$]|[^a-z0-9_$]/gi; return name.replaceAll(regex, "_").replaceAll(/_{2,}/g, "_"); } + +const SCRIPT_ESCAPE = /<\/(style|script)/gi; +const COMMENT_ESCAPE = /