@coffre/conformance 0.1.17 → 0.1.18
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/dist/main.js +216 -8
- package/package.json +4 -4
- package/src/browser.ts +5 -0
- package/src/checks/pages.ts +33 -0
- package/src/chrome.ts +171 -0
- package/src/main.ts +4 -1
- package/src/suite.ts +3 -1
package/dist/main.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { n as DevIdp, o as defaultGitHubAccount, r as GITHUB_RUN } from "./idp-BeOsuZ3p.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
|
-
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { delimiter, dirname, join, resolve } from "node:path";
|
|
4
4
|
import { inspect, parseArgs } from "node:util";
|
|
5
5
|
import { spawn } from "node:child_process";
|
|
6
6
|
import { createHash, createHmac, createPrivateKey, hkdfSync, randomBytes, randomUUID, sign } from "node:crypto";
|
|
7
|
-
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
8
|
-
import { tmpdir } from "node:os";
|
|
9
|
-
import { setTimeout } from "node:timers/promises";
|
|
7
|
+
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
|
|
8
|
+
import { homedir, tmpdir } from "node:os";
|
|
9
|
+
import { setTimeout as setTimeout$1 } from "node:timers/promises";
|
|
10
10
|
import { DatabaseSync } from "node:sqlite";
|
|
11
11
|
import pg from "pg";
|
|
12
12
|
import { postgresConnection } from "@coffre/db/connect";
|
|
@@ -244,7 +244,7 @@ async function until(what, probe, seconds, alive = () => true) {
|
|
|
244
244
|
for (let i = 0; i < seconds * 10; i++) {
|
|
245
245
|
if (!alive()) throw new Failure(`a process exited while waiting for ${what}`);
|
|
246
246
|
if (await probe().catch(() => false)) return;
|
|
247
|
-
await setTimeout(100);
|
|
247
|
+
await setTimeout$1(100);
|
|
248
248
|
}
|
|
249
249
|
throw new Failure(`timed out waiting for ${what}`);
|
|
250
250
|
}
|
|
@@ -396,7 +396,7 @@ async function boot(kind, at, options) {
|
|
|
396
396
|
for (const { child, exited } of children) if (!exited) try {
|
|
397
397
|
process.kill(-child.pid, signal);
|
|
398
398
|
} catch {}
|
|
399
|
-
await setTimeout(500);
|
|
399
|
+
await setTimeout$1(500);
|
|
400
400
|
}
|
|
401
401
|
await idp.stop().catch(() => {});
|
|
402
402
|
await cleanup().catch(() => {});
|
|
@@ -544,6 +544,10 @@ var Browser = class {
|
|
|
544
544
|
constructor(origin) {
|
|
545
545
|
this.origin = origin;
|
|
546
546
|
}
|
|
547
|
+
/** The cookies coffre left this browser, to hand a real one. */
|
|
548
|
+
cookies() {
|
|
549
|
+
return [...this.#jar];
|
|
550
|
+
}
|
|
547
551
|
/** Whether coffre left this browser a cookie. */
|
|
548
552
|
get hasCookies() {
|
|
549
553
|
return this.#jar.size > 0;
|
|
@@ -2152,7 +2156,7 @@ var Cli = class {
|
|
|
2152
2156
|
child.kill("SIGKILL");
|
|
2153
2157
|
throw new Failure(`coffre never got to ${what}`, output);
|
|
2154
2158
|
}
|
|
2155
|
-
await setTimeout(50);
|
|
2159
|
+
await setTimeout$1(50);
|
|
2156
2160
|
}
|
|
2157
2161
|
return pattern.exec(output);
|
|
2158
2162
|
}
|
|
@@ -2363,6 +2367,200 @@ async function verifyKeys(cli) {
|
|
|
2363
2367
|
return "this deployment's keys pass, vault ID included; a wrong vault key and a malformed app key are each named; no key shown";
|
|
2364
2368
|
}
|
|
2365
2369
|
//#endregion
|
|
2370
|
+
//#region src/chrome.ts
|
|
2371
|
+
const NAMES = [
|
|
2372
|
+
"google-chrome",
|
|
2373
|
+
"google-chrome-stable",
|
|
2374
|
+
"chromium",
|
|
2375
|
+
"chromium-browser",
|
|
2376
|
+
"chrome"
|
|
2377
|
+
];
|
|
2378
|
+
const MAC = ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "/Applications/Chromium.app/Contents/MacOS/Chromium"];
|
|
2379
|
+
/** A Chrome or Chromium on this machine: on PATH, where macOS keeps it, or Playwright's. */
|
|
2380
|
+
function findChrome() {
|
|
2381
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) for (const name of NAMES) if (dir !== "" && existsSync(join(dir, name))) return join(dir, name);
|
|
2382
|
+
for (const path of MAC) if (existsSync(path)) return path;
|
|
2383
|
+
const playwright = join(homedir(), ".cache", "ms-playwright");
|
|
2384
|
+
if (existsSync(playwright)) for (const dir of readdirSync(playwright).filter((name) => name.startsWith("chromium-")).sort().reverse()) for (const build of ["chrome-linux64", "chrome-linux"]) {
|
|
2385
|
+
const path = join(playwright, dir, build, "chrome");
|
|
2386
|
+
if (existsSync(path)) return path;
|
|
2387
|
+
}
|
|
2388
|
+
return null;
|
|
2389
|
+
}
|
|
2390
|
+
var Chrome = class Chrome {
|
|
2391
|
+
#process;
|
|
2392
|
+
#socket;
|
|
2393
|
+
#profile;
|
|
2394
|
+
#next = 0;
|
|
2395
|
+
#pending = /* @__PURE__ */ new Map();
|
|
2396
|
+
#listeners = /* @__PURE__ */ new Set();
|
|
2397
|
+
constructor(process, socket, profile) {
|
|
2398
|
+
this.#process = process;
|
|
2399
|
+
this.#socket = socket;
|
|
2400
|
+
this.#profile = profile;
|
|
2401
|
+
socket.addEventListener("message", (event) => {
|
|
2402
|
+
const message = JSON.parse(String(event.data));
|
|
2403
|
+
if (message.id !== void 0) {
|
|
2404
|
+
const pending = this.#pending.get(message.id);
|
|
2405
|
+
this.#pending.delete(message.id);
|
|
2406
|
+
if (message.error !== void 0) pending?.reject(new Error(message.error.message));
|
|
2407
|
+
else pending?.resolve(message.result ?? {});
|
|
2408
|
+
} else for (const listener of this.#listeners) listener(message);
|
|
2409
|
+
});
|
|
2410
|
+
}
|
|
2411
|
+
/** Chrome at `executable`, headless, with a profile of its own, gone when closed. */
|
|
2412
|
+
static async open(executable) {
|
|
2413
|
+
const profile = mkdtempSync(join(tmpdir(), "coffre-chrome-"));
|
|
2414
|
+
const child = spawn(executable, [
|
|
2415
|
+
"--headless=new",
|
|
2416
|
+
"--remote-debugging-port=0",
|
|
2417
|
+
`--user-data-dir=${profile}`,
|
|
2418
|
+
"--no-first-run",
|
|
2419
|
+
"--no-default-browser-check",
|
|
2420
|
+
"--disable-gpu",
|
|
2421
|
+
"--no-sandbox",
|
|
2422
|
+
"about:blank"
|
|
2423
|
+
], { stdio: [
|
|
2424
|
+
"ignore",
|
|
2425
|
+
"ignore",
|
|
2426
|
+
"pipe"
|
|
2427
|
+
] });
|
|
2428
|
+
const url = await new Promise((resolve, reject) => {
|
|
2429
|
+
let output = "";
|
|
2430
|
+
const timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`Chrome did not start: ${output.trim().split("\n").slice(-3).join(" ")}`)), 3e4);
|
|
2431
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
2432
|
+
output += chunk;
|
|
2433
|
+
const found = /DevTools listening on (ws:\/\/\S+)/.exec(output);
|
|
2434
|
+
if (found !== null) {
|
|
2435
|
+
clearTimeout(timer);
|
|
2436
|
+
resolve(found[1]);
|
|
2437
|
+
}
|
|
2438
|
+
});
|
|
2439
|
+
child.on("error", (error) => {
|
|
2440
|
+
clearTimeout(timer);
|
|
2441
|
+
reject(error);
|
|
2442
|
+
});
|
|
2443
|
+
child.on("exit", (code) => {
|
|
2444
|
+
clearTimeout(timer);
|
|
2445
|
+
reject(/* @__PURE__ */ new Error(`Chrome exited with ${code}: ${output.trim().split("\n").slice(-3).join(" ")}`));
|
|
2446
|
+
});
|
|
2447
|
+
});
|
|
2448
|
+
const socket = new WebSocket(url);
|
|
2449
|
+
await new Promise((resolve, reject) => {
|
|
2450
|
+
socket.addEventListener("open", () => resolve(), { once: true });
|
|
2451
|
+
socket.addEventListener("error", () => reject(/* @__PURE__ */ new Error(`could not reach Chrome at ${url}`)), { once: true });
|
|
2452
|
+
});
|
|
2453
|
+
return new Chrome(child, socket, profile);
|
|
2454
|
+
}
|
|
2455
|
+
#send(method, params = {}, sessionId) {
|
|
2456
|
+
const id = ++this.#next;
|
|
2457
|
+
return new Promise((resolve, reject) => {
|
|
2458
|
+
this.#pending.set(id, {
|
|
2459
|
+
resolve,
|
|
2460
|
+
reject
|
|
2461
|
+
});
|
|
2462
|
+
this.#socket.send(JSON.stringify({
|
|
2463
|
+
id,
|
|
2464
|
+
method,
|
|
2465
|
+
params,
|
|
2466
|
+
sessionId
|
|
2467
|
+
}));
|
|
2468
|
+
});
|
|
2469
|
+
}
|
|
2470
|
+
/**
|
|
2471
|
+
* `url` in a tab of its own, with `cookies` for its origin: the heading it
|
|
2472
|
+
* shows once its scripts have run and settled, and every uncaught error,
|
|
2473
|
+
* console error and failed load along the way.
|
|
2474
|
+
*/
|
|
2475
|
+
async load(url, cookies, settle = 2e3) {
|
|
2476
|
+
const { targetId } = await this.#send("Target.createTarget", { url: "about:blank" });
|
|
2477
|
+
const { sessionId } = await this.#send("Target.attachToTarget", {
|
|
2478
|
+
targetId,
|
|
2479
|
+
flatten: true
|
|
2480
|
+
});
|
|
2481
|
+
const errors = [];
|
|
2482
|
+
let loaded;
|
|
2483
|
+
const load = new Promise((resolve) => loaded = resolve);
|
|
2484
|
+
const listener = (message) => {
|
|
2485
|
+
if (message.sessionId !== sessionId) return;
|
|
2486
|
+
const params = message.params ?? {};
|
|
2487
|
+
if (message.method === "Runtime.exceptionThrown") {
|
|
2488
|
+
const details = params.exceptionDetails;
|
|
2489
|
+
errors.push(`${details.exception?.description?.split("\n")[0] ?? details.text}${details.url ? ` (${details.url}:${(details.lineNumber ?? 0) + 1})` : ""}`);
|
|
2490
|
+
} else if (message.method === "Runtime.consoleAPICalled" && params.type === "error") {
|
|
2491
|
+
const args = params.args;
|
|
2492
|
+
errors.push(`console.error: ${args.map((arg) => arg.description ?? String(arg.value)).join(" ")}`);
|
|
2493
|
+
} else if (message.method === "Log.entryAdded") {
|
|
2494
|
+
const entry = params.entry;
|
|
2495
|
+
if (entry.level === "error") errors.push(`${entry.text}${entry.url ? ` (${entry.url})` : ""}`);
|
|
2496
|
+
} else if (message.method === "Page.loadEventFired") loaded();
|
|
2497
|
+
};
|
|
2498
|
+
this.#listeners.add(listener);
|
|
2499
|
+
try {
|
|
2500
|
+
for (const domain of [
|
|
2501
|
+
"Runtime",
|
|
2502
|
+
"Log",
|
|
2503
|
+
"Page",
|
|
2504
|
+
"Network"
|
|
2505
|
+
]) await this.#send(`${domain}.enable`, {}, sessionId);
|
|
2506
|
+
for (const [name, value] of cookies) await this.#send("Network.setCookie", {
|
|
2507
|
+
name,
|
|
2508
|
+
value,
|
|
2509
|
+
url
|
|
2510
|
+
}, sessionId);
|
|
2511
|
+
await this.#send("Page.navigate", { url }, sessionId);
|
|
2512
|
+
await Promise.race([load, new Promise((resolve) => setTimeout(resolve, 3e4))]);
|
|
2513
|
+
await new Promise((resolve) => setTimeout(resolve, settle));
|
|
2514
|
+
const { result } = await this.#send("Runtime.evaluate", {
|
|
2515
|
+
expression: "document.querySelector('h1')?.textContent?.trim() || null",
|
|
2516
|
+
returnByValue: true
|
|
2517
|
+
}, sessionId);
|
|
2518
|
+
return {
|
|
2519
|
+
heading: result.value,
|
|
2520
|
+
errors
|
|
2521
|
+
};
|
|
2522
|
+
} finally {
|
|
2523
|
+
this.#listeners.delete(listener);
|
|
2524
|
+
await this.#send("Target.closeTarget", { targetId }).catch(() => {});
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2527
|
+
async close() {
|
|
2528
|
+
await this.#send("Browser.close").catch(() => {});
|
|
2529
|
+
this.#socket.close();
|
|
2530
|
+
if (this.#process.exitCode === null) {
|
|
2531
|
+
await Promise.race([new Promise((resolve) => this.#process.once("exit", resolve)), new Promise((resolve) => setTimeout(resolve, 5e3))]);
|
|
2532
|
+
this.#process.kill("SIGKILL");
|
|
2533
|
+
}
|
|
2534
|
+
rmSync(this.#profile, {
|
|
2535
|
+
recursive: true,
|
|
2536
|
+
force: true
|
|
2537
|
+
});
|
|
2538
|
+
}
|
|
2539
|
+
};
|
|
2540
|
+
//#endregion
|
|
2541
|
+
//#region src/checks/pages.ts
|
|
2542
|
+
/** Each page, and the heading it shows. */
|
|
2543
|
+
const PAGES = [
|
|
2544
|
+
["/projects", "Projects"],
|
|
2545
|
+
[`/projects/${PROJECT}`, "Conformance"],
|
|
2546
|
+
["/audit", "Audit"]
|
|
2547
|
+
];
|
|
2548
|
+
async function pagesInBrowser(deployment, admin, browser) {
|
|
2549
|
+
const executable = browser ?? findChrome();
|
|
2550
|
+
if (executable === null) throw new Skip("no Chrome or Chromium here: --browser <path> names one");
|
|
2551
|
+
const chrome = await Chrome.open(executable);
|
|
2552
|
+
try {
|
|
2553
|
+
for (const [path, heading] of PAGES) {
|
|
2554
|
+
const loaded = await chrome.load(new URL(path, deployment.origin).href, admin.browser.cookies());
|
|
2555
|
+
expect(loaded.errors.length === 0, `${path}, signed in, reported errors in the browser`, loaded.errors.join("\n"));
|
|
2556
|
+
expect(loaded.heading?.startsWith(heading), `${path}, signed in, shows ${JSON.stringify(loaded.heading)}, not the heading ${JSON.stringify(heading)}`);
|
|
2557
|
+
}
|
|
2558
|
+
} finally {
|
|
2559
|
+
await chrome.close();
|
|
2560
|
+
}
|
|
2561
|
+
return `${PAGES.map(([path]) => path).join(", ")}, signed in, in Chrome: each rendered, no error from their scripts`;
|
|
2562
|
+
}
|
|
2563
|
+
//#endregion
|
|
2366
2564
|
//#region src/checks/surface.ts
|
|
2367
2565
|
async function health(deployment) {
|
|
2368
2566
|
const { origin } = deployment;
|
|
@@ -2632,6 +2830,10 @@ async function conform(deployment, options) {
|
|
|
2632
2830
|
const admin = await report.check("sign-in", {}, () => signInAdmin(deployment));
|
|
2633
2831
|
await report.check("page load", { admin }, ({ admin }) => pageLoad(admin));
|
|
2634
2832
|
const canaries = await report.check("setup", { admin }, ({ admin }) => setUp(admin));
|
|
2833
|
+
await report.check("pages in a browser", {
|
|
2834
|
+
admin,
|
|
2835
|
+
canaries
|
|
2836
|
+
}, ({ admin }) => pagesInBrowser(deployment, admin, options.browser));
|
|
2635
2837
|
const people = await report.check("personas", {
|
|
2636
2838
|
admin,
|
|
2637
2839
|
canaries
|
|
@@ -2751,6 +2953,8 @@ const USAGE = `usage:
|
|
|
2751
2953
|
<dir> the deployment, as \`coffre init\` wrote it (default: here)
|
|
2752
2954
|
--port <n> coffre's port; the IdP gets the next, wrangler's inspector the one after (3082)
|
|
2753
2955
|
--bulk-limit <n> the vault's bulkLimit count, when not the default (1000)
|
|
2956
|
+
--browser <path> Chrome or Chromium, to load the pages in (default: one found here;
|
|
2957
|
+
without one, that check is skipped)
|
|
2754
2958
|
--postgres <url> workers: a Postgres login that may create databases; one is made for the run
|
|
2755
2959
|
--runtime <url> workers: the same server as coffre_runtime, the login the app runs as
|
|
2756
2960
|
--vault-runtime <url>
|
|
@@ -2769,6 +2973,7 @@ const { values, positionals } = parseArgs({
|
|
|
2769
2973
|
type: "string",
|
|
2770
2974
|
default: "1000"
|
|
2771
2975
|
},
|
|
2976
|
+
browser: { type: "string" },
|
|
2772
2977
|
postgres: { type: "string" },
|
|
2773
2978
|
runtime: { type: "string" },
|
|
2774
2979
|
"vault-runtime": { type: "string" },
|
|
@@ -2803,7 +3008,10 @@ try {
|
|
|
2803
3008
|
console.log(` up on ${deployment.origin}`);
|
|
2804
3009
|
let failed;
|
|
2805
3010
|
try {
|
|
2806
|
-
failed = await conform(deployment, {
|
|
3011
|
+
failed = await conform(deployment, {
|
|
3012
|
+
bulkLimit,
|
|
3013
|
+
browser: values.browser || null
|
|
3014
|
+
});
|
|
2807
3015
|
if (failed.length > 0) console.error(`\nThe processes' output, last lines:\n${deployment.output(4e3)}`);
|
|
2808
3016
|
} finally {
|
|
2809
3017
|
await deployment.stop();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coffre/conformance",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"description": "Checks a coffre deployment from outside: what it must never do, whatever code it runs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"jose": "6.2.3",
|
|
33
33
|
"pg": "8.16.3",
|
|
34
|
-
"@coffre/
|
|
35
|
-
"@coffre/
|
|
34
|
+
"@coffre/cli": "0.1.18",
|
|
35
|
+
"@coffre/db": "0.1.18"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "26.1.1",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"oauth4webapi": "3.8.8",
|
|
41
41
|
"tsdown": "0.23.0",
|
|
42
42
|
"typescript": "5.9.3",
|
|
43
|
-
"@coffre/client": "0.1.
|
|
43
|
+
"@coffre/client": "0.1.18"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsdown",
|
package/src/browser.ts
CHANGED
|
@@ -10,6 +10,11 @@ export class Browser {
|
|
|
10
10
|
this.origin = origin;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** The cookies coffre left this browser, to hand a real one. */
|
|
14
|
+
cookies(): [string, string][] {
|
|
15
|
+
return [...this.#jar];
|
|
16
|
+
}
|
|
17
|
+
|
|
13
18
|
/** Whether coffre left this browser a cookie. */
|
|
14
19
|
get hasCookies(): boolean {
|
|
15
20
|
return this.#jar.size > 0;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// coffre's pages in a real browser, signed in: each renders its heading,
|
|
2
|
+
// and its scripts run without an error. What a bundler does to the code a
|
|
3
|
+
// page receives shows only there: wrangler's keep_names once wrapped the
|
|
4
|
+
// functions seroval writes into a page in an `__name` that only the Worker
|
|
5
|
+
// had, and a signed-in page rendered, then went blank.
|
|
6
|
+
import { Chrome, findChrome } from '../chrome.ts';
|
|
7
|
+
import type { Deployment } from '../harness.ts';
|
|
8
|
+
import { expect, Skip } from '../report.ts';
|
|
9
|
+
import { type Person, PROJECT } from './people.ts';
|
|
10
|
+
|
|
11
|
+
/** Each page, and the heading it shows. */
|
|
12
|
+
const PAGES = [
|
|
13
|
+
['/projects', 'Projects'],
|
|
14
|
+
[`/projects/${PROJECT}`, 'Conformance'],
|
|
15
|
+
['/audit', 'Audit'],
|
|
16
|
+
] as const;
|
|
17
|
+
|
|
18
|
+
export async function pagesInBrowser(deployment: Deployment, admin: Person, browser: string | null): Promise<string> {
|
|
19
|
+
const executable = browser ?? findChrome();
|
|
20
|
+
if (executable === null) throw new Skip('no Chrome or Chromium here: --browser <path> names one');
|
|
21
|
+
const chrome = await Chrome.open(executable);
|
|
22
|
+
try {
|
|
23
|
+
for (const [path, heading] of PAGES) {
|
|
24
|
+
const loaded = await chrome.load(new URL(path, deployment.origin).href, admin.browser.cookies());
|
|
25
|
+
expect(loaded.errors.length === 0, `${path}, signed in, reported errors in the browser`, loaded.errors.join('\n'));
|
|
26
|
+
// The heading, then what may follow it in the h1, such as a project's slug.
|
|
27
|
+
expect(loaded.heading?.startsWith(heading), `${path}, signed in, shows ${JSON.stringify(loaded.heading)}, not the heading ${JSON.stringify(heading)}`);
|
|
28
|
+
}
|
|
29
|
+
} finally {
|
|
30
|
+
await chrome.close();
|
|
31
|
+
}
|
|
32
|
+
return `${PAGES.map(([path]) => path).join(', ')}, signed in, in Chrome: each rendered, no error from their scripts`;
|
|
33
|
+
}
|
package/src/chrome.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// A real browser, for what only one shows: coffre's pages as they render
|
|
2
|
+
// and hydrate, their scripts run. Headless Chrome, driven over the DevTools
|
|
3
|
+
// protocol with Node's own WebSocket, so that conformance depends on no
|
|
4
|
+
// browser package; the machine's Chrome or Chromium does.
|
|
5
|
+
import { spawn, type ChildProcess } from 'node:child_process';
|
|
6
|
+
import { existsSync, mkdtempSync, readdirSync, rmSync } from 'node:fs';
|
|
7
|
+
import { homedir, tmpdir } from 'node:os';
|
|
8
|
+
import { delimiter, join } from 'node:path';
|
|
9
|
+
|
|
10
|
+
/** What a page did, loaded: the heading it shows, and every error its scripts and console reported. */
|
|
11
|
+
export type Loaded = { heading: string | null; errors: string[] };
|
|
12
|
+
|
|
13
|
+
const NAMES = ['google-chrome', 'google-chrome-stable', 'chromium', 'chromium-browser', 'chrome'];
|
|
14
|
+
const MAC = ['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/Applications/Chromium.app/Contents/MacOS/Chromium'];
|
|
15
|
+
|
|
16
|
+
/** A Chrome or Chromium on this machine: on PATH, where macOS keeps it, or Playwright's. */
|
|
17
|
+
export function findChrome(): string | null {
|
|
18
|
+
for (const dir of (process.env.PATH ?? '').split(delimiter)) {
|
|
19
|
+
for (const name of NAMES) if (dir !== '' && existsSync(join(dir, name))) return join(dir, name);
|
|
20
|
+
}
|
|
21
|
+
for (const path of MAC) if (existsSync(path)) return path;
|
|
22
|
+
const playwright = join(homedir(), '.cache', 'ms-playwright');
|
|
23
|
+
if (existsSync(playwright)) {
|
|
24
|
+
for (const dir of readdirSync(playwright).filter((name) => name.startsWith('chromium-')).sort().reverse()) {
|
|
25
|
+
for (const build of ['chrome-linux64', 'chrome-linux']) {
|
|
26
|
+
const path = join(playwright, dir, build, 'chrome');
|
|
27
|
+
if (existsSync(path)) return path;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type Message = { id?: number; method?: string; params?: Record<string, unknown>; result?: Record<string, unknown>; error?: { message: string }; sessionId?: string };
|
|
35
|
+
|
|
36
|
+
export class Chrome {
|
|
37
|
+
readonly #process: ChildProcess;
|
|
38
|
+
readonly #socket: WebSocket;
|
|
39
|
+
readonly #profile: string;
|
|
40
|
+
#next = 0;
|
|
41
|
+
readonly #pending = new Map<number, { resolve: (result: Record<string, unknown>) => void; reject: (error: Error) => void }>();
|
|
42
|
+
readonly #listeners = new Set<(message: Message) => void>();
|
|
43
|
+
|
|
44
|
+
private constructor(process: ChildProcess, socket: WebSocket, profile: string) {
|
|
45
|
+
this.#process = process;
|
|
46
|
+
this.#socket = socket;
|
|
47
|
+
this.#profile = profile;
|
|
48
|
+
socket.addEventListener('message', (event) => {
|
|
49
|
+
const message = JSON.parse(String(event.data)) as Message;
|
|
50
|
+
if (message.id !== undefined) {
|
|
51
|
+
const pending = this.#pending.get(message.id);
|
|
52
|
+
this.#pending.delete(message.id);
|
|
53
|
+
if (message.error !== undefined) pending?.reject(new Error(message.error.message));
|
|
54
|
+
else pending?.resolve(message.result ?? {});
|
|
55
|
+
} else {
|
|
56
|
+
for (const listener of this.#listeners) listener(message);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Chrome at `executable`, headless, with a profile of its own, gone when closed. */
|
|
62
|
+
static async open(executable: string): Promise<Chrome> {
|
|
63
|
+
const profile = mkdtempSync(join(tmpdir(), 'coffre-chrome-'));
|
|
64
|
+
const child = spawn(
|
|
65
|
+
executable,
|
|
66
|
+
[
|
|
67
|
+
'--headless=new',
|
|
68
|
+
'--remote-debugging-port=0',
|
|
69
|
+
`--user-data-dir=${profile}`,
|
|
70
|
+
'--no-first-run',
|
|
71
|
+
'--no-default-browser-check',
|
|
72
|
+
'--disable-gpu',
|
|
73
|
+
// Only coffre's own pages are loaded; CI's runners have no user namespaces for Chrome's sandbox.
|
|
74
|
+
'--no-sandbox',
|
|
75
|
+
'about:blank',
|
|
76
|
+
],
|
|
77
|
+
{ stdio: ['ignore', 'ignore', 'pipe'] },
|
|
78
|
+
);
|
|
79
|
+
const url = await new Promise<string>((resolve, reject) => {
|
|
80
|
+
let output = '';
|
|
81
|
+
const timer = setTimeout(() => reject(new Error(`Chrome did not start: ${output.trim().split('\n').slice(-3).join(' ')}`)), 30_000);
|
|
82
|
+
child.stderr!.setEncoding('utf8').on('data', (chunk: string) => {
|
|
83
|
+
output += chunk;
|
|
84
|
+
const found = /DevTools listening on (ws:\/\/\S+)/.exec(output);
|
|
85
|
+
if (found !== null) {
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
resolve(found[1]!);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
child.on('error', (error) => {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
reject(error);
|
|
93
|
+
});
|
|
94
|
+
child.on('exit', (code) => {
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
reject(new Error(`Chrome exited with ${code}: ${output.trim().split('\n').slice(-3).join(' ')}`));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
const socket = new WebSocket(url);
|
|
100
|
+
await new Promise<void>((resolve, reject) => {
|
|
101
|
+
socket.addEventListener('open', () => resolve(), { once: true });
|
|
102
|
+
socket.addEventListener('error', () => reject(new Error(`could not reach Chrome at ${url}`)), { once: true });
|
|
103
|
+
});
|
|
104
|
+
return new Chrome(child, socket, profile);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#send(method: string, params: Record<string, unknown> = {}, sessionId?: string): Promise<Record<string, unknown>> {
|
|
108
|
+
const id = ++this.#next;
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
this.#pending.set(id, { resolve, reject });
|
|
111
|
+
this.#socket.send(JSON.stringify({ id, method, params, sessionId }));
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* `url` in a tab of its own, with `cookies` for its origin: the heading it
|
|
117
|
+
* shows once its scripts have run and settled, and every uncaught error,
|
|
118
|
+
* console error and failed load along the way.
|
|
119
|
+
*/
|
|
120
|
+
async load(url: string, cookies: [string, string][], settle = 2_000): Promise<Loaded> {
|
|
121
|
+
const { targetId } = (await this.#send('Target.createTarget', { url: 'about:blank' })) as { targetId: string };
|
|
122
|
+
const { sessionId } = (await this.#send('Target.attachToTarget', { targetId, flatten: true })) as { sessionId: string };
|
|
123
|
+
const errors: string[] = [];
|
|
124
|
+
let loaded!: () => void;
|
|
125
|
+
const load = new Promise<void>((resolve) => (loaded = resolve));
|
|
126
|
+
const listener = (message: Message) => {
|
|
127
|
+
if (message.sessionId !== sessionId) return;
|
|
128
|
+
const params = message.params ?? {};
|
|
129
|
+
if (message.method === 'Runtime.exceptionThrown') {
|
|
130
|
+
const details = params.exceptionDetails as { text: string; exception?: { description?: string }; url?: string; lineNumber?: number };
|
|
131
|
+
errors.push(`${details.exception?.description?.split('\n')[0] ?? details.text}${details.url ? ` (${details.url}:${(details.lineNumber ?? 0) + 1})` : ''}`);
|
|
132
|
+
} else if (message.method === 'Runtime.consoleAPICalled' && params.type === 'error') {
|
|
133
|
+
const args = params.args as { value?: unknown; description?: string }[];
|
|
134
|
+
errors.push(`console.error: ${args.map((arg) => arg.description ?? String(arg.value)).join(' ')}`);
|
|
135
|
+
} else if (message.method === 'Log.entryAdded') {
|
|
136
|
+
const entry = params.entry as { level: string; text: string; url?: string };
|
|
137
|
+
if (entry.level === 'error') errors.push(`${entry.text}${entry.url ? ` (${entry.url})` : ''}`);
|
|
138
|
+
} else if (message.method === 'Page.loadEventFired') {
|
|
139
|
+
loaded();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
this.#listeners.add(listener);
|
|
143
|
+
try {
|
|
144
|
+
for (const domain of ['Runtime', 'Log', 'Page', 'Network']) await this.#send(`${domain}.enable`, {}, sessionId);
|
|
145
|
+
for (const [name, value] of cookies) await this.#send('Network.setCookie', { name, value, url }, sessionId);
|
|
146
|
+
await this.#send('Page.navigate', { url }, sessionId);
|
|
147
|
+
await Promise.race([load, new Promise((resolve) => setTimeout(resolve, 30_000))]);
|
|
148
|
+
// What streams in after the load, and hydration, have their time: the page may render, then fail.
|
|
149
|
+
await new Promise((resolve) => setTimeout(resolve, settle));
|
|
150
|
+
const { result } = (await this.#send(
|
|
151
|
+
'Runtime.evaluate',
|
|
152
|
+
{ expression: "document.querySelector('h1')?.textContent?.trim() || null", returnByValue: true },
|
|
153
|
+
sessionId,
|
|
154
|
+
)) as { result: { value: string | null } };
|
|
155
|
+
return { heading: result.value, errors };
|
|
156
|
+
} finally {
|
|
157
|
+
this.#listeners.delete(listener);
|
|
158
|
+
await this.#send('Target.closeTarget', { targetId }).catch(() => {});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async close(): Promise<void> {
|
|
163
|
+
await this.#send('Browser.close').catch(() => {});
|
|
164
|
+
this.#socket.close();
|
|
165
|
+
if (this.#process.exitCode === null) {
|
|
166
|
+
await Promise.race([new Promise((resolve) => this.#process.once('exit', resolve)), new Promise((resolve) => setTimeout(resolve, 5_000))]);
|
|
167
|
+
this.#process.kill('SIGKILL');
|
|
168
|
+
}
|
|
169
|
+
rmSync(this.#profile, { recursive: true, force: true });
|
|
170
|
+
}
|
|
171
|
+
}
|
package/src/main.ts
CHANGED
|
@@ -17,6 +17,8 @@ const USAGE = `usage:
|
|
|
17
17
|
<dir> the deployment, as \`coffre init\` wrote it (default: here)
|
|
18
18
|
--port <n> coffre's port; the IdP gets the next, wrangler's inspector the one after (3082)
|
|
19
19
|
--bulk-limit <n> the vault's bulkLimit count, when not the default (1000)
|
|
20
|
+
--browser <path> Chrome or Chromium, to load the pages in (default: one found here;
|
|
21
|
+
without one, that check is skipped)
|
|
20
22
|
--postgres <url> workers: a Postgres login that may create databases; one is made for the run
|
|
21
23
|
--runtime <url> workers: the same server as coffre_runtime, the login the app runs as
|
|
22
24
|
--vault-runtime <url>
|
|
@@ -30,6 +32,7 @@ const { values, positionals } = parseArgs({
|
|
|
30
32
|
options: {
|
|
31
33
|
port: { type: 'string', default: '3082' },
|
|
32
34
|
'bulk-limit': { type: 'string', default: '1000' },
|
|
35
|
+
browser: { type: 'string' },
|
|
33
36
|
postgres: { type: 'string' },
|
|
34
37
|
runtime: { type: 'string' },
|
|
35
38
|
'vault-runtime': { type: 'string' },
|
|
@@ -63,7 +66,7 @@ try {
|
|
|
63
66
|
console.log(` up on ${deployment.origin}`);
|
|
64
67
|
let failed: string[];
|
|
65
68
|
try {
|
|
66
|
-
failed = await conform(deployment, { bulkLimit });
|
|
69
|
+
failed = await conform(deployment, { bulkLimit, browser: values.browser || null });
|
|
67
70
|
if (failed.length > 0) console.error(`\nThe processes' output, last lines:\n${deployment.output(4000)}`);
|
|
68
71
|
} finally {
|
|
69
72
|
await deployment.stop();
|
package/src/suite.ts
CHANGED
|
@@ -13,16 +13,18 @@ import { editedGeneration, forgedCredential, forgedIdentity, forgedApproval } fr
|
|
|
13
13
|
import { canaryScan } from './checks/canaries.ts';
|
|
14
14
|
import { Cli, cliLogin, verifyAsOwner, verifyAsUser, verifyInterrupted, verifyKeys, verifyLeftovers, verifyWithToken } from './checks/cli.ts';
|
|
15
15
|
import { pageLoad, personas, setUp, setUpLive, signInAdmin } from './checks/people.ts';
|
|
16
|
+
import { pagesInBrowser } from './checks/pages.ts';
|
|
16
17
|
import { health } from './checks/surface.ts';
|
|
17
18
|
import { cliSignsIn, exchangesLimited, runSignsIn, runsRefused, runUnbound, spentOnce, tokensUnlogged, trustRun } from './checks/workloads.ts';
|
|
18
19
|
|
|
19
20
|
/** The names of the checks that failed. */
|
|
20
|
-
export async function conform(deployment: Deployment, options: { bulkLimit: number }): Promise<string[]> {
|
|
21
|
+
export async function conform(deployment: Deployment, options: { bulkLimit: number; browser: string | null }): Promise<string[]> {
|
|
21
22
|
const report = new Report();
|
|
22
23
|
await report.check('health', {}, () => health(deployment));
|
|
23
24
|
const admin = await report.check('sign-in', {}, () => signInAdmin(deployment));
|
|
24
25
|
await report.check('page load', { admin }, ({ admin }) => pageLoad(admin));
|
|
25
26
|
const canaries = await report.check('setup', { admin }, ({ admin }) => setUp(admin));
|
|
27
|
+
await report.check('pages in a browser', { admin, canaries }, ({ admin }) => pagesInBrowser(deployment, admin, options.browser));
|
|
26
28
|
const people = await report.check('personas', { admin, canaries }, ({ admin }) => personas(deployment, admin));
|
|
27
29
|
const all = { people, canaries };
|
|
28
30
|
|