Skip to content

Commit

Permalink
feat(mv3): Adding brave specific redirects.
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
  • Loading branch information
whizzzkid committed Sep 8, 2023
1 parent faa7ea4 commit a7e6969
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 12 deletions.
6 changes: 5 additions & 1 deletion add-on/src/lib/redirect-handler/baseRegexFilter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {brave} from '../../lib/ipfs-client/brave.js'

export interface IRegexFilter {
originUrl: string
redirectUrl: string
Expand All @@ -18,6 +20,7 @@ export class RegexFilter {
readonly redirectURL: URL
readonly originNS: string
readonly redirectNS: string
readonly isBrave: boolean = brave !== undefined
// by default we cannot handle the request.
private _canHandle = false
regexFilter!: string
Expand Down Expand Up @@ -79,8 +82,9 @@ export class RegexFilter {
/**
* Compute the regex filter and substitution.
* This is the main method that needs to be implemented by subclasses.
* isBraveOverride is used to force the filter to be generated for Brave. For testing purposes only.
*/
computeFilter (): void {
computeFilter (isBraveOverride?: boolean): void {
throw new Error('Method not implemented.')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RULE_REGEX_ENDING, escapeURLRegex } from './blockOrObserve.js'
* destination: 'http://localhost:8081/ipns/awesome.ipfs.io/$1'
*/
export class CommonPatternRedirectRegexFilter extends RegexFilter {
computeFilter (): void {
computeFilter (isBraveOverride: boolean): void {
// this filter is the worst case scenario, we can handle any redirect.
this.canHandle = true
// We can traverse the URL from the end, and find the first character that is different.
Expand All @@ -32,10 +32,15 @@ export class CommonPatternRedirectRegexFilter extends RegexFilter {
// originUrl: "https://awesome.ipfs.io/"
// redirectUrl: "http://localhost:8081/ipns/awesome.ipfs.io/"
// that ends up with capturing all urls which we do not want.
// This rule can only apply to ipns subdomains.
if (this.regexFilter === `^https?\\:\\/${RULE_REGEX_ENDING}`) {
const subdomain = new URL(this.originUrl).hostname
this.regexFilter = `^https?\\:\\/\\/${escapeURLRegex(subdomain)}${RULE_REGEX_ENDING}`
this.regexSubstitution = this.regexSubstitution.replace('\\1', `/${subdomain}\\1`)
if (this.isBrave || isBraveOverride) {
this.regexSubstitution = `ipns://${subdomain}\\1`
} else {
this.regexSubstitution = this.regexSubstitution.replace('\\1', `/${subdomain}\\1`)
}
}
}
}
12 changes: 8 additions & 4 deletions add-on/src/lib/redirect-handler/namespaceRedirectRegexFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DEFAULT_NAMESPACES, RULE_REGEX_ENDING, defaultNSRegexStr, escapeURLRege
* destination: 'http://localhost:8080/$1/$2'
*/
export class NamespaceRedirectRegexFilter extends RegexFilter {
computeFilter (): void {
computeFilter (isBraveOverride: boolean): void {
this.canHandle = DEFAULT_NAMESPACES.has(this.originNS) &&
DEFAULT_NAMESPACES.has(this.redirectNS) &&
this.originNS === this.redirectNS &&
Expand All @@ -18,8 +18,12 @@ export class NamespaceRedirectRegexFilter extends RegexFilter {
// https://ipfs.io/ipfs/QmZMxU -> http://localhost:8080/ipfs/QmZMxU
const [originFirst, originLast] = this.originUrl.split(`/${this.originNS}/`)
this.regexFilter = `^${escapeURLRegex(originFirst)}\\/${defaultNSRegexStr}\\/${RULE_REGEX_ENDING}`
this.regexSubstitution = this.redirectUrl
.replace(`/${this.redirectNS}/`, '/\\1/')
.replace(originLast, '\\2')
if (this.isBrave || isBraveOverride) {
this.regexSubstitution = '\\1://\\2'
} else {
this.regexSubstitution = this.redirectUrl
.replace(`/${this.redirectNS}/`, '/\\1/')
.replace(originLast, '\\2')
}
}
}
15 changes: 10 additions & 5 deletions add-on/src/lib/redirect-handler/subdomainRedirectRegexFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class SubdomainRedirectRegexFilter extends RegexFilter {
super({ originUrl, redirectUrl })
}

computeFilter (): void {
computeFilter (isBraveOverride: boolean): void {
const isBrave = this.isBrave || isBraveOverride
this.regexSubstitution = this.redirectUrl
this.regexFilter = this.originUrl
if (!DEFAULT_NAMESPACES.has(this.originNS) && DEFAULT_NAMESPACES.has(this.redirectNS)) {
Expand Down Expand Up @@ -40,12 +41,16 @@ export class SubdomainRedirectRegexFilter extends RegexFilter {
// e.g https://bafybeib3bzis4mejzsnzsb65od3rnv5ffit7vsllratddjkgfgq4wiamqu.ipfs.dweb.link
this.regexFilter = `${commonStaticUrlStart}(.*?)\\.${defaultNSRegexStr}${commonStaticUrlEnd}`

this.regexSubstitution = this._redirectUrl
.replace(urlParts.reverse().join('.'), '\\1') // replace urlParts or CID.
.replace(`/${subdomainPart}/`, '/\\2/') // replace namespace dynamically.
if (isBrave) {
this.regexSubstitution = '\\2://\\1'
} else {
this.regexSubstitution = this._redirectUrl
.replace(urlParts.reverse().join('.'), '\\1') // replace urlParts or CID.
.replace(`/${subdomainPart}/`, '/\\2/') // replace namespace dynamically.
}

const pathWithSearch = this.originURL.pathname + this.originURL.search
if (pathWithSearch !== '/') {
if (pathWithSearch !== '/' && !isBrave) {
this.regexSubstitution = this.regexSubstitution.replace(pathWithSearch, '/\\3') // replace path
} else {
this.regexSubstitution += '\\3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai'
import browserMock from 'sinon-chrome'
import isManifestV3 from '../../../helpers/is-mv3-testing-enabled.js'
import { CommonPatternRedirectRegexFilter } from '../../../../add-on/src/lib/redirect-handler/commonPatternRedirectRegexFilter.js'

describe('lib/redirect-handler/commonPatternRedirectRegexFilter', () => {
before(function () {
if (!isManifestV3) {
return this.skip()
}
browserMock.runtime.id = 'testid'
})

describe('isBrave', () => {
it('should create filter for brave', () => {
const filter = new CommonPatternRedirectRegexFilter({
originUrl: 'https://awesome.ipfs.io/',
redirectUrl: 'http://localhost:8080/ipns/awesome.ipfs.io/'
})
filter.computeFilter(true)
filter.normalizeRegexFilter()
expect(filter.regexFilter).to.equal('^https?\\:\\/\\/awesome\\.ipfs\\.io((?:[^\\.]|$).*)$')
expect(filter.regexSubstitution).to.equal('ipns://awesome.ipfs.io\\1')
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai'
import browserMock from 'sinon-chrome'
import { NamespaceRedirectRegexFilter } from '../../../../add-on/src/lib/redirect-handler/namespaceRedirectRegexFilter'
import isManifestV3 from '../../../helpers/is-mv3-testing-enabled'

describe('lib/redirect-handler/namespaceRedirectRegexFilter', () => {
before(function () {
if (!isManifestV3) {
return this.skip()
}
browserMock.runtime.id = 'testid'
})

describe('isBrave', () => {
it('should create filter for brave', () => {
const filter = new NamespaceRedirectRegexFilter({
originUrl: 'https://ipfs.io/ipfs/QmZMxU',
redirectUrl: 'http://localhost:8080/ipfs/QmZMxU'
})
filter.computeFilter(true)
filter.normalizeRegexFilter()
expect(filter.regexFilter).to.equal('^https?\\:\\/\\/ipfs\\.io\\/(ipfs|ipns)\\/((?:[^\\.]|$).*)$')
expect(filter.regexSubstitution).to.equal('\\1://\\2')
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai'
import browserMock from 'sinon-chrome'
import { SubdomainRedirectRegexFilter } from '../../../../add-on/src/lib/redirect-handler/subdomainRedirectRegexFilter.js'
import isManifestV3 from '../../../helpers/is-mv3-testing-enabled'

describe('lib/redirect-handler/subdomainRedirectRegexFilter', () => {
before(function () {
if (!isManifestV3) {
return this.skip()
}
browserMock.runtime.id = 'testid'
})

describe('isBrave', () => {
it('should create filter for brave', () => {
const filter = new SubdomainRedirectRegexFilter({
originUrl: 'https://en.wikipedia-on-ipfs.org.ipns.dweb.link/wiki/InterPlanetary_File_System',
redirectUrl: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org/wiki/InterPlanetary_File_System'
})
filter.computeFilter(true)
filter.normalizeRegexFilter()
expect(filter.regexFilter).to.equal('^https?\\:\\/\\/(.*?)\\.(ipfs|ipns)\\.dweb\\.link\\/((?:[^\\.]|$).*)$')
expect(filter.regexSubstitution).to.equal('\\2://\\1\\3')
})
})
})

0 comments on commit a7e6969

Please sign in to comment.