Repository navigation
fix(core): sanitize planted footnote content in Bible HTML #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@youversion/platform-core": patch | ||
| "@youversion/platform-react-ui": patch | ||
| --- | ||
|
|
||
| Sanitized `data-verse-footnote-content` found in Bible HTML input with the same allowlist as passage markup, so planted footnote markup can no longer run script in the Bible reader. `transformBibleHtml` now removes input `data-footnote-key` attributes and `data-yv-*` attributes other than `data-yv-transformed`, and drops all SVG and MathML content. `FootnoteContentProps` now documents that `notes` and `verseHtml` must be sanitized HTML. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,49 +6,74 @@ const TRANSFORMED_ATTR = 'data-yv-transformed'; | |
|
|
||
| const NEEDS_SPACE_BEFORE = /^[^\s.,;:!?)}\]'"'»›]/; | ||
|
|
||
| const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'; | ||
|
|
||
| const ALLOWED_TAGS = new Set([ | ||
| 'DIV', | ||
| 'P', | ||
| 'SPAN', | ||
| 'SUP', | ||
| 'SUB', | ||
| 'EM', | ||
| 'STRONG', | ||
| 'I', | ||
| 'B', | ||
| 'SMALL', | ||
| 'BR', | ||
| 'SECTION', | ||
| 'TABLE', | ||
| 'THEAD', | ||
| 'TBODY', | ||
| 'TR', | ||
| 'TD', | ||
| 'TH', | ||
| 'div', | ||
| 'p', | ||
| 'span', | ||
| 'sup', | ||
| 'sub', | ||
| 'em', | ||
| 'strong', | ||
| 'i', | ||
| 'b', | ||
| 'small', | ||
| 'br', | ||
| 'section', | ||
| 'table', | ||
| 'thead', | ||
| 'tbody', | ||
| 'tr', | ||
| 'td', | ||
| 'th', | ||
| ]); | ||
|
|
||
| const DROP_ENTIRELY_TAGS = new Set([ | ||
| 'SCRIPT', | ||
| 'STYLE', | ||
| 'IFRAME', | ||
| 'OBJECT', | ||
| 'EMBED', | ||
| 'SVG', | ||
| 'MATH', | ||
| 'FORM', | ||
| 'INPUT', | ||
| 'BUTTON', | ||
| 'TEXTAREA', | ||
| 'SELECT', | ||
| 'TEMPLATE', | ||
| 'LINK', | ||
| 'META', | ||
| 'BASE', | ||
| 'NOSCRIPT', | ||
| 'script', | ||
| 'style', | ||
| 'iframe', | ||
| 'object', | ||
| 'embed', | ||
| 'svg', | ||
| 'math', | ||
| 'form', | ||
| 'input', | ||
| 'button', | ||
| 'textarea', | ||
| 'select', | ||
| 'template', | ||
| 'link', | ||
| 'meta', | ||
| 'base', | ||
| 'noscript', | ||
| ]); | ||
|
|
||
| const ALLOWED_ATTRS = new Set(['class', 'v', 'colspan', 'rowspan', 'dir', 'usfm']); | ||
|
|
||
| const FOOTNOTE_ANCHOR_ATTR = 'data-verse-footnote'; | ||
|
|
||
| const FOOTNOTE_CONTENT_ATTR = 'data-verse-footnote-content'; | ||
|
|
||
| const FOOTNOTE_DATA_ATTRS = new Set([FOOTNOTE_ANCHOR_ATTR, FOOTNOTE_CONTENT_ATTR]); | ||
|
|
||
| type SanitizeMode = 'passage' | 'footnote'; | ||
|
|
||
| // Other data-* attributes stay because consumers style and query them (for | ||
| // example data-slot), and they carry no behavior. SDK-internal names are | ||
| // reserved so a passage cannot plant them. The transformed marker stays so | ||
| // re-transforming output is idempotent; footnote content is re-sanitized as | ||
| // HTML instead of trusted, and cannot nest further footnote anchors. | ||
| function isAllowedDataAttr(name: string, mode: SanitizeMode): boolean { | ||
| if (!name.startsWith('data-')) return false; | ||
| if (name === FOOTNOTE_KEY_ATTR) return false; | ||
| if (name.startsWith('data-yv-') && name !== TRANSFORMED_ATTR) return false; | ||
| if (mode === 'footnote' && (FOOTNOTE_DATA_ATTRS.has(name) || name === TRANSFORMED_ATTR)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| export type TextDirection = 'ltr' | 'rtl'; | ||
|
|
||
| function topLevelElements(doc: Document): Element[] { | ||
|
|
@@ -64,17 +89,16 @@ function resolveTextDirection(roots: Element[]): TextDirection | undefined { | |
| return undefined; | ||
| } | ||
|
|
||
| function sanitizeBibleHtmlDocument(doc: Document): void { | ||
| const root = doc.body ?? doc.documentElement; | ||
| function sanitizeBibleHtmlTree(root: ParentNode, mode: SanitizeMode): void { | ||
| for (const el of Array.from(root.querySelectorAll('*'))) { | ||
| const tag = el.tagName; | ||
|
|
||
| if (DROP_ENTIRELY_TAGS.has(tag)) { | ||
| // SVG and MathML elements keep lowercase tag names and their own parsing | ||
| // rules, so anything outside the HTML namespace is dropped with its text. | ||
| if (el.namespaceURI !== HTML_NAMESPACE || DROP_ENTIRELY_TAGS.has(el.localName)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: Checking the namespace closes the SVG/MathML mutation-XSS route. The old uppercase tag-name check missed those elements, because they keep lowercase tag names. |
||
| el.remove(); | ||
| continue; | ||
| } | ||
|
|
||
| if (!ALLOWED_TAGS.has(tag)) { | ||
| if (!ALLOWED_TAGS.has(el.localName)) { | ||
| el.replaceWith(...Array.from(el.childNodes)); | ||
| continue; | ||
| } | ||
|
|
@@ -86,13 +110,30 @@ function sanitizeBibleHtmlDocument(doc: Document): void { | |
| continue; | ||
| } | ||
|
|
||
| if (!ALLOWED_ATTRS.has(name) && !name.startsWith('data-')) { | ||
| if (!ALLOWED_ATTRS.has(name) && !isAllowedDataAttr(name, mode)) { | ||
| el.removeAttribute(attr.name); | ||
| } | ||
| } | ||
|
|
||
| const footnoteContent = el.getAttribute(FOOTNOTE_CONTENT_ATTR); | ||
| if (footnoteContent !== null) { | ||
| el.setAttribute( | ||
| FOOTNOTE_CONTENT_ATTR, | ||
| sanitizeFootnoteHtml(footnoteContent, el.ownerDocument), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Footnote content is rendered as HTML by consumers, so it must pass the same | ||
| // allowlist as passage markup. An inert template avoids a full parse per note. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: Parsing each note in an inert |
||
| function sanitizeFootnoteHtml(html: string, doc: Document): string { | ||
| const template = doc.createElement('template'); | ||
| template.innerHTML = html; | ||
| sanitizeBibleHtmlTree(template.content, 'footnote'); | ||
| return template.innerHTML; | ||
| } | ||
|
|
||
| /** | ||
| * Options for transforming Bible HTML. Requires DOM adapter functions | ||
| * to parse and serialize HTML, making the transformer runtime-agnostic. | ||
|
|
@@ -113,6 +154,12 @@ export type TransformBibleHtmlOptions = { | |
| * | ||
| * Consumers can access verse context by walking up from a footnote anchor | ||
| * to `.closest('.yv-v[v]')`. | ||
| * | ||
| * Sanitization: `html` and every `data-verse-footnote-content` value pass the | ||
| * same tag and attribute allowlist, so both are safe to inject as HTML. Input | ||
| * `data-footnote-key` and `data-yv-*` attributes (except `data-yv-transformed`) | ||
| * are removed; footnote content also has no `data-verse-footnote`, | ||
| * `data-verse-footnote-content`, or `data-yv-transformed` attributes. | ||
| */ | ||
| export type TransformedBibleHtml = { | ||
| /** The transformed HTML with footnotes replaced by marker elements */ | ||
|
|
@@ -290,8 +337,8 @@ function replaceFootnotesWithAnchors(doc: Document, footnotes: Element[]): void | |
| } | ||
|
|
||
| const anchor = doc.createElement('span'); | ||
| anchor.setAttribute('data-verse-footnote', key); | ||
| anchor.setAttribute('data-verse-footnote-content', fn.innerHTML); | ||
| anchor.setAttribute(FOOTNOTE_ANCHOR_ATTR, key); | ||
| anchor.setAttribute(FOOTNOTE_CONTENT_ATTR, sanitizeFootnoteHtml(fn.innerHTML, doc)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: Cleaning genuine footnote HTML here also fixes a case where the output changed on each transform. A planted anchor inside a real |
||
| fn.replaceWith(anchor); | ||
| } | ||
| } | ||
|
|
@@ -344,6 +391,13 @@ function fixIrregularTables(doc: Document): void { | |
| * Verse context is available by walking up from a footnote anchor: | ||
| * `anchor.closest('.yv-v[v]')` returns the verse wrapper (null for intro footnotes). | ||
| * | ||
| * Sanitization: input is treated as untrusted. Disallowed tags, SVG/MathML, | ||
| * `on*` handlers, and non-allowlisted attributes are removed. Footnote content | ||
| * already present in input (`data-verse-footnote-content`) is sanitized the same | ||
| * way, so the output and its footnote content are safe to inject as HTML. Input | ||
| * `data-footnote-key` and `data-yv-*` attributes (except `data-yv-transformed`) | ||
| * are removed. Other `data-*` attributes are kept. | ||
| * | ||
| * @param html - The raw Bible HTML from the YouVersion API | ||
| * @param options - DOM adapter options for parsing and serializing HTML | ||
| * @returns The transformed HTML | ||
|
|
@@ -366,7 +420,7 @@ export function transformBibleHtml( | |
| ): TransformedBibleHtml { | ||
| const doc = options.parseHtml(html); | ||
|
|
||
| sanitizeBibleHtmlDocument(doc); | ||
| sanitizeBibleHtmlTree(doc.body ?? doc.documentElement, 'passage'); | ||
|
|
||
| // Only trust the marker when every top-level element carries it. A nested or | ||
| // partially-marked fragment (transformed HTML concatenated with raw HTML, or | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,13 @@ export type FootnoteData = { | |
| reference?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Props for {@link FootnoteContent}. | ||
| * | ||
| * Security: `notes` and `verseHtml` are injected as HTML without further | ||
| * sanitization. Pass only output from `transformBibleHtml` (or HTML that went | ||
| * through an equivalent allowlist sanitizer); never pass raw API or user HTML. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (non-blocking): The follow-up to this doc comment should also cover the footnote key.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. The follow-up will cover the footnote key as well: check it against |
||
| */ | ||
| export type FootnoteContentProps = FootnoteData & { | ||
| scriptureDirection?: TextDirection; | ||
| fontSize?: number; | ||
|
|
@@ -95,7 +102,7 @@ function FootnoteContentImplementation({ | |
| <div | ||
| className="yv:mb-3 yv:font-serif yv:*:font-serif" | ||
| style={{ fontSize: fontSize ? `${fontSize}px` : '1.25rem' }} | ||
| // biome-ignore lint/security/noDangerouslySetInnerHtml: Bible footnote HTML comes from our YouVersion APIs and is safe | ||
| // Caller must pass sanitized HTML; see FootnoteContentProps. | ||
| dangerouslySetInnerHTML={{ __html: verseHtml }} | ||
| /> | ||
| </> | ||
|
|
@@ -109,7 +116,7 @@ function FootnoteContentImplementation({ | |
| className="yv:flex yv:gap-2 yv:text-xs yv:border-b yv:border-solid yv:border-border yv:py-2" | ||
| > | ||
| <span>{marker}.</span> | ||
| {/** biome-ignore lint/security/noDangerouslySetInnerHtml: Bible footnote HTML comes from our YouVersion APIs and is safe */} | ||
| {/* Caller must pass sanitized HTML; see FootnoteContentProps. */} | ||
| <span data-slot="yv-bible-note" dangerouslySetInnerHTML={{ __html: note }} /> | ||
| </li> | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change needed before approval: no test covers removing
data-yv-transformedin footnote mode. When we deleted this check, all 50 tests still passed. Please add a test that fails without it (docs/testing.md).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 57569dd. The planted note in the first workflow test has
data-yv-transformed=""on a nested span, and the test expects the exact value<span class="ft" data-slot="n">Note</span>…. If I remove|| name === TRANSFORMED_ATTR, that test fails.