sitesmith self-review: keep var() shorthands (background/border) in the capture
Measured in Chromium and Firefox: the CSSOM exposes `background: var(--x)` and `border: 1px solid var(--x)` only as pending-substitution longhands whose values read back as "", so the capture sanitiser (which re-serialises the parsed declarations) dropped them. The self-review screenshot then showed dark heroes, image placeholders and card borders as white, and the model "fixed" things that were fine on the real page. The sanitiser now emits the declared shorthand once, at its first longhand, through the same value/URL gate as every other declaration. Sandbox, CSP and the single URL allow-list are unchanged. - scripts/e2e-capture-fidelity.mjs: real-browser test through the whole capture path (sanitise -> inline -> sandboxed frame -> html-to-image), decodes the JPEG and samples pixels. Failed 9/12 checks per browser before, passes after. - e2e-capture-security: hostile var() shorthand / var() fallback url() probes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01a78c9b3d
commit
4c1c8db97e
5 files changed
+149
-3
No files matched your search
@@ -140,12 +140,58 @@ function canonicalize(value: string, pol: Policy, font: boolean): string {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthands whose value the CSSOM keeps only on the shorthand when it contains var(): the indexed
|
||||
* longhands then read back as "" (pending substitution, measured in Chromium and Firefox), so walking
|
||||
* longhands alone silently dropped `background: var(--wsb-secondary)` and `border: 1px solid var(--x)`
|
||||
* (the self-review screenshot showed a dark hero as white). More specific names first.
|
||||
*/
|
||||
const SHORTHANDS = [
|
||||
'background-position', 'border-top', 'border-right', 'border-bottom', 'border-left', 'border-color', 'border-style',
|
||||
'border-width', 'border-radius', 'border-image', 'border-block', 'border-inline', 'border', 'background', 'padding',
|
||||
'margin', 'inset', 'font', 'outline', 'text-decoration', 'list-style', 'flex-flow', 'flex', 'gap', 'grid-template',
|
||||
'grid-area', 'grid-row', 'grid-column', 'grid', 'place-items', 'place-content', 'place-self', 'columns', 'column-rule',
|
||||
'overflow', 'transition', 'animation', 'mask', 'text-emphasis', 'scroll-margin', 'scroll-padding',
|
||||
] as const;
|
||||
const COVER_EXTRA: Record<string, RegExp> = {
|
||||
'border-color': /^border-(top|right|bottom|left)-color$/,
|
||||
'border-style': /^border-(top|right|bottom|left)-style$/,
|
||||
'border-width': /^border-(top|right|bottom|left)-width$/,
|
||||
'border-radius': /^border-[a-z-]+-radius$/,
|
||||
inset: /^(top|right|bottom|left)$/,
|
||||
font: /^line-height$/,
|
||||
gap: /^(row|column)-gap$/,
|
||||
'flex-flow': /^flex-(direction|wrap)$/,
|
||||
'place-items': /^(align|justify)-items$/,
|
||||
'place-content': /^(align|justify)-content$/,
|
||||
'place-self': /^(align|justify)-self$/,
|
||||
columns: /^column-(width|count)$/,
|
||||
};
|
||||
function shorthandFor(style: CSSStyleDeclaration, prop: string): string | null {
|
||||
for (const sh of SHORTHANDS) {
|
||||
if (!(prop.startsWith(sh + '-') || COVER_EXTRA[sh]?.test(prop))) continue;
|
||||
const v = style.getPropertyValue(sh);
|
||||
if (v && /var\(/i.test(v)) return sh;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function declarations(style: CSSStyleDeclaration, pol: Policy, font = false): string {
|
||||
const out: string[] = [];
|
||||
const seenShorthand = new Set<string>();
|
||||
for (let i = 0; i < style.length; i++) {
|
||||
const prop = style[i];
|
||||
let prop = style[i];
|
||||
if (!prop || /[^A-Za-z0-9_-]/.test(prop) || BAD_PROP.test(prop)) continue;
|
||||
const value = style.getPropertyValue(prop);
|
||||
let value = style.getPropertyValue(prop);
|
||||
if (!value) {
|
||||
// A var() shorthand: emit the shorthand once, where its first longhand stood (cascade order kept),
|
||||
// judged by exactly the same value gate as any other declaration.
|
||||
const sh = shorthandFor(style, prop);
|
||||
if (!sh || seenShorthand.has(sh)) continue;
|
||||
seenShorthand.add(sh);
|
||||
prop = sh;
|
||||
value = style.getPropertyValue(sh);
|
||||
}
|
||||
if (!value || !valueOk(value, pol, font || prop === 'src')) continue;
|
||||
const pri = style.getPropertyPriority(prop) ? ' !important' : '';
|
||||
out.push(`${prop}:${canonicalize(value, pol, font || prop === 'src')}${pri}`);
|
||||
|
||||
Reference in new issue
Block a user