|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { test } from 'node:test' |
| 3 | +import { mentionCss } from '../src/client/mention-style.js' |
| 4 | +import { applyPublishedLook } from '../src/host/remote-chat.js' |
| 5 | +import { cssImageUrl, safeLookImage } from '../src/shared/look-image.js' |
| 6 | + |
| 7 | +// The payload from #2646: a friend's Bot publishes a look whose image closes url("…"), |
| 8 | +// closes the rule, and adds a page-wide `body{…}` rule to the mention stylesheet. |
| 9 | +const PAYLOAD = `data:image/png;base64,AA");}body{background:url(https://evil.example/beacon)}.z{'` |
| 10 | +const evil = { |
| 11 | + id: 'remote:evil.home', |
| 12 | + name: 'Evil', |
| 13 | + remote: { handle: 'evil.home' }, |
| 14 | + avatar: { image: PAYLOAD }, |
| 15 | +} |
| 16 | + |
| 17 | +const urlQuotes = css => (css.match(/url\("/g) ?? []).length |
| 18 | +const urlCloses = css => (css.match(/"\)/g) ?? []).length |
| 19 | + |
| 20 | +test('a payload image cannot leave url("…") in the page-wide mention stylesheet (#2646)', () => { |
| 21 | + const css = mentionCss([evil]) |
| 22 | + assert.equal(css.includes('body{'), false) |
| 23 | + assert.equal(css.includes('evil.example'), false) |
| 24 | + assert.equal(urlQuotes(css), urlCloses(css)) |
| 25 | + // The sink drops the photo and draws the colour/mask look instead. |
| 26 | + assert.match(css, /-webkit-mask-image:/) |
| 27 | + assert.equal(css.includes(PAYLOAD), false) |
| 28 | +}) |
| 29 | + |
| 30 | +test('a valid image still draws the photo in the mention stylesheet (#2646)', () => { |
| 31 | + // A display name with a space owns no @Name chip, so the DEFAULT_MASK url() stays out of the line. |
| 32 | + const friend = { id: 'remote:ok', name: 'Ok Friend', remote: { handle: 'ok.home' }, avatar: { image: 'https://ok.example/a.png' } } |
| 33 | + const css = mentionCss([friend]) |
| 34 | + assert.match(css, /background:center\/cover url\("https:\/\/ok\.example\/a\.png"\)/) |
| 35 | + assert.equal(urlQuotes(css), 1) |
| 36 | + assert.equal(urlCloses(css), 1) |
| 37 | + // A local Bot's gif avatar is a stored form and keeps drawing (roster.js accepts it). |
| 38 | + const local = { id: 'local', name: 'Local Bot', avatar: { image: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=' } } |
| 39 | + assert.match(mentionCss([local]), /url\("data:image\/gif;base64,R0lGODlhAQABAAAAACw="\)/) |
| 40 | +}) |
| 41 | + |
| 42 | +test('a published look drops an unsafe image and keeps a plain one (#2646)', () => { |
| 43 | + const dropped = {} |
| 44 | + applyPublishedLook(dropped, { avatar: { shape: 'moon', image: PAYLOAD }, avatarUpdatedAt: 't1' }) |
| 45 | + assert.deepEqual(dropped.avatar, { shape: 'moon' }) |
| 46 | + |
| 47 | + const kept = {} |
| 48 | + applyPublishedLook(kept, { avatar: { image: 'https://ok.example/a.png' }, avatarUpdatedAt: 't2' }) |
| 49 | + assert.deepEqual(kept.avatar, { image: 'https://ok.example/a.png' }) |
| 50 | +}) |
| 51 | + |
| 52 | +test('cssImageUrl refuses every value that could close the url or the rule (#2646)', () => { |
| 53 | + for (const bad of [ |
| 54 | + 'https://ok.example/a".png', |
| 55 | + "https://ok.example/a'.png", |
| 56 | + 'https://ok.example/a(.png', |
| 57 | + 'https://ok.example/a).png', |
| 58 | + 'https://ok.example/a\\.png', |
| 59 | + 'https://ok.example/a\n.png', |
| 60 | + 'https://ok.example/a\u0007.png', |
| 61 | + 'javascript:alert(1)', |
| 62 | + 'data:image/png;base64,AA");}body{background:red}', |
| 63 | + '', |
| 64 | + ]) assert.equal(cssImageUrl(bad), null, bad) |
| 65 | + |
| 66 | + assert.equal(cssImageUrl('https://ok.example/a.png'), 'url("https://ok.example/a.png")') |
| 67 | + assert.equal(safeLookImage('data:image/webp;base64,AAAA'), 'data:image/webp;base64,AAAA') |
| 68 | + assert.equal(safeLookImage('https://ok.example/a.png'), 'https://ok.example/a.png') |
| 69 | +}) |
0 commit comments