import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { EventStatus, MatrixError, MatrixEvent } from 'matrix-js-sdk'; import { CONSENT_ERRCODE, failedForConsent, safeConsentUri } from './consent'; describe('safeConsentUri', () => { it('keeps http(s) links from any homeserver', () => { assert.equal( safeConsentUri('https://matrix.example.org/_matrix/consent?u=a&h=b'), 'https://matrix.example.org/_matrix/consent?u=a&h=b', ); assert.equal( safeConsentUri('http://localhost:8008/_matrix/consent'), 'http://localhost:8008/_matrix/consent', ); }); it('rejects other schemes and junk', () => { // eslint-disable-next-line no-script-url assert.equal(safeConsentUri('javascript:alert(1)'), undefined); assert.equal(safeConsentUri('file:///etc/passwd'), undefined); assert.equal(safeConsentUri('not a url'), undefined); assert.equal(safeConsentUri(undefined), undefined); assert.equal(safeConsentUri(42), undefined); }); }); describe('failedForConsent', () => { const echo = (status: EventStatus, errcode?: string) => { const ev = new MatrixEvent({ type: 'm.room.message', content: { body: 'hi' } }); ev.setStatus(status); if (errcode) ev.error = new MatrixError({ errcode, error: 'x' }, 403); return ev; }; it('matches a send that failed for consent', () => { assert.equal(failedForConsent(echo(EventStatus.NOT_SENT, CONSENT_ERRCODE)), true); }); it('ignores other failures and non-failed echoes', () => { assert.equal(failedForConsent(echo(EventStatus.NOT_SENT, 'M_FORBIDDEN')), false); assert.equal(failedForConsent(echo(EventStatus.NOT_SENT)), false); assert.equal(failedForConsent(echo(EventStatus.SENDING, CONSENT_ERRCODE)), false); }); });