standdown 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/POLICIES.md +44 -0
- package/README.md +171 -0
- package/dist/chunk-25A7QG5Z.mjs +274 -0
- package/dist/chunk-25A7QG5Z.mjs.map +1 -0
- package/dist/chunk-SEZFAOUU.mjs +382 -0
- package/dist/chunk-SEZFAOUU.mjs.map +1 -0
- package/dist/chunk-SVN6UWA4.mjs +857 -0
- package/dist/chunk-SVN6UWA4.mjs.map +1 -0
- package/dist/content.cjs +1288 -0
- package/dist/content.cjs.map +1 -0
- package/dist/content.d.cts +61 -0
- package/dist/content.d.ts +61 -0
- package/dist/content.mjs +201 -0
- package/dist/content.mjs.map +1 -0
- package/dist/index.cjs +1375 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.mjs +242 -0
- package/dist/index.mjs.map +1 -0
- package/dist/policies.cjs +620 -0
- package/dist/policies.cjs.map +1 -0
- package/dist/policies.d.cts +1517 -0
- package/dist/policies.d.ts +1517 -0
- package/dist/policies.mjs +606 -0
- package/dist/policies.mjs.map +1 -0
- package/dist/session-C5gH-LaQ.d.ts +21 -0
- package/dist/session-n0px3Agb.d.cts +21 -0
- package/dist/stores-CELX9aHN.d.ts +70 -0
- package/dist/stores-CKCdKDdX.d.cts +70 -0
- package/dist/types-A9OJ7SWj.d.cts +190 -0
- package/dist/types-A9OJ7SWj.d.ts +190 -0
- package/dist/webext.cjs +1733 -0
- package/dist/webext.cjs.map +1 -0
- package/dist/webext.d.cts +110 -0
- package/dist/webext.d.ts +110 -0
- package/dist/webext.mjs +346 -0
- package/dist/webext.mjs.map +1 -0
- package/package.json +77 -0
|
@@ -0,0 +1,857 @@
|
|
|
1
|
+
// src/validation.ts
|
|
2
|
+
var BEHAVIORS = /* @__PURE__ */ new Set([
|
|
3
|
+
"suppress-prompts",
|
|
4
|
+
"no-cookie-write",
|
|
5
|
+
"no-redirect",
|
|
6
|
+
"no-background-tracking"
|
|
7
|
+
]);
|
|
8
|
+
var PARAM_MATCHES = /* @__PURE__ */ new Set(["exists", "equals", "contains"]);
|
|
9
|
+
var COOKIE_MATCHES = /* @__PURE__ */ new Set(["exact", "substring"]);
|
|
10
|
+
var DOMAIN_KINDS = /* @__PURE__ */ new Set(["suffix", "regex"]);
|
|
11
|
+
var REFERRER_CLASSES = /* @__PURE__ */ new Set([
|
|
12
|
+
"own-site",
|
|
13
|
+
"organic",
|
|
14
|
+
"direct",
|
|
15
|
+
"advertiser-internal",
|
|
16
|
+
"other"
|
|
17
|
+
]);
|
|
18
|
+
var ACTIVATION_REFERRER_CLASSES = /* @__PURE__ */ new Set(["own-site", "organic", "direct"]);
|
|
19
|
+
function validatePolicy(value) {
|
|
20
|
+
const policy = object(value, "policy");
|
|
21
|
+
string(policy.id, "policy.id");
|
|
22
|
+
literal(policy.schemaVersion, 3, "policy.schemaVersion");
|
|
23
|
+
string(policy.policyVersion, "policy.policyVersion");
|
|
24
|
+
const network = object(policy.network, "policy.network");
|
|
25
|
+
string(network.id, "policy.network.id");
|
|
26
|
+
string(network.name, "policy.network.name");
|
|
27
|
+
if (network.policyUrl !== void 0) {
|
|
28
|
+
urlString(network.policyUrl, "policy.network.policyUrl");
|
|
29
|
+
}
|
|
30
|
+
const detection = object(policy.detection, "policy.detection");
|
|
31
|
+
optionalArray(detection.advertiserHosts, "policy.detection.advertiserHosts").forEach(
|
|
32
|
+
validateDomainRule
|
|
33
|
+
);
|
|
34
|
+
optionalArray(detection.landingParams, "policy.detection.landingParams").forEach(
|
|
35
|
+
validateParamRule
|
|
36
|
+
);
|
|
37
|
+
optionalArray(detection.redirectDomains, "policy.detection.redirectDomains").forEach(
|
|
38
|
+
validateDomainRule
|
|
39
|
+
);
|
|
40
|
+
optionalArray(detection.cookiePatterns, "policy.detection.cookiePatterns").forEach(
|
|
41
|
+
validateCookieRule
|
|
42
|
+
);
|
|
43
|
+
optionalArray(detection.initiatorRules, "policy.detection.initiatorRules").forEach(
|
|
44
|
+
validateInitiatorRule
|
|
45
|
+
);
|
|
46
|
+
const standdown = object(policy.standdown, "policy.standdown");
|
|
47
|
+
literal(standdown.scope, "advertiser", "policy.standdown.scope");
|
|
48
|
+
if (standdown.sessionRule !== "session-or-min" && standdown.sessionRule !== "inactivity-window") {
|
|
49
|
+
throw new TypeError("policy.standdown.sessionRule is invalid");
|
|
50
|
+
}
|
|
51
|
+
nonNegativeFiniteNumber(
|
|
52
|
+
standdown.minDurationMs,
|
|
53
|
+
"policy.standdown.minDurationMs"
|
|
54
|
+
);
|
|
55
|
+
if (standdown.inactivityMs !== void 0) {
|
|
56
|
+
nonNegativeFiniteNumber(
|
|
57
|
+
standdown.inactivityMs,
|
|
58
|
+
"policy.standdown.inactivityMs"
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const behaviors = array(standdown.behaviors, "policy.standdown.behaviors");
|
|
62
|
+
for (const behavior of behaviors) {
|
|
63
|
+
if (!BEHAVIORS.has(behavior)) {
|
|
64
|
+
throw new TypeError(`policy.standdown.behaviors contains invalid behavior`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const activation = object(policy.activation, "policy.activation");
|
|
68
|
+
if (activation.mode !== "user-click" && activation.mode !== "never") {
|
|
69
|
+
throw new TypeError("policy.activation.mode is invalid");
|
|
70
|
+
}
|
|
71
|
+
if (activation.maxPromptsPerJourney !== void 0) {
|
|
72
|
+
nonNegativeFiniteNumber(
|
|
73
|
+
activation.maxPromptsPerJourney,
|
|
74
|
+
"policy.activation.maxPromptsPerJourney"
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
for (const referrerClass of optionalArray(
|
|
78
|
+
activation.allowedReferrerClasses,
|
|
79
|
+
"policy.activation.allowedReferrerClasses"
|
|
80
|
+
)) {
|
|
81
|
+
if (!ACTIVATION_REFERRER_CLASSES.has(referrerClass)) {
|
|
82
|
+
throw new TypeError(
|
|
83
|
+
"policy.activation.allowedReferrerClasses contains invalid class"
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const metadata = object(policy.metadata, "policy.metadata");
|
|
88
|
+
urlString(metadata.sourceUrl, "policy.metadata.sourceUrl");
|
|
89
|
+
dateString(metadata.lastVerified, "policy.metadata.lastVerified");
|
|
90
|
+
if (metadata.notes !== void 0) {
|
|
91
|
+
string(metadata.notes, "policy.metadata.notes");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function validatePolicies(values) {
|
|
95
|
+
values.forEach(validatePolicy);
|
|
96
|
+
}
|
|
97
|
+
function validateParamRule(ruleValue) {
|
|
98
|
+
const rule = object(ruleValue, "ParamRule");
|
|
99
|
+
const anyOf = array(rule.anyOf, "ParamRule.anyOf");
|
|
100
|
+
if (anyOf.length === 0) {
|
|
101
|
+
throw new TypeError("ParamRule.anyOf must not be empty");
|
|
102
|
+
}
|
|
103
|
+
for (const groupValue of anyOf) {
|
|
104
|
+
const group = object(groupValue, "ParamRule.anyOf[]");
|
|
105
|
+
const allOf = array(group.allOf, "ParamRule.anyOf[].allOf");
|
|
106
|
+
if (allOf.length === 0) {
|
|
107
|
+
throw new TypeError("ParamRule.anyOf[].allOf must not be empty");
|
|
108
|
+
}
|
|
109
|
+
allOf.forEach(validateParamMatcher);
|
|
110
|
+
}
|
|
111
|
+
if (rule.reason !== void 0) {
|
|
112
|
+
string(rule.reason, "ParamRule.reason");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function validateParamMatcher(matcherValue) {
|
|
116
|
+
const matcher = object(matcherValue, "ParamMatcher");
|
|
117
|
+
string(matcher.name, "ParamMatcher.name");
|
|
118
|
+
if (matcher.name.trim() === "") {
|
|
119
|
+
throw new TypeError("ParamMatcher.name must not be empty");
|
|
120
|
+
}
|
|
121
|
+
if (matcher.value !== void 0) {
|
|
122
|
+
string(matcher.value, "ParamMatcher.value");
|
|
123
|
+
}
|
|
124
|
+
if (matcher.match !== void 0 && !PARAM_MATCHES.has(matcher.match)) {
|
|
125
|
+
throw new TypeError("ParamMatcher.match is invalid");
|
|
126
|
+
}
|
|
127
|
+
if ((matcher.match === "equals" || matcher.match === "contains") && matcher.value === void 0) {
|
|
128
|
+
throw new TypeError("ParamMatcher.value is required for equals/contains");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function validateDomainRule(ruleValue) {
|
|
132
|
+
const rule = object(ruleValue, "DomainRule");
|
|
133
|
+
string(rule.pattern, "DomainRule.pattern");
|
|
134
|
+
if (!DOMAIN_KINDS.has(rule.kind)) {
|
|
135
|
+
throw new TypeError("DomainRule.kind is invalid");
|
|
136
|
+
}
|
|
137
|
+
if (rule.kind === "regex") {
|
|
138
|
+
try {
|
|
139
|
+
new RegExp(rule.pattern);
|
|
140
|
+
} catch {
|
|
141
|
+
throw new TypeError("DomainRule.pattern must be a valid regex");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (rule.comment !== void 0) {
|
|
145
|
+
string(rule.comment, "DomainRule.comment");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function validateCookieRule(ruleValue) {
|
|
149
|
+
const rule = object(ruleValue, "CookieRule");
|
|
150
|
+
string(rule.name, "CookieRule.name");
|
|
151
|
+
if (!COOKIE_MATCHES.has(rule.match)) {
|
|
152
|
+
throw new TypeError("CookieRule.match is invalid");
|
|
153
|
+
}
|
|
154
|
+
if (rule.reason !== void 0) {
|
|
155
|
+
string(rule.reason, "CookieRule.reason");
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function validateInitiatorRule(ruleValue) {
|
|
159
|
+
const rule = object(ruleValue, "InitiatorRule");
|
|
160
|
+
if (!REFERRER_CLASSES.has(rule.referrerClass)) {
|
|
161
|
+
throw new TypeError("InitiatorRule.referrerClass is invalid");
|
|
162
|
+
}
|
|
163
|
+
if (rule.reason !== void 0) {
|
|
164
|
+
string(rule.reason, "InitiatorRule.reason");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function object(value, path) {
|
|
168
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
169
|
+
throw new TypeError(`${path} must be an object`);
|
|
170
|
+
}
|
|
171
|
+
return value;
|
|
172
|
+
}
|
|
173
|
+
function string(value, path) {
|
|
174
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
175
|
+
throw new TypeError(`${path} must be a non-empty string`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function literal(value, expected, path) {
|
|
179
|
+
if (value !== expected) {
|
|
180
|
+
throw new TypeError(`${path} must be ${String(expected)}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function array(value, path) {
|
|
184
|
+
if (!Array.isArray(value)) {
|
|
185
|
+
throw new TypeError(`${path} must be an array`);
|
|
186
|
+
}
|
|
187
|
+
return value;
|
|
188
|
+
}
|
|
189
|
+
function optionalArray(value, path) {
|
|
190
|
+
if (value === void 0) {
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
return array(value, path);
|
|
194
|
+
}
|
|
195
|
+
function nonNegativeFiniteNumber(value, path) {
|
|
196
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
197
|
+
throw new TypeError(`${path} must be a non-negative finite number`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function urlString(value, path) {
|
|
201
|
+
string(value, path);
|
|
202
|
+
try {
|
|
203
|
+
new URL(value);
|
|
204
|
+
} catch {
|
|
205
|
+
throw new TypeError(`${path} must be an absolute URL`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function dateString(value, path) {
|
|
209
|
+
string(value, path);
|
|
210
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
211
|
+
throw new TypeError(`${path} must be YYYY-MM-DD`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// src/detect.ts
|
|
216
|
+
var ORGANIC_REFERRER_SUFFIXES = [
|
|
217
|
+
"google.com",
|
|
218
|
+
"bing.com",
|
|
219
|
+
"duckduckgo.com",
|
|
220
|
+
"yahoo.com",
|
|
221
|
+
"ecosia.org",
|
|
222
|
+
"baidu.com",
|
|
223
|
+
"yandex.com"
|
|
224
|
+
];
|
|
225
|
+
function detect(signals, policies) {
|
|
226
|
+
const currentUrl = parseUrl(signals.url);
|
|
227
|
+
if (!currentUrl) {
|
|
228
|
+
return {
|
|
229
|
+
matched: [],
|
|
230
|
+
selfMatch: false,
|
|
231
|
+
failClosedReason: "invalid-url"
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const advertiserHost = currentUrl.hostname.toLowerCase();
|
|
235
|
+
const matched = [];
|
|
236
|
+
let selfMatch = false;
|
|
237
|
+
for (const policy of policies) {
|
|
238
|
+
const scopedSelfMatch = hasScopedSelfExemption(
|
|
239
|
+
currentUrl,
|
|
240
|
+
signals.selfPatterns,
|
|
241
|
+
policy
|
|
242
|
+
);
|
|
243
|
+
const unscopedSelfMatch = hasUnscopedSelfExemption(
|
|
244
|
+
currentUrl,
|
|
245
|
+
signals.selfPatterns
|
|
246
|
+
);
|
|
247
|
+
if (scopedSelfMatch || unscopedSelfMatch) {
|
|
248
|
+
selfMatch = true;
|
|
249
|
+
}
|
|
250
|
+
if (scopedSelfMatch) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
matched.push(
|
|
254
|
+
...collectPolicyMatches(policy, signals, currentUrl, advertiserHost)
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
matched.sort((left, right) => kindPriority(left.kind) - kindPriority(right.kind));
|
|
258
|
+
const strongest = matched[0] ? {
|
|
259
|
+
policyId: matched[0].policyId,
|
|
260
|
+
advertiserHost: matched[0].advertiserHost,
|
|
261
|
+
reason: matched[0].reason
|
|
262
|
+
} : void 0;
|
|
263
|
+
return strongest ? { matched, selfMatch, strongest } : { matched, selfMatch };
|
|
264
|
+
}
|
|
265
|
+
function classifyReferrer(signals, advertiserHost) {
|
|
266
|
+
const candidate = signals.initiator ?? signals.referrer;
|
|
267
|
+
if (!candidate) {
|
|
268
|
+
return "direct";
|
|
269
|
+
}
|
|
270
|
+
const referrerHost = hostFromUrl(candidate);
|
|
271
|
+
if (!referrerHost) {
|
|
272
|
+
return "other";
|
|
273
|
+
}
|
|
274
|
+
if ((signals.publisherSites ?? []).some(
|
|
275
|
+
(site) => domainSuffixMatches(referrerHost, site)
|
|
276
|
+
)) {
|
|
277
|
+
return "own-site";
|
|
278
|
+
}
|
|
279
|
+
if (domainSuffixMatches(referrerHost, advertiserHost)) {
|
|
280
|
+
return "advertiser-internal";
|
|
281
|
+
}
|
|
282
|
+
if (ORGANIC_REFERRER_SUFFIXES.some(
|
|
283
|
+
(suffix) => domainSuffixMatches(referrerHost, suffix)
|
|
284
|
+
)) {
|
|
285
|
+
return "organic";
|
|
286
|
+
}
|
|
287
|
+
return "other";
|
|
288
|
+
}
|
|
289
|
+
function domainRuleMatchesUrl(rule, value) {
|
|
290
|
+
const url = parseUrl(value);
|
|
291
|
+
const host = url?.hostname.toLowerCase() ?? value.toLowerCase();
|
|
292
|
+
if (rule.kind === "suffix") {
|
|
293
|
+
return domainSuffixMatches(host, rule.pattern);
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
const regex = new RegExp(rule.pattern, "i");
|
|
297
|
+
return regex.test(host) || (url ? regex.test(url.href) : regex.test(value));
|
|
298
|
+
} catch {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function collectPolicyMatches(policy, signals, currentUrl, advertiserHost) {
|
|
303
|
+
const matches = [];
|
|
304
|
+
const advertiserHostMatches = policy.detection.advertiserHosts === void 0 || policy.detection.advertiserHosts.some(
|
|
305
|
+
(rule) => domainRuleMatchesUrl(rule, advertiserHost)
|
|
306
|
+
);
|
|
307
|
+
if (advertiserHostMatches) {
|
|
308
|
+
for (const rule of policy.detection.landingParams ?? []) {
|
|
309
|
+
if (paramRuleMatches(rule, currentUrl.searchParams)) {
|
|
310
|
+
matches.push(
|
|
311
|
+
matchedRule(
|
|
312
|
+
policy,
|
|
313
|
+
"landing-param",
|
|
314
|
+
describeParamRule(rule),
|
|
315
|
+
advertiserHost,
|
|
316
|
+
rule.reason ?? "landing parameter matched"
|
|
317
|
+
)
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
for (const rule of policy.detection.redirectDomains ?? []) {
|
|
323
|
+
if ((signals.redirectChain ?? []).some((url) => domainRuleMatchesUrl(rule, url))) {
|
|
324
|
+
matches.push(
|
|
325
|
+
matchedRule(
|
|
326
|
+
policy,
|
|
327
|
+
"redirect-domain",
|
|
328
|
+
`${rule.kind}:${rule.pattern}`,
|
|
329
|
+
advertiserHost,
|
|
330
|
+
rule.comment ?? "redirect domain matched"
|
|
331
|
+
)
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (advertiserHostMatches) {
|
|
336
|
+
for (const rule of policy.detection.cookiePatterns ?? []) {
|
|
337
|
+
if (cookieRuleMatches(rule, signals.cookieNames ?? [])) {
|
|
338
|
+
matches.push(
|
|
339
|
+
matchedRule(
|
|
340
|
+
policy,
|
|
341
|
+
"cookie",
|
|
342
|
+
`${rule.match}:${rule.name}`,
|
|
343
|
+
advertiserHost,
|
|
344
|
+
rule.reason ?? "first-party cookie name matched"
|
|
345
|
+
)
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (advertiserHostMatches) {
|
|
351
|
+
for (const rule of policy.detection.initiatorRules ?? []) {
|
|
352
|
+
if (initiatorRuleMatches(rule, signals, advertiserHost)) {
|
|
353
|
+
matches.push(
|
|
354
|
+
matchedRule(
|
|
355
|
+
policy,
|
|
356
|
+
"initiator",
|
|
357
|
+
`referrer-class:${rule.referrerClass}`,
|
|
358
|
+
advertiserHost,
|
|
359
|
+
rule.reason ?? "initiator/referrer class matched"
|
|
360
|
+
)
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return matches;
|
|
366
|
+
}
|
|
367
|
+
function matchedRule(policy, kind, rule, advertiserHost, reason) {
|
|
368
|
+
return {
|
|
369
|
+
policyId: policy.id,
|
|
370
|
+
networkId: policy.network.id,
|
|
371
|
+
networkName: policy.network.name,
|
|
372
|
+
kind,
|
|
373
|
+
rule,
|
|
374
|
+
advertiserHost,
|
|
375
|
+
reason,
|
|
376
|
+
sourceUrl: policy.metadata.sourceUrl
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
function kindPriority(kind) {
|
|
380
|
+
if (kind === "redirect-domain") {
|
|
381
|
+
return 0;
|
|
382
|
+
}
|
|
383
|
+
if (kind === "landing-param") {
|
|
384
|
+
return 1;
|
|
385
|
+
}
|
|
386
|
+
if (kind === "cookie") {
|
|
387
|
+
return 2;
|
|
388
|
+
}
|
|
389
|
+
return 3;
|
|
390
|
+
}
|
|
391
|
+
function paramRuleMatches(rule, params) {
|
|
392
|
+
return rule.anyOf.some(
|
|
393
|
+
(group) => group.allOf.every((matcher) => paramMatcherMatches(matcher, params))
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
function paramMatcherMatches(matcher, params) {
|
|
397
|
+
const values = params.getAll(matcher.name);
|
|
398
|
+
const mode = matcher.match ?? (matcher.value === void 0 ? "exists" : "equals");
|
|
399
|
+
if (mode === "exists") {
|
|
400
|
+
return values.length > 0;
|
|
401
|
+
}
|
|
402
|
+
const expectedValue = matcher.value;
|
|
403
|
+
if (expectedValue === void 0) {
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
if (mode === "equals") {
|
|
407
|
+
return values.some((value) => value === expectedValue);
|
|
408
|
+
}
|
|
409
|
+
return values.some((value) => value.includes(expectedValue));
|
|
410
|
+
}
|
|
411
|
+
function cookieRuleMatches(rule, cookieNames) {
|
|
412
|
+
if (rule.match === "exact") {
|
|
413
|
+
return cookieNames.some((name) => name === rule.name);
|
|
414
|
+
}
|
|
415
|
+
return cookieNames.some((name) => name.includes(rule.name));
|
|
416
|
+
}
|
|
417
|
+
function initiatorRuleMatches(rule, signals, advertiserHost) {
|
|
418
|
+
return classifyReferrer(signals, advertiserHost) === rule.referrerClass;
|
|
419
|
+
}
|
|
420
|
+
function hasScopedSelfExemption(currentUrl, selfPatterns, policy) {
|
|
421
|
+
return (selfPatterns ?? []).some((pattern) => {
|
|
422
|
+
const scopedToPolicy = pattern.policyId === policy.id;
|
|
423
|
+
const scopedToNetwork = pattern.networkId === policy.network.id;
|
|
424
|
+
return (scopedToPolicy || scopedToNetwork) && paramMatcherMatches(pattern, currentUrl.searchParams);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
function hasUnscopedSelfExemption(currentUrl, selfPatterns) {
|
|
428
|
+
return (selfPatterns ?? []).some(
|
|
429
|
+
(pattern) => pattern.policyId === void 0 && pattern.networkId === void 0 && paramMatcherMatches(pattern, currentUrl.searchParams)
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
function domainSuffixMatches(host, pattern) {
|
|
433
|
+
const normalizedHost = host.toLowerCase().replace(/\.$/, "");
|
|
434
|
+
const normalizedPattern = pattern.toLowerCase().replace(/^\./, "").replace(/\.$/, "");
|
|
435
|
+
return normalizedHost === normalizedPattern || normalizedHost.endsWith(`.${normalizedPattern}`);
|
|
436
|
+
}
|
|
437
|
+
function describeParamRule(rule) {
|
|
438
|
+
return rule.anyOf.map(
|
|
439
|
+
(group) => group.allOf.map((matcher) => {
|
|
440
|
+
const mode = matcher.match ?? (matcher.value === void 0 ? "exists" : "equals");
|
|
441
|
+
return matcher.value === void 0 ? `${matcher.name}:${mode}` : `${matcher.name}:${mode}:${matcher.value}`;
|
|
442
|
+
}).join("&")
|
|
443
|
+
).join("|");
|
|
444
|
+
}
|
|
445
|
+
function parseUrl(value) {
|
|
446
|
+
try {
|
|
447
|
+
return new URL(value);
|
|
448
|
+
} catch {
|
|
449
|
+
return void 0;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function hostFromUrl(value) {
|
|
453
|
+
return parseUrl(value)?.hostname.toLowerCase();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// src/session.ts
|
|
457
|
+
var MemoryStateStore = class {
|
|
458
|
+
#state;
|
|
459
|
+
constructor(initialState) {
|
|
460
|
+
this.#state = initialState ? cloneState(initialState) : void 0;
|
|
461
|
+
}
|
|
462
|
+
async load() {
|
|
463
|
+
return this.#state ? cloneState(this.#state) : void 0;
|
|
464
|
+
}
|
|
465
|
+
async save(state) {
|
|
466
|
+
this.#state = cloneState(state);
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
var StanddownSession = class {
|
|
470
|
+
#store;
|
|
471
|
+
#auditLog;
|
|
472
|
+
#maxAuditEntries;
|
|
473
|
+
#readOnlyAuditLog = [];
|
|
474
|
+
constructor(store, opts) {
|
|
475
|
+
this.#store = store;
|
|
476
|
+
this.#auditLog = opts?.auditLog ?? true;
|
|
477
|
+
this.#maxAuditEntries = Math.max(0, opts?.maxAuditEntries ?? 1e3);
|
|
478
|
+
}
|
|
479
|
+
async ingest(signals, policies) {
|
|
480
|
+
const advertiserHost = hostFromUrl2(signals.url);
|
|
481
|
+
try {
|
|
482
|
+
validatePolicies(policies);
|
|
483
|
+
} catch (error) {
|
|
484
|
+
return this.#failClosedWithAudit(
|
|
485
|
+
signals.now,
|
|
486
|
+
"ingest",
|
|
487
|
+
advertiserHost,
|
|
488
|
+
`malformed-policy: ${messageFromError(error)}`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
const detection = detect(signals, policies);
|
|
492
|
+
if (detection.failClosedReason) {
|
|
493
|
+
return this.#failClosedWithAudit(
|
|
494
|
+
signals.now,
|
|
495
|
+
"ingest",
|
|
496
|
+
advertiserHost,
|
|
497
|
+
detection.failClosedReason,
|
|
498
|
+
detection
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
return this.#withState(signals.now, (state) => {
|
|
502
|
+
pruneExpiredSessions(state, signals.now);
|
|
503
|
+
if (detection.strongest) {
|
|
504
|
+
const matchedPolicies = policiesForDetection(policies, detection);
|
|
505
|
+
if (matchedPolicies.length === 0) {
|
|
506
|
+
return {
|
|
507
|
+
decision: failClosedDecision("matched-policy-missing"),
|
|
508
|
+
detection
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
const record = upsertSessionRecord(
|
|
512
|
+
state,
|
|
513
|
+
detection.strongest.advertiserHost,
|
|
514
|
+
detection.strongest.policyId,
|
|
515
|
+
matchedPolicies,
|
|
516
|
+
signals.now
|
|
517
|
+
);
|
|
518
|
+
return {
|
|
519
|
+
decision: decisionFromRecord(record, signals.now, {
|
|
520
|
+
reason: detection.strongest.reason,
|
|
521
|
+
referrerClass: classifyReferrer(signals, record.advertiserHost)
|
|
522
|
+
}),
|
|
523
|
+
detection
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
const activeDecision = advertiserHost ? activeDecisionForHost(state, advertiserHost, signals.now) : void 0;
|
|
527
|
+
return {
|
|
528
|
+
decision: activeDecision ?? {
|
|
529
|
+
standDown: false,
|
|
530
|
+
reason: detection.selfMatch ? "self-exempted-no-active-standdown" : "no-active-standdown",
|
|
531
|
+
behaviors: [],
|
|
532
|
+
referrerClass: advertiserHost ? classifyReferrer(signals, advertiserHost) : "other"
|
|
533
|
+
},
|
|
534
|
+
detection
|
|
535
|
+
};
|
|
536
|
+
}, "ingest");
|
|
537
|
+
}
|
|
538
|
+
async shouldStandDown(advertiserHost, now) {
|
|
539
|
+
return this.#withState(
|
|
540
|
+
now,
|
|
541
|
+
(state) => {
|
|
542
|
+
pruneExpiredSessions(state, now);
|
|
543
|
+
return {
|
|
544
|
+
decision: activeDecisionForHost(state, advertiserHost, now) ?? {
|
|
545
|
+
standDown: false,
|
|
546
|
+
reason: "no-active-standdown",
|
|
547
|
+
behaviors: []
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
},
|
|
551
|
+
"shouldStandDown",
|
|
552
|
+
normalizeHost(advertiserHost),
|
|
553
|
+
{ persist: false }
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
async recordActivity(now) {
|
|
557
|
+
await this.#withState(
|
|
558
|
+
now,
|
|
559
|
+
(state) => {
|
|
560
|
+
pruneExpiredSessions(state, now);
|
|
561
|
+
for (const record of Object.values(state.sessions)) {
|
|
562
|
+
if (record.sessionRule === "inactivity-window") {
|
|
563
|
+
record.lastActivityAt = now;
|
|
564
|
+
const expiresAt = computeExpiresAt(record);
|
|
565
|
+
if (expiresAt !== void 0) {
|
|
566
|
+
record.expiresAt = expiresAt;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
decision: {
|
|
572
|
+
standDown: false,
|
|
573
|
+
reason: "activity-recorded",
|
|
574
|
+
behaviors: []
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
},
|
|
578
|
+
"recordActivity"
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
async exportAuditLog() {
|
|
582
|
+
const state = await this.#loadState();
|
|
583
|
+
return trimAuditLog(
|
|
584
|
+
[...state.auditLog, ...this.#readOnlyAuditLog],
|
|
585
|
+
this.#maxAuditEntries
|
|
586
|
+
).map(cloneAuditEntry);
|
|
587
|
+
}
|
|
588
|
+
async #withState(now, fn, action, advertiserHost, opts = {}) {
|
|
589
|
+
let state;
|
|
590
|
+
try {
|
|
591
|
+
state = await this.#loadState();
|
|
592
|
+
} catch {
|
|
593
|
+
return failClosedDecision("store-error");
|
|
594
|
+
}
|
|
595
|
+
const result = fn(state);
|
|
596
|
+
if (this.#auditLog) {
|
|
597
|
+
const entry = auditEntry({
|
|
598
|
+
time: now,
|
|
599
|
+
action,
|
|
600
|
+
advertiserHost: advertiserHost ?? result.detection?.strongest?.advertiserHost,
|
|
601
|
+
detection: result.detection,
|
|
602
|
+
decision: result.decision
|
|
603
|
+
});
|
|
604
|
+
if (opts.persist === false) {
|
|
605
|
+
appendAuditEntry(this.#readOnlyAuditLog, entry, this.#maxAuditEntries);
|
|
606
|
+
} else {
|
|
607
|
+
appendAuditEntry(state.auditLog, entry, this.#maxAuditEntries);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (opts.persist === false) {
|
|
611
|
+
return result.decision;
|
|
612
|
+
}
|
|
613
|
+
try {
|
|
614
|
+
await this.#store.save(state);
|
|
615
|
+
} catch {
|
|
616
|
+
return failClosedDecision("store-error");
|
|
617
|
+
}
|
|
618
|
+
return result.decision;
|
|
619
|
+
}
|
|
620
|
+
async #failClosedWithAudit(now, action, advertiserHost, reason, detection) {
|
|
621
|
+
const decision = failClosedDecision(reason);
|
|
622
|
+
if (!this.#auditLog) {
|
|
623
|
+
return decision;
|
|
624
|
+
}
|
|
625
|
+
try {
|
|
626
|
+
const state = await this.#loadState();
|
|
627
|
+
appendAuditEntry(
|
|
628
|
+
state.auditLog,
|
|
629
|
+
auditEntry({
|
|
630
|
+
time: now,
|
|
631
|
+
action,
|
|
632
|
+
advertiserHost,
|
|
633
|
+
detection,
|
|
634
|
+
decision
|
|
635
|
+
}),
|
|
636
|
+
this.#maxAuditEntries
|
|
637
|
+
);
|
|
638
|
+
await this.#store.save(state);
|
|
639
|
+
} catch {
|
|
640
|
+
return failClosedDecision("store-error");
|
|
641
|
+
}
|
|
642
|
+
return decision;
|
|
643
|
+
}
|
|
644
|
+
async #loadState() {
|
|
645
|
+
return await this.#store.load() ?? { sessions: {}, auditLog: [] };
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
function appendAuditEntry(auditLog, entry, maxEntries) {
|
|
649
|
+
if (maxEntries === 0) {
|
|
650
|
+
auditLog.length = 0;
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
auditLog.push(entry);
|
|
654
|
+
if (auditLog.length > maxEntries) {
|
|
655
|
+
auditLog.splice(0, auditLog.length - maxEntries);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
function trimAuditLog(auditLog, maxEntries) {
|
|
659
|
+
if (maxEntries === 0) {
|
|
660
|
+
return [];
|
|
661
|
+
}
|
|
662
|
+
return auditLog.slice(Math.max(0, auditLog.length - maxEntries));
|
|
663
|
+
}
|
|
664
|
+
function upsertSessionRecord(state, advertiserHost, primaryPolicyId, policies, now) {
|
|
665
|
+
const key = normalizeHost(advertiserHost);
|
|
666
|
+
const existing = state.sessions[key];
|
|
667
|
+
const startedAt = existing?.startedAt ?? now;
|
|
668
|
+
const lastActivityAt = now;
|
|
669
|
+
const sessionRule = existing?.sessionRule === "session-or-min" || policies.some((policy) => policy.standdown.sessionRule === "session-or-min") ? "session-or-min" : "inactivity-window";
|
|
670
|
+
const baseRecord = {
|
|
671
|
+
advertiserHost: key,
|
|
672
|
+
policyId: primaryPolicyId,
|
|
673
|
+
startedAt,
|
|
674
|
+
lastActivityAt,
|
|
675
|
+
sessionRule,
|
|
676
|
+
minDurationMs: Math.max(
|
|
677
|
+
existing?.minDurationMs ?? 0,
|
|
678
|
+
...policies.map((policy) => policy.standdown.minDurationMs)
|
|
679
|
+
),
|
|
680
|
+
behaviors: unionBehaviors([
|
|
681
|
+
...existing?.behaviors ?? [],
|
|
682
|
+
...policies.flatMap((policy) => policy.standdown.behaviors)
|
|
683
|
+
])
|
|
684
|
+
};
|
|
685
|
+
if (sessionRule === "inactivity-window") {
|
|
686
|
+
const inactivityMs = Math.max(
|
|
687
|
+
existing?.inactivityMs ?? 0,
|
|
688
|
+
...policies.flatMap(
|
|
689
|
+
(policy) => policy.standdown.inactivityMs === void 0 ? [] : [policy.standdown.inactivityMs]
|
|
690
|
+
)
|
|
691
|
+
);
|
|
692
|
+
if (inactivityMs > 0) {
|
|
693
|
+
baseRecord.inactivityMs = inactivityMs;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
const computedExpiresAt = computeExpiresAt(baseRecord);
|
|
697
|
+
if (computedExpiresAt !== void 0) {
|
|
698
|
+
baseRecord.expiresAt = Math.max(existing?.expiresAt ?? 0, computedExpiresAt);
|
|
699
|
+
}
|
|
700
|
+
state.sessions[key] = baseRecord;
|
|
701
|
+
return baseRecord;
|
|
702
|
+
}
|
|
703
|
+
function policiesForDetection(policies, detection) {
|
|
704
|
+
if (detection.strongest === void 0) {
|
|
705
|
+
return [];
|
|
706
|
+
}
|
|
707
|
+
const matchingPolicyIds = new Set(
|
|
708
|
+
detection.matched.filter(
|
|
709
|
+
(match) => match.advertiserHost === detection.strongest?.advertiserHost
|
|
710
|
+
).map((match) => match.policyId)
|
|
711
|
+
);
|
|
712
|
+
return policies.filter((policy) => matchingPolicyIds.has(policy.id));
|
|
713
|
+
}
|
|
714
|
+
function unionBehaviors(behaviors) {
|
|
715
|
+
return [...new Set(behaviors)];
|
|
716
|
+
}
|
|
717
|
+
function activeDecisionForHost(state, advertiserHost, now) {
|
|
718
|
+
const record = state.sessions[normalizeHost(advertiserHost)];
|
|
719
|
+
if (!record || !isActive(record, now)) {
|
|
720
|
+
return void 0;
|
|
721
|
+
}
|
|
722
|
+
return decisionFromRecord(record, now);
|
|
723
|
+
}
|
|
724
|
+
function decisionFromRecord(record, now, overrides) {
|
|
725
|
+
const expiresAt = computeExpiresAt(record);
|
|
726
|
+
const decision = {
|
|
727
|
+
standDown: true,
|
|
728
|
+
policyId: record.policyId,
|
|
729
|
+
reason: overrides?.reason ?? `active-standdown:${record.policyId}`,
|
|
730
|
+
behaviors: [...record.behaviors]
|
|
731
|
+
};
|
|
732
|
+
if (expiresAt !== void 0) {
|
|
733
|
+
decision.expiresAt = expiresAt;
|
|
734
|
+
}
|
|
735
|
+
if (overrides?.referrerClass !== void 0) {
|
|
736
|
+
decision.referrerClass = overrides.referrerClass;
|
|
737
|
+
}
|
|
738
|
+
if (!isActive({ ...record, ...expiresAt === void 0 ? {} : { expiresAt } }, now)) {
|
|
739
|
+
return {
|
|
740
|
+
standDown: false,
|
|
741
|
+
reason: "standdown-expired",
|
|
742
|
+
behaviors: []
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
return decision;
|
|
746
|
+
}
|
|
747
|
+
function computeExpiresAt(record) {
|
|
748
|
+
if (record.sessionRule === "inactivity-window" && record.inactivityMs !== void 0) {
|
|
749
|
+
return Math.max(
|
|
750
|
+
record.startedAt + record.minDurationMs,
|
|
751
|
+
record.lastActivityAt + record.inactivityMs
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
return record.expiresAt;
|
|
755
|
+
}
|
|
756
|
+
function isActive(record, now) {
|
|
757
|
+
if (record.sessionRule === "session-or-min") {
|
|
758
|
+
return true;
|
|
759
|
+
}
|
|
760
|
+
const expiresAt = computeExpiresAt(record);
|
|
761
|
+
return expiresAt === void 0 ? false : now < expiresAt;
|
|
762
|
+
}
|
|
763
|
+
function pruneExpiredSessions(state, now) {
|
|
764
|
+
for (const [host, record] of Object.entries(state.sessions)) {
|
|
765
|
+
if (!isActive(record, now)) {
|
|
766
|
+
delete state.sessions[host];
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
function failClosedDecision(reason) {
|
|
771
|
+
return {
|
|
772
|
+
standDown: true,
|
|
773
|
+
reason,
|
|
774
|
+
behaviors: [
|
|
775
|
+
"suppress-prompts",
|
|
776
|
+
"no-cookie-write",
|
|
777
|
+
"no-redirect",
|
|
778
|
+
"no-background-tracking"
|
|
779
|
+
]
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
function auditEntry(value) {
|
|
783
|
+
const entry = {
|
|
784
|
+
time: value.time,
|
|
785
|
+
action: value.action
|
|
786
|
+
};
|
|
787
|
+
if (value.advertiserHost !== void 0) {
|
|
788
|
+
entry.advertiserHost = value.advertiserHost;
|
|
789
|
+
}
|
|
790
|
+
if (value.detection !== void 0) {
|
|
791
|
+
entry.detection = value.detection;
|
|
792
|
+
}
|
|
793
|
+
if (value.decision !== void 0) {
|
|
794
|
+
entry.decision = value.decision;
|
|
795
|
+
}
|
|
796
|
+
return entry;
|
|
797
|
+
}
|
|
798
|
+
function cloneState(state) {
|
|
799
|
+
return {
|
|
800
|
+
sessions: Object.fromEntries(
|
|
801
|
+
Object.entries(state.sessions).map(([host, record]) => [
|
|
802
|
+
host,
|
|
803
|
+
{
|
|
804
|
+
...record,
|
|
805
|
+
behaviors: [...record.behaviors]
|
|
806
|
+
}
|
|
807
|
+
])
|
|
808
|
+
),
|
|
809
|
+
auditLog: state.auditLog.map(cloneAuditEntry)
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
function cloneAuditEntry(entry) {
|
|
813
|
+
const cloned = {
|
|
814
|
+
time: entry.time,
|
|
815
|
+
action: entry.action
|
|
816
|
+
};
|
|
817
|
+
if (entry.advertiserHost !== void 0) {
|
|
818
|
+
cloned.advertiserHost = entry.advertiserHost;
|
|
819
|
+
}
|
|
820
|
+
if (entry.detection !== void 0) {
|
|
821
|
+
const detection = {
|
|
822
|
+
matched: entry.detection.matched.map((match) => ({ ...match })),
|
|
823
|
+
selfMatch: entry.detection.selfMatch
|
|
824
|
+
};
|
|
825
|
+
if (entry.detection.strongest !== void 0) {
|
|
826
|
+
detection.strongest = { ...entry.detection.strongest };
|
|
827
|
+
}
|
|
828
|
+
if (entry.detection.failClosedReason !== void 0) {
|
|
829
|
+
detection.failClosedReason = entry.detection.failClosedReason;
|
|
830
|
+
}
|
|
831
|
+
cloned.detection = detection;
|
|
832
|
+
}
|
|
833
|
+
if (entry.decision !== void 0) {
|
|
834
|
+
cloned.decision = {
|
|
835
|
+
...entry.decision,
|
|
836
|
+
behaviors: [...entry.decision.behaviors]
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
return cloned;
|
|
840
|
+
}
|
|
841
|
+
function normalizeHost(host) {
|
|
842
|
+
return host.toLowerCase().replace(/\.$/, "");
|
|
843
|
+
}
|
|
844
|
+
function hostFromUrl2(value) {
|
|
845
|
+
try {
|
|
846
|
+
return normalizeHost(new URL(value).hostname);
|
|
847
|
+
} catch {
|
|
848
|
+
return void 0;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function messageFromError(error) {
|
|
852
|
+
return error instanceof Error ? error.message : String(error);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
export { MemoryStateStore, StanddownSession, classifyReferrer, detect, domainRuleMatchesUrl, validatePolicies, validatePolicy };
|
|
856
|
+
//# sourceMappingURL=chunk-SVN6UWA4.mjs.map
|
|
857
|
+
//# sourceMappingURL=chunk-SVN6UWA4.mjs.map
|