Skip to content

Commit 91f4b7a

Browse files
committed
fix: define allowed styles in readme
This changes the sanitization to set `allowedStyles` to only what shiki emits, since nothing else we run needs to emit inline styles. Also escapes image attributes.
1 parent 973b9c7 commit 91f4b7a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

app/components/Readme.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function handleClick(event: MouseEvent) {
8989
min-width: 0;
9090
/* Contain all children z-index values inside this container */
9191
isolation: isolate;
92+
contain: layout paint;
9293
}
9394
9495
/* README headings - styled by visual level (data-level), not semantic level */

server/utils/readme.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ const ALLOWED_ATTR: Record<string, string[]> = {
198198
'blockquote': ['data-callout'],
199199
'details': ['open'],
200200
'code': ['class'],
201-
'pre': ['class', 'style'],
201+
'pre': ['class'],
202202
'span': ['class', 'style'],
203-
'div': ['class', 'style', 'align'],
203+
'div': ['class', 'align'],
204204
'p': ['align'],
205205
}
206206

@@ -597,8 +597,8 @@ ${html}
597597
// Resolve image URLs (with GitHub blob → raw conversion)
598598
renderer.image = ({ href, title, text }: Tokens.Image) => {
599599
const resolvedHref = resolveImageUrl(href, packageName, repoInfo)
600-
const titleAttr = title ? ` title="${title}"` : ''
601-
const altAttr = text ? ` alt="${text}"` : ''
600+
const titleAttr = title ? ` title="${escapeHtml(title)}"` : ''
601+
const altAttr = text ? ` alt="${escapeHtml(text)}"` : ''
602602
return `<img src="${resolvedHref}"${altAttr}${titleAttr}>`
603603
}
604604

@@ -673,6 +673,13 @@ ${html}
673673
allowedTags: ALLOWED_TAGS,
674674
allowedAttributes: ALLOWED_ATTR,
675675
allowedSchemes: ['http', 'https', 'mailto'],
676+
// disallow styles other than the ones shiki emits
677+
allowedStyles: {
678+
'span': {
679+
'color': [/^#[0-9a-fA-F]{3,8}$/],
680+
'--shiki-light': [/^#[0-9a-fA-F]{3,8}$/],
681+
},
682+
},
676683
// Transform img src URLs (GitHub blob → raw, relative → GitHub raw)
677684
transformTags: {
678685
// Headings are already processed to correct semantic levels by processHeading()

0 commit comments

Comments
 (0)