Skip to content

Commit

Permalink
Bugfix: getBaseDomain should return null when invalid (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
englehardt committed Sep 19, 2022
1 parent d7d6b2b commit e51650f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 4 additions & 1 deletion shared/js/background/utils.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export async function sendTabMessage (id, message, details) {
*/
export function getBaseDomain (urlString) {
const parsedUrl = tldts.parse(urlString, { allowPrivateDomains: true })
return parsedUrl.domain || parsedUrl.hostname
if (parsedUrl.hostname === 'localhost' || parsedUrl.hostname?.endsWith('.localhost') || parsedUrl.isIp) {
return parsedUrl.hostname
}
return parsedUrl.domain
}

export function extractHostFromURL (url, shouldKeepWWW) {
Expand Down
2 changes: 1 addition & 1 deletion unit-test/background/trackers.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('tracker blocking', () => {
const result = tds.getTrackerData(test.tracker, test.site, test.req)
expect(result.action).toBe(test.result.action)
expect(result.reason).toBe(test.result.reason)
expect(result.firstParty).toBe(test.result.firstParty)
expect(result.sameEntity).toBe(test.result.sameEntity)
})
})
})
26 changes: 26 additions & 0 deletions unit-test/background/utils.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ describe('utils.isSameTopLevelDomain()', () => {
})
})

describe('utils.getBaseDomain()', () => {
[
['com', null],
['.com', null],
['example.com', 'example.com'],
['www.example.com', 'example.com'],
['foo.www.example.com', 'example.com'],
['foo.www.example.com:8000', 'example.com'],
['www.example.app', 'example.app'],
['foo.apps.fbsbx.com', 'foo.apps.fbsbx.com'],
['bar.foo.apps.fbsbx.com', 'foo.apps.fbsbx.com'],
['apps.fbsbx.com', null],
['1.2.3.4', '1.2.3.4'],
['127.0.0.1', '127.0.0.1'],
['localhost', 'localhost'],
['ddg.localhost', 'ddg.localhost'],
['sub.ddg.local', 'ddg.local'],
['abcefg', null],
['1234', null]
].forEach(([hostname, baseDomain]) => {
it(`returns ${baseDomain} for ${hostname}`, () => {
expect(utils.getBaseDomain(hostname)).toBe(baseDomain)
})
})
})

describe('utils.parseVersionString', () => {
const cases = [
['12', { major: 12, minor: 0, patch: 0 }],
Expand Down

0 comments on commit e51650f

Please sign in to comment.