diff --git a/src/components.d.ts b/src/components.d.ts
index c818a522..86456cec 100644
--- a/src/components.d.ts
+++ b/src/components.d.ts
@@ -42,7 +42,7 @@ export namespace Components {
*/
"name"?: string;
/**
- * When set to `false`, SVG content that is HTTP fetched will not be checked if the response SVG content has any ``);
+ });
+
+ test('should not run handlers from a utf8 svg data url', async ({ page }) => {
+ await addIcon(page, 'attack', utf8DataUrl(imageOnErrorSvg('attack')));
+ await addIcon(page, 'control', utf8DataUrl(imageOnErrorSvg('control')), false);
+ await waitForControl(page);
+
+ expect(await getFired(page)).not.toContain('attack');
+ await expect(page.locator('#attack .icon-inner svg')).toHaveCount(0);
+ });
+
+ test('should render a clean utf8 svg data url', async ({ page }) => {
+ const clean = ``;
+ await addIcon(page, 'clean', utf8DataUrl(clean));
+
+ await expect(page.locator('#clean .icon-inner svg path')).toHaveCount(1);
+ });
+
+ test('should warn when an svg is rejected', async ({ page }) => {
+ const warnings: string[] = [];
+ page.on('console', (msg) => {
+ if (msg.type() === 'warning') {
+ warnings.push(msg.text());
+ }
+ });
+
+ await addIcon(page, 'attack', utf8DataUrl(imageOnErrorSvg('attack')));
+
+ await expect.poll(() => warnings.some((warning) => warning.includes('[Ionicons Warning]'))).toBe(true);
+ });
+
+ test('should not warn about sanitizing when the response is not an svg', async ({ page }) => {
+ const warnings: string[] = [];
+ page.on('console', (msg) => {
+ if (msg.type() === 'warning') {
+ warnings.push(msg.text());
+ }
+ });
+ await page.route('**/missing.svg', (route) =>
+ route.fulfill({ status: 404, contentType: 'text/html', body: '
Not found' }),
+ );
+ await page.route('**/rejected.svg', (route) =>
+ route.fulfill({ contentType: 'image/svg+xml', body: imageOnErrorSvg('attack') }),
+ );
+
+ const missingLoaded = page.waitForResponse('**/missing.svg');
+ await addIcon(page, 'missing', '/missing.svg');
+ await missingLoaded;
+ // This takes the same fetch and sanitize steps, so once it warns the 404 has been handled
+ await addIcon(page, 'rejected', '/rejected.svg');
+ await expect.poll(() => warnings.some((warning) => warning.includes('rejected.svg'))).toBe(true);
+
+ expect(warnings.filter((warning) => warning.includes('missing.svg'))).toHaveLength(0);
+ });
+
+ test('should not run handlers from a fetched svg while sanitizing it', async ({ page }) => {
+ await page.route('**/xss/*.svg', (route) => {
+ const label = route.request().url().includes('control') ? 'control' : 'attack';
+ route.fulfill({
+ contentType: 'image/svg+xml',
+ body: `
${imageOnErrorSvg(label)}`,
+ });
+ });
+
+ // Fetches resolve independently, so let the attack load before the control starts
+ const attackLoaded = page.waitForResponse('**/xss/attack.svg');
+ await addIcon(page, 'attack', '/xss/attack.svg');
+ await attackLoaded;
+ await addIcon(page, 'control', '/xss/control.svg', false);
+ await waitForControl(page);
+
+ expect(await getFired(page)).not.toContain('attack');
+ await expect(page.locator('#attack .icon-inner svg')).toHaveCount(0);
+ });
+
+ test('should reject a fetched svg with more than one root svg', async ({ page }) => {
+ await page.route('**/xss/*.svg', (route) => {
+ const label = route.request().url().includes('control') ? 'control' : 'attack';
+ route.fulfill({
+ contentType: 'image/svg+xml',
+ body: `${imageOnErrorSvg(label)}`,
+ });
+ });
+
+ const attackLoaded = page.waitForResponse('**/xss/attack.svg');
+ await addIcon(page, 'attack', '/xss/attack.svg');
+ await attackLoaded;
+ await addIcon(page, 'control', '/xss/control.svg', false);
+ await waitForControl(page);
+
+ expect(await getFired(page)).not.toContain('attack');
+ await expect(page.locator('#attack .icon-inner svg')).toHaveCount(0);
+ });
+
+ const htmlInSvgPayloads = [
+ {
+ name: 'noscript',
+ inner: ``,
+ },
+ {
+ name: 'template',
+ inner: `
`,
+ },
+ ];
+
+ htmlInSvgPayloads.forEach(({ name, inner }) => {
+ test(`should reject an svg containing an html ${name} element`, async ({ page }) => {
+ const svg = ``;
+ await addIcon(page, 'attack', utf8DataUrl(svg));
+ await addIcon(page, 'control', utf8DataUrl(imageOnErrorSvg('control')), false);
+ await waitForControl(page);
+
+ expect(await getFired(page)).not.toContain('attack');
+ await expect(page.locator('#attack .icon-inner svg')).toHaveCount(0);
+ });
+ });
+
+ const foreignObjectPayloads = [
+ {
+ name: 'iframe srcdoc',
+ inner: ``,
+ },
+ {
+ name: 'iframe javascript: src',
+ inner: ``,
+ },
+ ];
+
+ foreignObjectPayloads.forEach(({ name, inner }) => {
+ test(`should remove a foreignObject containing an ${name}`, async ({ page }) => {
+ const svg = ``;
+ await addIcon(page, 'attack', utf8DataUrl(svg));
+ await addIcon(page, 'control', utf8DataUrl(imageOnErrorSvg('control')), false);
+ await waitForControl(page);
+
+ expect(await getFired(page)).not.toContain('attack');
+ await expect(page.locator('#attack .icon-inner svg path')).toHaveCount(1);
+ await expect(page.locator('#attack .icon-inner svg > :not(path)')).toHaveCount(0);
+ });
+ });
+
+ test('should render an illustrator export that uses a switch with a foreignObject', async ({ page }) => {
+ const svg = ``;
+ await addIcon(page, 'illustrator', utf8DataUrl(svg));
+
+ await expect(page.locator('#illustrator .icon-inner svg path')).toHaveCount(1);
+ });
+
+ const javascriptUrlPayloads = [
+ { name: 'an anchor href', inner: `` },
+ {
+ name: 'an obfuscated xlink:href',
+ inner: ``,
+ },
+ {
+ name: 'an animate element',
+ inner: ``,
+ },
+ {
+ name: 'a set element',
+ inner: ``,
+ },
+ ];
+
+ javascriptUrlPayloads.forEach(({ name, inner }) => {
+ test(`should reject an svg with a javascript: url in ${name}`, async ({ page }) => {
+ const svg = ``;
+ await addIcon(page, 'attack', utf8DataUrl(svg));
+ await addIcon(page, 'control', utf8DataUrl(imageOnErrorSvg('control')), false);
+ await waitForControl(page);
+
+ await expect(page.locator('#attack .icon-inner svg')).toHaveCount(0);
+ });
+ });
+
+ test('should not reuse unsanitized content cached for the same data url', async ({ page }) => {
+ const shared = utf8DataUrl(imageOnErrorSvg('shared'));
+
+ await addIcon(page, 'unsanitized', shared, false);
+ await page.waitForFunction(() => (window as any).xssFired.includes('shared'));
+
+ await addIcon(page, 'sanitized', shared);
+ await addIcon(page, 'control', utf8DataUrl(imageOnErrorSvg('control')), false);
+ await waitForControl(page);
+
+ await expect(page.locator('#sanitized .icon-inner svg')).toHaveCount(0);
+ expect((await getFired(page)).filter((label) => label === 'shared')).toHaveLength(1);
+ });
+
+ test('should not reuse sanitized content cached for the same data url', async ({ page }) => {
+ const shared = utf8DataUrl(imageOnErrorSvg('shared'));
+
+ await addIcon(page, 'sanitized', shared);
+ await addIcon(page, 'unsanitized', shared, false);
+
+ await expect(page.locator('#unsanitized .icon-inner svg')).toHaveCount(1);
+ });
+
+ test('should not let a src forge the unsanitized cache key', async ({ page }) => {
+ const shared = utf8DataUrl(imageOnErrorSvg('shared'));
+
+ await addIcon(page, 'unsanitized', shared, false);
+ await page.waitForFunction(() => (window as any).xssFired.includes('shared'));
+
+ await addIcon(page, 'sanitized', `unsanitized:${shared}`);
+ await addIcon(page, 'control', utf8DataUrl(imageOnErrorSvg('control')), false);
+ await waitForControl(page);
+
+ await expect(page.locator('#sanitized .icon-inner svg')).toHaveCount(0);
+ expect((await getFired(page)).filter((label) => label === 'shared')).toHaveLength(1);
+ });
+
+ test('should not reuse an unsanitized in-flight fetch for the same url', async ({ page }) => {
+ await page.route('**/xss/*.svg', (route) => {
+ const label = route.request().url().includes('control') ? 'control' : 'shared';
+ // Sanitizing drops the
sibling and keeps the clean