This commit is contained in:
1 parent
7b43d260a2
commit
4357ce4094
9 files changed
+337
-100
No files matched your search
@@ -38,7 +38,7 @@ function makeChromeEnv() {
|
||||
let sessionRules = {};
|
||||
|
||||
const webRequest = {
|
||||
onBeforeSendHeaders: { addListener: (cb) => { handlers.onBeforeSendHeaders = cb; } },
|
||||
onBeforeSendHeaders: { addListener: (cb, filter) => { handlers.onBeforeSendHeaders = cb; handlers.onBeforeSendHeadersFilter = filter; } },
|
||||
onBeforeRequest: { addListener: (cb) => { handlers.onBeforeRequest = cb; } },
|
||||
};
|
||||
|
||||
@@ -113,7 +113,7 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
if (!cap) throw new Error('onBeforeSendHeaders listener was not registered');
|
||||
await cap(videoDetails(11, 'https://cdnA.example.com/hls/live/seg-1.ts', 'https://pageA.example.com/watch'));
|
||||
|
||||
const rA = env.storage['goon_referers']?.['cdna.example.com'];
|
||||
const rA = env.storage['goon_referers']?.['example.com'];
|
||||
if (!rA) throw new Error('referer was not captured for cdnA');
|
||||
if (rA.referer !== 'https://pageA.example.com/watch') {
|
||||
throw new Error('captured referer wrong: ' + JSON.stringify(rA));
|
||||
@@ -123,10 +123,10 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
await env.handlers.onRemoved(22);
|
||||
|
||||
const after = env.storage['goon_referers'];
|
||||
if (!after || !after['cdna.example.com']) {
|
||||
if (!after || !after['example.com']) {
|
||||
throw new Error('closing tab 22 wiped goon_referers — host A referer lost');
|
||||
}
|
||||
if (after['cdna.example.com'].referer !== 'https://pageA.example.com/watch') {
|
||||
if (after['example.com'].referer !== 'https://pageA.example.com/watch') {
|
||||
throw new Error('closing tab 22 corrupted the host A referer: ' + JSON.stringify(after));
|
||||
}
|
||||
});
|
||||
@@ -143,7 +143,7 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
// The very tab that captured the referer now closes.
|
||||
await env.handlers.onRemoved(11);
|
||||
|
||||
const after = env.storage['goon_referers']?.['cdnb.example.com'];
|
||||
const after = env.storage['goon_referers']?.['example.com'];
|
||||
if (!after) throw new Error('goon_referers for the capturing host was wiped on its own tab close');
|
||||
if (after.referer !== 'https://pageB.example.com/v') {
|
||||
throw new Error('capturing-tab close corrupted referer: ' + JSON.stringify(after));
|
||||
@@ -153,7 +153,7 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
// (b) Should-fix + coverage: two hosts proxied concurrently must each keep
|
||||
// their own referer. With a shared rule id + non-atomic install this regressed;
|
||||
// per-host rule ids + the per-host lock keep them distinct.
|
||||
it('concurrent proxy fetches for two hosts keep distinct per-host referer rules', async () => {
|
||||
it('concurrent proxy fetches for two distinct registrable domains keep distinct referer rules', async () => {
|
||||
const env = makeChromeEnv();
|
||||
let fetchCalls = 0;
|
||||
global.chrome = env.chrome;
|
||||
@@ -173,7 +173,7 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
const handler = env.handlers.onMessage[env.handlers.onMessage.length - 1];
|
||||
let respA, respB;
|
||||
handler({ type: 'GOON_PROXY_FETCH', url: 'https://cdnA.example.com/hls/live/seg-1.ts', referer: 'https://pageA.example.com/watch' }, {}, (r) => { respA = r; });
|
||||
handler({ type: 'GOON_PROXY_FETCH', url: 'https://cdnB.example.com/hls/live/seg-1.ts', referer: 'https://pageB.example.com/watch' }, {}, (r) => { respB = r; });
|
||||
handler({ type: 'GOON_PROXY_FETCH', url: 'https://cdnB.other.com/hls/live/seg-1.ts', referer: 'https://pageB.other.com/watch' }, {}, (r) => { respB = r; });
|
||||
|
||||
await new Promise(r => setTimeout(r, 30)); // let async sends + installs flush
|
||||
|
||||
@@ -184,8 +184,8 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
// Each host must have its OWN installed DNR rule with its OWN referer —
|
||||
// host A must NOT be served host B's referer (the 412 regression).
|
||||
const rules = env.getSessionRules();
|
||||
const ruleA = Object.values(rules).find(r => r.condition?.requestDomains?.includes('cdna.example.com'));
|
||||
const ruleB = Object.values(rules).find(r => r.condition?.requestDomains?.includes('cdnb.example.com'));
|
||||
const ruleA = Object.values(rules).find(r => r.condition?.requestDomains?.includes('example.com'));
|
||||
const ruleB = Object.values(rules).find(r => r.condition?.requestDomains?.includes('other.com'));
|
||||
if (!ruleA) throw new Error('no DNR rule installed for cdna; rules=' + JSON.stringify(rules));
|
||||
if (!ruleB) throw new Error('no DNR rule installed for cdnb; rules=' + JSON.stringify(rules));
|
||||
if (ruleA.id === ruleB.id) {
|
||||
@@ -196,7 +196,7 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
if (refA !== 'https://pageA.example.com/watch') {
|
||||
throw new Error('cdnA served wrong referer: ' + refA);
|
||||
}
|
||||
if (refB !== 'https://pageB.example.com/watch') {
|
||||
if (refB !== 'https://pageB.other.com/watch') {
|
||||
throw new Error('cdnB served wrong referer: ' + refB);
|
||||
}
|
||||
if (refA === refB) throw new Error('both hosts got the same referer — cross-host bleed');
|
||||
@@ -236,4 +236,123 @@ describe('PR#5: per-host referer capture + proxy race', () => {
|
||||
throw new Error(`expected idempotent install, but re-added rule ${installs - installsAfterFirst} extra time(s)`);
|
||||
}
|
||||
});
|
||||
|
||||
// (c) Domain generalization — the core 412 fix. A provider referer captured for
|
||||
// ONE edge subdomain must cover a SIBLING subdomain: the DNR rule is scoped to
|
||||
// the registrable domain and the store is domain-keyed, so a proxy for ev-h
|
||||
// needs no per-subdomain capture.
|
||||
it('a proxy for a sibling subdomain reuses the captured referer via the registrable domain', async () => {
|
||||
const env = makeChromeEnv();
|
||||
global.chrome = env.chrome;
|
||||
global.fetch = async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
headers: { get: () => 'application/vnd.apple.mpegurl' },
|
||||
arrayBuffer: async () => new Uint8Array([0]).buffer,
|
||||
});
|
||||
await loadBackground(env);
|
||||
|
||||
// Capture the provider referer for hv-h.phncdn.com (one edge subdomain).
|
||||
await env.handlers.onBeforeSendHeaders(
|
||||
videoDetails(11, 'https://hv-h.phncdn.com/hls/live/seg-1.ts', 'https://provider.example.com/watch'));
|
||||
|
||||
// Proxy a SIBLING subdomain ev-h.phncdn.com with NO referer in the message:
|
||||
// the SW must resolve it from the domain-keyed store.
|
||||
const handler = env.handlers.onMessage[env.handlers.onMessage.length - 1];
|
||||
let resp;
|
||||
handler({ type: 'GOON_PROXY_FETCH', url: 'https://ev-h.phncdn.com/hls/live/seg-1.ts' }, {}, (r) => { resp = r; });
|
||||
await new Promise(r => setTimeout(r, 20));
|
||||
|
||||
if (!resp || !resp.ok) throw new Error('sibling proxy failed: ' + JSON.stringify(resp));
|
||||
const rules = env.getSessionRules();
|
||||
const rule = Object.values(rules).find(r => r.condition?.requestDomains?.includes('phncdn.com'));
|
||||
if (!rule) throw new Error('no domain-scoped DNR rule for phncdn.com; rules=' + JSON.stringify(rules));
|
||||
const ref = rule.action.requestHeaders?.find(h => h.header.toLowerCase() === 'referer')?.value;
|
||||
if (ref !== 'https://provider.example.com/watch') {
|
||||
throw new Error('sibling edge served wrong referer: ' + ref);
|
||||
}
|
||||
// The rule is scoped to the registrable domain, NOT the exact subdomain.
|
||||
if (rule.condition.requestDomains?.includes('ev-h.phncdn.com')) {
|
||||
throw new Error('rule scoped to exact subdomain instead of registrable domain');
|
||||
}
|
||||
});
|
||||
|
||||
// (d) With no captured referer and none in the message, install NO rule: the
|
||||
// self-origin fallback (guaranteed-wrong for hotlink protection) is dropped.
|
||||
it('no captured referer and no message referer installs no DNR rule', async () => {
|
||||
const env = makeChromeEnv();
|
||||
let fetchCalls = 0;
|
||||
global.chrome = env.chrome;
|
||||
global.fetch = async () => {
|
||||
fetchCalls++;
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
headers: { get: () => 'application/vnd.apple.mpegurl' },
|
||||
arrayBuffer: async () => new Uint8Array([0]).buffer,
|
||||
};
|
||||
};
|
||||
await loadBackground(env);
|
||||
const handler = env.handlers.onMessage[env.handlers.onMessage.length - 1];
|
||||
let resp;
|
||||
handler({ type: 'GOON_PROXY_FETCH', url: 'https://cdnA.example.com/hls/live/seg-1.ts' }, {}, (r) => { resp = r; });
|
||||
await new Promise(r => setTimeout(r, 20));
|
||||
|
||||
if (!resp || !resp.ok) throw new Error('proxy fetch failed: ' + JSON.stringify(resp));
|
||||
if (fetchCalls < 1) throw new Error('proxy fetch did not run');
|
||||
const rules = env.getSessionRules();
|
||||
if (Object.keys(rules).length !== 0) {
|
||||
throw new Error('installed a rule with no referer (self-origin fallback not dropped): ' + JSON.stringify(rules));
|
||||
}
|
||||
});
|
||||
|
||||
// (e) GOON_GET_REFERERS (used by scan-time capture) resolves each manifest host
|
||||
// via the domain-keyed store: a sibling subdomain gets the provider referer,
|
||||
// and an uncaptured host is omitted.
|
||||
it('GOON_GET_REFERERS resolves manifest hosts through the domain-keyed store', async () => {
|
||||
const env = makeChromeEnv();
|
||||
global.chrome = env.chrome;
|
||||
await loadBackground(env);
|
||||
|
||||
// Capture for hv-h.phncdn.com only.
|
||||
await env.handlers.onBeforeSendHeaders(
|
||||
videoDetails(11, 'https://hv-h.phncdn.com/hls/live/seg-1.ts', 'https://provider.example.com/watch'));
|
||||
|
||||
const handler = env.handlers.onMessage[env.handlers.onMessage.length - 1];
|
||||
let resp;
|
||||
handler(
|
||||
{ type: 'GOON_GET_REFERERS', hosts: ['ev-h.phncdn.com', 'hv-h.phncdn.com', 'cdnA.example.com'] },
|
||||
{},
|
||||
(r) => { resp = r; });
|
||||
await new Promise(r => setTimeout(r, 15));
|
||||
|
||||
if (!resp || !resp.ok) throw new Error('GOON_GET_REFERERS failed: ' + JSON.stringify(resp));
|
||||
const referers = resp.referers || {};
|
||||
if (referers['ev-h.phncdn.com'] !== 'https://provider.example.com/watch') {
|
||||
throw new Error('sibling subdomain did not resolve the captured referer: ' + JSON.stringify(referers));
|
||||
}
|
||||
if (referers['hv-h.phncdn.com'] !== 'https://provider.example.com/watch') {
|
||||
throw new Error('captured subdomain did not resolve its own referer: ' + JSON.stringify(referers));
|
||||
}
|
||||
if ('cdnA.example.com' in referers) {
|
||||
throw new Error('uncaptured host should be omitted from referers: ' + JSON.stringify(referers));
|
||||
}
|
||||
});
|
||||
|
||||
// Regression for the live 412 bug: the capture listener MUST request
|
||||
// requestHeaders via extraInfoSpec, or details.requestHeaders is undefined and
|
||||
// readReferer() returns '' for every request — so no referer is ever stored,
|
||||
// every entry stays referer-less, and the phncdn proxy 412s. The callback-level
|
||||
// tests above pass even without this (they hand requestHeaders in directly), so
|
||||
// this asserts the registration itself.
|
||||
it('the onBeforeSendHeaders capture listener requests requestHeaders via extraInfoSpec', async () => {
|
||||
const env = makeChromeEnv();
|
||||
global.chrome = env.chrome;
|
||||
await loadBackground(env);
|
||||
const filter = env.handlers.onBeforeSendHeadersFilter;
|
||||
if (!filter) throw new Error('onBeforeSendHeaders listener registered no filter object');
|
||||
if (!Array.isArray(filter.extraInfoSpec) || !filter.extraInfoSpec.includes('requestHeaders')) {
|
||||
throw new Error('capture listener did not request requestHeaders via extraInfoSpec: ' + JSON.stringify(filter));
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user