|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * The `html` / `richtext` display pipeline's trust boundary |
| 11 | + * (`../sanitizedHtml.tsx`), read through the renderer every read surface uses. |
| 12 | + * |
| 13 | + * Two halves, and both are load-bearing: |
| 14 | + * |
| 15 | + * - HOSTILE: each class of stored markup that can carry script comes out |
| 16 | + * inert. The assertions are STRUCTURAL — no `on*` attribute anywhere, no |
| 17 | + * script-bearing URL in any attribute, none of the embedding / foreign |
| 18 | + * content elements, no `style` — read off the rendered DOM, so they hold |
| 19 | + * for any spelling of a payload within its class, not just the one |
| 20 | + * written here. |
| 21 | + * - FORMATTING: a representative rich-text document renders back byte for |
| 22 | + * byte. A sanitizer that drops everything passes the first half; this |
| 23 | + * half is what refuses it. |
| 24 | + */ |
| 25 | + |
| 26 | +import React from 'react'; |
| 27 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 28 | +import { render, cleanup } from '@testing-library/react'; |
| 29 | +import '@testing-library/jest-dom'; |
| 30 | +import { HtmlCellRenderer, RICH_TEXT_CELL_RENDERERS } from '../richTextDisplay'; |
| 31 | +import { RICH_TEXT_SANITIZE_SCHEMA } from '../sanitizedHtml'; |
| 32 | + |
| 33 | +afterEach(cleanup); |
| 34 | + |
| 35 | +function renderHtml(value: string): HTMLElement { |
| 36 | + const { container } = render( |
| 37 | + <HtmlCellRenderer value={value} field={{ name: 'notes', type: 'richtext' } as never} />, |
| 38 | + ); |
| 39 | + const prose = container.querySelector<HTMLElement>('div.prose'); |
| 40 | + expect(prose, 'a populated value opens its prose container').not.toBeNull(); |
| 41 | + return prose!; |
| 42 | +} |
| 43 | + |
| 44 | +/** Elements whose presence alone means the value was not neutralised. */ |
| 45 | +const FORBIDDEN_ELEMENTS = |
| 46 | + 'script, style, iframe, frame, object, embed, applet, svg, math, link, meta, base, form, template, noscript'; |
| 47 | + |
| 48 | +/** Attributes that take a URL, in either namespace. */ |
| 49 | +const URL_ATTRIBUTES = ['href', 'src', 'action', 'formaction', 'data', 'xlink:href', 'srcset', 'cite', 'poster', 'background']; |
| 50 | + |
| 51 | +/** Everything a hostile value must NOT have left in the DOM. */ |
| 52 | +function expectInert(prose: HTMLElement, label: string) { |
| 53 | + expect(prose.querySelector(FORBIDDEN_ELEMENTS), `${label}: no embedding or foreign-content element`).toBeNull(); |
| 54 | + for (const element of Array.from(prose.querySelectorAll('*'))) { |
| 55 | + for (const attribute of Array.from(element.attributes)) { |
| 56 | + const name = attribute.name.toLowerCase(); |
| 57 | + expect(name.startsWith('on'), `${label}: <${element.localName}> keeps no ${name}`).toBe(false); |
| 58 | + expect(name, `${label}: <${element.localName}> keeps no style`).not.toBe('style'); |
| 59 | + expect(name, `${label}: <${element.localName}> keeps no srcdoc`).not.toBe('srcdoc'); |
| 60 | + if (URL_ATTRIBUTES.includes(name)) { |
| 61 | + // Normalised the way a URL parser reads it: control characters and |
| 62 | + // whitespace removed, case folded. |
| 63 | + const url = Array.from(attribute.value) |
| 64 | + .filter((char) => char.charCodeAt(0) > 0x20) |
| 65 | + .join('') |
| 66 | + .toLowerCase(); |
| 67 | + expect(url, `${label}: <${element.localName} ${name}> is not a script URL`).not.toMatch( |
| 68 | + /^(javascript|vbscript|data):/, |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const HOSTILE: ReadonlyArray<readonly [string, string]> = [ |
| 76 | + // Slash-separated attributes: no whitespace before the handler name. |
| 77 | + ['slash-separated img handler', '<p>a</p><img/src=x/onerror=alert(1)>'], |
| 78 | + ['slash-separated svg handler', '<svg/onload=alert(1)>'], |
| 79 | + ['slash before a quoted handler', '<img src="x"/onerror="alert(1)">'], |
| 80 | + ['handler on an allowed element', '<p onclick="alert(1)" onmouseover=alert(1)>text</p>'], |
| 81 | + ['mixed-case handler', '<a href="https://x.test" OnClIcK="alert(1)">x</a>'], |
| 82 | + // Unquoted and otherwise disguised script URLs. |
| 83 | + ['unquoted javascript href', '<a href=javascript:alert(1)>x</a>'], |
| 84 | + ['mixed-case javascript href', '<a href="JaVaScRiPt:alert(1)">x</a>'], |
| 85 | + ['leading-space javascript href', '<a href=" javascript:alert(1)">x</a>'], |
| 86 | + ['tab inside the scheme', '<a href="java\tscript:alert(1)">x</a>'], |
| 87 | + ['vbscript href', '<a href="vbscript:msgbox(1)">x</a>'], |
| 88 | + ['data URL href', '<a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">x</a>'], |
| 89 | + ['javascript img src', '<img src="javascript:alert(1)">'], |
| 90 | + // Entity-encoded schemes. |
| 91 | + ['decimal-entity scheme', '<a href="javascript:alert(1)">x</a>'], |
| 92 | + ['hex-entity scheme', '<a href="javascript:alert(1)">x</a>'], |
| 93 | + ['entity-encoded tab in the scheme', '<a href="jav	ascript:alert(1)">x</a>'], |
| 94 | + ['padded decimal entities', '<a href="javascript:alert(1)">x</a>'], |
| 95 | + // SVG and MathML. |
| 96 | + ['svg animate handler', '<svg><animate onbegin=alert(1) attributeName=x dur=1s></svg>'], |
| 97 | + ['svg link', '<svg><a xlink:href="javascript:alert(1)"><text x="0" y="20">x</text></a></svg>'], |
| 98 | + ['svg set href', '<svg><a><set attributeName="href" to="javascript:alert(1)"/><text>x</text></a></svg>'], |
| 99 | + ['svg foreignObject', '<svg><foreignObject><img src=x onerror=alert(1)></foreignObject></svg>'], |
| 100 | + ['math link', '<math><mi xlink:href="javascript:alert(1)">x</mi></math>'], |
| 101 | + ['math href', '<math href="javascript:alert(1)"><mtext>x</mtext></math>'], |
| 102 | + ['math style breakout', '<math><mtext><table><mglyph><style><img src=x onerror=alert(1)>'], |
| 103 | + // Embedding elements. |
| 104 | + ['iframe src', '<iframe src="javascript:alert(1)"></iframe>'], |
| 105 | + ['iframe srcdoc', '<iframe srcdoc="<script>alert(1)</script>"></iframe>'], |
| 106 | + ['object data', '<object data="javascript:alert(1)"></object>'], |
| 107 | + ['embed src', '<embed src="javascript:alert(1)">'], |
| 108 | + ['frameset', '<frameset onload=alert(1)><frame src="javascript:alert(1)"></frameset>'], |
| 109 | + // Styles. |
| 110 | + ['style attribute with a script URL', '<div style="background:url(javascript:alert(1))">x</div>'], |
| 111 | + ['style element with a script URL', '<style>p{background:url(javascript:alert(1))}</style><p>x</p>'], |
| 112 | + ['style attribute with an expression', '<p style="width:expression(alert(1))">x</p>'], |
| 113 | + // Script and its neighbours. |
| 114 | + ['script block', '<p>ok</p><script>alert(1)</script>'], |
| 115 | + ['script with a src', '<script src="https://evil.test/x.js"></script>'], |
| 116 | + ['nested script tags', '<scr<script>ipt>alert(1)</scr</script>ipt>'], |
| 117 | + ['noscript breakout', '<noscript><p title="</noscript><img src=x onerror=alert(1)>">'], |
| 118 | + ['template content', '<template><img src=x onerror=alert(1)></template>'], |
| 119 | + // Navigation and form vectors. |
| 120 | + ['form action', '<form action="javascript:alert(1)"><button>go</button></form>'], |
| 121 | + ['button formaction', '<button formaction="javascript:alert(1)">go</button>'], |
| 122 | + ['meta refresh', '<meta http-equiv="refresh" content="0;url=javascript:alert(1)">'], |
| 123 | + ['base href', '<base href="javascript:alert(1)//"><a href="/x">x</a>'], |
| 124 | + ['link import', '<link rel="import" href="https://evil.test/x.html">'], |
| 125 | + ['comment breakout', '<!--<img src="--><img src=x onerror=alert(1)//">'], |
| 126 | +]; |
| 127 | + |
| 128 | +describe('html / richtext display: stored markup comes out inert', () => { |
| 129 | + for (const [label, payload] of HOSTILE) { |
| 130 | + it(label, () => { |
| 131 | + expectInert(renderHtml(payload), label); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + it('hides the body of a stripped element rather than printing it as text', () => { |
| 136 | + const prose = renderHtml('<p>kept</p><script>alert(1)</script><style>p{}</style><svg><text>svg</text></svg>'); |
| 137 | + expect(prose.textContent).toBe('kept'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('prefixes stored ids so they cannot shadow the page’s own', () => { |
| 141 | + const prose = renderHtml('<h2 id="root">Title</h2><a name="main">x</a>'); |
| 142 | + expect(prose.querySelector('h2')?.id).toBe('user-content-root'); |
| 143 | + expect(prose.querySelector('a')?.getAttribute('name')).toBe('user-content-main'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('html and richtext share the one renderer', () => { |
| 147 | + expect(RICH_TEXT_CELL_RENDERERS.richtext).toBe(RICH_TEXT_CELL_RENDERERS.html); |
| 148 | + }); |
| 149 | + |
| 150 | + it('the schema strips the script-bearing elements with their content', () => { |
| 151 | + for (const tag of ['script', 'style', 'iframe', 'object', 'embed', 'svg', 'math']) { |
| 152 | + expect(RICH_TEXT_SANITIZE_SCHEMA.strip, tag).toContain(tag); |
| 153 | + } |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +/** |
| 158 | + * What a rich-text document is made of, already in the canonical form the |
| 159 | + * pipeline serialises back (lower-case tags, double-quoted attributes, an |
| 160 | + * image's `src` after its other attributes), so the comparison below is byte |
| 161 | + * for byte. |
| 162 | + */ |
| 163 | +const FORMATTED_DOCUMENT = [ |
| 164 | + '<h1>Account review</h1>', |
| 165 | + '<h2>Summary</h2>', |
| 166 | + '<p>Body with <strong>bold</strong>, <em>emphasis</em>, <b>b</b>, <i>i</i>, <del>removed</del>, ', |
| 167 | + '<sub>sub</sub>, <sup>sup</sup> and <code>inline code</code>.<br>Second line.</p>', |
| 168 | + '<h3>Links</h3>', |
| 169 | + '<p><a href="https://objectstack.ai/docs?x=1#top">absolute</a>, <a href="mailto:sales@example.com">mail</a>, ', |
| 170 | + '<a href="/app/crm/lead/1">relative</a> and <a href="http://example.com">plain http</a>.</p>', |
| 171 | + '<ul><li>one</li><li>two<ul><li>nested</li></ul></li></ul>', |
| 172 | + '<ol start="3"><li>three</li><li>four</li></ol>', |
| 173 | + '<blockquote><p>Quoted text.</p></blockquote>', |
| 174 | + '<pre><code>const x = 1;\n</code></pre>', |
| 175 | + '<table><thead><tr><th>Stage</th><th align="right">Amount</th></tr></thead>', |
| 176 | + '<tbody><tr><td>Won</td><td align="right">1,000</td></tr><tr><td colspan="2">Total</td></tr></tbody></table>', |
| 177 | + '<p><img alt="Logo" width="120" height="40" title="Logo" src="https://cdn.example.com/logo.png"></p>', |
| 178 | + '<hr>', |
| 179 | + '<dl><dt>Term</dt><dd>Definition</dd></dl>', |
| 180 | + '<details><summary>More</summary><p>Hidden detail.</p></details>', |
| 181 | +].join(''); |
| 182 | + |
| 183 | +describe('html / richtext display: a formatted document keeps its formatting', () => { |
| 184 | + it('round-trips byte for byte', () => { |
| 185 | + expect(renderHtml(FORMATTED_DOCUMENT).innerHTML).toBe(FORMATTED_DOCUMENT); |
| 186 | + }); |
| 187 | + |
| 188 | + it('keeps the safe part of a mixed value', () => { |
| 189 | + const prose = renderHtml( |
| 190 | + '<p onclick="alert(1)">Hello <strong>world</strong></p><a href="javascript:alert(1)">bad</a><a href="https://ok.test">good</a>', |
| 191 | + ); |
| 192 | + expect(prose.innerHTML).toBe('<p>Hello <strong>world</strong></p><a>bad</a><a href="https://ok.test">good</a>'); |
| 193 | + }); |
| 194 | + |
| 195 | + it('keeps the text of an element it does not allow', () => { |
| 196 | + expect(renderHtml('<p><u>underlined</u> and <font color="red">coloured</font></p>').innerHTML).toBe( |
| 197 | + '<p>underlined and coloured</p>', |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it('renders plain text that holds no markup as text', () => { |
| 202 | + expect(renderHtml('5 < 6 & 7 > 3').textContent).toBe('5 < 6 & 7 > 3'); |
| 203 | + }); |
| 204 | +}); |
0 commit comments