@crombolo/render 0.59.0 → 0.61.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/safe-html-lists.ts +145 -0
- package/src/safe-html.ts +248 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.61.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@crombolo/common": "0.
|
|
39
|
-
"@crombolo/core": "0.
|
|
38
|
+
"@crombolo/common": "0.61.0",
|
|
39
|
+
"@crombolo/core": "0.61.0",
|
|
40
40
|
"@types/react": "^19.3.0"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,8 @@ export * from './model.ts'
|
|
|
24
24
|
export * from './published-cache.ts'
|
|
25
25
|
export * from './redirects.ts'
|
|
26
26
|
export * from './renderer.ts'
|
|
27
|
+
export * from './safe-html.ts'
|
|
28
|
+
export * from './safe-html-lists.ts'
|
|
27
29
|
export * from './search.ts'
|
|
28
30
|
export * from './seo.ts'
|
|
29
31
|
export * from './sign-in.ts'
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTML Sanitizer API's built-in safe baseline (story 330), copied from the
|
|
3
|
+
* standard's own data so the rule is the standard's, not ours: what every safe
|
|
4
|
+
* `setHTML()` removes whatever else it is told to keep.
|
|
5
|
+
*
|
|
6
|
+
* Source: WICG/sanitizer-api `builtins/safe-baseline-configuration.json` and
|
|
7
|
+
* `builtins/event-handler-content-attributes.txt` at c56c3d7a95e5 (2026-08-31),
|
|
8
|
+
* merged into WHATWG HTML, "HTML sanitization"
|
|
9
|
+
* (https://html.spec.whatwg.org/#html-sanitization). Copied 2026-11-11.
|
|
10
|
+
* `tests/safe-html.test.ts` holds a copy of each list and fails when they part.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const HTML_NS = 'html'
|
|
14
|
+
export const SVG_NS = 'svg'
|
|
15
|
+
export const MATHML_NS = 'math'
|
|
16
|
+
|
|
17
|
+
/** `removeElements`: each removed with everything inside it, by namespace. */
|
|
18
|
+
export const BASELINE_REMOVE_ELEMENTS: readonly { namespace: string; name: string }[] = [
|
|
19
|
+
{ namespace: HTML_NS, name: 'embed' },
|
|
20
|
+
{ namespace: HTML_NS, name: 'frame' },
|
|
21
|
+
{ namespace: HTML_NS, name: 'iframe' },
|
|
22
|
+
{ namespace: HTML_NS, name: 'object' },
|
|
23
|
+
{ namespace: HTML_NS, name: 'script' },
|
|
24
|
+
{ namespace: SVG_NS, name: 'script' },
|
|
25
|
+
{ namespace: SVG_NS, name: 'use' },
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
/** Every event handler content attribute, which safe sanitization always removes. */
|
|
29
|
+
export const EVENT_HANDLER_ATTRIBUTES: readonly string[] = [
|
|
30
|
+
'onafterprint',
|
|
31
|
+
'onauxclick',
|
|
32
|
+
'onbeforeinput',
|
|
33
|
+
'onbeforematch',
|
|
34
|
+
'onbeforeprint',
|
|
35
|
+
'onbeforeunload',
|
|
36
|
+
'onbeforetoggle',
|
|
37
|
+
'onblur',
|
|
38
|
+
'oncancel',
|
|
39
|
+
'oncanplay',
|
|
40
|
+
'oncanplaythrough',
|
|
41
|
+
'onchange',
|
|
42
|
+
'onclick',
|
|
43
|
+
'onclose',
|
|
44
|
+
'oncontextlost',
|
|
45
|
+
'oncontextmenu',
|
|
46
|
+
'oncontextrestored',
|
|
47
|
+
'oncopy',
|
|
48
|
+
'oncuechange',
|
|
49
|
+
'oncut',
|
|
50
|
+
'ondblclick',
|
|
51
|
+
'ondrag',
|
|
52
|
+
'ondragend',
|
|
53
|
+
'ondragenter',
|
|
54
|
+
'ondragleave',
|
|
55
|
+
'ondragover',
|
|
56
|
+
'ondragstart',
|
|
57
|
+
'ondrop',
|
|
58
|
+
'ondurationchange',
|
|
59
|
+
'onemptied',
|
|
60
|
+
'onended',
|
|
61
|
+
'onerror',
|
|
62
|
+
'onfocus',
|
|
63
|
+
'onformdata',
|
|
64
|
+
'onhashchange',
|
|
65
|
+
'oninput',
|
|
66
|
+
'oninvalid',
|
|
67
|
+
'onkeydown',
|
|
68
|
+
'onkeypress',
|
|
69
|
+
'onkeyup',
|
|
70
|
+
'onlanguagechange',
|
|
71
|
+
'onload',
|
|
72
|
+
'onloadeddata',
|
|
73
|
+
'onloadedmetadata',
|
|
74
|
+
'onloadstart',
|
|
75
|
+
'onmessage',
|
|
76
|
+
'onmessageerror',
|
|
77
|
+
'onmousedown',
|
|
78
|
+
'onmouseenter',
|
|
79
|
+
'onmouseleave',
|
|
80
|
+
'onmousemove',
|
|
81
|
+
'onmouseout',
|
|
82
|
+
'onmouseover',
|
|
83
|
+
'onmouseup',
|
|
84
|
+
'onoffline',
|
|
85
|
+
'ononline',
|
|
86
|
+
'onpagehide',
|
|
87
|
+
'onpagereveal',
|
|
88
|
+
'onpageshow',
|
|
89
|
+
'onpageswap',
|
|
90
|
+
'onpaste',
|
|
91
|
+
'onpause',
|
|
92
|
+
'onplay',
|
|
93
|
+
'onplaying',
|
|
94
|
+
'onpopstate',
|
|
95
|
+
'onprogress',
|
|
96
|
+
'onratechange',
|
|
97
|
+
'onreset',
|
|
98
|
+
'onresize',
|
|
99
|
+
'onrejectionhandled',
|
|
100
|
+
'onscroll',
|
|
101
|
+
'onscrollend',
|
|
102
|
+
'onsecuritypolicyviolation',
|
|
103
|
+
'onseeked',
|
|
104
|
+
'onseeking',
|
|
105
|
+
'onselect',
|
|
106
|
+
'onslotchange',
|
|
107
|
+
'onstalled',
|
|
108
|
+
'onstorage',
|
|
109
|
+
'onsubmit',
|
|
110
|
+
'onsuspend',
|
|
111
|
+
'ontimeupdate',
|
|
112
|
+
'ontoggle',
|
|
113
|
+
'onunhandledrejection',
|
|
114
|
+
'onunload',
|
|
115
|
+
'onvolumechange',
|
|
116
|
+
'onwaiting',
|
|
117
|
+
'onwheel',
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The built-in navigating URL attributes: an element's attribute that is
|
|
122
|
+
* dropped when its value is a `javascript:` URL. HTML's entries come from each
|
|
123
|
+
* element's definition (`a`, `area` `href`; `form` `action`; `button`, `input`
|
|
124
|
+
* `formaction`); SVG's from the table. MathML's `href` is checked on any element.
|
|
125
|
+
*/
|
|
126
|
+
export const NAVIGATING_URL_ATTRIBUTES: readonly {
|
|
127
|
+
namespace: string
|
|
128
|
+
element: string
|
|
129
|
+
attribute: string
|
|
130
|
+
}[] = [
|
|
131
|
+
{ namespace: HTML_NS, element: 'a', attribute: 'href' },
|
|
132
|
+
{ namespace: HTML_NS, element: 'area', attribute: 'href' },
|
|
133
|
+
{ namespace: HTML_NS, element: 'form', attribute: 'action' },
|
|
134
|
+
{ namespace: HTML_NS, element: 'button', attribute: 'formaction' },
|
|
135
|
+
{ namespace: HTML_NS, element: 'input', attribute: 'formaction' },
|
|
136
|
+
{ namespace: SVG_NS, element: 'a', attribute: 'href' },
|
|
137
|
+
{ namespace: SVG_NS, element: 'a', attribute: 'xlink:href' },
|
|
138
|
+
]
|
|
139
|
+
|
|
140
|
+
/** The built-in animating URL attributes: dropped when they would animate `href`. */
|
|
141
|
+
export const ANIMATING_URL_ATTRIBUTES: readonly { element: string; attribute: string }[] = [
|
|
142
|
+
{ element: 'animate', attribute: 'attributeName' },
|
|
143
|
+
{ element: 'animateTransform', attribute: 'attributeName' },
|
|
144
|
+
{ element: 'set', attribute: 'attributeName' },
|
|
145
|
+
]
|
package/src/safe-html.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An extension component's HTML with what can run script taken out (story
|
|
3
|
+
* 330), by the HTML Sanitizer API's safe baseline (`safe-html-lists.ts`): the
|
|
4
|
+
* elements it removes with what is inside them, every event handler attribute,
|
|
5
|
+
* and a `javascript:` URL on a navigating or animating attribute. Everything
|
|
6
|
+
* else, classes, styles and images included, is kept as written.
|
|
7
|
+
*
|
|
8
|
+
* One pass over the string that builds no tree: tags and their attributes are
|
|
9
|
+
* read, text is copied as it is. A `<` that opens no tag the browser would
|
|
10
|
+
* close is written as `<`, so markup cut short cannot run on into the page's
|
|
11
|
+
* own. docs/extensions.md, "Components".
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
ANIMATING_URL_ATTRIBUTES,
|
|
15
|
+
BASELINE_REMOVE_ELEMENTS,
|
|
16
|
+
EVENT_HANDLER_ATTRIBUTES,
|
|
17
|
+
HTML_NS,
|
|
18
|
+
MATHML_NS,
|
|
19
|
+
NAVIGATING_URL_ATTRIBUTES,
|
|
20
|
+
SVG_NS,
|
|
21
|
+
} from './safe-html-lists.ts'
|
|
22
|
+
|
|
23
|
+
/** What was taken out, for the log: an element, or an attribute on one. */
|
|
24
|
+
export type SafeHtmlReport = (kind: 'element' | 'attribute', name: string) => void
|
|
25
|
+
|
|
26
|
+
const HANDLERS = new Set(EVENT_HANDLER_ATTRIBUTES)
|
|
27
|
+
const REMOVED = new Set(BASELINE_REMOVE_ELEMENTS.map((entry) => `${entry.namespace} ${entry.name}`))
|
|
28
|
+
const NAVIGATING = new Set(
|
|
29
|
+
NAVIGATING_URL_ATTRIBUTES.map(
|
|
30
|
+
(entry) => `${entry.namespace} ${entry.element} ${entry.attribute}`,
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
const ANIMATING = new Set(
|
|
34
|
+
ANIMATING_URL_ATTRIBUTES.map(
|
|
35
|
+
(entry) => `${entry.element.toLowerCase()} ${entry.attribute.toLowerCase()}`,
|
|
36
|
+
),
|
|
37
|
+
)
|
|
38
|
+
/** Elements whose content is text up to their end tag, not markup. */
|
|
39
|
+
const RAW_TEXT = new Set([
|
|
40
|
+
'script',
|
|
41
|
+
'style',
|
|
42
|
+
'textarea',
|
|
43
|
+
'title',
|
|
44
|
+
'xmp',
|
|
45
|
+
'noembed',
|
|
46
|
+
'noframes',
|
|
47
|
+
'iframe',
|
|
48
|
+
'noscript',
|
|
49
|
+
'plaintext',
|
|
50
|
+
])
|
|
51
|
+
const VOID = new Set([
|
|
52
|
+
'area',
|
|
53
|
+
'base',
|
|
54
|
+
'br',
|
|
55
|
+
'col',
|
|
56
|
+
'embed',
|
|
57
|
+
'frame',
|
|
58
|
+
'hr',
|
|
59
|
+
'img',
|
|
60
|
+
'input',
|
|
61
|
+
'link',
|
|
62
|
+
'meta',
|
|
63
|
+
'source',
|
|
64
|
+
'track',
|
|
65
|
+
'wbr',
|
|
66
|
+
])
|
|
67
|
+
|
|
68
|
+
const NAMED: Readonly<Record<string, string>> = {
|
|
69
|
+
colon: ':',
|
|
70
|
+
tab: '\t',
|
|
71
|
+
newline: '\n',
|
|
72
|
+
amp: '&',
|
|
73
|
+
lt: '<',
|
|
74
|
+
gt: '>',
|
|
75
|
+
quot: '"',
|
|
76
|
+
apos: "'",
|
|
77
|
+
sol: '/',
|
|
78
|
+
lpar: '(',
|
|
79
|
+
rpar: ')',
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** An attribute's value as the browser reads it: character references decoded. */
|
|
83
|
+
function decodeReferences(value: string): string {
|
|
84
|
+
return value.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);?/gi, (whole, ref: string) => {
|
|
85
|
+
if (ref[0] === '#') {
|
|
86
|
+
const code =
|
|
87
|
+
ref[1]?.toLowerCase() === 'x' ? Number.parseInt(ref.slice(2), 16) : Number(ref.slice(1))
|
|
88
|
+
return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : ''
|
|
89
|
+
}
|
|
90
|
+
return NAMED[ref.toLowerCase()] ?? whole
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Whether a URL is a `javascript:` one, as the URL parser would read it. */
|
|
95
|
+
export function isJavascriptUrl(value: string): boolean {
|
|
96
|
+
let read = decodeReferences(value)
|
|
97
|
+
// The parser strips leading and trailing C0 controls and spaces, and every tab and newline.
|
|
98
|
+
let start = 0
|
|
99
|
+
let end = read.length
|
|
100
|
+
while (start < end && read.charCodeAt(start) <= 0x20) start++
|
|
101
|
+
while (end > start && read.charCodeAt(end - 1) <= 0x20) end--
|
|
102
|
+
read = read.slice(start, end).replace(/[\t\n\r]/g, '')
|
|
103
|
+
return /^javascript:/i.test(read)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const ATTRIBUTE = /([^\s"'>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g
|
|
107
|
+
|
|
108
|
+
interface Tag {
|
|
109
|
+
name: string
|
|
110
|
+
/** As written, up to and including its `>`. */
|
|
111
|
+
raw: string
|
|
112
|
+
end: number
|
|
113
|
+
closing: boolean
|
|
114
|
+
selfClosing: boolean
|
|
115
|
+
attributes: { name: string; value: string | undefined; raw: string }[]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** A tag starting at `at`, or undefined when what is there is not one the browser would close. */
|
|
119
|
+
function readTag(html: string, at: number): Tag | undefined {
|
|
120
|
+
const match = /^<(\/?)([A-Za-z][^\s/>]*)/.exec(html.slice(at, at + 256))
|
|
121
|
+
if (!match) return undefined
|
|
122
|
+
let i = at + match[0].length
|
|
123
|
+
let quote: string | undefined
|
|
124
|
+
for (; i < html.length; i++) {
|
|
125
|
+
const c = html[i]
|
|
126
|
+
if (quote) {
|
|
127
|
+
if (c === quote) quote = undefined
|
|
128
|
+
} else if (c === '"' || c === "'") {
|
|
129
|
+
// Only a quote after `=` opens a value.
|
|
130
|
+
if (/=\s*$/.test(html.slice(Math.max(at, i - 32), i))) quote = c
|
|
131
|
+
} else if (c === '>') break
|
|
132
|
+
}
|
|
133
|
+
if (i >= html.length) return undefined
|
|
134
|
+
const raw = html.slice(at, i + 1)
|
|
135
|
+
const inside = raw.slice(match[0].length, -1)
|
|
136
|
+
const attributes: Tag['attributes'] = []
|
|
137
|
+
for (const found of inside.matchAll(ATTRIBUTE)) {
|
|
138
|
+
const value = found[2] ?? found[3] ?? found[4]
|
|
139
|
+
attributes.push({ name: (found[1] as string).toLowerCase(), value, raw: found[0] })
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
name: (match[2] as string).toLowerCase(),
|
|
143
|
+
raw,
|
|
144
|
+
end: i + 1,
|
|
145
|
+
closing: match[1] === '/',
|
|
146
|
+
selfClosing: /\/\s*$/.test(inside),
|
|
147
|
+
attributes,
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Where the end tag of a raw-text element starts, or the end of the string. */
|
|
152
|
+
function rawEnd(html: string, from: number, name: string): number {
|
|
153
|
+
const found = html.slice(from).search(new RegExp(`</${name}[\\s/>]`, 'i'))
|
|
154
|
+
return found === -1 ? html.length : from + found
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Where the element opened at `from` ends, its own end tag included, counting nested ones. */
|
|
158
|
+
function elementEnd(html: string, from: number, name: string): number {
|
|
159
|
+
let depth = 1
|
|
160
|
+
const pattern = new RegExp(`<(/?)${name}(?=[\\s/>])[^>]*>`, 'gi')
|
|
161
|
+
pattern.lastIndex = from
|
|
162
|
+
for (let found = pattern.exec(html); found; found = pattern.exec(html)) {
|
|
163
|
+
if (found[1] === '/') depth--
|
|
164
|
+
else if (!/\/\s*>$/.test(found[0])) depth++
|
|
165
|
+
if (depth === 0) return found.index + found[0].length
|
|
166
|
+
}
|
|
167
|
+
return html.length
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function sanitiseHtml(html: string, report: SafeHtmlReport = () => {}): string {
|
|
171
|
+
let out = ''
|
|
172
|
+
let i = 0
|
|
173
|
+
/** Foreign content, innermost last: SVG and MathML change what a name means. */
|
|
174
|
+
const foreign: string[] = []
|
|
175
|
+
const namespace = () => foreign.at(-1) ?? HTML_NS
|
|
176
|
+
while (i < html.length) {
|
|
177
|
+
const next = html.indexOf('<', i)
|
|
178
|
+
if (next === -1) {
|
|
179
|
+
out += html.slice(i)
|
|
180
|
+
break
|
|
181
|
+
}
|
|
182
|
+
out += html.slice(i, next)
|
|
183
|
+
i = next
|
|
184
|
+
if (html.startsWith('<!--', i)) {
|
|
185
|
+
const close = html.indexOf('-->', i + 4)
|
|
186
|
+
const stop = close === -1 ? html.length : close + 3
|
|
187
|
+
out += html.slice(i, stop)
|
|
188
|
+
i = stop
|
|
189
|
+
continue
|
|
190
|
+
}
|
|
191
|
+
if (/^<[!?]/.test(html.slice(i, i + 2))) {
|
|
192
|
+
const close = html.indexOf('>', i)
|
|
193
|
+
const stop = close === -1 ? html.length : close + 1
|
|
194
|
+
out += html.slice(i, stop)
|
|
195
|
+
i = stop
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
const tag = readTag(html, i)
|
|
199
|
+
if (!tag) {
|
|
200
|
+
out += '<'
|
|
201
|
+
i += 1
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
const ns = tag.name === 'svg' ? SVG_NS : tag.name === 'math' ? MATHML_NS : namespace()
|
|
205
|
+
if (tag.closing) {
|
|
206
|
+
if ((tag.name === 'svg' || tag.name === 'math') && foreign.at(-1) === tag.name) foreign.pop()
|
|
207
|
+
out += tag.raw
|
|
208
|
+
i = tag.end
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
if (REMOVED.has(`${ns} ${tag.name}`)) {
|
|
212
|
+
report('element', tag.name)
|
|
213
|
+
i =
|
|
214
|
+
VOID.has(tag.name) || tag.selfClosing
|
|
215
|
+
? tag.end
|
|
216
|
+
: RAW_TEXT.has(tag.name)
|
|
217
|
+
? Math.min(html.length, elementEnd(html, rawEnd(html, tag.end, tag.name), tag.name))
|
|
218
|
+
: elementEnd(html, tag.end, tag.name)
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
const kept: string[] = []
|
|
222
|
+
for (const attribute of tag.attributes) {
|
|
223
|
+
const value = attribute.value ?? ''
|
|
224
|
+
const dropped =
|
|
225
|
+
HANDLERS.has(attribute.name) ||
|
|
226
|
+
(NAVIGATING.has(`${ns} ${tag.name} ${attribute.name}`) && isJavascriptUrl(value)) ||
|
|
227
|
+
(ns === MATHML_NS &&
|
|
228
|
+
(attribute.name === 'href' || attribute.name === 'xlink:href') &&
|
|
229
|
+
isJavascriptUrl(value)) ||
|
|
230
|
+
(ns === SVG_NS &&
|
|
231
|
+
ANIMATING.has(`${tag.name} ${attribute.name}`) &&
|
|
232
|
+
/^(xlink:)?href$/i.test(decodeReferences(value).trim()))
|
|
233
|
+
if (dropped) report('attribute', `${tag.name} ${attribute.name}`)
|
|
234
|
+
else kept.push(attribute.raw)
|
|
235
|
+
}
|
|
236
|
+
const written = tag.raw.slice(0, 1 + tag.name.length).toLowerCase()
|
|
237
|
+
out += `${written}${kept.length > 0 ? ` ${kept.join(' ')}` : ''}${tag.selfClosing ? ' /' : ''}>`
|
|
238
|
+
i = tag.end
|
|
239
|
+
if ((tag.name === 'svg' || tag.name === 'math') && !tag.selfClosing) foreign.push(tag.name)
|
|
240
|
+
// A raw-text element's content is text, not markup: copied up to its end tag.
|
|
241
|
+
if (ns === HTML_NS && RAW_TEXT.has(tag.name) && !tag.selfClosing) {
|
|
242
|
+
const stop = rawEnd(html, i, tag.name)
|
|
243
|
+
out += html.slice(i, stop)
|
|
244
|
+
i = stop
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return out
|
|
248
|
+
}
|