| [fc40410] | 1 | // External embeds are a gated feature (FEP-633c): a ward's world outside the
|
|---|
| 2 | // fediverse is the guardians' call, and the gate is applied server-side.
|
|---|
| 3 | import { test } from 'node:test';
|
|---|
| 4 | import assert from 'node:assert/strict';
|
|---|
| 5 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 6 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 7 | const dbMod = await import('../src/config/database.js');
|
|---|
| 8 | dbMod.initializeDatabase();
|
|---|
| 9 | const { externalEmbedsAllowed } = await import('../src/services/guardianship/notes.js');
|
|---|
| 10 | const { firstExternalUrl, timelineEmbed } = await import('../src/services/ActivityPubService.js');
|
|---|
| 11 |
|
|---|
| 12 | test('auto (no setting): off for a ward, on for anyone else', () => {
|
|---|
| 13 | assert.equal(externalEmbedsAllowed(null, true), false, 'a ward gets no external embeds by default');
|
|---|
| 14 | assert.equal(externalEmbedsAllowed(null, false), true, 'a free actor does');
|
|---|
| 15 | assert.equal(externalEmbedsAllowed(undefined, true), false);
|
|---|
| 16 | });
|
|---|
| 17 |
|
|---|
| 18 | test('an explicit guardian decision wins over the default, both ways', () => {
|
|---|
| 19 | assert.equal(externalEmbedsAllowed(1, true), true, 'guardians may open it for a ward');
|
|---|
| 20 | assert.equal(externalEmbedsAllowed(0, false), false, 'and may close it for anyone');
|
|---|
| 21 | });
|
|---|
| 22 |
|
|---|
| 23 | test('firstExternalUrl picks the first real link, skipping mentions and hashtags', () => {
|
|---|
| 24 | const html = '<p><a href="https://s/@bob" class="u-url mention">@bob</a> '
|
|---|
| 25 | + '<a href="https://s/tags/x" class="mention hashtag">#x</a> '
|
|---|
| 26 | + 'kijk: <a href="https://v.example/watch/1">dit</a> en <a href="https://later.example/">dat</a></p>';
|
|---|
| 27 | assert.equal(firstExternalUrl(html), 'https://v.example/watch/1');
|
|---|
| 28 | });
|
|---|
| 29 |
|
|---|
| 30 | test('firstExternalUrl ignores non-http hrefs and empty content', () => {
|
|---|
| 31 | assert.equal(firstExternalUrl('<a href="javascript:alert(1)">x</a>'), null);
|
|---|
| 32 | assert.equal(firstExternalUrl('<p>geen links</p>'), null);
|
|---|
| 33 | assert.equal(firstExternalUrl(null), null);
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | test('timelineEmbed round-trips a stored card and refuses junk', () => {
|
|---|
| 37 | const stored = JSON.stringify({ url: 'https://v.example/1', kind: 'oembed', title: 'A talk', media: [{ url: 'https://v.example/t.jpg' }] });
|
|---|
| 38 | const back = timelineEmbed(stored);
|
|---|
| 39 | assert.equal(back.title, 'A talk');
|
|---|
| 40 | assert.equal(back.media[0].url, 'https://v.example/t.jpg');
|
|---|
| 41 | assert.equal(timelineEmbed(null), undefined);
|
|---|
| 42 | assert.equal(timelineEmbed('not json'), undefined);
|
|---|
| 43 | assert.equal(timelineEmbed('{"title":"no url"}'), undefined, 'a card without a url is not a card');
|
|---|
| 44 | });
|
|---|
| [b258a79] | 45 |
|
|---|
| 46 | // FEP-044f emit side: quoting a fediverse object must federate as a quote AND
|
|---|
| 47 | // tell the quoted author. This is the mirror of the ingest we already had.
|
|---|
| 48 | const { applyQuoteProps } = await import('../src/services/ActivityPubService.js');
|
|---|
| 49 |
|
|---|
| 50 | test('a quote is emitted in all three shapes the network reads', () => {
|
|---|
| 51 | const note = { to: ['https://www.w3.org/ns/activitystreams#Public'], cc: [], tag: [{ type: 'Hashtag', name: '#x' }] };
|
|---|
| 52 | applyQuoteProps(note, 'https://s/objects/9', 'https://s/users/alice');
|
|---|
| 53 | assert.equal(note.quote, 'https://s/objects/9', 'the FEP property');
|
|---|
| 54 | assert.equal(note.quoteUrl, 'https://s/objects/9', 'the as: alias Mastodon reads');
|
|---|
| 55 | assert.equal(note._misskey_quote, 'https://s/objects/9', 'the misskey alias');
|
|---|
| 56 | const link = note.tag.find((t) => t.type === 'Link');
|
|---|
| 57 | assert.ok(link, 'and an FEP-e232 Link tag');
|
|---|
| 58 | assert.equal(link.href, 'https://s/objects/9');
|
|---|
| 59 | assert.ok(link.mediaType.includes('activitystreams'));
|
|---|
| 60 | assert.ok(note.tag.some((t) => t.type === 'Hashtag'), 'existing tags survive');
|
|---|
| 61 | });
|
|---|
| 62 |
|
|---|
| 63 | test('the quoted author is addressed, so being quoted is not a surprise', () => {
|
|---|
| 64 | const note = { cc: ['https://s/users/me/followers'] };
|
|---|
| 65 | applyQuoteProps(note, 'https://s/objects/9', 'https://s/users/alice');
|
|---|
| 66 | assert.ok(note.cc.includes('https://s/users/alice'));
|
|---|
| 67 | assert.ok(note.cc.includes('https://s/users/me/followers'), 'without dropping the followers');
|
|---|
| 68 | });
|
|---|
| 69 |
|
|---|
| 70 | test('no quote, or a junk one, changes nothing', () => {
|
|---|
| 71 | const a = { cc: [], tag: [] };
|
|---|
| 72 | applyQuoteProps(a, null, null);
|
|---|
| 73 | assert.equal(a.quote, undefined);
|
|---|
| 74 | assert.equal(a.tag.length, 0);
|
|---|
| 75 | const b = { cc: [], tag: [] };
|
|---|
| 76 | applyQuoteProps(b, 'javascript:alert(1)', 'https://s/users/alice');
|
|---|
| 77 | assert.equal(b.quote, undefined, 'a non-http quote uri is refused');
|
|---|
| 78 | const c = { cc: [], tag: [] };
|
|---|
| 79 | applyQuoteProps(c, 'https://s/objects/9', 'not-a-url');
|
|---|
| 80 | assert.equal(c.quote, 'https://s/objects/9');
|
|---|
| 81 | assert.equal(c.cc.length, 0, 'a junk actor is simply not addressed');
|
|---|
| 82 | });
|
|---|
| 83 |
|
|---|
| 84 | test('a hostile oEmbed title is stored as plain text, not markup', async () => {
|
|---|
| 85 | const { resolveExternalEmbed } = await import('../src/services/ActivityPubService.js');
|
|---|
| 86 | // No network in the test env, so the resolver bails and returns null; the
|
|---|
| 87 | // point here is the contract: whatever comes back is never raw provider HTML.
|
|---|
| 88 | const out = await resolveExternalEmbed('<p><a href="https://v.example/1">x</a></p>');
|
|---|
| 89 | assert.ok(out === null || !/<script/i.test(out), 'never stores executable markup');
|
|---|
| 90 | });
|
|---|