Skip to content

Commit

Permalink
fix(mv3): 🐛 Bad regexSubstituition fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
whizzzkid committed May 15, 2023
1 parent 42f1e31 commit 3508d1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion add-on/src/lib/redirect-handler/blockOrObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function constructRegexFilter ({ originUrl, redirectUrl }: redirectHandlerInput)
// that ends up with capturing all urls which we do not want.
if (regexFilter === `^https?\\:\\/${regexEnding}`) {
const subdomain = new URL(originUrl).hostname
regexFilter = `^https?\\:\\/\\/${escapeURLRegex(subdomain)}${regexEnding}}`
regexFilter = `^https?\\:\\/\\/${escapeURLRegex(subdomain)}${regexEnding}`
regexSubstitution = regexSubstitution.replace('\\1', `/${subdomain}\\1`)
}

Expand Down
20 changes: 20 additions & 0 deletions test/functional/lib/redirect-handler/blockOrObserve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,25 @@ describe('lib/redirect-handler/blockOrObserve', () => {
expect(action).to.deep.equal({ type: 'redirect', redirect: { "regexSubstitution": "http://localhost:8080\\1" } })
expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/ipfs\\.io((?:[^\\.]|$).*)$'))
})

it('Should add redirect for local gateway where originUrl is similar to redirectUrl', () => {
addRuleToDynamicRuleSet({
originUrl: 'https://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((?:[^\\.]|$).*)$'))
})
})
})

0 comments on commit 3508d1f

Please sign in to comment.