From 21559b95ef35199207aa0217c6c47a3507e669bb Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Fri, 26 May 2023 01:50:38 -0600 Subject: [PATCH 1/3] fix(mv3): :necktie: Adding better regex replace to remove infinite redirects. --- add-on/src/lib/redirect-handler/blockOrObserve.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/add-on/src/lib/redirect-handler/blockOrObserve.ts b/add-on/src/lib/redirect-handler/blockOrObserve.ts index 1b21c201c..bf912a978 100644 --- a/add-on/src/lib/redirect-handler/blockOrObserve.ts +++ b/add-on/src/lib/redirect-handler/blockOrObserve.ts @@ -120,7 +120,7 @@ function constructRegexFilter ({ originUrl, redirectUrl }: redirectHandlerInput) const regexFilterFirst = escapeURLRegex(originUrl.slice(0, originUrl.length - commonIdx + 1)) // We need to match the rest of the URL, so we can use a wildcard. const regexEnding = '((?:[^\\.]|$).*)$' - let regexFilter = `^${regexFilterFirst}${regexEnding}`.replace('https', 'https?') + let regexFilter = `^${regexFilterFirst}${regexEnding}`.replace(/https?/ig, 'https?') // This method does not parse: // originUrl: "https://awesome.ipfs.io/" @@ -162,7 +162,7 @@ function validateIfRuleChanged (rule: browser.DeclarativeNetRequest.Rule): boole /** * Clean up all the rules, when extension is disabled. */ -async function cleanupRules (resetInMemory: boolean = false): Promise { +export async function cleanupRules (resetInMemory: boolean = false): Promise { const existingRules = await browser.declarativeNetRequest.getDynamicRules() const existingRulesIds = existingRules.map(({ id }): number => id) await browser.declarativeNetRequest.updateDynamicRules({ addRules: [], removeRuleIds: existingRulesIds }) @@ -349,7 +349,7 @@ export function addRuleToDynamicRuleSetGenerator ( ) // refresh the tab to apply the new rule. - const tabs = await browser.tabs.query({ url: originUrl }) + const tabs = await browser.tabs.query({url: `${originUrl}*`}) await Promise.all(tabs.map(async tab => await browser.tabs.reload(tab.id))) } From 5d5102e3e341c1ce02da3ee3ee02e62369847111 Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Fri, 26 May 2023 01:51:49 -0600 Subject: [PATCH 2/3] fix(mv3): :test_tube: Adding more tests to account for local redirects --- .../redirect-handler/blockOrObserve.test.ts | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/test/functional/lib/redirect-handler/blockOrObserve.test.ts b/test/functional/lib/redirect-handler/blockOrObserve.test.ts index a7f9d5661..7a11911b9 100644 --- a/test/functional/lib/redirect-handler/blockOrObserve.test.ts +++ b/test/functional/lib/redirect-handler/blockOrObserve.test.ts @@ -1,11 +1,11 @@ -import { expect } from 'chai' -import { before, describe, it } from 'mocha' +import {expect} from 'chai' +import {before, describe, it} from 'mocha' import sinon from 'sinon' import browserMock from 'sinon-chrome' -import { optionDefaults } from '../../../../add-on/src/lib/options.js' -import { addRuleToDynamicRuleSetGenerator, isLocalHost } from '../../../../add-on/src/lib/redirect-handler/blockOrObserve' -import { initState } from '../../../../add-on/src/lib/state.js' +import {optionDefaults} from '../../../../add-on/src/lib/options.js' +import {addRuleToDynamicRuleSetGenerator, cleanupRules, isLocalHost} from '../../../../add-on/src/lib/redirect-handler/blockOrObserve' +import {initState} from '../../../../add-on/src/lib/state.js' import DeclarativeNetRequestMock from './declarativeNetRequest.mock.js' const dynamicRulesConditions = (regexFilter) => ({ @@ -64,10 +64,14 @@ describe('lib/redirect-handler/blockOrObserve', () => { addRuleToDynamicRuleSet = addRuleToDynamicRuleSetGenerator(() => state) }) - beforeEach(() => { + beforeEach(async () => { sinonSandbox.restore() - browserMock.flush() - browserMock.tabs.query.resolves([{ id: 1234 }]) + browserMock.tabs.query.resetHistory() + browserMock.tabs.reload.resetHistory() + browserMock.declarativeNetRequest = sinonSandbox.spy(new DeclarativeNetRequestMock()) + // this cleans up the rules from the previous test stored in memory. + await cleanupRules(true) + // this is just to reset the call count. browserMock.declarativeNetRequest = sinonSandbox.spy(new DeclarativeNetRequestMock()) }) @@ -126,13 +130,34 @@ describe('lib/redirect-handler/blockOrObserve', () => { expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/docs\\.ipfs\\.tech((?:[^\\.]|$).*)$')) }) + it('Should add redirect for local gateway where originUrl is similar to redirectUrl and is not https', async () => { + await addRuleToDynamicRuleSet({ + originUrl: 'http://docs.ipfs.tech', + redirectUrl: 'http://localhost:8080/ipns/docs.ipfs.tech' + }) + expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true + const [{ addRules, removeRuleIds }] = browserMock.declarativeNetRequest.updateDynamicRules.firstCall.args + expect(removeRuleIds).to.deep.equal([]) + expect(addRules).to.have.lengthOf(1) + const [{ id, priority, action, condition }] = addRules + expect(id).to.be.a('number') + expect(priority).to.equal(1) + expect(action).to.deep.equal({ + type: 'redirect', redirect: { + "regexSubstitution": "http://localhost:8080/ipns/docs.ipfs.tech\\1" + } + }) + expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/docs\\.ipfs\\.tech((?:[^\\.]|$).*)$')) + }) + it('Should refresh the tab when redirect URL is added', async () => { + browserMock.tabs.query.resolves([{id: 1234}]) await addRuleToDynamicRuleSet({ originUrl: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org', redirectUrl: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org' }) - expect(browserMock.tabs.query.calledWith({ url: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org' })).to.be.true - expect(browserMock.tabs.reload.calledWith(1234)).to.be.true + sinon.assert.calledWith(browserMock.tabs.query, { url: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org*' }) + sinon.assert.calledWith(browserMock.tabs.reload, 1234) }) }) }) From 8454cf83e52dbb7f33ca1902ce805e59160b06da Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Fri, 26 May 2023 02:29:36 -0600 Subject: [PATCH 3/3] fix: :rotating_light: Linter --- add-on/src/lib/redirect-handler/blockOrObserve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/add-on/src/lib/redirect-handler/blockOrObserve.ts b/add-on/src/lib/redirect-handler/blockOrObserve.ts index bf912a978..dcb31d107 100644 --- a/add-on/src/lib/redirect-handler/blockOrObserve.ts +++ b/add-on/src/lib/redirect-handler/blockOrObserve.ts @@ -349,7 +349,7 @@ export function addRuleToDynamicRuleSetGenerator ( ) // refresh the tab to apply the new rule. - const tabs = await browser.tabs.query({url: `${originUrl}*`}) + const tabs = await browser.tabs.query({ url: `${originUrl}*` }) await Promise.all(tabs.map(async tab => await browser.tabs.reload(tab.id))) }