playwright-security-scan 0.0.0-stage → 0.1.0-beta.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/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +334 -2
- package/dist/chunk-6HZSEXCR.js +2727 -0
- package/dist/chunk-BBF7BGCZ.cjs +10 -0
- package/dist/chunk-GHGZTTAG.cjs +1581 -0
- package/dist/chunk-HWVW6NBJ.cjs +2740 -0
- package/dist/chunk-IHU2HYVH.js +1554 -0
- package/dist/chunk-SFWIEETP.js +8 -0
- package/dist/config.cjs +153 -0
- package/dist/config.d.cts +219 -0
- package/dist/config.d.ts +219 -0
- package/dist/config.js +144 -0
- package/dist/fixture.cjs +733 -0
- package/dist/fixture.d.cts +35 -0
- package/dist/fixture.d.ts +35 -0
- package/dist/fixture.js +725 -0
- package/dist/reporter.cjs +1118 -0
- package/dist/reporter.d.cts +18 -0
- package/dist/reporter.d.ts +18 -0
- package/dist/reporter.js +1113 -0
- package/dist/rules.cjs +10 -0
- package/dist/rules.d.cts +4 -0
- package/dist/rules.d.ts +4 -0
- package/dist/rules.js +1 -0
- package/package.json +90 -4
- package/schemas/attachment.schema.json +15 -0
- package/schemas/baseline.schema.json +47 -0
- package/schemas/common.schema.json +307 -0
- package/schemas/outcome.schema.json +28 -0
- package/schemas/run-report.schema.json +192 -0
package/dist/fixture.cjs
ADDED
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkHWVW6NBJ_cjs = require('./chunk-HWVW6NBJ.cjs');
|
|
4
|
+
var chunkGHGZTTAG_cjs = require('./chunk-GHGZTTAG.cjs');
|
|
5
|
+
var chunkBBF7BGCZ_cjs = require('./chunk-BBF7BGCZ.cjs');
|
|
6
|
+
var crypto = require('crypto');
|
|
7
|
+
var path = require('path');
|
|
8
|
+
|
|
9
|
+
var NEVER = /* @__PURE__ */ new Set([
|
|
10
|
+
"image",
|
|
11
|
+
"font",
|
|
12
|
+
"media",
|
|
13
|
+
"websocket",
|
|
14
|
+
"eventsource",
|
|
15
|
+
"manifest",
|
|
16
|
+
"texttrack"
|
|
17
|
+
]);
|
|
18
|
+
var NO_BODY = /* @__PURE__ */ new Set([204, 304]);
|
|
19
|
+
var STATIC_TYPES = /* @__PURE__ */ new Set(["script", "stylesheet"]);
|
|
20
|
+
function mediaType(contentType) {
|
|
21
|
+
return (contentType ?? "").split(";")[0]?.trim().toLowerCase() ?? "";
|
|
22
|
+
}
|
|
23
|
+
function typeAllowed(type, allowed) {
|
|
24
|
+
return allowed.some(
|
|
25
|
+
(entry) => entry.startsWith("*") ? type.endsWith(entry.slice(1)) : entry.toLowerCase() === type
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
function bodySkipReason(input) {
|
|
29
|
+
if (NEVER.has(input.resourceType)) return "resource-type";
|
|
30
|
+
if (NO_BODY.has(input.status) || input.status >= 300 && input.status < 400) return "status";
|
|
31
|
+
if (!input.wanted) return "no-rule";
|
|
32
|
+
if (!typeAllowed(mediaType(input.contentType), input.allowedTypes)) return "content-type";
|
|
33
|
+
const length = Number(input.contentLength);
|
|
34
|
+
if (!STATIC_TYPES.has(input.resourceType) && Number.isFinite(length) && length > input.maxBodyBytes) {
|
|
35
|
+
return "too-large";
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
function bodyView(bytes, contentType, maxBodyBytes) {
|
|
40
|
+
const truncated = bytes.length > maxBodyBytes;
|
|
41
|
+
const slice = truncated ? bytes.subarray(0, maxBodyBytes) : bytes;
|
|
42
|
+
return {
|
|
43
|
+
contentType: mediaType(contentType),
|
|
44
|
+
text: slice.toString("utf8"),
|
|
45
|
+
truncated,
|
|
46
|
+
sha256: crypto.createHash("sha256").update(bytes).digest("hex")
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/collector/api.ts
|
|
51
|
+
var API_METHODS = ["fetch", "get", "post", "put", "patch", "delete", "head"];
|
|
52
|
+
var API_DRAIN_BUDGET_MS = 5e3;
|
|
53
|
+
var depth = 0;
|
|
54
|
+
var METHOD = /^[A-Z]+$/u;
|
|
55
|
+
function callString(target, name) {
|
|
56
|
+
if (typeof target !== "object" || target === null) return void 0;
|
|
57
|
+
const fn = target[name];
|
|
58
|
+
if (typeof fn !== "function") return void 0;
|
|
59
|
+
const value = fn.call(target);
|
|
60
|
+
return typeof value === "string" ? value : void 0;
|
|
61
|
+
}
|
|
62
|
+
function describeApiCall(name, args) {
|
|
63
|
+
const [target, options] = args;
|
|
64
|
+
const opts = typeof options === "object" && options !== null ? options : {};
|
|
65
|
+
let method = name;
|
|
66
|
+
if (name === "fetch") {
|
|
67
|
+
method = (typeof opts.method === "string" ? opts.method : void 0) ?? callString(target, "method") ?? "GET";
|
|
68
|
+
}
|
|
69
|
+
method = method.toUpperCase();
|
|
70
|
+
if (!METHOD.test(method)) return void 0;
|
|
71
|
+
const headers = [];
|
|
72
|
+
if (typeof opts.headers === "object" && opts.headers !== null) {
|
|
73
|
+
for (const [key, value] of Object.entries(opts.headers)) {
|
|
74
|
+
if (typeof value === "string") headers.push({ name: key.toLowerCase(), value });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return { method, headers };
|
|
78
|
+
}
|
|
79
|
+
function isPromise(value) {
|
|
80
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
81
|
+
}
|
|
82
|
+
function isApiResponse(value) {
|
|
83
|
+
if (typeof value !== "object" || value === null) return false;
|
|
84
|
+
const r = value;
|
|
85
|
+
return ["url", "status", "headersArray", "body"].every((n) => typeof r[n] === "function");
|
|
86
|
+
}
|
|
87
|
+
function isApiRequestContext(value) {
|
|
88
|
+
if (typeof value !== "object" || value === null) return false;
|
|
89
|
+
const target = value;
|
|
90
|
+
return ["fetch", "get", "post", "dispose", "storageState"].every(
|
|
91
|
+
(name) => typeof target[name] === "function"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
async function analyze(sink, call, response) {
|
|
95
|
+
const { collector, keys } = sink;
|
|
96
|
+
const url = response.url();
|
|
97
|
+
const origin = chunkGHGZTTAG_cjs.canonicalOrigin(url);
|
|
98
|
+
if (origin === null) return;
|
|
99
|
+
if (!collector.options.scope.has(origin)) {
|
|
100
|
+
collector.outOfScope();
|
|
101
|
+
if (!sink.reportedOrigins.has(origin)) {
|
|
102
|
+
sink.reportedOrigins.add(origin);
|
|
103
|
+
collector.diagnostic(
|
|
104
|
+
"API_ORIGIN_OUT_OF_SCOPE",
|
|
105
|
+
"info",
|
|
106
|
+
`API calls to ${origin} are out of scope; add it to scope.origins to scan them`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const key = keys({ method: call.method, url, resourceType: "api" });
|
|
112
|
+
if (!key.ok) return;
|
|
113
|
+
const responseHeaders = response.headersArray().map((h) => ({ name: h.name.toLowerCase(), value: h.value }));
|
|
114
|
+
const record = {
|
|
115
|
+
id: 0,
|
|
116
|
+
source: "api",
|
|
117
|
+
method: call.method,
|
|
118
|
+
url,
|
|
119
|
+
endpointKey: key.key,
|
|
120
|
+
origin,
|
|
121
|
+
resourceType: "api",
|
|
122
|
+
isNavigation: false,
|
|
123
|
+
requestHeaders: call.headers,
|
|
124
|
+
status: response.status(),
|
|
125
|
+
responseHeaders,
|
|
126
|
+
context: collector.test
|
|
127
|
+
};
|
|
128
|
+
const header = (name) => responseHeaders.find((h) => h.name === name)?.value;
|
|
129
|
+
const limits = collector.options.config.limits;
|
|
130
|
+
const skip = bodySkipReason({
|
|
131
|
+
resourceType: "api",
|
|
132
|
+
status: record.status,
|
|
133
|
+
contentType: header("content-type"),
|
|
134
|
+
contentLength: header("content-length"),
|
|
135
|
+
maxBodyBytes: limits.maxBodyBytes,
|
|
136
|
+
allowedTypes: limits.bodyContentTypes,
|
|
137
|
+
wanted: collector.bodyTypes.has("api")
|
|
138
|
+
});
|
|
139
|
+
if (skip === null) {
|
|
140
|
+
try {
|
|
141
|
+
record.body = bodyView(await response.body(), header("content-type"), limits.maxBodyBytes);
|
|
142
|
+
collector.coverage.bodiesAnalyzed += 1;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
collector.skipBody("read-failed");
|
|
145
|
+
collector.diagnostic(
|
|
146
|
+
"BODY_READ_FAILED",
|
|
147
|
+
"info",
|
|
148
|
+
`collector: the body of an API response could not be read: ${(error instanceof Error ? error.message : String(error)).split("\n")[0] ?? ""}`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
} else if (skip !== "no-rule") {
|
|
152
|
+
collector.skipBody(skip);
|
|
153
|
+
}
|
|
154
|
+
collector.analyzeRecord(record);
|
|
155
|
+
delete record.body;
|
|
156
|
+
}
|
|
157
|
+
function wrapRequestContext(context, sink) {
|
|
158
|
+
const wrapped = chunkGHGZTTAG_cjs.wrappedRequestContexts();
|
|
159
|
+
if (wrapped.has(context)) return false;
|
|
160
|
+
wrapped.add(context);
|
|
161
|
+
const target = context;
|
|
162
|
+
for (const name of API_METHODS) {
|
|
163
|
+
const original = target[name];
|
|
164
|
+
if (typeof original !== "function") continue;
|
|
165
|
+
const fn = original;
|
|
166
|
+
const wrapper = function(...args) {
|
|
167
|
+
const active = depth === 0 ? sink() : void 0;
|
|
168
|
+
if (active === void 0) return fn.apply(this, args);
|
|
169
|
+
let call;
|
|
170
|
+
try {
|
|
171
|
+
call = describeApiCall(name, args);
|
|
172
|
+
} catch {
|
|
173
|
+
call = void 0;
|
|
174
|
+
}
|
|
175
|
+
depth += 1;
|
|
176
|
+
let result;
|
|
177
|
+
try {
|
|
178
|
+
result = fn.apply(this, args);
|
|
179
|
+
} finally {
|
|
180
|
+
depth -= 1;
|
|
181
|
+
}
|
|
182
|
+
if (call === void 0 || !isPromise(result)) return result;
|
|
183
|
+
const described = call;
|
|
184
|
+
const analysis = result.then(
|
|
185
|
+
(response) => isApiResponse(response) ? analyze(active, described, response) : void 0,
|
|
186
|
+
() => {
|
|
187
|
+
active.collector.skipBody("request-failed");
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
const tracked = analysis.catch((error) => {
|
|
191
|
+
active.collector.diagnostic(
|
|
192
|
+
"COLLECTOR_ERROR",
|
|
193
|
+
"info",
|
|
194
|
+
`collector: an API response was skipped: ${error instanceof Error ? error.message : String(error)}`
|
|
195
|
+
);
|
|
196
|
+
});
|
|
197
|
+
active.pending.add(tracked);
|
|
198
|
+
void tracked.finally(() => active.pending.delete(tracked));
|
|
199
|
+
return result;
|
|
200
|
+
};
|
|
201
|
+
Object.defineProperty(target, name, {
|
|
202
|
+
value: wrapper,
|
|
203
|
+
configurable: true,
|
|
204
|
+
enumerable: false,
|
|
205
|
+
writable: true
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
async function drainApi(sink, budgetMs = API_DRAIN_BUDGET_MS) {
|
|
211
|
+
if (sink.pending.size === 0) return;
|
|
212
|
+
let timer;
|
|
213
|
+
const timeout = new Promise((resolve) => {
|
|
214
|
+
timer = setTimeout(() => resolve("timeout"), budgetMs);
|
|
215
|
+
});
|
|
216
|
+
const result = await Promise.race([Promise.all([...sink.pending]).then(() => "done"), timeout]);
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
if (result === "timeout") {
|
|
219
|
+
sink.collector.diagnostic(
|
|
220
|
+
"API_ANALYSIS_TIMEOUT",
|
|
221
|
+
"warn",
|
|
222
|
+
`collector: ${sink.pending.size} API responses were still being analysed ${budgetMs} ms after the test ended`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/collector/page-script.ts
|
|
228
|
+
function cspListenerScript(binding) {
|
|
229
|
+
return `(()=>{try{if(typeof document==='undefined')return;var b=${JSON.stringify(binding)};document.addEventListener('securitypolicyviolation',function(e){try{var f=window[b];if(typeof f==='function')f({directive:String(e.effectiveDirective||e.violatedDirective||''),blocked:String(e.blockedURI||''),disposition:String(e.disposition||''),documentURI:String(e.documentURI||'')});}catch(_){}},true);}catch(_){}})();`;
|
|
230
|
+
}
|
|
231
|
+
function readStorage() {
|
|
232
|
+
const read = (storage) => {
|
|
233
|
+
const entries = [];
|
|
234
|
+
for (let i = 0; i < storage.length; i++) {
|
|
235
|
+
const key = storage.key(i);
|
|
236
|
+
if (key !== null) entries.push([key, storage.getItem(key) ?? ""]);
|
|
237
|
+
}
|
|
238
|
+
return entries;
|
|
239
|
+
};
|
|
240
|
+
let local = [];
|
|
241
|
+
let session = [];
|
|
242
|
+
try {
|
|
243
|
+
local = read(window.localStorage);
|
|
244
|
+
} catch {
|
|
245
|
+
}
|
|
246
|
+
try {
|
|
247
|
+
session = read(window.sessionStorage);
|
|
248
|
+
} catch {
|
|
249
|
+
}
|
|
250
|
+
return { origin: window.location.origin, local, session };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/collector/browser.ts
|
|
254
|
+
var STORAGE_SCAN_BUDGET_MS = 2e3;
|
|
255
|
+
var instrumented = /* @__PURE__ */ new WeakSet();
|
|
256
|
+
async function instrumentContext(context, bindingName, current) {
|
|
257
|
+
if (instrumented.has(context)) return;
|
|
258
|
+
instrumented.add(context);
|
|
259
|
+
await context.exposeBinding(bindingName, (_source, payload) => {
|
|
260
|
+
const collector = current();
|
|
261
|
+
if (collector === void 0 || typeof payload !== "object" || payload === null) return;
|
|
262
|
+
try {
|
|
263
|
+
const documentOrigin = chunkGHGZTTAG_cjs.canonicalOrigin(String(payload.documentURI ?? ""));
|
|
264
|
+
if (documentOrigin === null || !collector.options.scope.has(documentOrigin)) return;
|
|
265
|
+
const blocked = String(payload.blocked ?? "");
|
|
266
|
+
if (/\/favicon\.ico(?:[?#]|$)/i.test(blocked)) return;
|
|
267
|
+
collector.analyzeEvent({
|
|
268
|
+
kind: "csp-violation",
|
|
269
|
+
documentOrigin,
|
|
270
|
+
directive: String(payload.directive ?? ""),
|
|
271
|
+
blockedOrigin: chunkGHGZTTAG_cjs.canonicalOrigin(blocked) ?? blocked.slice(0, 40),
|
|
272
|
+
disposition: payload.disposition === "report" ? "report" : "enforce",
|
|
273
|
+
context: collector.test
|
|
274
|
+
});
|
|
275
|
+
} catch {
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
await context.addInitScript(cspListenerScript(bindingName));
|
|
279
|
+
}
|
|
280
|
+
function withTimeout(promise, ms) {
|
|
281
|
+
return Promise.race([
|
|
282
|
+
promise,
|
|
283
|
+
new Promise(
|
|
284
|
+
(_, reject) => setTimeout(() => reject(new Error(`timed out after ${ms} ms`)), ms)
|
|
285
|
+
)
|
|
286
|
+
]);
|
|
287
|
+
}
|
|
288
|
+
var sameSite = (value) => value === "Strict" || value === "None" ? value : "Lax";
|
|
289
|
+
async function scanContext(context, collector, storageScan) {
|
|
290
|
+
const scope = collector.options.scope;
|
|
291
|
+
const work = async () => {
|
|
292
|
+
const origins = scope.listed.filter((o) => !o.includes("*"));
|
|
293
|
+
const originOf = (domain) => origins.find((o) => chunkHWVW6NBJ_cjs.domainMatches(new URL(o).hostname, domain));
|
|
294
|
+
const cookies = (await context.cookies()).filter((c) => originOf(c.domain) !== void 0).map((c) => ({
|
|
295
|
+
origin: originOf(c.domain),
|
|
296
|
+
name: c.name,
|
|
297
|
+
value: c.value,
|
|
298
|
+
domain: c.domain,
|
|
299
|
+
path: c.path,
|
|
300
|
+
secure: c.secure,
|
|
301
|
+
httpOnly: c.httpOnly,
|
|
302
|
+
sameSite: sameSite(c.sameSite),
|
|
303
|
+
expires: c.expires
|
|
304
|
+
}));
|
|
305
|
+
if (cookies.length > 0) {
|
|
306
|
+
collector.analyzeEvent({ kind: "cookie-jar", cookies, context: collector.test });
|
|
307
|
+
}
|
|
308
|
+
if (!storageScan) return;
|
|
309
|
+
const seen = /* @__PURE__ */ new Set();
|
|
310
|
+
for (const page of context.pages()) {
|
|
311
|
+
if (page.isClosed()) continue;
|
|
312
|
+
const origin = chunkGHGZTTAG_cjs.canonicalOrigin(page.url());
|
|
313
|
+
if (origin === null || !scope.has(origin) || seen.has(origin)) continue;
|
|
314
|
+
seen.add(origin);
|
|
315
|
+
const storage = await page.evaluate(readStorage);
|
|
316
|
+
for (const area of ["local", "session"]) {
|
|
317
|
+
const entries = (area === "local" ? storage.local : storage.session).map(
|
|
318
|
+
([key, value]) => ({
|
|
319
|
+
key,
|
|
320
|
+
value
|
|
321
|
+
})
|
|
322
|
+
);
|
|
323
|
+
collector.analyzeEvent({ kind: "storage", origin, area, entries, context: collector.test });
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
try {
|
|
328
|
+
await withTimeout(work(), STORAGE_SCAN_BUDGET_MS);
|
|
329
|
+
} catch (error) {
|
|
330
|
+
collector.diagnostic(
|
|
331
|
+
"STORAGE_SCAN_FAILED",
|
|
332
|
+
"info",
|
|
333
|
+
`collector: the end-of-test scan of cookies and Web Storage was skipped: ${(error instanceof Error ? error.message : String(error)).split("\n")[0] ?? ""}`
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/collector/tracker.ts
|
|
339
|
+
var CONCURRENCY = 4;
|
|
340
|
+
var DRAIN_BUDGET_MS = 1e4;
|
|
341
|
+
var lower = (headers) => headers.map((h) => ({ name: h.name.toLowerCase(), value: h.value }));
|
|
342
|
+
var errorText = (error) => (error instanceof Error ? error.message : String(error)).split("\n")[0] ?? "";
|
|
343
|
+
var ResponseTracker = class {
|
|
344
|
+
context;
|
|
345
|
+
#collector;
|
|
346
|
+
#keys;
|
|
347
|
+
#pending = [];
|
|
348
|
+
#running = /* @__PURE__ */ new Set();
|
|
349
|
+
#detach = [];
|
|
350
|
+
/** Responses with headers but no requestfinished yet: their bodies are still arriving. */
|
|
351
|
+
#awaiting = /* @__PURE__ */ new Set();
|
|
352
|
+
#nextId = 1;
|
|
353
|
+
#accepting = true;
|
|
354
|
+
constructor(context, collector, keys) {
|
|
355
|
+
this.context = context;
|
|
356
|
+
this.#collector = collector;
|
|
357
|
+
this.#keys = keys;
|
|
358
|
+
const awaiting = this.#awaiting;
|
|
359
|
+
const lost = (count) => {
|
|
360
|
+
if (count === 0) return;
|
|
361
|
+
this.#collector.diagnostic(
|
|
362
|
+
"BODY_READ_FAILED",
|
|
363
|
+
"info",
|
|
364
|
+
`collector: ${count} response bodies were not complete when the request failed or the context closed`
|
|
365
|
+
);
|
|
366
|
+
};
|
|
367
|
+
const onResponse = (response) => {
|
|
368
|
+
awaiting.add(response.request());
|
|
369
|
+
};
|
|
370
|
+
const onFinished = (request) => {
|
|
371
|
+
awaiting.delete(request);
|
|
372
|
+
this.#enqueue(request);
|
|
373
|
+
};
|
|
374
|
+
const onFailed = (request) => {
|
|
375
|
+
this.#collector.skipBody("request-failed");
|
|
376
|
+
if (awaiting.delete(request)) lost(1);
|
|
377
|
+
};
|
|
378
|
+
const onClose = () => {
|
|
379
|
+
lost(awaiting.size);
|
|
380
|
+
awaiting.clear();
|
|
381
|
+
};
|
|
382
|
+
context.on("response", onResponse);
|
|
383
|
+
context.on("requestfinished", onFinished);
|
|
384
|
+
context.on("requestfailed", onFailed);
|
|
385
|
+
context.on("close", onClose);
|
|
386
|
+
this.#detach.push(
|
|
387
|
+
() => context.off("response", onResponse),
|
|
388
|
+
() => context.off("requestfinished", onFinished),
|
|
389
|
+
() => context.off("requestfailed", onFailed),
|
|
390
|
+
() => context.off("close", onClose)
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
#enqueue(request) {
|
|
394
|
+
if (!this.#accepting) return;
|
|
395
|
+
this.#pending.push(() => this.#process(request));
|
|
396
|
+
this.#pump();
|
|
397
|
+
}
|
|
398
|
+
#pump() {
|
|
399
|
+
while (this.#running.size < CONCURRENCY && this.#pending.length > 0) {
|
|
400
|
+
const task = this.#pending.shift();
|
|
401
|
+
const run = task().catch((error) => {
|
|
402
|
+
this.#collector.diagnostic(
|
|
403
|
+
"COLLECTOR_ERROR",
|
|
404
|
+
"info",
|
|
405
|
+
`collector: a response was skipped: ${errorText(error)}`
|
|
406
|
+
);
|
|
407
|
+
}).finally(() => {
|
|
408
|
+
this.#running.delete(run);
|
|
409
|
+
this.#pump();
|
|
410
|
+
});
|
|
411
|
+
this.#running.add(run);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/** Waits for queued records (before the base fixture closes the context, 05-REQ-09), then detaches. */
|
|
415
|
+
async drain(budgetMs = DRAIN_BUDGET_MS) {
|
|
416
|
+
const deadline = Date.now() + budgetMs;
|
|
417
|
+
const busy = () => this.#running.size > 0 || this.#pending.length > 0 || this.#awaiting.size > 0;
|
|
418
|
+
while (busy() && Date.now() < deadline) {
|
|
419
|
+
await Promise.race([
|
|
420
|
+
this.#running.size > 0 ? Promise.all([...this.#running]) : new Promise((r) => setTimeout(r, 20)),
|
|
421
|
+
new Promise((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now())))
|
|
422
|
+
]);
|
|
423
|
+
}
|
|
424
|
+
this.#accepting = false;
|
|
425
|
+
if (this.#pending.length > 0 || this.#running.size > 0) {
|
|
426
|
+
this.#collector.diagnostic(
|
|
427
|
+
"BODY_READ_FAILED",
|
|
428
|
+
"info",
|
|
429
|
+
`collector: ${this.#pending.length + this.#running.size} responses were still being read when the test ended`
|
|
430
|
+
);
|
|
431
|
+
this.#pending.length = 0;
|
|
432
|
+
}
|
|
433
|
+
for (const detach of this.#detach) detach();
|
|
434
|
+
this.#detach.length = 0;
|
|
435
|
+
}
|
|
436
|
+
async #process(request) {
|
|
437
|
+
const response = await request.response();
|
|
438
|
+
if (response === null) return;
|
|
439
|
+
if (request.serviceWorker() !== null) return;
|
|
440
|
+
const url = request.url();
|
|
441
|
+
const origin = chunkGHGZTTAG_cjs.canonicalOrigin(url);
|
|
442
|
+
if (origin === null) return;
|
|
443
|
+
const collector = this.#collector;
|
|
444
|
+
const opts = collector.options;
|
|
445
|
+
const inScope = opts.scope.has(origin);
|
|
446
|
+
if (!inScope) collector.outOfScope();
|
|
447
|
+
const resourceType = request.resourceType();
|
|
448
|
+
const key = this.#keys({ method: request.method(), url, resourceType });
|
|
449
|
+
if (!key.ok) return;
|
|
450
|
+
if (inScope && await response.serverAddr() === null) {
|
|
451
|
+
collector.skipBody("mocked");
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const [requestHeaders, responseHeaders] = await Promise.all([
|
|
455
|
+
request.headersArray().then(lower),
|
|
456
|
+
response.headersArray().then(lower)
|
|
457
|
+
]);
|
|
458
|
+
let frameUrl;
|
|
459
|
+
try {
|
|
460
|
+
frameUrl = request.frame().url();
|
|
461
|
+
} catch {
|
|
462
|
+
frameUrl = void 0;
|
|
463
|
+
}
|
|
464
|
+
const record = {
|
|
465
|
+
id: this.#nextId++,
|
|
466
|
+
source: "browser",
|
|
467
|
+
method: request.method().toUpperCase(),
|
|
468
|
+
url,
|
|
469
|
+
endpointKey: key.key,
|
|
470
|
+
origin,
|
|
471
|
+
resourceType,
|
|
472
|
+
isNavigation: request.isNavigationRequest(),
|
|
473
|
+
requestHeaders,
|
|
474
|
+
status: response.status(),
|
|
475
|
+
responseHeaders,
|
|
476
|
+
context: collector.test
|
|
477
|
+
};
|
|
478
|
+
if (frameUrl !== void 0) record.frameUrl = frameUrl;
|
|
479
|
+
const redirectedFrom = request.redirectedFrom()?.url();
|
|
480
|
+
if (redirectedFrom !== void 0) record.redirectedFrom = redirectedFrom;
|
|
481
|
+
if (!inScope) {
|
|
482
|
+
collector.analyzeOutOfScope(record);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (resourceType === "document" && url.startsWith("https:")) {
|
|
486
|
+
const details = await response.securityDetails().catch(() => null);
|
|
487
|
+
if (details?.protocol !== void 0) record.tls = { protocol: details.protocol };
|
|
488
|
+
}
|
|
489
|
+
const header = (name) => responseHeaders.find((h) => h.name === name)?.value;
|
|
490
|
+
const skip = bodySkipReason({
|
|
491
|
+
resourceType,
|
|
492
|
+
status: record.status,
|
|
493
|
+
contentType: header("content-type"),
|
|
494
|
+
contentLength: header("content-length"),
|
|
495
|
+
maxBodyBytes: opts.config.limits.maxBodyBytes,
|
|
496
|
+
allowedTypes: opts.config.limits.bodyContentTypes,
|
|
497
|
+
wanted: collector.bodyTypes.has(resourceType)
|
|
498
|
+
});
|
|
499
|
+
if (skip !== null) {
|
|
500
|
+
if (skip !== "no-rule") collector.skipBody(skip);
|
|
501
|
+
collector.analyzeRecord(record);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const validator = header("etag") ?? header("last-modified") ?? header("content-length");
|
|
505
|
+
const staticKey = STATIC_TYPES.has(resourceType) && validator !== void 0 ? `${url}\0${validator}` : void 0;
|
|
506
|
+
const knownSha = staticKey === void 0 ? void 0 : opts.cache.hash(staticKey);
|
|
507
|
+
if (knownSha !== void 0) {
|
|
508
|
+
collector.skipBody("cached");
|
|
509
|
+
collector.analyzeRecord(record, knownSha);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
try {
|
|
513
|
+
const bytes = await response.body();
|
|
514
|
+
record.body = bodyView(bytes, header("content-type"), opts.config.limits.maxBodyBytes);
|
|
515
|
+
collector.coverage.bodiesAnalyzed += 1;
|
|
516
|
+
if (staticKey !== void 0) opts.cache.setHash(staticKey, record.body.sha256);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
collector.skipBody("read-failed");
|
|
519
|
+
collector.diagnostic(
|
|
520
|
+
"BODY_READ_FAILED",
|
|
521
|
+
"info",
|
|
522
|
+
`collector: the body of a ${resourceType} response could not be read: ${errorText(error)}`
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
collector.analyzeRecord(record);
|
|
526
|
+
delete record.body;
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// src/fixture/fixtures.ts
|
|
531
|
+
var worker;
|
|
532
|
+
function workerState(baseURL) {
|
|
533
|
+
if (worker !== void 0) return worker;
|
|
534
|
+
const registered = chunkGHGZTTAG_cjs.getRegistered();
|
|
535
|
+
const fallback = chunkGHGZTTAG_cjs.resolveConfig(void 0);
|
|
536
|
+
const config = registered?.config ?? (fallback.ok ? fallback.config : void 0);
|
|
537
|
+
const baseOrigin = baseURL === void 0 ? null : chunkGHGZTTAG_cjs.canonicalOrigin(baseURL);
|
|
538
|
+
const baseOrigins = registered?.baseOrigins ?? (baseOrigin === null ? [] : [baseOrigin]);
|
|
539
|
+
const salt = process.env[chunkGHGZTTAG_cjs.SALT_ENV] || registered?.salt || crypto.randomBytes(16).toString("hex");
|
|
540
|
+
const paramNames = config.rules["data.token-in-url"]?.options.paramNames;
|
|
541
|
+
worker = {
|
|
542
|
+
config,
|
|
543
|
+
scope: new chunkHWVW6NBJ_cjs.Scope({
|
|
544
|
+
baseOrigins,
|
|
545
|
+
includeBaseURL: config.scope.includeBaseURL,
|
|
546
|
+
origins: config.scope.origins,
|
|
547
|
+
exclude: config.scope.exclude
|
|
548
|
+
}),
|
|
549
|
+
keys: chunkGHGZTTAG_cjs.createKeyBuilder(config.normalization, { baseOrigins }),
|
|
550
|
+
redactor: new chunkHWVW6NBJ_cjs.Redactor({
|
|
551
|
+
salt,
|
|
552
|
+
paramNames: Array.isArray(paramNames) ? paramNames : []
|
|
553
|
+
}),
|
|
554
|
+
cache: new chunkHWVW6NBJ_cjs.AnalysisCache(),
|
|
555
|
+
binding: `__pwSec_${salt.slice(0, 8)}`,
|
|
556
|
+
scannedProjects: registered?.scannedProjects ?? "all",
|
|
557
|
+
configMissing: registered === void 0
|
|
558
|
+
};
|
|
559
|
+
return worker;
|
|
560
|
+
}
|
|
561
|
+
function testRef(testInfo) {
|
|
562
|
+
const configFile = testInfo.config.configFile;
|
|
563
|
+
const root = configFile === void 0 ? testInfo.config.rootDir : path.dirname(configFile);
|
|
564
|
+
return {
|
|
565
|
+
testId: testInfo.testId,
|
|
566
|
+
// titlePath starts with the file name; the reports show the file separately.
|
|
567
|
+
titlePath: testInfo.titlePath.slice(1),
|
|
568
|
+
file: path.relative(root, testInfo.file).split("\\").join("/"),
|
|
569
|
+
line: testInfo.line,
|
|
570
|
+
project: testInfo.project.name
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
var TestScan = class {
|
|
574
|
+
collector;
|
|
575
|
+
api;
|
|
576
|
+
#state;
|
|
577
|
+
#trackers = /* @__PURE__ */ new Map();
|
|
578
|
+
#finished = /* @__PURE__ */ new Set();
|
|
579
|
+
constructor(collector, state) {
|
|
580
|
+
this.collector = collector;
|
|
581
|
+
this.#state = state;
|
|
582
|
+
this.api = { collector, keys: state.keys, pending: /* @__PURE__ */ new Set(), reportedOrigins: /* @__PURE__ */ new Set() };
|
|
583
|
+
}
|
|
584
|
+
/** Wraps an APIRequestContext once (15-REQ-01); calls go to the active test. */
|
|
585
|
+
wrapRequest(requestContext) {
|
|
586
|
+
if (!this.#state.config.scope.api) return;
|
|
587
|
+
wrapRequestContext(requestContext, () => chunkGHGZTTAG_cjs.activeCollector()?.api);
|
|
588
|
+
}
|
|
589
|
+
async track(context) {
|
|
590
|
+
if (this.#trackers.has(context)) return;
|
|
591
|
+
this.#trackers.set(context, new ResponseTracker(context, this.collector, this.#state.keys));
|
|
592
|
+
this.wrapRequest(context.request);
|
|
593
|
+
if (this.#state.config.browser.cspEvents) {
|
|
594
|
+
try {
|
|
595
|
+
await instrumentContext(
|
|
596
|
+
context,
|
|
597
|
+
this.#state.binding,
|
|
598
|
+
() => chunkGHGZTTAG_cjs.activeCollector()?.collector
|
|
599
|
+
);
|
|
600
|
+
} catch (error) {
|
|
601
|
+
this.collector.diagnostic(
|
|
602
|
+
"COLLECTOR_ERROR",
|
|
603
|
+
"info",
|
|
604
|
+
`collector: the CSP listener could not be added: ${error instanceof Error ? error.message : String(error)}`
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
/** End-of-test scan and drain of one context, before it closes (05-REQ-23, 05-REQ-09). */
|
|
610
|
+
async finish(context) {
|
|
611
|
+
if (this.#finished.has(context)) return;
|
|
612
|
+
this.#finished.add(context);
|
|
613
|
+
const tracker = this.#trackers.get(context);
|
|
614
|
+
if (tracker === void 0) return;
|
|
615
|
+
await scanContext(context, this.collector, this.#state.config.browser.storageScan);
|
|
616
|
+
await drainApi(this.api);
|
|
617
|
+
await tracker.drain();
|
|
618
|
+
}
|
|
619
|
+
async finishAll() {
|
|
620
|
+
for (const context of this.#trackers.keys()) await this.finish(context);
|
|
621
|
+
await drainApi(this.api);
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
var PASSIVE_API = {
|
|
625
|
+
track: async () => {
|
|
626
|
+
},
|
|
627
|
+
trackRequestContext: () => {
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
function openBuffer(baseURL, testInfo) {
|
|
631
|
+
const state = workerState(baseURL);
|
|
632
|
+
if (state.scannedProjects !== "all" && !state.scannedProjects.includes(testInfo.project.name)) {
|
|
633
|
+
return { scan: null, api: PASSIVE_API };
|
|
634
|
+
}
|
|
635
|
+
const collector = new chunkHWVW6NBJ_cjs.TestCollector({
|
|
636
|
+
config: state.config,
|
|
637
|
+
rules: chunkHWVW6NBJ_cjs.BUILT_IN_RULES,
|
|
638
|
+
scope: state.scope,
|
|
639
|
+
redactor: state.redactor,
|
|
640
|
+
cache: state.cache,
|
|
641
|
+
test: testRef(testInfo)
|
|
642
|
+
});
|
|
643
|
+
if (state.configMissing) {
|
|
644
|
+
collector.diagnostic(
|
|
645
|
+
"CONFIG_MISSING",
|
|
646
|
+
"error",
|
|
647
|
+
"config: withSecurityFixtures() is used but withSecurity() was not applied to the Playwright config; the defaults are used"
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
const scan = new TestScan(collector, state);
|
|
651
|
+
return {
|
|
652
|
+
scan,
|
|
653
|
+
api: {
|
|
654
|
+
track: (context) => scan.track(context),
|
|
655
|
+
trackRequestContext: (requestContext) => scan.wrapRequest(requestContext)
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
var passiveFixtures = {
|
|
660
|
+
security: async ({}, use) => {
|
|
661
|
+
await use(PASSIVE_API);
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
var scanFixtures = {
|
|
665
|
+
_secBuffer: [
|
|
666
|
+
async ({ baseURL }, use, testInfo) => {
|
|
667
|
+
const buffer = openBuffer(baseURL, testInfo);
|
|
668
|
+
const scan = buffer.scan;
|
|
669
|
+
if (scan !== null) {
|
|
670
|
+
chunkGHGZTTAG_cjs.setActiveCollector(scan);
|
|
671
|
+
for (const context of chunkGHGZTTAG_cjs.trackedContexts()) await scan.track(context);
|
|
672
|
+
}
|
|
673
|
+
await use(buffer);
|
|
674
|
+
if (scan === null) return;
|
|
675
|
+
await scan.finishAll();
|
|
676
|
+
if (chunkGHGZTTAG_cjs.activeCollector() === scan) chunkGHGZTTAG_cjs.setActiveCollector(void 0);
|
|
677
|
+
await testInfo.attach(chunkHWVW6NBJ_cjs.ATTACHMENT_NAME, {
|
|
678
|
+
body: scan.collector.attachment(),
|
|
679
|
+
contentType: "application/json"
|
|
680
|
+
});
|
|
681
|
+
},
|
|
682
|
+
{ auto: true, box: true }
|
|
683
|
+
],
|
|
684
|
+
context: async ({ context, _secBuffer }, use) => {
|
|
685
|
+
await _secBuffer.scan?.track(context);
|
|
686
|
+
await use(context);
|
|
687
|
+
await _secBuffer.scan?.finish(context);
|
|
688
|
+
},
|
|
689
|
+
page: async ({ page, _secBuffer }, use) => {
|
|
690
|
+
await _secBuffer.scan?.track(page.context());
|
|
691
|
+
await use(page);
|
|
692
|
+
},
|
|
693
|
+
// The same object with its request methods wrapped (15-REQ-01); untouched without a scan.
|
|
694
|
+
request: async ({ request, _secBuffer }, use) => {
|
|
695
|
+
_secBuffer.scan?.wrapRequest(request);
|
|
696
|
+
await use(request);
|
|
697
|
+
if (_secBuffer.scan) await drainApi(_secBuffer.scan.api);
|
|
698
|
+
},
|
|
699
|
+
security: async ({ _secBuffer }, use) => {
|
|
700
|
+
await use(_secBuffer.api);
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
function withSecurityFixtures(base) {
|
|
704
|
+
const wrapped = chunkGHGZTTAG_cjs.wrappedTests();
|
|
705
|
+
if (wrapped.has(base)) return base;
|
|
706
|
+
const extend = base.extend;
|
|
707
|
+
const result = extend.call(base, chunkGHGZTTAG_cjs.scanModeOn() ? scanFixtures : passiveFixtures);
|
|
708
|
+
wrapped.add(result);
|
|
709
|
+
return result;
|
|
710
|
+
}
|
|
711
|
+
async function trackContext(context) {
|
|
712
|
+
if (!chunkGHGZTTAG_cjs.scanModeOn()) return;
|
|
713
|
+
const contexts = chunkGHGZTTAG_cjs.trackedContexts();
|
|
714
|
+
if (!contexts.has(context)) {
|
|
715
|
+
contexts.add(context);
|
|
716
|
+
context.once("close", () => contexts.delete(context));
|
|
717
|
+
}
|
|
718
|
+
await chunkGHGZTTAG_cjs.activeCollector()?.track(context);
|
|
719
|
+
}
|
|
720
|
+
function trackRequestContext(requestContext) {
|
|
721
|
+
if (!chunkGHGZTTAG_cjs.scanModeOn()) return;
|
|
722
|
+
const state = workerState(void 0);
|
|
723
|
+
if (!state.config.scope.api || !isApiRequestContext(requestContext)) return;
|
|
724
|
+
wrapRequestContext(requestContext, () => chunkGHGZTTAG_cjs.activeCollector()?.api);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
Object.defineProperty(exports, "VERSION", {
|
|
728
|
+
enumerable: true,
|
|
729
|
+
get: function () { return chunkBBF7BGCZ_cjs.VERSION; }
|
|
730
|
+
});
|
|
731
|
+
exports.trackContext = trackContext;
|
|
732
|
+
exports.trackRequestContext = trackRequestContext;
|
|
733
|
+
exports.withSecurityFixtures = withSecurityFixtures;
|