diff --git a/packages/extension-ui/src/util/sanitizeOrigin.spec.ts b/packages/extension-ui/src/util/sanitizeOrigin.spec.ts index f13164a..3db1f8f 100644 --- a/packages/extension-ui/src/util/sanitizeOrigin.spec.ts +++ b/packages/extension-ui/src/util/sanitizeOrigin.spec.ts @@ -5,31 +5,41 @@ import type * as _ from '@pezkuwi/dev-test/globals.d.ts'; import { sanitizeOrigin, validateOrigin } from './sanitizeOrigin.js'; +// The origin a site claims for itself is attacker-chosen text. +const WITH_MARKUP: string[] = [ + 'ipt>alert(1)', + '<img src=x onerror=alert(1)>', + 'ay' +]; + +// validateOrigin used to look for markup in the sanitized string, after the +// tags it was looking for had been removed: `x` passed as valid. +const FLAGGED: string[] = ['ipt>evil', 'x']; + +const ACCEPTED: string[] = ['https://app.pezkuwichain.io', 'http://localhost:3000', 'Pezkuwi Apps']; + describe('sanitizeOrigin', () => { it('keeps a normal origin as it is', () => { expect(sanitizeOrigin('Pezkuwi Apps')).toEqual('Pezkuwi Apps'); }); - // One pass of tag removal turns `ipt>` into `ipt>alert(1)', - '<img src=x onerror=alert(1)>', - 'ay' - ])('leaves no angle bracket in %s', (origin) => { - const out = sanitizeOrigin(origin); - - expect(out).not.toMatch(/[<>]/); + // One pass of tag removal turns `ipt>` into ` { + for (const origin of WITH_MARKUP) { + expect(/[<>]/.test(sanitizeOrigin(origin))).toBe(false); + } }); - // validateOrigin used to look for markup in the sanitized string, after the - // tags it was looking for had been removed: `x` passed as valid. - it.each(['ipt>evil', 'x'])('flags %s, which carried markup', (origin) => { - expect(validateOrigin(origin)).toBe(false); + it('flags an origin that carried markup', () => { + for (const origin of FLAGGED) { + expect(validateOrigin(origin)).toBe(false); + } }); - it.each(['https://app.pezkuwichain.io', 'http://localhost:3000', 'Pezkuwi Apps'])('accepts %s', (origin) => { - expect(validateOrigin(origin)).toBe(true); + it('accepts ordinary origins', () => { + for (const origin of ACCEPTED) { + expect(validateOrigin(origin)).toBe(true); + } }); });