|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { describe, it, beforeEach } from 'node:test'; |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | + |
| 6 | +// setting-page-filter reaches for chrome.* when it blocks something, so a stub |
| 7 | +// stands in for the browser and records what it was asked to do. |
| 8 | +const calls = { updates: [], goBacks: [] }; |
| 9 | + |
| 10 | +globalThis.chrome = { |
| 11 | + runtime: { |
| 12 | + getURL: path => `chrome-extension://testid/${path}`, |
| 13 | + }, |
| 14 | + tabs: { |
| 15 | + update: (tabId, props) => { |
| 16 | + calls.updates.push({ tabId, ...props }); |
| 17 | + return Promise.resolve(); |
| 18 | + }, |
| 19 | + goBack: tabId => { |
| 20 | + calls.goBacks.push(tabId); |
| 21 | + return Promise.resolve(); |
| 22 | + }, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +const { SettingPageFilter } = await import('../edge/setting-page-filter.js'); |
| 27 | + |
| 28 | +const EMPTY = { |
| 29 | + Enabled: true, |
| 30 | + NotifyOnBlocked: true, |
| 31 | + BlockedPrefixes: ['edge://settings'], |
| 32 | +}; |
| 33 | + |
| 34 | +function configure(overrides) { |
| 35 | + SettingPageFilter.applyConfig({ ...EMPTY, ...overrides }); |
| 36 | +} |
| 37 | + |
| 38 | +function navigate(url, frameId = 0) { |
| 39 | + return SettingPageFilter.onBeforeNavigate({ frameId, tabId: 7, url }); |
| 40 | +} |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + calls.updates = []; |
| 44 | + calls.goBacks = []; |
| 45 | + configure({}); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('isBlockedUrl', () => { |
| 49 | + it('matches a configured prefix', () => { |
| 50 | + assert.equal(SettingPageFilter.isBlockedUrl('edge://settings/privacy'), true); |
| 51 | + assert.equal(SettingPageFilter.isBlockedUrl('https://example.com/'), false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('matches any of several prefixes', () => { |
| 55 | + configure({ BlockedPrefixes: ['edge://settings', 'edge://flags'] }); |
| 56 | + |
| 57 | + assert.equal(SettingPageFilter.isBlockedUrl('edge://flags/#foo'), true); |
| 58 | + assert.equal(SettingPageFilter.isBlockedUrl('edge://policy'), false); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('applyConfig', () => { |
| 63 | + it('ignores a missing config rather than clearing everything', () => { |
| 64 | + configure({ BlockedPrefixes: ['edge://policy'] }); |
| 65 | + |
| 66 | + SettingPageFilter.applyConfig(undefined); |
| 67 | + |
| 68 | + assert.deepEqual(SettingPageFilter.blockedPrefixes, ['edge://policy']); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('onBeforeNavigate', () => { |
| 73 | + it('leaves an allowed address alone', () => { |
| 74 | + navigate('https://example.com/'); |
| 75 | + |
| 76 | + assert.deepEqual(calls.updates, []); |
| 77 | + assert.deepEqual(calls.goBacks, []); |
| 78 | + }); |
| 79 | + |
| 80 | + it('ignores navigation in a sub frame', () => { |
| 81 | + navigate('edge://settings/privacy', 1); |
| 82 | + |
| 83 | + assert.deepEqual(calls.updates, []); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does nothing while disabled', () => { |
| 87 | + configure({ Enabled: false }); |
| 88 | + |
| 89 | + navigate('edge://settings/privacy'); |
| 90 | + |
| 91 | + assert.deepEqual(calls.updates, []); |
| 92 | + assert.deepEqual(calls.goBacks, []); |
| 93 | + }); |
| 94 | + |
| 95 | + it('sends the tab to the bundled page carrying the blocked address', () => { |
| 96 | + navigate('edge://settings/privacy'); |
| 97 | + |
| 98 | + assert.equal(calls.updates.length, 1); |
| 99 | + const { tabId, url } = calls.updates[0]; |
| 100 | + assert.equal(tabId, 7); |
| 101 | + assert.ok(url.startsWith('chrome-extension://testid/blocked.html?')); |
| 102 | + assert.equal( |
| 103 | + new URL(url).searchParams.get('url'), |
| 104 | + 'edge://settings/privacy'); |
| 105 | + // The explanation replaces going back, so history is left alone. |
| 106 | + assert.deepEqual(calls.goBacks, []); |
| 107 | + }); |
| 108 | + |
| 109 | + it('goes back silently when notifications are turned off', () => { |
| 110 | + configure({ NotifyOnBlocked: false }); |
| 111 | + |
| 112 | + navigate('edge://settings/privacy'); |
| 113 | + |
| 114 | + assert.deepEqual(calls.goBacks, [7]); |
| 115 | + assert.deepEqual(calls.updates, []); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('blockedPageUrl', () => { |
| 120 | + // A crafted address must not be able to add parameters of its own. |
| 121 | + it('escapes the address it carries', () => { |
| 122 | + const url = SettingPageFilter.blockedPageUrl('edge://settings/?a=1&b=2#x'); |
| 123 | + |
| 124 | + assert.equal( |
| 125 | + new URL(url).searchParams.get('url'), |
| 126 | + 'edge://settings/?a=1&b=2#x'); |
| 127 | + }); |
| 128 | +}); |
0 commit comments