-
Notifications
You must be signed in to change notification settings - Fork 134
Automagically add default-browser UA/CH mitigations to sites in unprotected-temporary #5490
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
Open
laghee
wants to merge
3
commits into
main
Choose a base branch
from
kmc/unprotected-temporary-ua-ch-fba6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add temporary unprotected UA mitigations
- Loading branch information
commit 2d599321400a928e57ccaa57d56443cf40385336
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,102 @@ export function mergeInterferenceTypes(base, override) { | |
| return { ...base, ...override }; | ||
| } | ||
|
|
||
| function getEntryDomain(entry) { | ||
| return typeof entry === 'string' ? entry : entry.domain; | ||
| } | ||
|
|
||
| function appendMissingDomainEntries(items, exceptions, createEntry) { | ||
| if (!Array.isArray(items)) { | ||
| return; | ||
| } | ||
|
|
||
| const domains = new Set(items.map(getEntryDomain)); | ||
| for (const exception of exceptions) { | ||
| if (domains.has(exception.domain)) { | ||
| continue; | ||
| } | ||
| items.push(createEntry(exception)); | ||
| domains.add(exception.domain); | ||
| } | ||
| } | ||
|
|
||
| function removeDomainEntries(items, exceptions) { | ||
| if (!Array.isArray(items)) { | ||
| return; | ||
| } | ||
|
|
||
| const domains = new Set(exceptions.map(({ domain }) => domain)); | ||
| for (let i = items.length - 1; i >= 0; i--) { | ||
| if (domains.has(getEntryDomain(items[i]))) { | ||
| items.splice(i, 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function conditionMatchesDomain(condition, domain) { | ||
| if (Array.isArray(condition)) { | ||
| return condition.some((entry) => conditionMatchesDomain(entry, domain)); | ||
| } | ||
| return condition?.domain === domain; | ||
| } | ||
|
|
||
| function addClientBrandHintDomains(config, exceptions, brand) { | ||
| const domains = config.features.clientBrandHint?.settings?.domains; | ||
| appendMissingDomainEntries(domains, exceptions, ({ domain }) => ({ domain, brand })); | ||
| } | ||
|
|
||
| function addWindowsChromeUserAgentStrategies(config, exceptions) { | ||
| const strategies = config.features.customUserAgent?.features?.userAgentStrategies?.settings?.strategies; | ||
| appendMissingDomainEntries(strategies, exceptions, ({ domain }) => ({ strategy: 'ChromeUA', domain })); | ||
| } | ||
|
|
||
| function addWindowsUaChBrands(config, exceptions) { | ||
| const feature = config.features.uaChBrands; | ||
| const conditionalChanges = feature?.settings?.conditionalChanges; | ||
| if (!Array.isArray(conditionalChanges)) { | ||
| return; | ||
| } | ||
|
|
||
| removeDomainEntries(feature.exceptions, exceptions); | ||
| for (const exception of exceptions) { | ||
| if (conditionalChanges.some((change) => conditionMatchesDomain(change.condition, exception.domain))) { | ||
| continue; | ||
| } | ||
| conditionalChanges.push({ | ||
| condition: { | ||
| domain: exception.domain, | ||
| }, | ||
| patchSettings: [ | ||
| { | ||
| op: 'add', | ||
| path: '/brandName', | ||
| value: 'Google Chrome', | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export function addUnprotectedTemporaryUserAgentMitigations(platform, config, exceptions) { | ||
| if (!exceptions.length) { | ||
| return; | ||
| } | ||
|
|
||
| const customUserAgentSettings = config.features.customUserAgent?.settings; | ||
| if (platform === 'ios') { | ||
| appendMissingDomainEntries(customUserAgentSettings?.ddgFixedSites, exceptions, (entry) => ({ ...entry })); | ||
| appendMissingDomainEntries(customUserAgentSettings?.omitApplicationSites, exceptions, (entry) => ({ ...entry })); | ||
| } else if (platform === 'macos') { | ||
| appendMissingDomainEntries(customUserAgentSettings?.defaultSites, exceptions, (entry) => ({ ...entry })); | ||
| } else if (platform === 'android') { | ||
| addClientBrandHintDomains(config, exceptions, 'CHROME'); | ||
| } else if (platform === 'windows') { | ||
| addWindowsChromeUserAgentStrategies(config, exceptions); | ||
| addClientBrandHintDomains(config, exceptions, 'Google Chrome'); | ||
| addWindowsUaChBrands(config, exceptions); | ||
| } | ||
|
Comment on lines
+231
to
+242
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does seem correct. Are you able to add an unprotected, temporary demo your domain to a follow-up PR that's based off this one, and create a draft PR, and then validate the output? (perhaps even in the client too) |
||
| } | ||
|
|
||
| /** | ||
| * Traverse the input (JSON data) and ensure any "reason" fields are strings in the output. | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be part of the same if statement above?