Skip to content

Commit

Permalink
Add Chrome MV3 support for "surrogate script" redirections (#1398)
Browse files Browse the repository at this point in the history
The extension redirects requests to common third-party scripts to
subbed out "surrogate scripts". We do that instead of simply blocking
the third-party scripts since this way, we avoid breaking websites
that assume the APIs provided by the third-party scripts will always
be present.
  • Loading branch information
kzar committed Oct 10, 2022
1 parent 1f71efe commit 23e7b92
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"dependencies": {
"@duckduckgo/autofill": "github:duckduckgo/duckduckgo-autofill#5.1.0",
"@duckduckgo/content-scope-scripts": "github:duckduckgo/content-scope-scripts#2.6.0",
"@duckduckgo/ddg2dnr": "github:duckduckgo/ddg2dnr#0.1.4",
"@duckduckgo/ddg2dnr": "github:duckduckgo/ddg2dnr#0.2.0",
"@duckduckgo/jsbloom": "^1.0.2",
"@duckduckgo/privacy-grade": "github:duckduckgo/privacy-grade#2.1.1",
"@duckduckgo/privacy-reference-tests": "github:duckduckgo/privacy-reference-tests#00f50d6bb1085b08f2fcc27050345fdd581fad56",
Expand Down
19 changes: 13 additions & 6 deletions shared/js/background/declarative-net-request.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as browserWrapper from './wrapper.es6'
import settings from './settings.es6'
import tdsStorage from './storage/tds.es6'
import trackers from './trackers.es6'
import * as startup from './startup.es6'

import {
generateExtensionConfigurationRuleset
} from '@duckduckgo/ddg2dnr/lib/extensionConfiguration'
import {
generateTrackerBlockingRuleset
} from '@duckduckgo/ddg2dnr/lib/trackerBlocking'
generateTdsRuleset
} from '@duckduckgo/ddg2dnr/lib/tds'

export const SETTING_PREFIX = 'declarative_net_request-'

Expand Down Expand Up @@ -87,17 +89,22 @@ async function onUpdate (configName, etag, configValue) {
let addRules
let lookup

// Tracker blocking.
// TDS.
if (configName === 'tds') {
await startup.ready()
const supportedSurrogates = new Set(Object.keys(trackers.surrogateList))

const {
ruleset, trackerDomainByRuleId
} = await generateTrackerBlockingRuleset(
ruleset, matchDetailsByRuleId
} = await generateTdsRuleset(
configValue,
supportedSurrogates,
'/web_accessible_resources/',
chrome.declarativeNetRequest.isRegexSupported,
ruleIdStart + 1
)
addRules = ruleset
lookup = trackerDomainByRuleId
lookup = matchDetailsByRuleId
// Extension configuration.
} else if (configName === 'config') {
const {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"shared/js/background/broken-site-report.js",
"shared/js/background/csp-blocking.es6.js",
"shared/js/background/debug.es6.js",
"shared/js/background/declarative-net-request.js",
"shared/js/background/events.es6.js",
"shared/js/background/events/3p-tracking-cookie-blocking.js",
"shared/js/background/message-handlers.js",
Expand Down
49 changes: 43 additions & 6 deletions unit-test/background/declarative-net-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as tds from '../data/tds.json'
import * as browserWrapper from '../../shared/js/background/wrapper.es6'
import * as testConfig from '../data/extension-config.json'
import * as tdsStorageStub from '../helpers/tds.es6'
import startup from '../../shared/js/background/startup.es6'
import settings from '../../shared/js/background/settings.es6'

const TEST_ETAGS = ['flib', 'flob', 'cabbage']
Expand All @@ -27,12 +28,44 @@ const expectedRuleIdsByConfigName = {
}

const expectedLookupByConfigName = {
tds: [
undefined, undefined, 'facebook.com,facebook.net',
'google-analytics.com', 'google-analytics.com', 'google-analytics.com',
'google-analytics.com', 'google-analytics.com', 'google-analytics.com',
'google-analytics.com', 'yahoo.com'
],
tds: {
2: {
type: 'trackerBlocking',
possibleTrackerDomains: ['facebook.com', 'facebook.net']
},
3: {
type: 'trackerBlocking',
possibleTrackerDomains: ['google-analytics.com']
},
4: {
type: 'trackerBlocking',
possibleTrackerDomains: ['google-analytics.com']
},
5: {
type: 'trackerBlocking',
possibleTrackerDomains: ['google-analytics.com']
},
6: {
type: 'trackerBlocking',
possibleTrackerDomains: ['google-analytics.com']
},
7: {
type: 'surrogateScript',
possibleTrackerDomains: ['google-analytics.com']
},
8: {
type: 'trackerBlocking',
possibleTrackerDomains: ['google-analytics.com']
},
9: {
type: 'trackerBlocking',
possibleTrackerDomains: ['google-analytics.com']
},
10: {
type: 'trackerBlocking',
possibleTrackerDomains: ['yahoo.com']
}
},
config: {
10002: {
type: 'trackerAllowlist',
Expand Down Expand Up @@ -128,6 +161,10 @@ describe('declarativeNetRequest', () => {

onUpdateListeners = tdsStorageStub.stub({ config }).onUpdateListeners

spyOn(startup, 'ready').and.callFake(
() => Promise.resolve()
)

spyOn(settings, 'getSetting').and.callFake(
name => settingsStorage.get(name)
)
Expand Down
6 changes: 6 additions & 0 deletions unit-test/helpers/mock-browser-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ globalThis.browser = {
browserAction: {
setIcon: () => {}
},
contextMenus: {
create: () => {},
onClicked: {
addListener: () => {}
}
},
runtime: {
id: '577dc9b9-c381-115a-2246-3f95fe0e6ffe',
sendMessage: () => {},
Expand Down

0 comments on commit 23e7b92

Please sign in to comment.