-
Notifications
You must be signed in to change notification settings - Fork 31
Add Windows client hint brands override to match header we override natively #2028
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
29
commits into
main
Choose a base branch
from
km/add-ch-brand-shim
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 9 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
bee4106
Initial PoC windows brand hints override
laghee b1a0458
Clean up extraneous quick test and comments
laghee 9592f42
Mirror native approach for headers in js api override
laghee dbdebd9
Fix errant space
laghee d142735
Return early if brands config is missing or malformed
laghee cc7a316
Rename feature more generally
laghee 3ce98f8
Merge remote-tracking branch 'origin/main' into km/add-ch-brand-shim
laghee 4bc32a4
Clean up unnecessary bits, fix auto-complaints
laghee 0bf2a5f
Merge remote-tracking branch 'origin/main' into km/add-ch-brand-shim
laghee 271e18d
Fix missing GREASE value
laghee 1125d9d
Lint
laghee c6b0a1f
Add debug logging
laghee bbc26a4
Add different logging attempt
laghee 56e2faa
Force debug to surface logging
laghee 308501b
Force debug logging in content-feature
laghee 8aa428c
LOG, DAMMIT
laghee 0ef8669
Figure out why not running
laghee ec67adf
Switch order to match header indices + clean up debug logging
laghee 129f447
Address bot comment
laghee 152f3d5
Merge branch 'origin/main' into km/add-ch-brand-shim
laghee ae9abf9
Clean up old cached code
laghee 2f535c0
Simplify approach by replicating native: overwrite Edge, zap WebView2…
laghee 941714d
Overwrite fullVersionList brands as well
laghee deb3631
Address cursorbot comment on not directly modifying browser objects
laghee 2fefa76
Make test names clearer
laghee c90d720
Merge 'origin/main' into km/add-ch-brand-shim
laghee 537a8d5
Address further cranky bot comment
laghee 94796ff
Botttttt
laghee 9e4d0a6
Dimishing returns critical mass
laghee 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
injected/integration-test/test-pages/ua-ch-brands/config/brand-override.json
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "readme": "This config tests that uaChBrands can override navigator.userAgentData.brands", | ||
| "version": 1, | ||
| "unprotectedTemporary": [], | ||
| "features": { | ||
| "uaChBrands": { | ||
| "state": "enabled", | ||
| "exceptions": [], | ||
| "settings": { | ||
| "brands": [ | ||
| { | ||
| "brand": "Chromium", | ||
| "version": "140" | ||
| }, | ||
| { | ||
| "brand": "DuckDuckGo", | ||
| "version": "140" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
injected/integration-test/test-pages/ua-ch-brands/config/brands-missing.json
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "readme": "This config tests that uaChBrands feature returns early when brands setting is missing (feature is enabled but settings.brands is undefined)", | ||
| "version": 1, | ||
| "unprotectedTemporary": [], | ||
| "features": { | ||
| "uaChBrands": { | ||
| "state": "enabled", | ||
| "exceptions": [], | ||
| "settings": { | ||
| } | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
injected/integration-test/test-pages/ua-ch-brands/index.html
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width"> | ||
| <title>UA CH Brands</title> | ||
| <link rel="stylesheet" href="../shared/style.css"> | ||
| </head> | ||
| <body> | ||
| <p><a href="../index.html">[Home]</a></p> | ||
|
|
||
| <p>UA CH Brands</p> | ||
| <ul> | ||
| <li><a href="./pages/brand-override.html">Brand Override</a> - <a href="./config/brand-override.json">Config</a></li> | ||
| <li><a href="./pages/brands-missing.html">Brands Missing</a> - <a href="./config/brands-missing.json">Config</a></li> | ||
| </ul> | ||
| </body> | ||
| </html> |
103 changes: 103 additions & 0 deletions
103
injected/integration-test/test-pages/ua-ch-brands/pages/brand-override.html
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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width"> | ||
| <title>UA CH Brands - Brand Override</title> | ||
| <link rel="stylesheet" href="../../shared/style.css"> | ||
| </head> | ||
| <body> | ||
| <script src="../../shared/utils.js"></script> | ||
| <p><a href="../index.html">[UA CH Brands]</a></p> | ||
|
|
||
| <p>This page verifies that navigator.userAgentData.brands uses the configured brands from the uaChBrands feature settings</p> | ||
|
|
||
| <script> | ||
| test('Brand override', async () => { | ||
| const results = []; | ||
|
|
||
| if (navigator.userAgentData && navigator.userAgentData.brands) { | ||
| const brands = navigator.userAgentData.brands; | ||
| const brandNames = new Set(brands.map(b => b.brand)); | ||
|
|
||
| results.push({ | ||
| name: 'contains Chromium brand', | ||
| result: brandNames.has('Chromium'), | ||
| expected: true | ||
| }); | ||
|
|
||
| results.push({ | ||
| name: 'contains DuckDuckGo brand', | ||
| result: brandNames.has('DuckDuckGo'), | ||
| expected: true | ||
| }); | ||
|
|
||
| results.push({ | ||
| name: 'has correct number of brands', | ||
| result: brands.length, | ||
| expected: 2 | ||
| }); | ||
|
|
||
| results.push({ | ||
| name: 'does not contain Microsoft Edge brand', | ||
| result: !brandNames.has('Microsoft Edge'), | ||
| expected: true | ||
| }); | ||
|
|
||
| results.push({ | ||
| name: 'does not contain Microsoft Edge WebView2 brand', | ||
| result: !brandNames.has('Microsoft Edge WebView2'), | ||
| expected: true | ||
| }); | ||
|
|
||
| if (navigator.userAgentData.getHighEntropyValues) { | ||
| try { | ||
| const highEntropyValues = await navigator.userAgentData.getHighEntropyValues(['brands']); | ||
| if (highEntropyValues.brands) { | ||
| const heBrandNames = new Set(highEntropyValues.brands.map(b => b.brand)); | ||
| results.push({ | ||
| name: 'high entropy contains Chromium', | ||
| result: heBrandNames.has('Chromium'), | ||
| expected: true | ||
| }); | ||
| results.push({ | ||
| name: 'high entropy contains DuckDuckGo', | ||
| result: heBrandNames.has('DuckDuckGo'), | ||
| expected: true | ||
| }); | ||
|
|
||
| results.push({ | ||
| name: 'high entropy does not contain Microsoft Edge', | ||
| result: !heBrandNames.has('Microsoft Edge'), | ||
| expected: true | ||
| }); | ||
|
|
||
| results.push({ | ||
| name: 'high entropy does not contain Microsoft Edge WebView2', | ||
| result: !heBrandNames.has('Microsoft Edge WebView2'), | ||
| expected: true | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| results.push({ | ||
| name: 'getHighEntropyValues works', | ||
| result: false, | ||
| expected: true | ||
| }); | ||
| } | ||
| } | ||
| } else { | ||
| results.push({ | ||
| name: 'navigator.userAgentData.brands available', | ||
| result: false, | ||
| expected: true | ||
| }); | ||
| } | ||
|
|
||
| return results; | ||
| }); | ||
|
|
||
| renderResults(); | ||
| </script> | ||
| </body> | ||
| </html> |
70 changes: 70 additions & 0 deletions
70
injected/integration-test/test-pages/ua-ch-brands/pages/brands-missing.html
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width"> | ||
| <title>UA CH Brands - Brands Missing</title> | ||
| <link rel="stylesheet" href="../../shared/style.css"> | ||
| </head> | ||
| <body> | ||
| <script src="../../shared/utils.js"></script> | ||
| <p><a href="../index.html">[UA CH Brands]</a></p> | ||
|
|
||
| <p>This page verifies that when brands setting is missing/null/empty, the feature returns early and original brands are preserved</p> | ||
|
|
||
| <script> | ||
| test('Feature returns early when brands missing', async () => { | ||
| const results = []; | ||
|
|
||
| if (navigator.userAgentData && navigator.userAgentData.brands) { | ||
| // When brands setting is missing, the feature returns early in init() | ||
|
|
||
| const brands = navigator.userAgentData.brands; | ||
|
|
||
| // Verify brands still works normally | ||
| results.push({ | ||
| name: 'brands array exists and has content', | ||
| result: Array.isArray(brands) && brands.length > 0, | ||
| expected: true | ||
| }); | ||
|
|
||
| // Verify getHighEntropyValues also works and returns matching values | ||
| if (navigator.userAgentData.getHighEntropyValues) { | ||
| try { | ||
| const highEntropyValues = await navigator.userAgentData.getHighEntropyValues(['brands']); | ||
| const heBrands = highEntropyValues.brands; | ||
|
|
||
| if (heBrands) { | ||
| // Both should return the same unwrapped values | ||
| const brandsStr = JSON.stringify(brands); | ||
| const heBrandsStr = JSON.stringify(heBrands); | ||
|
|
||
| results.push({ | ||
| name: 'getHighEntropyValues and direct access return matching values', | ||
| result: brandsStr === heBrandsStr, | ||
| expected: true | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| results.push({ | ||
| name: 'getHighEntropyValues works', | ||
| result: false, | ||
| expected: true | ||
| }); | ||
| } | ||
| } | ||
| } else { | ||
| results.push({ | ||
| name: 'navigator.userAgentData.brands available', | ||
| result: false, | ||
| expected: true | ||
| }); | ||
| } | ||
|
|
||
| return results; | ||
| }); | ||
|
|
||
| renderResults(); | ||
| </script> | ||
| </body> | ||
| </html> |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
| import { ResultsCollector } from './page-objects/results-collector.js'; | ||
|
|
||
| test('UA CH Brands override', async ({ page }, testInfo) => { | ||
| const collector = ResultsCollector.create(page, testInfo.project.use); | ||
| await collector.load( | ||
| '/ua-ch-brands/pages/brand-override.html', | ||
| './integration-test/test-pages/ua-ch-brands/config/brand-override.json', | ||
| ); | ||
| const results = await collector.results(); | ||
|
|
||
| for (const key in results) { | ||
| for (const result of results[key]) { | ||
| await test.step(`${key}: ${result.name}`, () => { | ||
| expect(result.result).toEqual(result.expected); | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test('UA CH Brands when brands missing', async ({ page }, testInfo) => { | ||
| const collector = ResultsCollector.create(page, testInfo.project.use); | ||
| await collector.load( | ||
| '/ua-ch-brands/pages/brands-missing.html', | ||
| './integration-test/test-pages/ua-ch-brands/config/brands-missing.json', | ||
| ); | ||
| const results = await collector.results(); | ||
|
|
||
| for (const key in results) { | ||
| for (const result of results[key]) { | ||
| await test.step(`${key}: ${result.name}`, () => { | ||
| expect(result.result).toEqual(result.expected); | ||
| }); | ||
| } | ||
| } | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import ContentFeature from '../content-feature'; | ||
|
|
||
| export default class UaChBrands extends ContentFeature { | ||
| constructor(featureName, importConfig, args) { | ||
| super(featureName, importConfig, args); | ||
|
|
||
| this.cachedBrands = null; | ||
| this.originalBrands = null; | ||
| } | ||
|
|
||
| init() { | ||
| const configuredBrands = this.getFeatureSetting('brands'); | ||
|
|
||
| if (!configuredBrands || configuredBrands.length === 0) { | ||
| this.log.info('No client hint brands correctly configured, feature disabled'); | ||
| return; | ||
| } | ||
| this.shimUserAgentDataBrands(); | ||
| } | ||
|
|
||
| /** | ||
| * Override navigator.userAgentData.brands to match the Sec-CH-UA header | ||
| */ | ||
| shimUserAgentDataBrands() { | ||
| try { | ||
| // @ts-expect-error - userAgentData not yet standard | ||
| if (!navigator.userAgentData || !navigator.userAgentData.brands) { | ||
| return; | ||
| } | ||
|
|
||
| if (!this.originalBrands) { | ||
| // @ts-expect-error - userAgentData not yet standard | ||
| this.originalBrands = [...navigator.userAgentData.brands]; | ||
| } | ||
|
|
||
| if (this.cachedBrands) { | ||
laghee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.applyBrandsOverride(this.cachedBrands); | ||
| return; | ||
laghee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const mutatedBrands = this.applyBrandMutations(); | ||
|
|
||
| if (mutatedBrands) { | ||
| this.cachedBrands = mutatedBrands; | ||
| this.applyBrandsOverride(mutatedBrands); | ||
| } | ||
| } catch (error) { | ||
| this.log.error('Error in shimUserAgentDataBrands:', error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Apply brand mutations using the configured brands from feature settings | ||
| * @returns {Array<{brand: string, version: string}>|null} - Configured brands or null if no changes | ||
| */ | ||
| applyBrandMutations() { | ||
| const configuredBrands = this.getFeatureSetting('brands'); | ||
|
|
||
| if (!configuredBrands || configuredBrands.length === 0) { | ||
| this.log.info('No CH brands configured, skipping mutations'); | ||
| return null; | ||
| } | ||
|
|
||
| this.log.info('Applying configured brands:', configuredBrands); | ||
| return configuredBrands; | ||
| } | ||
|
|
||
| /** | ||
| * Apply the brand override to navigator.userAgentData.brands | ||
| * @param {Array<{brand: string, version: string}>} newBrands - Brands to apply | ||
| */ | ||
| applyBrandsOverride(newBrands) { | ||
| // @ts-expect-error - userAgentData not yet standard | ||
| const proto = Object.getPrototypeOf(navigator.userAgentData); | ||
|
|
||
| this.wrapProperty(proto, 'brands', { | ||
| get: () => newBrands, | ||
| }); | ||
|
|
||
| if (proto.getHighEntropyValues) { | ||
| this.wrapMethod(proto, 'getHighEntropyValues', async (originalFn, ...args) => { | ||
| // @ts-expect-error - userAgentData not yet standard | ||
| const result = await originalFn.apply(navigator.userAgentData, args); | ||
| if (args[0] && args[0].includes('brands')) { | ||
| result.brands = newBrands; | ||
| } | ||
| return result; | ||
| }); | ||
laghee marked this conversation as resolved.
Show resolved
Hide resolved
laghee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.