Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: the svg style text pass judges a call after a colon unless it is a selector function (#582)

nyblnet70af47ecb583
diff --git a/scripts/test-sanitize.ts b/scripts/test-sanitize.ts
index 5bf2a180..ee735d5b 100644
--- a/scripts/test-sanitize.ts
+++ b/scripts/test-sanitize.ts
@@ -199,6 +199,24 @@ for (const at of ['@media screen{.p{fill:red}}', '@supports (fill:red){.p{fill:r
ok(sanitizeSvgCss('.p::before{content:"a@b"}') === '.p::before{content:"a@b"}',
'an @ that does not start a token is not an at-rule')
+// A call written straight after a colon is a SELECTOR only if its name is a
+// pseudo-class/-element function; any other is a value, judged by the function
+// allowlist like a call anywhere else. The text pass used to spare every
+// colon-preceded call, leaving `prop:fn(` (no space) to the CSSOM check alone.
+// `bento-probe` stands in for any function off the allowlist.
+for (const sel of ['.a:not(.b){fill:red}', '.a:nth-child(2n+1):is(.b,.c){fill:red}', '.a:where(.b):has(.c){fill:red}',
+ '.a::part(x){fill:red}', '::view-transition-group(x){opacity:1}', '.a:lang(ja):dir(rtl){fill:red}']) {
+ ok(sanitizeSvgCss(sel) === sel, `selector functions after a colon are kept: ${sel.slice(0, sel.indexOf('{'))}`)
+}
+ok(sanitizeSvgCss('.p{fill:rgb(1,2,3);width:calc(1px + 2px)}') === '.p{fill:rgb(1,2,3);width:calc(1px + 2px)}',
+ 'allowed value functions straight after a colon are kept')
+ok(sanitizeSvgCss('.p{fill:bento-probe(1)}').includes('fill:bento-refused('),
+ 'an off-list function straight after a property colon is refused by the text pass')
+ok(sanitizeSvgCss('.p{fill: bento-probe(1)}').includes('bento-refused('),
+ '…as it already was with a space')
+ok(sanitizeSvgCss('.p{fill:not(1)}') === '.p{fill:not(1)}',
+ 'a pseudo name used as a value is spared (it can fetch nothing, and CSS drops it as invalid)')
+
// --- 2. the table, end to end ------------------------------------------------
//
// renderTableHtml is a string builder with no DOM in it, so the real output can
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 3a00475a..3f65b62f 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -664,9 +664,34 @@ const CSS_FN_ALLOWED = new Set([
'url',
])
-/** A function call in CSS text: a name (letters, escapes, non-ASCII) then `(`,
- * not after a colon — `:not(`, `:nth-child(` are selectors, not values. */
-const CSS_FN_CALL = /(^|[^:\w\\-])((?:[-\w]|\\[\s\S]|[^\x00-\x7f])+)\(/g
+/** A function call in CSS text: a name (letters, escapes, non-ASCII) then `(`.
+ * `pre` is the character before it, so a caller can tell a call written
+ * straight after a colon. */
+const CSS_FN_CALL = /(^|[^\w\\-])((?:[-\w]|\\[\s\S]|[^\x00-\x7f])+)\(/g
+
+/**
+ * Selector pseudo-classes and pseudo-elements that take an argument. After a
+ * colon, a name on THIS list is a selector (`:not(`, `::part(`), not a value;
+ * none of them can fetch. Every other call after a colon is a value
+ * (`fill:rgb(`, `width:calc(`) and is judged by CSS_FN_ALLOWED like any other.
+ * The text pass used to spare EVERY colon-preceded call, which left
+ * `prop:fn(` with no space to the CSSOM check alone — caught there in every
+ * browser, but this pass is meant to fail closed on its own.
+ */
+const CSS_PSEUDO_FNS = new Set([
+ 'not', 'is', 'where', 'has', 'matches', '-webkit-any', '-moz-any',
+ 'nth-child', 'nth-last-child', 'nth-of-type', 'nth-last-of-type', 'nth-col', 'nth-last-col',
+ 'lang', 'dir', 'host', 'host-context', 'state', 'active-view-transition-type',
+ 'slotted', 'part', 'cue', 'cue-region', 'highlight',
+ 'view-transition-group', 'view-transition-image-pair', 'view-transition-old', 'view-transition-new',
+])
+
+/** May this sheet call `name`? `pre` is the character in front of it. */
+function cssCallAllowed(pre: string, name: string): boolean {
+ if (name.includes('\\')) return false
+ const n = name.toLowerCase()
+ return CSS_FN_ALLOWED.has(n) || (pre === ':' && CSS_PSEUDO_FNS.has(n))
+}
/**
* Neutralise every function call a sheet may not make: RENAMED to one no
@@ -676,7 +701,7 @@ const CSS_FN_CALL = /(^|[^:\w\\-])((?:[-\w]|\\[\s\S]|[^\x00-\x7f])+)\(/g
*/
function cssRefuseFunctions(css: string): string {
return css.replace(CSS_FN_CALL, (m, pre: string, name: string) =>
- !name.includes('\\') && CSS_FN_ALLOWED.has(name.toLowerCase()) ? m : `${pre}bento-refused(`)
+ cssCallAllowed(pre, name) ? m : `${pre}bento-refused(`)
}
/** Does this CSS text carry a fetch — an off-list function, or a url() to
@@ -684,7 +709,7 @@ function cssRefuseFunctions(css: string): string {
function cssFetches(text: string): boolean {
const t = cssDecodeIdentEscapes(text)
if (urlTargets(t).some((u) => !(u.startsWith('#') || u.startsWith('data:image/')))) return true
- return Array.from(t.matchAll(CSS_FN_CALL)).some((m) => m[2].includes('\\') || !CSS_FN_ALLOWED.has(m[2].toLowerCase()))
+ return Array.from(t.matchAll(CSS_FN_CALL)).some((m) => !cssCallAllowed(m[1], m[2]))
}
/**