-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(mv3): 👔 Adding better regex replace to remove infinite redirects. #1210
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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?') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work, because the |
||||||
|
||||||
// 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<void> { | ||||||
export async function cleanupRules (resetInMemory: boolean = false): Promise<void> { | ||||||
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))) | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { expect } from 'chai' | ||
import { before, describe, it } from 'mocha' | ||
import {expect} from 'chai' | ||
import {before, describe, it} from 'mocha' | ||
Comment on lines
-1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arent these style changes the opposite of aegir/standardjs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what's up with aegir, looks like its not fixing the styling as needed. 😞 |
||
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) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need case insensitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not necessarily but doesn't harm.