|
| 1 | +import { describe, test, expect } from 'vitest'; |
| 2 | +import { Defuddle } from '../src/node'; |
| 3 | +import { parseLinkedomHTML } from '../src/utils/linkedom-compat'; |
| 4 | + |
| 5 | +// Regression test for GHSA-jg4p-g6xj-4qmf: XSS via unescaped attribute |
| 6 | +// interpolation in site extractors. Extractor output is built from template |
| 7 | +// strings and previously bypassed DOM-based sanitization, so attacker- |
| 8 | +// controlled attribute values (e.g. an image alt read off the page) could |
| 9 | +// close the attribute and inject an event handler or a javascript: URL. |
| 10 | + |
| 11 | +const X_URL = 'https://x.com/testuser/article/123456789'; |
| 12 | + |
| 13 | +// Header image lives in the read view but OUTSIDE the article container, so |
| 14 | +// extractHeaderImage() emits it via a template string. |
| 15 | +function makeXArticleHTML(headerImgAttrs: string): string { |
| 16 | + return ` |
| 17 | + <html><head><title>Test Article</title></head> |
| 18 | + <body> |
| 19 | + <div data-testid="twitterArticleReadView"> |
| 20 | + <div data-testid="tweetPhoto"><img ${headerImgAttrs}></div> |
| 21 | + <div data-testid="twitterArticleRichTextView"> |
| 22 | + <h1 data-testid="twitter-article-title">Test Article</h1> |
| 23 | + <div class="public-DraftStyleDefault-block">Body text</div> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + </body></html> |
| 27 | + `; |
| 28 | +} |
| 29 | + |
| 30 | +// Re-parse the extractor output and assert no element carries an executable |
| 31 | +// attribute. This is the true security property: an escaped "onerror" living |
| 32 | +// inside an alt value is harmless; a real onerror attribute is not. |
| 33 | +function assertNoExecutableAttributes(html: string) { |
| 34 | + const doc = parseLinkedomHTML(`<body>${html}</body>`, X_URL); |
| 35 | + for (const el of Array.from(doc.querySelectorAll('*'))) { |
| 36 | + for (const attr of Array.from((el as Element).attributes)) { |
| 37 | + expect(attr.name.toLowerCase().startsWith('on')).toBe(false); |
| 38 | + if (['src', 'href'].includes(attr.name.toLowerCase())) { |
| 39 | + expect(attr.value.toLowerCase().replace(/\s+/g, '')).not.toContain('javascript:'); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +describe('Extractor output XSS sanitization (GHSA-jg4p-g6xj-4qmf)', () => { |
| 46 | + test('does not emit an event handler attribute from X header image alt', async () => { |
| 47 | + // alt contains a double-quote that, unescaped, would close the attribute |
| 48 | + // and turn the rest into a real onerror handler. |
| 49 | + const html = makeXArticleHTML('src="https://example.com/img.jpg" alt=\'x" onerror="alert(1)\''); |
| 50 | + const doc = parseLinkedomHTML(html, X_URL); |
| 51 | + const response = await Defuddle(doc, X_URL); |
| 52 | + |
| 53 | + assertNoExecutableAttributes(response.content); |
| 54 | + }); |
| 55 | + |
| 56 | + test('strips javascript: URL injected via X header image src', async () => { |
| 57 | + const html = makeXArticleHTML('src="javascript:alert(1)" alt="ok"'); |
| 58 | + const doc = parseLinkedomHTML(html, X_URL); |
| 59 | + const response = await Defuddle(doc, X_URL); |
| 60 | + |
| 61 | + assertNoExecutableAttributes(response.content); |
| 62 | + }); |
| 63 | +}); |
0 commit comments