Skip to content

Commit ec88772

Browse files
Enforce HtmlHead link and document data attribute restrictions at runtime (#11424)
Co-authored-by: StarbirdTech <64431622+StarbirdTech@users.noreply.github.com>
1 parent 890703f commit ec88772

7 files changed

Lines changed: 41 additions & 26 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
`HtmlHead.link` now omits `nonce` and event attributes supplied through extra runtime keys. The `html.data` and `body.data` records now emit only valid `data-*` attributes, excluding the host-managed `data-eui` marker.

‎packages/effect/src/ui/html/HtmlHead.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ export const identity = (entry: Meta | Link | JsonLd): string => {
652652
}
653653

654654
/**
655-
* The attributes of a meta or link tag, in a stable order, without the
656-
* marker.
655+
* Returns a meta or link tag's attribute pairs, excluding `data-eui`.
656+
* For links, also excludes `nonce` and event attributes.
657657
*
658658
* @stability unstable
659659
* @category utils
@@ -683,12 +683,8 @@ const slotsOf = (head: HtmlHead): ReadonlyMap<string, string> => {
683683
}
684684
for (const entry of head.links) out.set(identity(entry), linkSignature(entry))
685685
for (const entry of head.jsonLd) out.set(identity(entry), entry.json)
686-
for (const name in head.html.data) {
687-
out.set(dataSlot("html", name), head.html.data[name]!)
688-
}
689-
for (const name in head.body.data) {
690-
out.set(dataSlot("body", name), head.body.data[name]!)
691-
}
686+
for (const [name, value] of headAttributes.data(head.html.data)) out.set(dataSlot("html", name), value)
687+
for (const [name, value] of headAttributes.data(head.body.data)) out.set(dataSlot("body", name), value)
692688
return out
693689
}
694690

@@ -751,12 +747,8 @@ const combine = (
751747
if (child.html.dir !== undefined) dirValue = child.html.dir
752748
for (const token of child.html.class) htmlClasses.add(token)
753749
for (const token of child.body.class) bodyClasses.add(token)
754-
for (const [name, value] of Object.entries(child.html.data)) {
755-
htmlDataMap.set(name, value)
756-
}
757-
for (const [name, value] of Object.entries(child.body.data)) {
758-
bodyDataMap.set(name, value)
759-
}
750+
for (const [name, value] of headAttributes.data(child.html.data)) htmlDataMap.set(name, value)
751+
for (const [name, value] of headAttributes.data(child.body.data)) bodyDataMap.set(name, value)
760752
}
761753
if (conflicted.has("lang")) lang = undefined
762754
if (conflicted.has("dir")) dirValue = undefined

‎packages/effect/src/ui/html/internal/head.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ export const makeSync = (
220220
}
221221
}
222222
for (
223-
const [name, value] of headAttributes.normalize(
224-
Object.entries(want.data)
225-
)
223+
const [name, value] of headAttributes.data(want.data)
226224
) {
227225
if (
228226
names.includes(name) && owner.element.getAttribute(name) !== value
@@ -278,7 +276,7 @@ export const makeSync = (
278276
}
279277
owner.tokens = tokens
280278
if (tokens.size > 0) set.push("class")
281-
const pairs = headAttributes.normalize(Object.entries(want.data))
279+
const pairs = headAttributes.data(want.data)
282280
const data = new Set(pairs.map(([name]) => name))
283281
for (const name of owner.data) {
284282
if (!data.has(name)) attribute(owner, name, undefined)

‎packages/effect/src/ui/html/internal/headAttributes.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ export const normalize = (
2727
return [...out]
2828
}
2929

30-
/** The attributes of a meta or link tag, in a stable order, without the marker. @internal */
30+
/** Normalized `data-*` attributes with non-empty suffixes, excluding `data-eui`. @internal */
31+
export const data = (
32+
record: Readonly<Record<string, string>>
33+
): ReadonlyArray<readonly [string, string]> =>
34+
normalize(Object.entries(record)).filter(([name]) => name.length > 5 && name.startsWith("data-"))
35+
36+
/** @internal */
3137
export const attributes = (
3238
entry: HtmlHead.Meta | HtmlHead.Link
3339
): ReadonlyArray<readonly [string, string]> => {
@@ -40,8 +46,11 @@ export const attributes = (
4046
]
4147
}
4248
const out: Array<readonly [string, string]> = []
43-
for (const [name, value] of Object.entries(entry.attributes)) {
44-
if (value !== undefined) out.push([name, value])
49+
for (const [key, value] of Object.entries(entry.attributes)) {
50+
const normalized = name(key)
51+
if (
52+
value !== undefined && normalized !== undefined && normalized !== "nonce" && !normalized.startsWith("on")
53+
) out.push([key, value])
4554
}
4655
return out
4756
}

‎packages/effect/src/ui/html/internal/server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ export const bodyOwned = (
443443
...(head.body.class.length === 0
444444
? []
445445
: [["class", head.body.class.join(" ")] as const]),
446-
...Object.entries(head.body.data)
446+
...headAttributes.data(head.body.data)
447447
]
448448

449449
/** Escaped attributes, plus the marker listing their names; empty when the head sets none. */

‎packages/effect/test/ui/html/Head.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ describe("BrowserHost head", { concurrent: false }, () => {
9494
href: "/first",
9595
...injected,
9696
HREF: "/last",
97-
"data-EUI": "fake"
97+
"data-EUI": "fake",
98+
onLoad: "alert(1)",
99+
NONCE: "n"
98100
}
99101
const entries = [
100102
HtmlHead.link(linkAttributes),
@@ -106,7 +108,12 @@ describe("BrowserHost head", { concurrent: false }, () => {
106108
HtmlHead.bodyData("data-theme", "middle"),
107109
HtmlHead.bodyData("data-THEME", "a\"<&"),
108110
HtmlHead.htmlData("data-EUI", "fake"),
109-
HtmlHead.bodyData("data-EUI", "fake")
111+
HtmlHead.bodyData("data-EUI", "fake"),
112+
HtmlHead.reduce((below) => ({
113+
...below,
114+
html: { ...below.html, data: { ...below.html.data, title: "x", "data-": "x" } },
115+
body: { ...below.body, data: { ...below.body.data, title: "x", "data-": "x" } }
116+
}))
110117
]
111118
const rendered = await render(entries)
112119
const invalidOnly = await render(unsafeData)

‎packages/effect/test/ui/html/HtmlHead.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ describe("HtmlHead", () => {
2727
HtmlHead.link({ rel: "preload", href: "/font.woff2", as: "font" }),
2828
HtmlHead.jsonLd({ a: 1 }, { key: "page" }),
2929
HtmlHead.htmlData("data-theme", "light"),
30-
HtmlHead.htmlClass("a")
30+
HtmlHead.htmlClass("a"),
31+
HtmlHead.reduce((below) => ({ ...below, body: { ...below.body, data: { "data-THEME": "a", title: "a" } } }))
3132
])
3233
const b = at("/App/B[b]", [
3334
HtmlHead.lang("fr"),
@@ -38,7 +39,8 @@ describe("HtmlHead", () => {
3839
HtmlHead.link({ as: "font", href: "/font.woff2", rel: "preload" }),
3940
HtmlHead.jsonLd({ b: 1 }, { key: "page" }),
4041
HtmlHead.htmlData("data-theme", "dark"),
41-
HtmlHead.htmlClass("b")
42+
HtmlHead.htmlClass("b"),
43+
HtmlHead.reduce((below) => ({ ...below, body: { ...below.body, data: { "data-theme": "b", title: "b" } } }))
4244
])
4345
const fold = (children: ReadonlyArray<Head.Tree<HtmlHead.Entry>>) => {
4446
const conflicts: Array<Head.Conflict> = []
@@ -66,6 +68,7 @@ describe("HtmlHead", () => {
6668
assert.strictEqual(folded.head.title, "App")
6769
assert.strictEqual(folded.head.html.lang, undefined)
6870
assert.deepStrictEqual(folded.head.html.data, {})
71+
assert.deepStrictEqual(folded.head.body.data, {})
6972
assert.deepStrictEqual(folded.head.meta.map((meta) => meta.content), [
7073
"Ada",
7174
"Site"
@@ -84,6 +87,7 @@ describe("HtmlHead", () => {
8487
assert.deepStrictEqual(folded.head.html.class, ["app", "a", "b"])
8588
const both = ["/App/A[a]", "/App/B[b]"]
8689
assert.deepStrictEqual(folded.conflicts, [
90+
["body data-theme", both],
8791
["html data-theme", both],
8892
["jsonld key:page", both],
8993
["lang", both],

0 commit comments

Comments
 (0)