Skip to content
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

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions add-on/src/lib/redirect-handler/blockOrObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?')
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let regexFilter = `^${regexFilterFirst}${regexEnding}`.replace(/https?/ig, 'https?')
let regexFilter = `^${regexFilterFirst}${regexEnding}`.replace(/^https?/, '^https?')

?

Copy link
Contributor Author

@whizzzkid whizzzkid May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work, because the regexFilterFirst is already a string ^http... so regex match /^http/ does not work, it would need to be ^\^http or something similar.


// This method does not parse:
// originUrl: "https://awesome.ipfs.io/"
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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)))
}

Expand Down
45 changes: 35 additions & 10 deletions test/functional/lib/redirect-handler/blockOrObserve.test.ts
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arent these style changes the opposite of aegir/standardjs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) => ({
Expand Down Expand Up @@ -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())
})

Expand Down Expand Up @@ -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)
})
})
})