|
| 1 | +import{describe,expect,it}from'vitest' |
| 2 | +import{matchDomain}from'../../packages/script/src/runtime/server/utils/match-domain' |
| 3 | + |
| 4 | +describe('matchDomain',()=>{ |
| 5 | +it('matches exact hostname',()=>{ |
| 6 | +expect(matchDomain('www.google-analytics.com','www.google-analytics.com')).toBe(true) |
| 7 | +}) |
| 8 | + |
| 9 | +it('matches subdomain via parent pattern',()=>{ |
| 10 | +expect(matchDomain('mail.google.com','google.com')).toBe(true) |
| 11 | +expect(matchDomain('google.com','google.com')).toBe(true) |
| 12 | +}) |
| 13 | + |
| 14 | +it('rejects non-matching hostname',()=>{ |
| 15 | +expect(matchDomain('evil.com','google.com')).toBe(false) |
| 16 | +expect(matchDomain('googleX.com','google.com')).toBe(false) |
| 17 | +}) |
| 18 | + |
| 19 | +// Issue #728: ga-audiences fires to www.google.<cctld> based on geo |
| 20 | +it('matches geo-localized Google ccTLDs via wildcard',()=>{ |
| 21 | +expect(matchDomain('www.google.com','www.google.*')).toBe(true) |
| 22 | +expect(matchDomain('www.google.com.tw','www.google.*')).toBe(true) |
| 23 | +expect(matchDomain('www.google.co.jp','www.google.*')).toBe(true) |
| 24 | +expect(matchDomain('www.google.com.hk','www.google.*')).toBe(true) |
| 25 | +}) |
| 26 | + |
| 27 | +it('wildcard does not match a different host root',()=>{ |
| 28 | +expect(matchDomain('evil.google.com','www.google.*')).toBe(false) |
| 29 | +expect(matchDomain('www.googleX.com','www.google.*')).toBe(false) |
| 30 | +}) |
| 31 | + |
| 32 | +// Security: the wildcard must not match attacker-controlled subdomains. |
| 33 | +// Without a TLD shape constraint, `*` would match `attacker.com` here. |
| 34 | +it('wildcard rejects attacker-controlled suffixes',()=>{ |
| 35 | +expect(matchDomain('www.google.attacker.com','www.google.*')).toBe(false) |
| 36 | +expect(matchDomain('www.google.com.attacker.com','www.google.*')).toBe(false) |
| 37 | +expect(matchDomain('www.google.evil-domain.com','www.google.*')).toBe(false) |
| 38 | +// Three or more labels in the suffix → not a valid ccTLD shape |
| 39 | +expect(matchDomain('www.google.a.b.c','www.google.*')).toBe(false) |
| 40 | +// Two arbitrary 3-letter labels are not a real ccTLD shape; only com.<cc> / co.<cc> allowed |
| 41 | +expect(matchDomain('www.google.foo.bar','www.google.*')).toBe(false) |
| 42 | +expect(matchDomain('www.google.abc.xyz','www.google.*')).toBe(false) |
| 43 | +}) |
| 44 | + |
| 45 | +it('escapes regex metachars in the pattern',()=>{ |
| 46 | +expect(matchDomain('foo.bar.com','foo+bar.com')).toBe(false) |
| 47 | +expect(matchDomain('foo+bar.com','foo+bar.com')).toBe(true) |
| 48 | +}) |
| 49 | +}) |
0 commit comments