Skip to content

Add priv interface loading #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion inject/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function initCode () {
load({
platform: processedConfig.platform,
trackerLookup: processedConfig.trackerLookup,
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup)
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup),
site: processedConfig.site
})

const messageSecret = processedConfig.messageSecret
Expand Down
3 changes: 2 additions & 1 deletion inject/apple.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function initCode () {
load({
platform: processedConfig.platform,
trackerLookup: processedConfig.trackerLookup,
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup)
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup),
site: processedConfig.site
})

init(processedConfig)
Expand Down
2 changes: 2 additions & 0 deletions inject/chrome-mv3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { load, init, update } from '../src/content-scope-features.js'
import { isTrackerOrigin } from '../src/trackers'
import { computeLimitedSiteObject } from '../src/utils.js'

const secret = (crypto.getRandomValues(new Uint32Array(1))[0] / 2 ** 32).toString().replace('0.', '')

Expand All @@ -15,6 +16,7 @@ load({
},
trackerLookup,
documentOriginIsTracker: isTrackerOrigin(trackerLookup),
site: computeLimitedSiteObject(),
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
bundledConfig: $BUNDLED_CONFIG$
})
Expand Down
3 changes: 3 additions & 0 deletions inject/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @category Content Scope Scripts Integrations
*/
import { isTrackerOrigin } from '../src/trackers'
import { computeLimitedSiteObject } from '../src/utils'

/**
* Inject all the overwrites into the page.
Expand Down Expand Up @@ -46,13 +47,15 @@ function init () {
const randomPassword = '_p' + randomString()
const reusableMethodName = '_rm' + randomString()
const reusableSecret = '_r' + randomString()
const siteObject = computeLimitedSiteObject()
const initialScript = `
/* global contentScopeFeatures */
contentScopeFeatures.load({
platform: {
name: 'extension'
},
trackerLookup: ${JSON.stringify(trackerLookup)},
site: ${JSON.stringify(siteObject)},
documentOriginIsTracker: ${documentOriginIsTracker},
bundledConfig: ${JSON.stringify(bundledConfig)}
})
Expand Down
3 changes: 2 additions & 1 deletion inject/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ async function initCode () {
load({
platform: processedConfig.platform,
trackerLookup: processedConfig.trackerLookup,
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup)
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup),
site: processedConfig.site
})

// mark this phase as loaded
Expand Down
2 changes: 2 additions & 0 deletions inject/mozilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { load, init, update } from '../src/content-scope-features.js'
import { isTrackerOrigin } from '../src/trackers'
import { computeLimitedSiteObject } from '../src/utils.js'

const allowedMessages = [
'getClickToLoadState',
Expand All @@ -28,6 +29,7 @@ function initCode () {
},
trackerLookup,
documentOriginIsTracker: isTrackerOrigin(trackerLookup),
site: computeLimitedSiteObject(),
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
bundledConfig: $BUNDLED_CONFIG$
})
Expand Down
3 changes: 2 additions & 1 deletion inject/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function initCode () {
load({
platform: processedConfig.platform,
trackerLookup: processedConfig.trackerLookup,
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup)
documentOriginIsTracker: isTrackerOrigin(processedConfig.trackerLookup),
site: processedConfig.site
})

init(processedConfig)
Expand Down
1 change: 1 addition & 0 deletions src/content-scope-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const performanceMonitor = new PerformanceMonitor()

/**
* @typedef {object} LoadArgs
* @property {object} site
* @property {object} platform
* @property {string} platform.name
* @property {string} [platform.version]
Expand Down
54 changes: 32 additions & 22 deletions src/features/navigator-interface.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import { defineProperty, DDGPromise } from '../utils'
import ContentFeature from '../content-feature'

function injectNavigatorInterface (args) {
try {
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
if (navigator.duckduckgo) {
return
}
if (!args.platform || !args.platform.name) {
return
}
defineProperty(Navigator.prototype, 'duckduckgo', {
value: {
platform: args.platform.name,
isDuckDuckGo () {
return DDGPromise.resolve(true)
}
},
enumerable: true,
configurable: false,
writable: false
})
} catch {
// todo: Just ignore this exception?
}
}

export default class NavigatorInterface extends ContentFeature {
init (args) {
try {
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
if (navigator.duckduckgo) {
return
}
if (!args.platform || !args.platform.name) {
return
}
defineProperty(Navigator.prototype, 'duckduckgo', {
value: {
platform: args.platform.name,
isDuckDuckGo () {
return DDGPromise.resolve(true)
}
},
enumerable: true,
configurable: false,
writable: false
})
} catch {
// todo: Just ignore this exception?
load (args) {
if (this.matchDomainFeatureSetting('privilegedDomains').length) {
injectNavigatorInterface(args)
}
}

init (args) {
injectNavigatorInterface(args)
}
}
18 changes: 15 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,16 @@ export function isUnprotectedDomain (topLevelHostname, featureList) {
* @property {string} sessionKey
*/

/**
* Used to inialize extension code in the load phase
*/
export function computeLimitedSiteObject () {
const topLevelHostname = getTabHostname()
return {
domain: topLevelHostname
}
}

/**
* Expansion point to add platform specific versioning logic
* @param {UserPreferences} preferences
Expand Down Expand Up @@ -576,7 +586,10 @@ function isSupportedVersion (minSupportedVersion, currentVersion) {
*/
export function processConfig (data, userList, preferences, platformSpecificFeatures = []) {
const topLevelHostname = getTabHostname()
const site = computeLimitedSiteObject()
const allowlisted = userList.filter(domain => domain === topLevelHostname).length > 0
const remoteFeatureNames = Object.keys(data.features)
const platformSpecificFeaturesNotInRemoteConfig = platformSpecificFeatures.filter((featureName) => !remoteFeatureNames.includes(featureName))
/** @type {Record<string, any>} */
const output = { ...preferences }
if (output.platform) {
Expand All @@ -587,12 +600,11 @@ export function processConfig (data, userList, preferences, platformSpecificFeat
}
const enabledFeatures = computeEnabledFeatures(data, topLevelHostname, preferences.platform?.version, platformSpecificFeatures)
const isBroken = isUnprotectedDomain(topLevelHostname, data.unprotectedTemporary)
output.site = {
domain: topLevelHostname,
output.site = Object.assign(site, {
isBroken,
allowlisted,
enabledFeatures
}
})
// TODO
output.cookie = {}

Expand Down