-
Notifications
You must be signed in to change notification settings - Fork 92
fix(google-analytics): allow ga-audiences regional Google domains through proxy #729
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f3a54f
fix(google-analytics): allow ga-audiences regional Google domains thrβ¦
harlan-zw 78b158b
refactor(match-domain): walk literal segments instead of building a rβ¦
harlan-zw af4ac3f
fix(match-domain): constrain wildcard to ccTLD shape
harlan-zw 91b472b
fix(match-domain): tighten TLD wildcard to canonical ccTLD shapes only
harlan-zw 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
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Match a hostname against an allowlist pattern. | ||
| * | ||
| * Patterns may include `*` as a TLD wildcard that matches a top-level domain | ||
| * suffix shaped like a real ccTLD or gTLD: | ||
| * - `com` (the canonical gTLD we care about) | ||
| * - any 2-letter ccTLD (`tw`, `jp`, `de`, ...) | ||
| * - regional `com.<cc>` or `co.<cc>` (e.g. `com.tw`, `co.jp`, `com.hk`) | ||
| * | ||
| * Used for geo-localized Google ccTLDs: | ||
| * `www.google.*` matches `www.google.com`, `www.google.com.tw`, `www.google.co.jp`. | ||
| * | ||
| * The pattern is intentionally narrow: it rejects attacker-controlled suffixes | ||
| * like `www.google.foo.bar` (two arbitrary 3-letter labels) or | ||
| * `www.google.attacker.com` (long second-level label). | ||
| * | ||
| * Bare patterns also match subdomains, e.g. `google.com` matches `mail.google.com`. | ||
| */ | ||
| const TLD_WILDCARD_RE = /^(?:com|[a-z]{2}|(?:com|co)\.[a-z]{2})$/i | ||
|
|
||
| export function matchDomain(domain: string, pattern: string): boolean { | ||
| if (!pattern.includes('*')) | ||
| return domain === pattern || domain.endsWith(`.${pattern}`) | ||
|
|
||
| // Only support a trailing single `*` wildcard for TLD matching (the only | ||
| // shape we use in practice). Reject any other pattern shape rather than | ||
| // silently allowing it. | ||
| if (!pattern.endsWith('*') || pattern.indexOf('*') !== pattern.length - 1) | ||
| return false | ||
|
|
||
| const prefix = pattern.slice(0, -1) // includes trailing dot, e.g. "www.google." | ||
| if (!domain.startsWith(prefix)) | ||
| return false | ||
|
|
||
| const tld = domain.slice(prefix.length) | ||
| return TLD_WILDCARD_RE.test(tld) | ||
| } |
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,49 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { matchDomain } from '../../packages/script/src/runtime/server/utils/match-domain' | ||
|
|
||
| describe('matchDomain', () => { | ||
| it('matches exact hostname', () => { | ||
| expect(matchDomain('www.google-analytics.com', 'www.google-analytics.com')).toBe(true) | ||
| }) | ||
|
|
||
| it('matches subdomain via parent pattern', () => { | ||
| expect(matchDomain('mail.google.com', 'google.com')).toBe(true) | ||
| expect(matchDomain('google.com', 'google.com')).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects non-matching hostname', () => { | ||
| expect(matchDomain('evil.com', 'google.com')).toBe(false) | ||
| expect(matchDomain('googleX.com', 'google.com')).toBe(false) | ||
| }) | ||
|
|
||
| // Issue #728: ga-audiences fires to www.google.<cctld> based on geo | ||
| it('matches geo-localized Google ccTLDs via wildcard', () => { | ||
| expect(matchDomain('www.google.com', 'www.google.*')).toBe(true) | ||
| expect(matchDomain('www.google.com.tw', 'www.google.*')).toBe(true) | ||
| expect(matchDomain('www.google.co.jp', 'www.google.*')).toBe(true) | ||
| expect(matchDomain('www.google.com.hk', 'www.google.*')).toBe(true) | ||
| }) | ||
|
|
||
| it('wildcard does not match a different host root', () => { | ||
| expect(matchDomain('evil.google.com', 'www.google.*')).toBe(false) | ||
| expect(matchDomain('www.googleX.com', 'www.google.*')).toBe(false) | ||
| }) | ||
|
|
||
| // Security: the wildcard must not match attacker-controlled subdomains. | ||
| // Without a TLD shape constraint, `*` would match `attacker.com` here. | ||
| it('wildcard rejects attacker-controlled suffixes', () => { | ||
| expect(matchDomain('www.google.attacker.com', 'www.google.*')).toBe(false) | ||
| expect(matchDomain('www.google.com.attacker.com', 'www.google.*')).toBe(false) | ||
| expect(matchDomain('www.google.evil-domain.com', 'www.google.*')).toBe(false) | ||
| // Three or more labels in the suffix β not a valid ccTLD shape | ||
| expect(matchDomain('www.google.a.b.c', 'www.google.*')).toBe(false) | ||
| // Two arbitrary 3-letter labels are not a real ccTLD shape; only com.<cc> / co.<cc> allowed | ||
| expect(matchDomain('www.google.foo.bar', 'www.google.*')).toBe(false) | ||
| expect(matchDomain('www.google.abc.xyz', 'www.google.*')).toBe(false) | ||
| }) | ||
|
|
||
| it('escapes regex metachars in the pattern', () => { | ||
| expect(matchDomain('foo.bar.com', 'foo+bar.com')).toBe(false) | ||
| expect(matchDomain('foo+bar.com', 'foo+bar.com')).toBe(true) | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.