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
@@ -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];
}