Skip to content

Commit

Permalink
Referrer cleanup and test fixes to merge latest C-S-S (#2157)
Browse files Browse the repository at this point in the history
* Mock document.URL to fix the referrer trimming tests

* Remove referrer argument as no longer needed

* Disable referrer feature when tab.referrer is not set

* Fix referrer trimming test to only call protection when argument object permits it

* Remove unused variable in test

* Update C-S-S to 4.30.0
  • Loading branch information
jonathanKingston committed Aug 1, 2023
1 parent 4fcbb1b commit a057821
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
12 changes: 6 additions & 6 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 @@ -64,7 +64,7 @@
},
"dependencies": {
"@duckduckgo/autofill": "github:duckduckgo/duckduckgo-autofill#8.0.0",
"@duckduckgo/content-scope-scripts": "github:duckduckgo/content-scope-scripts#4.27.1",
"@duckduckgo/content-scope-scripts": "github:duckduckgo/content-scope-scripts#4.30.0",
"@duckduckgo/ddg2dnr": "file:packages/ddg2dnr",
"@duckduckgo/jsbloom": "^1.0.2",
"@duckduckgo/privacy-dashboard": "github:duckduckgo/privacy-dashboard#1.6.1",
Expand Down
13 changes: 9 additions & 4 deletions shared/js/background/helpers/arguments-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function getArgumentsObject (tabId, sender, documentUrl, sessionKey) {
// Clone site so we don't retain any site changes
// @ts-ignore
const site = tabClone.site
const referrer = tab?.referrer || ''
let cookie = {}

// Special case for iframes that are blank we check if it's also enabled
Expand All @@ -26,10 +25,17 @@ export function getArgumentsObject (tabId, sender, documentUrl, sessionKey) {
site.enabledFeatures = site.enabledFeatures.filter(feature => aboutBlankEnabled.includes(feature))
}

site.enabledFeatures = site.enabledFeatures.filter((feature) => {
// Prune out the tracker allowlist feature setting as it's not needed and is large
if (feature === 'trackerAllowlist') return false

// Disable referrer trimming when we're not changing the referrer for the tab
if (feature === 'referrer' && !tab.referrer?.referrer) return false
return true
})

const featureSettings = {}
for (const feature of site.enabledFeatures) {
// Prune out the tracker allowlist feature setting as it's not needed and is large
if (feature === 'trackerAllowlist') continue
const featureSetting = utils.getFeatureSettings(feature)
if (Object.keys(featureSetting).length) {
featureSettings[feature] = featureSetting
Expand Down Expand Up @@ -67,7 +73,6 @@ export function getArgumentsObject (tabId, sender, documentUrl, sessionKey) {
stringExemptionLists: utils.getBrokenScriptLists(),
sessionKey,
site,
referrer,
platform: constants.platform,
locale: getUserLocale(),
assets: {
Expand Down
13 changes: 7 additions & 6 deletions unit-test/background/reference-tests/referrer-trimming-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const tdsStorageStub = require('../../helpers/tds')
const tdsStorage = require('../../../shared/js/background/storage/tds').default

const tabManager = require('../../../shared/js/background/tab-manager')
const { getArgumentsObject } = require('../../../shared/js/background/helpers/arguments-object')
const JsReferrerProtection = require('@duckduckgo/content-scope-scripts/src/features/referrer').default
const jsReferrerProtection = new JsReferrerProtection('jsReferrer')
const { isFeatureBroken } = require('@duckduckgo/content-scope-scripts/src/utils')

const limitReferrerData = require('../../../shared/js/background/events/referrer-trimming')

Expand Down Expand Up @@ -82,25 +84,24 @@ for (const setName of Object.keys(testSets)) {
})
}

const tab = tabManager.get({ tabId: 1 })

const FakeDocument = function () {}
Object.defineProperty(FakeDocument.prototype, 'referrer', {
get: () => test.referrerValue,
configurable: true,
enumerable: true,
writeable: true
})
FakeDocument.prototype.location = {
url: test.frameURL || test.siteURL
}
const orgDocument = globalThis.Document

// replacing real document and Document with fake ones
globalThis.Document = FakeDocument
spyOnProperty(document, 'referrer', 'get').and.returnValue(test.referrerValue)
spyOnProperty(document, 'URL', 'get').and.returnValue(test.frameURL || test.siteURL)

jsReferrerProtection.init({ referrer: tab.referrer })
const args = getArgumentsObject(1, { url: test.siteURL, frameId: 0 }, test.siteURL, 'abc123')
if (!isFeatureBroken(args, 'referrer')) {
jsReferrerProtection.callInit(args)
}

// clean up
globalThis.Document = orgDocument
Expand Down

0 comments on commit a057821

Please sign in to comment.