@coffre/conformance 0.0.0-stage → 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/README.md +22 -2
- package/bin/coffre-conformance.js +13 -0
- package/dist/idp-DJlqS-JW.js +1122 -0
- package/dist/idp.d.ts +146 -0
- package/dist/idp.js +2 -0
- package/dist/main.js +2652 -0
- package/package.json +45 -3
- package/src/browser.ts +67 -0
- package/src/checks/access.ts +141 -0
- package/src/checks/audit.ts +267 -0
- package/src/checks/canaries.ts +121 -0
- package/src/checks/checkpoints.ts +64 -0
- package/src/checks/live.ts +321 -0
- package/src/checks/logins.ts +107 -0
- package/src/checks/members.ts +167 -0
- package/src/checks/people.ts +186 -0
- package/src/checks/readiness.ts +93 -0
- package/src/checks/signin.ts +130 -0
- package/src/checks/storage.ts +142 -0
- package/src/checks/surface.ts +74 -0
- package/src/database.ts +47 -0
- package/src/fixtures.ts +7 -0
- package/src/harness.ts +275 -0
- package/src/idp/authorize.ts +325 -0
- package/src/idp/github.ts +207 -0
- package/src/idp/http.ts +97 -0
- package/src/idp/idp.ts +331 -0
- package/src/idp/index.ts +5 -0
- package/src/idp/oidc.ts +209 -0
- package/src/idp/pages.ts +107 -0
- package/src/idp/people.ts +105 -0
- package/src/main.ts +123 -0
- package/src/report.ts +106 -0
- package/src/routes.ts +112 -0
- package/src/suite.ts +84 -0
package/dist/main.js
ADDED
|
@@ -0,0 +1,2652 @@
|
|
|
1
|
+
import { i as defaultGitHubAccount, n as DevIdp } from "./idp-DJlqS-JW.js";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { inspect, parseArgs } from "node:util";
|
|
4
|
+
import { setTimeout } from "node:timers/promises";
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
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 { DatabaseSync } from "node:sqlite";
|
|
10
|
+
import pg from "pg";
|
|
11
|
+
//#region ../client/dist/index.js
|
|
12
|
+
/** The API's error shape, `{ error, message }`, with the HTTP status. */
|
|
13
|
+
const COFFRE_ERROR = Symbol.for("@coffre/client:CoffreError");
|
|
14
|
+
var CoffreError = class extends Error {
|
|
15
|
+
/**
|
|
16
|
+
* `@coffre/ui`'s prebuilt pages bundle a copy of this class, and a page is
|
|
17
|
+
* handed a client the server made with this package's. So `instanceof`
|
|
18
|
+
* asks for the mark every copy leaves, rather than for this copy's prototype.
|
|
19
|
+
*/
|
|
20
|
+
static [Symbol.hasInstance](value) {
|
|
21
|
+
return typeof value === "object" && value !== null && COFFRE_ERROR in value;
|
|
22
|
+
}
|
|
23
|
+
[COFFRE_ERROR] = true;
|
|
24
|
+
status;
|
|
25
|
+
code;
|
|
26
|
+
/** The vault's own code when it refused: `no_grant`, `removed`, `bulk_limit`, ... */
|
|
27
|
+
reason;
|
|
28
|
+
constructor(status, code, message, reason) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = "CoffreError";
|
|
31
|
+
this.status = status;
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.reason = reason;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/** `market/prod/KEY` as route parameters. */
|
|
37
|
+
function place(path) {
|
|
38
|
+
const [project = "", environment = "", key = ""] = path.replace(/^\/+|\/+$/g, "").split("/");
|
|
39
|
+
return {
|
|
40
|
+
project,
|
|
41
|
+
environment,
|
|
42
|
+
key
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function createClient(options) {
|
|
46
|
+
const origin = options.url.replace(/\/+$/, "");
|
|
47
|
+
const transport = options.transport ?? ((request) => fetch(request));
|
|
48
|
+
async function send(method, path, input) {
|
|
49
|
+
const url = new URL(`${origin}/api${path}`);
|
|
50
|
+
const headers = new Headers(await options.headers?.());
|
|
51
|
+
let body;
|
|
52
|
+
if (method === "GET") {
|
|
53
|
+
for (const [name, value] of Object.entries(input ?? {})) if (value !== void 0 && value !== null) url.searchParams.set(name, String(value));
|
|
54
|
+
} else if (input !== void 0) {
|
|
55
|
+
headers.set("content-type", "application/json");
|
|
56
|
+
body = JSON.stringify(input);
|
|
57
|
+
}
|
|
58
|
+
const response = await transport(new Request(url, {
|
|
59
|
+
method,
|
|
60
|
+
headers,
|
|
61
|
+
body
|
|
62
|
+
}));
|
|
63
|
+
const payload = await response.json().catch(() => null);
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
const error = payload;
|
|
66
|
+
throw new CoffreError(response.status, typeof error?.error === "string" ? error.error : "http_error", typeof error?.message === "string" ? error.message : `request failed with status ${response.status}`, typeof error?.reason === "string" ? error.reason : void 0);
|
|
67
|
+
}
|
|
68
|
+
return payload;
|
|
69
|
+
}
|
|
70
|
+
/** A route key and its parameters as a method and a path. */
|
|
71
|
+
function address(key, params) {
|
|
72
|
+
const [method, pattern] = key.split(" ");
|
|
73
|
+
return [method, pattern.replace(/:(\w+)/g, (_, name) => encodeURIComponent(params[name]))];
|
|
74
|
+
}
|
|
75
|
+
/** Any route by its key: `call('GET /secrets/:project/:environment', { project, environment })`. */
|
|
76
|
+
async function call(key, ...[params, input]) {
|
|
77
|
+
const [method, path] = address(key, params);
|
|
78
|
+
return await send(method, path, input);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
call,
|
|
82
|
+
/** How this instance signs people in. Answers anyone, signed in or not. */
|
|
83
|
+
auth: () => send("GET", "/auth", void 0),
|
|
84
|
+
/** Who I am, and every place I can reach. */
|
|
85
|
+
me: () => call("GET /me", {}),
|
|
86
|
+
projects: {
|
|
87
|
+
/** With their environments. */
|
|
88
|
+
list: () => call("GET /projects", {}),
|
|
89
|
+
create: (project, input) => call("PUT /projects/:project", { project }, input),
|
|
90
|
+
update: (project, patch) => call("PATCH /projects/:project", { project }, patch)
|
|
91
|
+
},
|
|
92
|
+
environments: {
|
|
93
|
+
create: (path, input) => call("PUT /projects/:project/:environment", place(path), input),
|
|
94
|
+
update: (path, patch) => call("PATCH /projects/:project/:environment", place(path), patch)
|
|
95
|
+
},
|
|
96
|
+
secrets: {
|
|
97
|
+
/** Keys, versions and who changed what; never values. */
|
|
98
|
+
list: (path) => call("GET /secrets/:project/:environment", place(path)),
|
|
99
|
+
/** Decrypts one secret, or every secret in an environment. Logged in your name. */
|
|
100
|
+
reveal: (path) => call("POST /reveals", {}, { path }),
|
|
101
|
+
/** One transaction, a version and an audit entry per key; `null` archives. */
|
|
102
|
+
set: (path, values) => call("PATCH /secrets/:project/:environment", place(path), values),
|
|
103
|
+
/**
|
|
104
|
+
* What `set` would do to each key, `added`, `changed`, `unchanged` or
|
|
105
|
+
* `archived`, and nothing else: no value comes back and nothing is
|
|
106
|
+
* written. Comparing opens the current values, logged as reads.
|
|
107
|
+
*/
|
|
108
|
+
dryRun: (path, values) => {
|
|
109
|
+
const [method, route] = address("PATCH /secrets/:project/:environment", place(path));
|
|
110
|
+
return send(method, `${route}?dryRun=1`, values);
|
|
111
|
+
},
|
|
112
|
+
history: (path) => call("GET /secrets/:project/:environment/:key/versions", place(path)),
|
|
113
|
+
/** A new version holding the old one's value. */
|
|
114
|
+
restore: (path, version) => call("POST /secrets/:project/:environment/:key/restore", place(path), { version }),
|
|
115
|
+
rename: (path, key) => call("PATCH /secrets/:project/:environment/:key", place(path), { key }),
|
|
116
|
+
update: (path, patch) => call("PATCH /secrets/:project/:environment/:key", place(path), patch)
|
|
117
|
+
},
|
|
118
|
+
members: {
|
|
119
|
+
/** People and tokens, their role and their access; at a place, those who reach it. */
|
|
120
|
+
list: (path) => call("GET /members", {}, { path }),
|
|
121
|
+
/** What they hold, and what to rotate if they leave. */
|
|
122
|
+
get: (member) => call("GET /members/:member", { member }),
|
|
123
|
+
add: (member, input = {}) => call("PUT /members/:member", { member }, input),
|
|
124
|
+
/** Offboards; returns what to rotate. */
|
|
125
|
+
remove: (member) => call("DELETE /members/:member", { member })
|
|
126
|
+
},
|
|
127
|
+
tokens: {
|
|
128
|
+
list: (member) => call("GET /members/:member/tokens", { member }),
|
|
129
|
+
/** The value is in the answer, and only there. */
|
|
130
|
+
issue: (member, input) => call("POST /members/:member/tokens", { member }, input),
|
|
131
|
+
revoke: (member, id) => call("DELETE /members/:member/tokens/:id", {
|
|
132
|
+
member,
|
|
133
|
+
id
|
|
134
|
+
})
|
|
135
|
+
},
|
|
136
|
+
access: {
|
|
137
|
+
/** What they should hold at each place; the server applies the difference. `null` revokes. */
|
|
138
|
+
set: (member, access) => call("PATCH /access/:member", { member }, access) },
|
|
139
|
+
/** Where I am signed in. Only where coffre runs its own sign-in. */
|
|
140
|
+
sessions: {
|
|
141
|
+
list: () => call("GET /sessions", {}),
|
|
142
|
+
revoke: (id) => call("DELETE /sessions/:id", { id })
|
|
143
|
+
},
|
|
144
|
+
/** The accounts I sign in with. */
|
|
145
|
+
identities: {
|
|
146
|
+
list: () => call("GET /identities", {}),
|
|
147
|
+
unlink: (id) => call("DELETE /identities/:id", { id })
|
|
148
|
+
},
|
|
149
|
+
/** A `coffre login` waiting for someone to approve it, by the code it shows. */
|
|
150
|
+
deviceLogins: {
|
|
151
|
+
get: (code) => call("GET /device-logins/:code", { code }),
|
|
152
|
+
decide: (code, approve) => call("POST /device-logins/:code", { code }, { approve })
|
|
153
|
+
},
|
|
154
|
+
syncs: {
|
|
155
|
+
/** Where a sync can push on this instance, and what each asks for. */
|
|
156
|
+
providers: () => call("GET /syncs/providers", {}),
|
|
157
|
+
list: (path) => call("GET /syncs/:project/:environment", place(path)),
|
|
158
|
+
add: (path, input) => call("POST /syncs/:project/:environment", place(path), input),
|
|
159
|
+
update: (id, patch) => call("PATCH /syncs/by-id/:id", { id }, patch),
|
|
160
|
+
remove: (id) => call("DELETE /syncs/by-id/:id", { id }),
|
|
161
|
+
run: (id) => call("POST /syncs/by-id/:id/runs", { id })
|
|
162
|
+
},
|
|
163
|
+
audit: {
|
|
164
|
+
list: (query = {}) => call("GET /audit", {}, query),
|
|
165
|
+
verify: () => call("GET /audit/verification", {})
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/browser.ts
|
|
171
|
+
var Browser = class {
|
|
172
|
+
origin;
|
|
173
|
+
#jar = /* @__PURE__ */ new Map();
|
|
174
|
+
constructor(origin) {
|
|
175
|
+
this.origin = origin;
|
|
176
|
+
}
|
|
177
|
+
/** Whether coffre left this browser a cookie. */
|
|
178
|
+
get hasCookies() {
|
|
179
|
+
return this.#jar.size > 0;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* A request as the browser makes it, cookies included, redirects not
|
|
183
|
+
* followed. Nothing else is added: a caller playing another site's page
|
|
184
|
+
* sets its own `origin`.
|
|
185
|
+
*/
|
|
186
|
+
async fetch(path, init = {}) {
|
|
187
|
+
const request = typeof path === "string" ? new Request(new URL(path, this.origin), init) : path;
|
|
188
|
+
const ours = new URL(request.url).origin === this.origin;
|
|
189
|
+
if (ours && this.#jar.size > 0) request.headers.set("cookie", [...this.#jar].map(([name, value]) => `${name}=${value}`).join("; "));
|
|
190
|
+
const response = await fetch(request, { redirect: "manual" });
|
|
191
|
+
if (ours) for (const cookie of response.headers.getSetCookie()) {
|
|
192
|
+
const [pair = ""] = cookie.split(";");
|
|
193
|
+
const at = pair.indexOf("=");
|
|
194
|
+
const value = pair.slice(at + 1);
|
|
195
|
+
if (value === "" || /max-age=0/i.test(cookie)) this.#jar.delete(pair.slice(0, at));
|
|
196
|
+
else this.#jar.set(pair.slice(0, at), value);
|
|
197
|
+
}
|
|
198
|
+
return response;
|
|
199
|
+
}
|
|
200
|
+
/** A call one of coffre's pages makes: JSON, from its own origin. */
|
|
201
|
+
send(method, path, body) {
|
|
202
|
+
return this.fetch(path, {
|
|
203
|
+
method,
|
|
204
|
+
headers: {
|
|
205
|
+
origin: this.origin,
|
|
206
|
+
"sec-fetch-site": "same-origin",
|
|
207
|
+
"content-type": "application/json"
|
|
208
|
+
},
|
|
209
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/** The API as this browser's pages call it. */
|
|
213
|
+
client() {
|
|
214
|
+
return createClient({
|
|
215
|
+
url: this.origin,
|
|
216
|
+
transport: (request) => {
|
|
217
|
+
request.headers.set("origin", this.origin);
|
|
218
|
+
request.headers.set("sec-fetch-site", "same-origin");
|
|
219
|
+
return this.fetch(request);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
/** The API as the CLI or a service calls it, with a bearer token. */
|
|
225
|
+
function bearer(origin, token) {
|
|
226
|
+
return createClient({
|
|
227
|
+
url: origin,
|
|
228
|
+
headers: () => ({ authorization: `Bearer ${token}` })
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region src/report.ts
|
|
233
|
+
/** A check that failed: what went wrong, and what was seen instead. */
|
|
234
|
+
var Failure = class extends Error {
|
|
235
|
+
detail;
|
|
236
|
+
constructor(message, detail) {
|
|
237
|
+
super(message);
|
|
238
|
+
this.detail = detail;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
/** A check that cannot run against this deployment, and why. */
|
|
242
|
+
var Skip = class extends Error {};
|
|
243
|
+
function expect(condition, message, detail) {
|
|
244
|
+
if (!condition) throw new Failure(message, detail);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The status coffre turned a call away with. A call that went through
|
|
248
|
+
* fails the check: `what` says what it should not have done.
|
|
249
|
+
*/
|
|
250
|
+
async function refused(what, call) {
|
|
251
|
+
const outcome = await call.then((value) => ({ value }), (error) => ({ error }));
|
|
252
|
+
if ("value" in outcome) throw new Failure(`${what}, and was let`, outcome.value);
|
|
253
|
+
if (!(outcome.error instanceof CoffreError)) throw outcome.error;
|
|
254
|
+
expect(outcome.error.status >= 400 && outcome.error.status < 500, `${what}, and failed with ${outcome.error.status}`, outcome.error.message);
|
|
255
|
+
return outcome.error;
|
|
256
|
+
}
|
|
257
|
+
async function until(what, probe, seconds, alive = () => true) {
|
|
258
|
+
for (let i = 0; i < seconds * 10; i++) {
|
|
259
|
+
if (!alive()) throw new Failure(`a process exited while waiting for ${what}`);
|
|
260
|
+
if (await probe().catch(() => false)) return;
|
|
261
|
+
await setTimeout(100);
|
|
262
|
+
}
|
|
263
|
+
throw new Failure(`timed out waiting for ${what}`);
|
|
264
|
+
}
|
|
265
|
+
var Report = class {
|
|
266
|
+
#results = [];
|
|
267
|
+
#width;
|
|
268
|
+
constructor(width = 19) {
|
|
269
|
+
this.#width = width;
|
|
270
|
+
}
|
|
271
|
+
get failed() {
|
|
272
|
+
return this.#results.filter((result) => result.status === "FAIL").map((result) => result.name);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Run one check, and print what came of it. `needs` holds what earlier
|
|
276
|
+
* checks produced: if any is missing, this one is skipped rather than run
|
|
277
|
+
* against a deployment that is not in the state it expects.
|
|
278
|
+
*/
|
|
279
|
+
async check(name, needs, run) {
|
|
280
|
+
const missing = Object.keys(needs).filter((key) => needs[key] === void 0 || needs[key] === null);
|
|
281
|
+
if (missing.length > 0) {
|
|
282
|
+
this.#print(name, "skip", `no ${missing.join(" or ")}: an earlier check failed`);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
const passed = await run(needs);
|
|
287
|
+
this.#print(name, "ok", typeof passed === "string" ? passed : passed.detail);
|
|
288
|
+
return typeof passed === "string" ? void 0 : passed.value;
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (error instanceof Skip) {
|
|
291
|
+
this.#print(name, "skip", error.message);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const detail = error instanceof Failure ? error.detail : error instanceof Error ? error.stack : error;
|
|
295
|
+
this.#print(name, "FAIL", error instanceof Error ? error.message : String(error), detail);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
#print(name, status, line, detail) {
|
|
300
|
+
this.#results.push({
|
|
301
|
+
name,
|
|
302
|
+
status
|
|
303
|
+
});
|
|
304
|
+
const text = ` ${status.padEnd(5)} ${name.padEnd(this.#width)} ${line}`;
|
|
305
|
+
if (status !== "FAIL") {
|
|
306
|
+
console.log(text);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
console.error(text);
|
|
310
|
+
if (detail !== void 0) {
|
|
311
|
+
const shown = typeof detail === "string" ? detail : inspect(detail, {
|
|
312
|
+
depth: 6,
|
|
313
|
+
breakLength: 100
|
|
314
|
+
});
|
|
315
|
+
console.error(shown.replace(/^/gm, " ".repeat(this.#width + 9)));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/routes.ts
|
|
321
|
+
function getCalls({ places, secrets, members, services }) {
|
|
322
|
+
const first = places[0];
|
|
323
|
+
return {
|
|
324
|
+
"GET /me": [{ params: {} }],
|
|
325
|
+
"GET /projects": [{ params: {} }],
|
|
326
|
+
"GET /secrets/:project/:environment": places.map((params) => ({ params })),
|
|
327
|
+
"GET /secrets/:project/:environment/:key/versions": secrets.map((params) => ({ params })),
|
|
328
|
+
"GET /members": [{ params: {} }, ...first === void 0 ? [] : [{
|
|
329
|
+
params: {},
|
|
330
|
+
input: { path: `${first.project}/${first.environment}` }
|
|
331
|
+
}]],
|
|
332
|
+
"GET /members/:member": members.map((member) => ({ params: { member } })),
|
|
333
|
+
"GET /members/:member/tokens": services.map((member) => ({ params: { member } })),
|
|
334
|
+
"GET /sessions": [{ params: {} }],
|
|
335
|
+
"GET /identities": [{ params: {} }],
|
|
336
|
+
"GET /device-logins/:code": [{ params: { code: "BCDF-GHJK" } }],
|
|
337
|
+
"GET /syncs/providers": [{ params: {} }],
|
|
338
|
+
"GET /syncs/:project/:environment": places.map((params) => ({ params })),
|
|
339
|
+
"GET /audit": [{
|
|
340
|
+
params: {},
|
|
341
|
+
input: { limit: 500 }
|
|
342
|
+
}, {
|
|
343
|
+
params: {},
|
|
344
|
+
input: {
|
|
345
|
+
limit: 500,
|
|
346
|
+
detail: "1"
|
|
347
|
+
}
|
|
348
|
+
}],
|
|
349
|
+
"GET /audit/verification": [{ params: {} }]
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
/** Each GET call as a URL, with its parameters and query filled in. */
|
|
353
|
+
function getUrls(origin, calls) {
|
|
354
|
+
const urls = [];
|
|
355
|
+
for (const [key, asks] of Object.entries(calls)) for (const { params, input } of asks) urls.push({
|
|
356
|
+
key,
|
|
357
|
+
url: address(origin, key, params, input)
|
|
358
|
+
});
|
|
359
|
+
return urls;
|
|
360
|
+
}
|
|
361
|
+
/** Every route, whatever its method. */
|
|
362
|
+
const EVERY_ROUTE = {
|
|
363
|
+
"GET /me": true,
|
|
364
|
+
"GET /projects": true,
|
|
365
|
+
"PUT /projects/:project": true,
|
|
366
|
+
"PATCH /projects/:project": true,
|
|
367
|
+
"PUT /projects/:project/:environment": true,
|
|
368
|
+
"PATCH /projects/:project/:environment": true,
|
|
369
|
+
"GET /secrets/:project/:environment": true,
|
|
370
|
+
"PATCH /secrets/:project/:environment": true,
|
|
371
|
+
"PATCH /secrets/:project/:environment/:key": true,
|
|
372
|
+
"GET /secrets/:project/:environment/:key/versions": true,
|
|
373
|
+
"POST /secrets/:project/:environment/:key/restore": true,
|
|
374
|
+
"POST /reveals": true,
|
|
375
|
+
"GET /members": true,
|
|
376
|
+
"GET /members/:member": true,
|
|
377
|
+
"PUT /members/:member": true,
|
|
378
|
+
"DELETE /members/:member": true,
|
|
379
|
+
"GET /members/:member/tokens": true,
|
|
380
|
+
"POST /members/:member/tokens": true,
|
|
381
|
+
"DELETE /members/:member/tokens/:id": true,
|
|
382
|
+
"PATCH /access/:member": true,
|
|
383
|
+
"GET /sessions": true,
|
|
384
|
+
"DELETE /sessions/:id": true,
|
|
385
|
+
"GET /identities": true,
|
|
386
|
+
"DELETE /identities/:id": true,
|
|
387
|
+
"GET /device-logins/:code": true,
|
|
388
|
+
"POST /device-logins/:code": true,
|
|
389
|
+
"GET /syncs/providers": true,
|
|
390
|
+
"GET /syncs/:project/:environment": true,
|
|
391
|
+
"POST /syncs/:project/:environment": true,
|
|
392
|
+
"PATCH /syncs/by-id/:id": true,
|
|
393
|
+
"DELETE /syncs/by-id/:id": true,
|
|
394
|
+
"POST /syncs/by-id/:id/runs": true,
|
|
395
|
+
"GET /audit": true,
|
|
396
|
+
"GET /audit/verification": true
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* Every route as a method and a URL, each parameter a made-up value: what
|
|
400
|
+
* a caller with no right to any of them would send.
|
|
401
|
+
*/
|
|
402
|
+
function everyRoute(origin) {
|
|
403
|
+
return Object.keys(EVERY_ROUTE).map((key) => {
|
|
404
|
+
const [method, pattern] = key.split(" ");
|
|
405
|
+
return {
|
|
406
|
+
key,
|
|
407
|
+
method,
|
|
408
|
+
url: `${origin}/api${pattern.replace(/:(\w+)/g, (_, name) => encodeURIComponent(name === "member" ? "token:conformance-nobody" : "conformance-nobody"))}`
|
|
409
|
+
};
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
function address(origin, key, params, input) {
|
|
413
|
+
const path = key.slice(4).replace(/:(\w+)/g, (_, name) => encodeURIComponent(params[name]));
|
|
414
|
+
const url = new URL(`${origin}/api${path}`);
|
|
415
|
+
for (const [name, value] of Object.entries(input ?? {})) url.searchParams.set(name, String(value));
|
|
416
|
+
return url.href;
|
|
417
|
+
}
|
|
418
|
+
//#endregion
|
|
419
|
+
//#region src/checks/surface.ts
|
|
420
|
+
async function health(deployment) {
|
|
421
|
+
const { origin } = deployment;
|
|
422
|
+
const live = await fetch(`${origin}/livez`);
|
|
423
|
+
expect(live.ok, `/livez answered ${live.status}`);
|
|
424
|
+
if (deployment.beforeFirstBeat) {
|
|
425
|
+
const early = await fetch(`${origin}/readyz`);
|
|
426
|
+
expect(early.status === 503, `/readyz answered ${early.status} before any heartbeat`, await early.text());
|
|
427
|
+
await deployment.scheduled();
|
|
428
|
+
}
|
|
429
|
+
await until("/readyz", async () => (await fetch(`${origin}/readyz`)).ok, 20);
|
|
430
|
+
const ready = await (await fetch(`${origin}/readyz`)).json();
|
|
431
|
+
expect(ready.checkpointed === true, "/readyz passed without a checkpoint covering the heartbeat", ready);
|
|
432
|
+
return deployment.beforeFirstBeat ? "/livez, and /readyz only once the heartbeat ran and was checkpointed" : "/livez and /readyz";
|
|
433
|
+
}
|
|
434
|
+
/** An instance someone else runs: up, and its scheduled job beating. */
|
|
435
|
+
async function reachable(origin) {
|
|
436
|
+
const live = await fetch(`${origin}/livez`);
|
|
437
|
+
expect(live.ok, `/livez answered ${live.status}`);
|
|
438
|
+
const ready = await fetch(`${origin}/readyz`);
|
|
439
|
+
expect(ready.ok, `/readyz answered ${ready.status}: is the scheduled job running?`, await ready.text());
|
|
440
|
+
return "/livez and /readyz";
|
|
441
|
+
}
|
|
442
|
+
async function headers(origin) {
|
|
443
|
+
const anonymous = await fetch(`${origin}/api/me`);
|
|
444
|
+
expect(anonymous.status === 401, `/api/me answered ${anonymous.status} to no one`);
|
|
445
|
+
securityHeaders(anonymous, "a refusal");
|
|
446
|
+
const login = await fetch(`${origin}/login`);
|
|
447
|
+
expect(login.status === 200, `/login answered ${login.status}`);
|
|
448
|
+
const csp = securityHeaders(login, "/login");
|
|
449
|
+
const html = await login.text();
|
|
450
|
+
const nonce = /'nonce-([^']+)'/.exec(csp)[1];
|
|
451
|
+
expect(html.includes(`nonce="${nonce}"`), "/login's scripts do not carry the policy's nonce");
|
|
452
|
+
expect(!securityHeaders(await fetch(`${origin}/login`), "/login").includes(`'nonce-${nonce}'`), "two pages were served with the same nonce");
|
|
453
|
+
const asset = /\/_coffre\/assets\/[\w.-]+\.js/.exec(html)?.[0];
|
|
454
|
+
expect(asset !== void 0, "/login loads no script from /_coffre/assets/", html.slice(0, 2e3));
|
|
455
|
+
const script = await fetch(`${origin}${asset}`);
|
|
456
|
+
expect(script.ok && /javascript/.test(script.headers.get("content-type") ?? ""), `${asset} answered ${script.status} ${script.headers.get("content-type")}`);
|
|
457
|
+
const allowed = (await fetch(`${origin}/api/me`, { headers: { origin: "https://attacker.example" } })).headers.get("access-control-allow-origin");
|
|
458
|
+
expect(allowed === null, `the API allows another origin to read it: ${allowed}`);
|
|
459
|
+
return `a fresh CSP nonce per page, frames refused, nosniff; no CORS; ${asset} served`;
|
|
460
|
+
}
|
|
461
|
+
function securityHeaders(response, what) {
|
|
462
|
+
const csp = response.headers.get("content-security-policy") ?? "";
|
|
463
|
+
expect(/script-src 'self' 'nonce-[^']+'/.test(csp), `${what}: no nonce-based script-src`, csp);
|
|
464
|
+
expect(csp.includes("frame-ancestors 'none'"), `${what}: frames not refused`, csp);
|
|
465
|
+
expect(response.headers.get("x-content-type-options") === "nosniff", `${what}: no nosniff`);
|
|
466
|
+
expect(response.headers.get("x-frame-options") === "DENY", `${what}: no x-frame-options DENY`);
|
|
467
|
+
expect(response.headers.get("cross-origin-resource-policy") === "same-origin", `${what}: other sites may embed it`);
|
|
468
|
+
expect(/no-store/.test(response.headers.get("cache-control") ?? ""), `${what}: may be cached`);
|
|
469
|
+
if (response.url.startsWith("https:")) {
|
|
470
|
+
const hsts = response.headers.get("strict-transport-security") ?? "";
|
|
471
|
+
expect(/max-age=\d{7,}/.test(hsts), `${what}: no strict-transport-security for months or more`, hsts);
|
|
472
|
+
}
|
|
473
|
+
return csp;
|
|
474
|
+
}
|
|
475
|
+
//#endregion
|
|
476
|
+
//#region src/checks/live.ts
|
|
477
|
+
/** The page anyone may open. Every other page sends no one to /login. */
|
|
478
|
+
const OPEN_PAGES = ["/login"];
|
|
479
|
+
/** The pages behind sign-in, whatever the instance holds. */
|
|
480
|
+
const CLOSED_PAGES = [
|
|
481
|
+
"/",
|
|
482
|
+
"/account",
|
|
483
|
+
"/access",
|
|
484
|
+
"/audit",
|
|
485
|
+
"/auth/device",
|
|
486
|
+
"/settings",
|
|
487
|
+
"/projects",
|
|
488
|
+
"/users",
|
|
489
|
+
"/tokens",
|
|
490
|
+
"/unregistered"
|
|
491
|
+
];
|
|
492
|
+
/** A made-up place and member, which no instance has. */
|
|
493
|
+
const NOWHERE = {
|
|
494
|
+
project: "conformance-nowhere",
|
|
495
|
+
environment: "none"
|
|
496
|
+
};
|
|
497
|
+
const NOBODY = "token:conformance-nobody";
|
|
498
|
+
/** Parse `<project>/<environment>/<KEY>=<value>`, or the path alone with the value given apart. */
|
|
499
|
+
function parseCanary(text, value) {
|
|
500
|
+
const at = text.indexOf("=");
|
|
501
|
+
const path = at === -1 ? text : text.slice(0, at);
|
|
502
|
+
const given = at === -1 ? value : text.slice(at + 1);
|
|
503
|
+
const parts = path.split("/");
|
|
504
|
+
if (parts.length !== 3 || parts.some((part) => part === "")) throw new Error(`the canary is <project>/<environment>/<KEY>, not "${path}"`);
|
|
505
|
+
if (given === void 0 || given === "") throw new Error("the canary has no value: pass it after =, on stdin, or in COFFRE_CONFORMANCE_CANARY");
|
|
506
|
+
const [project, environment, key] = parts;
|
|
507
|
+
return {
|
|
508
|
+
project,
|
|
509
|
+
environment,
|
|
510
|
+
key,
|
|
511
|
+
value: given
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
/** The checks anyone can run: no sign-in, nothing written. `health` false when the caller checked it already. */
|
|
515
|
+
async function anonymousChecks(report, origin, options) {
|
|
516
|
+
if (options.health) await report.check("health", {}, () => reachable(origin));
|
|
517
|
+
await report.check("headers", {}, () => headers(origin));
|
|
518
|
+
await report.check("anonymous api", {}, () => anonymousApi(origin));
|
|
519
|
+
await report.check("forged cross-site", {}, () => forgedCrossSite(origin));
|
|
520
|
+
await report.check("sign-in info", {}, () => signinInfo(origin));
|
|
521
|
+
await report.check("anonymous answers", {}, () => anonymousAnswers(origin));
|
|
522
|
+
}
|
|
523
|
+
/** The checks with a service token that reads the canary, and audits its project. */
|
|
524
|
+
async function tokenChecks(report, origin, setup) {
|
|
525
|
+
const api = setup.token === void 0 ? void 0 : bearer(origin, setup.token);
|
|
526
|
+
const token = await report.check("token", {
|
|
527
|
+
api,
|
|
528
|
+
canary: setup.canary
|
|
529
|
+
}, ({ api, canary }) => whoIsToken(origin, api, canary));
|
|
530
|
+
const needs = {
|
|
531
|
+
api,
|
|
532
|
+
canary: setup.canary,
|
|
533
|
+
token,
|
|
534
|
+
secret: setup.token
|
|
535
|
+
};
|
|
536
|
+
await report.check("token reveal", needs, ({ api, canary, token }) => tokenReveal(api, canary, token));
|
|
537
|
+
await report.check("token scan", needs, ({ canary, secret }) => tokenScan(origin, secret, canary));
|
|
538
|
+
await report.check("token scope", needs, ({ api, canary }) => tokenScope(api, canary));
|
|
539
|
+
await report.check("token verification", { api }, ({ api }) => tokenVerification(api));
|
|
540
|
+
}
|
|
541
|
+
/** Every route, whatever its method, turns away a caller with no credential, and says nothing else. */
|
|
542
|
+
async function anonymousApi(origin) {
|
|
543
|
+
const routes = everyRoute(origin);
|
|
544
|
+
for (const { key, method, url } of routes) {
|
|
545
|
+
const response = await fetch(url, {
|
|
546
|
+
method,
|
|
547
|
+
headers: { "content-type": "application/json" },
|
|
548
|
+
body: method === "GET" ? void 0 : "{}"
|
|
549
|
+
});
|
|
550
|
+
const text = await response.text();
|
|
551
|
+
expect(response.status === 401, `${key} answered ${response.status} to no one`, text);
|
|
552
|
+
expect(onlyRefusal(text), `${key} refused no one with more than a refusal`, text);
|
|
553
|
+
}
|
|
554
|
+
return `${routes.length} routes, every method: 401, and nothing but the refusal`;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* A change sent with a session cookie from another site's page is refused
|
|
558
|
+
* before the cookie is even looked at, so a made-up one shows it: a real
|
|
559
|
+
* cookie would get the same answer. Behind Cloudflare Access the same holds
|
|
560
|
+
* for its cookie and assertion.
|
|
561
|
+
*/
|
|
562
|
+
async function forgedCrossSite(origin) {
|
|
563
|
+
const changes = everyRoute(origin).filter((route) => route.method !== "GET");
|
|
564
|
+
const forged = {
|
|
565
|
+
cookie: "coffre_session=conformance-forged; __Host-coffre_session=conformance-forged; CF_Authorization=conformance-forged",
|
|
566
|
+
"cf-access-jwt-assertion": "conformance-forged",
|
|
567
|
+
origin: "https://attacker.example",
|
|
568
|
+
"sec-fetch-site": "cross-site",
|
|
569
|
+
"content-type": "application/json"
|
|
570
|
+
};
|
|
571
|
+
for (const { key, method, url } of [...changes, {
|
|
572
|
+
key: "POST /auth/signout",
|
|
573
|
+
method: "POST",
|
|
574
|
+
url: `${origin}/auth/signout`
|
|
575
|
+
}]) {
|
|
576
|
+
const response = await fetch(url, {
|
|
577
|
+
method,
|
|
578
|
+
headers: forged,
|
|
579
|
+
body: "{}",
|
|
580
|
+
redirect: "manual"
|
|
581
|
+
});
|
|
582
|
+
const text = await response.text();
|
|
583
|
+
expect(response.status === 403, `${key} from another site answered ${response.status}`, text);
|
|
584
|
+
}
|
|
585
|
+
return `${changes.length + 1} changes, sign-out included, from another site with a session cookie: 403`;
|
|
586
|
+
}
|
|
587
|
+
/** `GET /api/auth` names how to sign in, and nothing more. */
|
|
588
|
+
async function signinInfo(origin) {
|
|
589
|
+
const response = await fetch(`${origin}/api/auth`);
|
|
590
|
+
expect(response.ok, `/api/auth answered ${response.status}`);
|
|
591
|
+
const info = await response.json();
|
|
592
|
+
expect(sameKeys(info, ["access", "signin"]), "/api/auth says more than how to sign in", info);
|
|
593
|
+
expect(info.signin === null !== (info.access === null), "/api/auth names neither or both ways in", info);
|
|
594
|
+
if (info.access !== null) {
|
|
595
|
+
expect(sameKeys(info.access, ["assertion"]) && info.access.assertion === false, "/api/auth says more of Access than whether it vouched", info);
|
|
596
|
+
return "Cloudflare Access, and nothing more";
|
|
597
|
+
}
|
|
598
|
+
const signin = info.signin;
|
|
599
|
+
expect(sameKeys(signin, [
|
|
600
|
+
"note",
|
|
601
|
+
"providers",
|
|
602
|
+
"title"
|
|
603
|
+
]), "/api/auth says more of its sign-in than a page needs", signin);
|
|
604
|
+
for (const provider of signin.providers) expect(sameKeys(provider, [
|
|
605
|
+
"brand",
|
|
606
|
+
"id",
|
|
607
|
+
"label"
|
|
608
|
+
]), `/api/auth says more of ${provider.id} than its button`, provider);
|
|
609
|
+
return `coffre's sign-in through ${signin.providers.map((provider) => provider.id).join(", ")}, and nothing more`;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Nothing that could be a stored value in what no one is shown. Without a
|
|
613
|
+
* canary this is a best effort: the API's refusals are checked to be only
|
|
614
|
+
* refusals above, every closed page must send no one to /login with an
|
|
615
|
+
* empty body, and the open pages must carry no credential coffre issues.
|
|
616
|
+
*/
|
|
617
|
+
async function anonymousAnswers(origin) {
|
|
618
|
+
const pages = [
|
|
619
|
+
...CLOSED_PAGES,
|
|
620
|
+
`/projects/${NOWHERE.project}`,
|
|
621
|
+
`/projects/${NOWHERE.project}/${NOWHERE.environment}`
|
|
622
|
+
];
|
|
623
|
+
for (const path of pages) {
|
|
624
|
+
let at = path;
|
|
625
|
+
for (let hop = 0; at !== "/login"; hop++) {
|
|
626
|
+
const response = await fetch(`${origin}${at}`, { redirect: "manual" });
|
|
627
|
+
const text = await response.text();
|
|
628
|
+
const to = new URL(response.headers.get("location") ?? at, origin);
|
|
629
|
+
expect(hop < 3 && response.status >= 300 && response.status < 400 && to.origin === origin, `${at} answered no one ${response.status}${response.headers.has("location") ? ` to ${to.href}` : ""}, not a redirect on to /login`, text.slice(0, 2e3));
|
|
630
|
+
expect(text.trim().length < 512, `${at} sent no one a body with its redirect`, text.slice(0, 2e3));
|
|
631
|
+
at = to.pathname;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
for (const path of OPEN_PAGES) {
|
|
635
|
+
const response = await fetch(`${origin}${path}`, { redirect: "manual" });
|
|
636
|
+
const text = await response.text();
|
|
637
|
+
expect(response.status === 200, `${path} answered ${response.status}`);
|
|
638
|
+
const issued = /coffre_(svc|cli|web)_[A-Za-z0-9_-]{20,}/.exec(text);
|
|
639
|
+
expect(issued === null, `${path} carries a credential coffre issues`, issued?.[0]);
|
|
640
|
+
}
|
|
641
|
+
return `best effort, without a canary: ${pages.length} closed pages send no one to /login with nothing else; /login carries no credential`;
|
|
642
|
+
}
|
|
643
|
+
/** The token is a service's, and reads the canary's environment. */
|
|
644
|
+
async function whoIsToken(origin, api, canary) {
|
|
645
|
+
if ((await (await fetch(`${origin}/api/auth`)).json()).signin === null) throw new Skip("behind Cloudflare Access, coffre issues no service tokens: the token's checks need coffre's own sign-in");
|
|
646
|
+
const me = await api.me();
|
|
647
|
+
expect(me.principal.type === "service", `the token is ${me.principal.type}:${me.principal.id}'s, not a service's`);
|
|
648
|
+
const member = `token:${me.principal.id}`;
|
|
649
|
+
const place = `${canary.project}/${canary.environment}`;
|
|
650
|
+
expect(me.environments.find((env) => `${env.project}/${env.environment}` === place)?.permissions.includes("secret.read"), `${member} cannot read ${place}`, me.environments);
|
|
651
|
+
const { keys } = await api.secrets.list(place);
|
|
652
|
+
expect(keys.some((key) => key.key === canary.key), `${place} holds no ${canary.key}`, keys.map((key) => key.key));
|
|
653
|
+
return {
|
|
654
|
+
detail: `${member}, reading ${place}`,
|
|
655
|
+
value: { member }
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
/** Reveal the canary once, and find its `secret.read` in the audit log, by the reveal's request. */
|
|
659
|
+
async function tokenReveal(api, canary, token) {
|
|
660
|
+
const path = `${canary.project}/${canary.environment}`;
|
|
661
|
+
const { operationId, values } = await api.secrets.reveal(`${path}/${canary.key}`);
|
|
662
|
+
expect(values[canary.key] === canary.value, `${path}/${canary.key} is not the value given as the canary`);
|
|
663
|
+
let entries;
|
|
664
|
+
try {
|
|
665
|
+
({entries} = await api.audit.list({
|
|
666
|
+
path,
|
|
667
|
+
actor: token.member,
|
|
668
|
+
limit: 50
|
|
669
|
+
}));
|
|
670
|
+
} catch (error) {
|
|
671
|
+
if (error instanceof CoffreError && error.status === 403) throw new Skip(`the token holds no audit.read on ${canary.project}, so the reveal's entry cannot be looked for`);
|
|
672
|
+
throw error;
|
|
673
|
+
}
|
|
674
|
+
const read = entries.filter((entry) => entry.operationId === operationId);
|
|
675
|
+
expect(read.length === 1, `the reveal is logged ${read.length} times, not once`, entries.slice(0, 5));
|
|
676
|
+
const [entry] = read;
|
|
677
|
+
expect(entry.action === "secret.read" && entry.decision === "allow" && entry.key === canary.key && entry.requestId !== null, "the reveal's entry is not an allowed read of the canary, under a request", entry);
|
|
678
|
+
return `one secret.read of ${canary.key}, entry ${entry.seq}, under request ${entry.requestId}`;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* The canary's value, in any answer to a GET route as the token or as no
|
|
682
|
+
* one, or in any page. The reveal is the only answer allowed to carry it.
|
|
683
|
+
*/
|
|
684
|
+
async function tokenScan(origin, token, canary) {
|
|
685
|
+
const me = await bearer(origin, token).me();
|
|
686
|
+
const member = `token:${me.principal.id}`;
|
|
687
|
+
const listed = me.environments.map(({ project, environment }) => ({
|
|
688
|
+
project,
|
|
689
|
+
environment
|
|
690
|
+
}));
|
|
691
|
+
const calls = getCalls({
|
|
692
|
+
places: [...listed, NOWHERE],
|
|
693
|
+
secrets: [{
|
|
694
|
+
project: canary.project,
|
|
695
|
+
environment: canary.environment,
|
|
696
|
+
key: canary.key
|
|
697
|
+
}, {
|
|
698
|
+
...NOWHERE,
|
|
699
|
+
key: "NONE"
|
|
700
|
+
}],
|
|
701
|
+
members: [
|
|
702
|
+
member,
|
|
703
|
+
NOBODY,
|
|
704
|
+
"user:nobody@conformance.example"
|
|
705
|
+
],
|
|
706
|
+
services: [member, NOBODY]
|
|
707
|
+
});
|
|
708
|
+
const forms = [
|
|
709
|
+
canary.value,
|
|
710
|
+
Buffer.from(canary.value).toString("base64"),
|
|
711
|
+
Buffer.from(canary.value).toString("hex")
|
|
712
|
+
];
|
|
713
|
+
const leaks = [];
|
|
714
|
+
const callers = [["the token", { authorization: `Bearer ${token}` }], ["no one", {}]];
|
|
715
|
+
let answers = 0;
|
|
716
|
+
const look = async (what, url, headers) => {
|
|
717
|
+
const text = await (await fetch(url, {
|
|
718
|
+
headers,
|
|
719
|
+
redirect: "manual"
|
|
720
|
+
})).text();
|
|
721
|
+
answers++;
|
|
722
|
+
if (forms.some((form) => text.includes(form))) leaks.push(what);
|
|
723
|
+
};
|
|
724
|
+
for (const { key, url } of getUrls(origin, calls)) for (const [name, headers] of callers) await look(`${key} as ${name}`, url, headers);
|
|
725
|
+
const pages = [
|
|
726
|
+
...OPEN_PAGES,
|
|
727
|
+
...CLOSED_PAGES,
|
|
728
|
+
...listed.flatMap(({ project, environment }) => [`/projects/${project}`, `/projects/${project}/${environment}`])
|
|
729
|
+
];
|
|
730
|
+
for (const path of new Set(pages)) for (const [name, headers] of callers) await look(`the page ${path} as ${name}`, `${origin}${path}`, headers);
|
|
731
|
+
expect(leaks.length === 0, "the canary's value was found outside its reveal", leaks);
|
|
732
|
+
return `${answers} answers from ${Object.keys(calls).length} GET routes and ${new Set(pages).size} pages, as the token and as no one: no value`;
|
|
733
|
+
}
|
|
734
|
+
/** The token reaches the canary's project and nothing else: not another environment, project or member. */
|
|
735
|
+
async function tokenScope(api, canary) {
|
|
736
|
+
const { projects } = await api.projects.list();
|
|
737
|
+
const seen = projects.map((project) => project.slug);
|
|
738
|
+
expect(seen.length === 1 && seen[0] === canary.project, `the token sees ${seen.join(", ") || "no project"}, not only ${canary.project}`);
|
|
739
|
+
const me = await api.me();
|
|
740
|
+
const own = `${canary.project}/${canary.environment}`;
|
|
741
|
+
const reads = me.environments.filter((env) => env.permissions.includes("secret.read")).map((env) => `${env.project}/${env.environment}`);
|
|
742
|
+
expect(reads.length === 1 && reads[0] === own, `the token reads ${reads.join(", ")}, not only ${own}`);
|
|
743
|
+
const more = [...new Set(me.environments.flatMap((env) => env.permissions))].filter((permission) => permission !== "secret.read" && permission !== "audit.read");
|
|
744
|
+
expect(more.length === 0, `the token holds more than reading: ${more.join(", ")}`);
|
|
745
|
+
const others = projects[0].environments.map((env) => `${canary.project}/${env.slug}`).filter((path) => path !== own);
|
|
746
|
+
const nowhere = `${NOWHERE.project}/${NOWHERE.environment}`;
|
|
747
|
+
let refusals = 0;
|
|
748
|
+
for (const path of [...others, nowhere]) {
|
|
749
|
+
await refused(`the token listed ${path}`, api.secrets.list(path));
|
|
750
|
+
await refused(`the token revealed ${path}`, api.secrets.reveal(path));
|
|
751
|
+
await refused(`the token read ${path}'s syncs`, api.syncs.list(path));
|
|
752
|
+
refusals += 3;
|
|
753
|
+
}
|
|
754
|
+
await refused(`the token read ${nowhere}'s audit`, api.audit.list({
|
|
755
|
+
path: nowhere,
|
|
756
|
+
limit: 1
|
|
757
|
+
}));
|
|
758
|
+
await refused("the token listed the members", api.members.list());
|
|
759
|
+
await refused(`the token read ${NOBODY}`, api.members.get(NOBODY));
|
|
760
|
+
refusals += 3;
|
|
761
|
+
try {
|
|
762
|
+
const { entries } = await api.audit.list({ limit: 500 });
|
|
763
|
+
const outside = entries.filter((entry) => entry.project !== null && entry.project !== canary.project);
|
|
764
|
+
expect(outside.length === 0, `the token reads audit entries about ${[...new Set(outside.map((entry) => entry.project))].join(", ")}`);
|
|
765
|
+
} catch (error) {
|
|
766
|
+
if (!(error instanceof CoffreError && error.status === 403)) throw error;
|
|
767
|
+
}
|
|
768
|
+
const elsewhere = others.length === 0 ? "no other environment" : `${others.length} other environment${others.length === 1 ? "" : "s"}`;
|
|
769
|
+
return `only ${own}; ${refusals} reads elsewhere refused: ${elsewhere} of ${canary.project}, a made-up place, the members`;
|
|
770
|
+
}
|
|
771
|
+
async function tokenVerification(api) {
|
|
772
|
+
let verified;
|
|
773
|
+
try {
|
|
774
|
+
verified = await api.audit.verify();
|
|
775
|
+
} catch (error) {
|
|
776
|
+
if (error instanceof CoffreError && error.status === 403) throw new Skip("verification is for owners and root admins, which this token is not: not checked");
|
|
777
|
+
throw error;
|
|
778
|
+
}
|
|
779
|
+
expect(verified.ok, "the audit log does not verify", verified);
|
|
780
|
+
return `${verified.entries} entries verify, through entry ${verified.through}`;
|
|
781
|
+
}
|
|
782
|
+
/** `{ error, message }` and nothing else. */
|
|
783
|
+
function onlyRefusal(text) {
|
|
784
|
+
try {
|
|
785
|
+
return sameKeys(JSON.parse(text), ["error", "message"]);
|
|
786
|
+
} catch {
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
function sameKeys(value, keys) {
|
|
791
|
+
return typeof value === "object" && value !== null && JSON.stringify(Object.keys(value).sort()) === JSON.stringify(keys);
|
|
792
|
+
}
|
|
793
|
+
//#endregion
|
|
794
|
+
//#region src/database.ts
|
|
795
|
+
async function postgres(url) {
|
|
796
|
+
const client = new pg.Client(url);
|
|
797
|
+
await client.connect();
|
|
798
|
+
return {
|
|
799
|
+
engine: "postgres",
|
|
800
|
+
query: async (statement, params = []) => (await client.query(statement, params)).rows,
|
|
801
|
+
exec: async (statements) => void await client.query(statements),
|
|
802
|
+
close: () => client.end()
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
/** A SQLite file, perhaps open in another process: it waits out their locks. */
|
|
806
|
+
function sqlite(file) {
|
|
807
|
+
const db = new DatabaseSync(file, { timeout: 5e3 });
|
|
808
|
+
return {
|
|
809
|
+
engine: "sqlite",
|
|
810
|
+
query: async (statement, params = []) => db.prepare(statement).all(...params),
|
|
811
|
+
exec: async (statements) => db.exec(statements),
|
|
812
|
+
close: async () => db.close()
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
async function using(open, use) {
|
|
816
|
+
const sql = await open;
|
|
817
|
+
try {
|
|
818
|
+
return await use(sql);
|
|
819
|
+
} finally {
|
|
820
|
+
await sql.close();
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
//#endregion
|
|
824
|
+
//#region src/fixtures.ts
|
|
825
|
+
const KEYS = {
|
|
826
|
+
KEK_ID: "conformance-1",
|
|
827
|
+
KEK: "Y29mZnJlLWxvY2FsLWRldi1rZWstMzItYnl0ZXMhISE=",
|
|
828
|
+
SIGNING_KEY: "Y29mZnJlLWxvY2FsLXZhdWx0LXNpZ25pbmctc2VlZCE=",
|
|
829
|
+
AUDIT_CHAIN_KEY: "Y29mZnJlLWxvY2FsLWF1ZGl0LWNoYWluLWtleS0zMmI="
|
|
830
|
+
};
|
|
831
|
+
//#endregion
|
|
832
|
+
//#region src/harness.ts
|
|
833
|
+
const ROOT_ADMIN = "root@conformance.example";
|
|
834
|
+
async function boot(kind, at, options) {
|
|
835
|
+
const { port } = options;
|
|
836
|
+
const dir = resolve(at);
|
|
837
|
+
const origin = `http://127.0.0.1:${port}`;
|
|
838
|
+
for (const taken of [
|
|
839
|
+
port,
|
|
840
|
+
port + 1,
|
|
841
|
+
port + 2
|
|
842
|
+
]) if (await fetch(`http://127.0.0.1:${taken}/`).then(() => true, () => false)) throw new Failure(`port ${taken} is taken; pass --port for a free run of three`);
|
|
843
|
+
if (kind === "workers" && (options.postgres === void 0 || options.runtime === void 0 || options.vaultRuntime === void 0)) throw new Failure("workers run on Postgres: pass --postgres <owner URL>, --runtime <coffre_runtime URL> and --vault-runtime <coffre_vault_runtime URL>");
|
|
844
|
+
const scratch = mkdtempSync(join(tmpdir(), `coffre-conformance-${kind}-`));
|
|
845
|
+
const shell = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("COFFRE_")));
|
|
846
|
+
const children = [];
|
|
847
|
+
const output = (tail = Infinity) => children.map((entry) => `--- ${entry.name}\n${Buffer.concat(entry.output).toString("utf8").slice(-tail)}`).join("\n");
|
|
848
|
+
const logs = () => output(4e3);
|
|
849
|
+
const alive = () => !children.some((entry) => entry.daemon && entry.exited);
|
|
850
|
+
function start(name, command, args, env, daemon = true) {
|
|
851
|
+
const child = spawn(command, args, {
|
|
852
|
+
cwd: dir,
|
|
853
|
+
env: {
|
|
854
|
+
...shell,
|
|
855
|
+
...env
|
|
856
|
+
},
|
|
857
|
+
detached: true,
|
|
858
|
+
stdio: [
|
|
859
|
+
"ignore",
|
|
860
|
+
"pipe",
|
|
861
|
+
"pipe"
|
|
862
|
+
]
|
|
863
|
+
});
|
|
864
|
+
const entry = {
|
|
865
|
+
name,
|
|
866
|
+
child,
|
|
867
|
+
daemon,
|
|
868
|
+
exited: false,
|
|
869
|
+
output: []
|
|
870
|
+
};
|
|
871
|
+
child.stdout.on("data", (chunk) => entry.output.push(chunk));
|
|
872
|
+
child.stderr.on("data", (chunk) => entry.output.push(chunk));
|
|
873
|
+
child.once("exit", () => entry.exited = true);
|
|
874
|
+
child.once("error", (error) => {
|
|
875
|
+
entry.output.push(Buffer.from(`${error.message}\n`));
|
|
876
|
+
entry.exited = true;
|
|
877
|
+
});
|
|
878
|
+
children.push(entry);
|
|
879
|
+
return entry;
|
|
880
|
+
}
|
|
881
|
+
async function run(command, args) {
|
|
882
|
+
const entry = start(args[0] ?? command, command, args, {}, false);
|
|
883
|
+
if (await new Promise((done) => {
|
|
884
|
+
entry.child.once("exit", done);
|
|
885
|
+
entry.child.once("error", done);
|
|
886
|
+
}) !== 0) throw new Failure(`${command} ${args.join(" ")} failed`, Buffer.concat(entry.output).toString("utf8"));
|
|
887
|
+
}
|
|
888
|
+
function bin(name) {
|
|
889
|
+
const path = join(dir, "node_modules/.bin", name);
|
|
890
|
+
if (!existsSync(path)) throw new Failure(`${path} is missing: install ${dir} first`);
|
|
891
|
+
return path;
|
|
892
|
+
}
|
|
893
|
+
const idp = new DevIdp();
|
|
894
|
+
idp.listenPort = port + 1;
|
|
895
|
+
await idp.start();
|
|
896
|
+
const github = {
|
|
897
|
+
GITHUB_CLIENT_ID: "coffre-local",
|
|
898
|
+
GITHUB_CLIENT_SECRET: "coffre-local-secret",
|
|
899
|
+
GITHUB_URL: `${idp.origin}/github`,
|
|
900
|
+
GITHUB_API_URL: `${idp.origin}/github/api`
|
|
901
|
+
};
|
|
902
|
+
let cleanup = async () => {};
|
|
903
|
+
async function stop() {
|
|
904
|
+
for (const signal of ["SIGTERM", "SIGKILL"]) {
|
|
905
|
+
for (const { child, exited } of children) if (!exited) try {
|
|
906
|
+
process.kill(-child.pid, signal);
|
|
907
|
+
} catch {}
|
|
908
|
+
await setTimeout(500);
|
|
909
|
+
}
|
|
910
|
+
await idp.stop().catch(() => {});
|
|
911
|
+
await cleanup().catch(() => {});
|
|
912
|
+
rmSync(scratch, {
|
|
913
|
+
recursive: true,
|
|
914
|
+
force: true
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
try {
|
|
918
|
+
const deployment = kind === "workers" ? await startWorkers() : await startNode();
|
|
919
|
+
await until(`${origin}/livez`, async () => (await fetch(`${origin}/livez`)).ok, 120, alive);
|
|
920
|
+
return deployment;
|
|
921
|
+
} catch (error) {
|
|
922
|
+
const seen = logs();
|
|
923
|
+
await stop();
|
|
924
|
+
if (error instanceof Failure) throw new Failure(error.message, `${error.detail ?? ""}\n${seen}`.trim());
|
|
925
|
+
throw error;
|
|
926
|
+
}
|
|
927
|
+
async function startWorkers() {
|
|
928
|
+
const name = `coffre_conformance_${randomBytes(4).toString("hex")}`;
|
|
929
|
+
const owner = withDatabase(options.postgres, name);
|
|
930
|
+
const runtime = withDatabase(options.runtime, name);
|
|
931
|
+
const vaultRuntime = withDatabase(options.vaultRuntime, name);
|
|
932
|
+
await using(postgres(withDatabase(options.postgres, "postgres")), (sql) => sql.exec(`CREATE DATABASE ${name}`));
|
|
933
|
+
cleanup = () => using(postgres(withDatabase(options.postgres, "postgres")), (sql) => sql.exec(`DROP DATABASE IF EXISTS ${name} WITH (FORCE)`));
|
|
934
|
+
await run(bin("coffre-server"), ["migrate", owner]);
|
|
935
|
+
const state = join(scratch, "state");
|
|
936
|
+
start("wrangler", bin("wrangler"), [
|
|
937
|
+
"dev",
|
|
938
|
+
...[
|
|
939
|
+
"-c",
|
|
940
|
+
"app/wrangler.jsonc",
|
|
941
|
+
"-c",
|
|
942
|
+
"vault/wrangler.jsonc"
|
|
943
|
+
],
|
|
944
|
+
...[
|
|
945
|
+
"--ip",
|
|
946
|
+
"127.0.0.1",
|
|
947
|
+
"--port",
|
|
948
|
+
String(port),
|
|
949
|
+
"--inspector-port",
|
|
950
|
+
String(port + 2)
|
|
951
|
+
],
|
|
952
|
+
...[
|
|
953
|
+
"--persist-to",
|
|
954
|
+
state,
|
|
955
|
+
"--show-interactive-dev-session=false"
|
|
956
|
+
],
|
|
957
|
+
...[
|
|
958
|
+
"--var",
|
|
959
|
+
`GITHUB_URL:${github.GITHUB_URL}`,
|
|
960
|
+
"--var",
|
|
961
|
+
`GITHUB_API_URL:${github.GITHUB_API_URL}`
|
|
962
|
+
]
|
|
963
|
+
], {
|
|
964
|
+
PUBLIC_URL: origin,
|
|
965
|
+
GITHUB_CLIENT_ID: github.GITHUB_CLIENT_ID,
|
|
966
|
+
GITHUB_CLIENT_SECRET: github.GITHUB_CLIENT_SECRET,
|
|
967
|
+
AUDIT_CHAIN_KEY: KEYS.AUDIT_CHAIN_KEY,
|
|
968
|
+
KEK_ID: KEYS.KEK_ID,
|
|
969
|
+
KEK: KEYS.KEK,
|
|
970
|
+
SIGNING_KEY: KEYS.SIGNING_KEY,
|
|
971
|
+
ROOT_ADMINS: ROOT_ADMIN,
|
|
972
|
+
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE: runtime,
|
|
973
|
+
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_VAULT_HYPERDRIVE: vaultRuntime,
|
|
974
|
+
WRANGLER_SEND_METRICS: "false"
|
|
975
|
+
});
|
|
976
|
+
return {
|
|
977
|
+
kind,
|
|
978
|
+
origin,
|
|
979
|
+
idp,
|
|
980
|
+
rootAdmin: ROOT_ADMIN,
|
|
981
|
+
beforeFirstBeat: true,
|
|
982
|
+
async scheduled(options = {}) {
|
|
983
|
+
const response = await fetch(`${origin}/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*`);
|
|
984
|
+
if (!response.ok && !options.allowFailure) throw new Failure(`the scheduled handler answered ${response.status}`, `${await response.text()}\n${logs()}`);
|
|
985
|
+
},
|
|
986
|
+
database: () => postgres(owner),
|
|
987
|
+
runtime: () => postgres(runtime),
|
|
988
|
+
vaultRuntime: () => postgres(vaultRuntime),
|
|
989
|
+
databaseFile: null,
|
|
990
|
+
output,
|
|
991
|
+
stop
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
async function startNode() {
|
|
995
|
+
const database = join(scratch, "coffre.db");
|
|
996
|
+
const socket = join(scratch, "vault.sock");
|
|
997
|
+
await run(bin("coffre-server"), ["migrate", `file:${database}`]);
|
|
998
|
+
start("vault", process.execPath, ["src/vault.ts"], {
|
|
999
|
+
VAULT_SOCKET: socket,
|
|
1000
|
+
DATABASE_URL: `file:${database}`,
|
|
1001
|
+
KEK_ID: KEYS.KEK_ID,
|
|
1002
|
+
KEK: KEYS.KEK,
|
|
1003
|
+
SIGNING_KEY: KEYS.SIGNING_KEY,
|
|
1004
|
+
ROOT_ADMINS: ROOT_ADMIN
|
|
1005
|
+
});
|
|
1006
|
+
await until("the vault socket", async () => existsSync(socket), 30, alive);
|
|
1007
|
+
const startServer = () => start("server", process.execPath, ["src/server.ts"], {
|
|
1008
|
+
PORT: String(port),
|
|
1009
|
+
PUBLIC_URL: origin,
|
|
1010
|
+
DATABASE_URL: `file:${database}`,
|
|
1011
|
+
VAULT_SOCKET: socket,
|
|
1012
|
+
AUDIT_CHAIN_KEY: KEYS.AUDIT_CHAIN_KEY,
|
|
1013
|
+
...github
|
|
1014
|
+
});
|
|
1015
|
+
let server = startServer();
|
|
1016
|
+
return {
|
|
1017
|
+
kind,
|
|
1018
|
+
origin,
|
|
1019
|
+
idp,
|
|
1020
|
+
rootAdmin: ROOT_ADMIN,
|
|
1021
|
+
beforeFirstBeat: false,
|
|
1022
|
+
async scheduled() {
|
|
1023
|
+
const exited = new Promise((done) => server.child.once("exit", done));
|
|
1024
|
+
server.daemon = false;
|
|
1025
|
+
process.kill(-server.child.pid, "SIGTERM");
|
|
1026
|
+
await exited;
|
|
1027
|
+
server = startServer();
|
|
1028
|
+
await until("the restarted server", async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
|
|
1029
|
+
},
|
|
1030
|
+
database: async () => sqlite(database),
|
|
1031
|
+
runtime: null,
|
|
1032
|
+
vaultRuntime: null,
|
|
1033
|
+
databaseFile: database,
|
|
1034
|
+
output,
|
|
1035
|
+
stop
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
function withDatabase(url, database) {
|
|
1040
|
+
const parsed = new URL(url);
|
|
1041
|
+
parsed.pathname = `/${database}`;
|
|
1042
|
+
return parsed.href;
|
|
1043
|
+
}
|
|
1044
|
+
//#endregion
|
|
1045
|
+
//#region src/checks/people.ts
|
|
1046
|
+
const PROJECT = "conformance";
|
|
1047
|
+
const DEV = `${PROJECT}/dev`;
|
|
1048
|
+
const PROD = `${PROJECT}/prod`;
|
|
1049
|
+
const BULK = `${PROJECT}/bulk`;
|
|
1050
|
+
const SERVICE = "token:conformance-ci";
|
|
1051
|
+
/** Where the live checks' token reads, as an operator would set it up. */
|
|
1052
|
+
const LIVE = `${PROJECT}/live`;
|
|
1053
|
+
const LIVE_SERVICE = "token:conformance-live";
|
|
1054
|
+
function canary() {
|
|
1055
|
+
return `coffre-canary-${randomBytes(12).toString("hex")}`;
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Sign in the way a browser does: to the stand-in GitHub, which asks for no
|
|
1059
|
+
* password, and back. The error is the one coffre sent the browser back to
|
|
1060
|
+
* /login with.
|
|
1061
|
+
*/
|
|
1062
|
+
async function signIn(deployment, browser, email) {
|
|
1063
|
+
const leave = await browser.fetch("/auth/signin/github");
|
|
1064
|
+
const authorize = new URL(leave.headers.get("location") ?? "", deployment.origin);
|
|
1065
|
+
expect(leave.status === 302 && authorize.origin === deployment.idp.origin, "sign-in did not send the browser to GitHub", `${leave.status} ${leave.headers.get("location")}`);
|
|
1066
|
+
const form = new URLSearchParams(authorize.searchParams);
|
|
1067
|
+
form.set("email", email);
|
|
1068
|
+
const approve = await fetch(authorize.origin + authorize.pathname, {
|
|
1069
|
+
method: "POST",
|
|
1070
|
+
body: form,
|
|
1071
|
+
redirect: "manual"
|
|
1072
|
+
});
|
|
1073
|
+
const callback = approve.headers.get("location") ?? "";
|
|
1074
|
+
expect(approve.status === 302 && callback.startsWith(`${deployment.origin}/auth/callback/github?`), "GitHub did not send the browser back", callback);
|
|
1075
|
+
const back = await browser.fetch(callback);
|
|
1076
|
+
expect(back.status === 302 || back.status === 303, `the callback answered ${back.status}`, await back.text());
|
|
1077
|
+
const error = new URL(back.headers.get("location") ?? "/", deployment.origin).searchParams.get("error");
|
|
1078
|
+
return error === null ? { ok: true } : {
|
|
1079
|
+
ok: false,
|
|
1080
|
+
error
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
async function signInAdmin(deployment) {
|
|
1084
|
+
const browser = new Browser(deployment.origin);
|
|
1085
|
+
const signed = await signIn(deployment, browser, deployment.rootAdmin);
|
|
1086
|
+
expect(signed.ok, `the root admin was refused: ${"error" in signed ? signed.error : ""}`);
|
|
1087
|
+
const api = browser.client();
|
|
1088
|
+
const me = await api.me();
|
|
1089
|
+
expect(me.principal.id === deployment.rootAdmin && me.isRootAdmin, "signed in as someone else", me);
|
|
1090
|
+
const admin = {
|
|
1091
|
+
email: deployment.rootAdmin,
|
|
1092
|
+
member: `user:${deployment.rootAdmin}`,
|
|
1093
|
+
browser,
|
|
1094
|
+
api
|
|
1095
|
+
};
|
|
1096
|
+
return {
|
|
1097
|
+
detail: `${admin.email}, root admin, through GitHub`,
|
|
1098
|
+
value: admin
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
async function setUp(admin) {
|
|
1102
|
+
await admin.api.projects.create(PROJECT, { name: "Conformance" });
|
|
1103
|
+
for (const [path, name] of [
|
|
1104
|
+
[DEV, "Development"],
|
|
1105
|
+
[PROD, "Production"],
|
|
1106
|
+
[BULK, "Bulk"]
|
|
1107
|
+
]) await admin.api.environments.create(path, { name });
|
|
1108
|
+
const canaries = {
|
|
1109
|
+
[`${DEV}/API_KEY`]: canary(),
|
|
1110
|
+
[`${DEV}/DATABASE_URL`]: canary(),
|
|
1111
|
+
[`${PROD}/API_KEY`]: canary()
|
|
1112
|
+
};
|
|
1113
|
+
for (const path of [DEV, PROD]) await admin.api.secrets.set(path, valuesIn(canaries, path));
|
|
1114
|
+
return {
|
|
1115
|
+
detail: `${PROJECT}: dev and prod, ${Object.keys(canaries).length} values, and bulk`,
|
|
1116
|
+
value: canaries
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* What `probe --token` asks an operator for: an environment holding one
|
|
1121
|
+
* canary, and a service that reads it there and audits the project. Its
|
|
1122
|
+
* value joins the others, for the scans that follow.
|
|
1123
|
+
*/
|
|
1124
|
+
async function setUpLive(admin, canaries) {
|
|
1125
|
+
await admin.api.environments.create(LIVE, { name: "Live" });
|
|
1126
|
+
const value = canary();
|
|
1127
|
+
await admin.api.secrets.set(LIVE, { CANARY: value });
|
|
1128
|
+
canaries[`${LIVE}/CANARY`] = value;
|
|
1129
|
+
await admin.api.members.add(LIVE_SERVICE);
|
|
1130
|
+
await admin.api.access.set(LIVE_SERVICE, {
|
|
1131
|
+
[LIVE]: "viewer",
|
|
1132
|
+
[PROJECT]: "auditor"
|
|
1133
|
+
});
|
|
1134
|
+
const { token } = await admin.api.tokens.issue(LIVE_SERVICE, {
|
|
1135
|
+
label: "conformance live",
|
|
1136
|
+
expiresInDays: 1
|
|
1137
|
+
});
|
|
1138
|
+
return {
|
|
1139
|
+
detail: `${LIVE}/CANARY, and ${LIVE_SERVICE}: viewer there, auditor on ${PROJECT}`,
|
|
1140
|
+
value: {
|
|
1141
|
+
token,
|
|
1142
|
+
canary: {
|
|
1143
|
+
project: PROJECT,
|
|
1144
|
+
environment: "live",
|
|
1145
|
+
key: "CANARY",
|
|
1146
|
+
value
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
/** The canaries in one environment, by key. */
|
|
1152
|
+
function valuesIn(canaries, environment) {
|
|
1153
|
+
return Object.fromEntries(Object.entries(canaries).filter(([path]) => path.startsWith(`${environment}/`)).map(([path, value]) => [path.slice(environment.length + 1), value]));
|
|
1154
|
+
}
|
|
1155
|
+
async function personas(deployment, admin) {
|
|
1156
|
+
async function person(name, access) {
|
|
1157
|
+
const email = `${name}@conformance.example`;
|
|
1158
|
+
const member = `user:${email}`;
|
|
1159
|
+
await admin.api.members.add(member);
|
|
1160
|
+
await admin.api.access.set(member, access);
|
|
1161
|
+
const browser = new Browser(deployment.origin);
|
|
1162
|
+
const signed = await signIn(deployment, browser, email);
|
|
1163
|
+
expect(signed.ok, `${email} was refused: ${"error" in signed ? signed.error : ""}`);
|
|
1164
|
+
return {
|
|
1165
|
+
email,
|
|
1166
|
+
member,
|
|
1167
|
+
browser,
|
|
1168
|
+
api: browser.client()
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
const reader = await person("reader", { [DEV]: "viewer" });
|
|
1172
|
+
const leaver = await person("leaver", { [DEV]: "developer" });
|
|
1173
|
+
const bulk = await person("bulk", { [BULK]: "viewer" });
|
|
1174
|
+
await admin.api.members.add(SERVICE);
|
|
1175
|
+
await admin.api.access.set(SERVICE, { [DEV]: "viewer" });
|
|
1176
|
+
const issued = await admin.api.tokens.issue(SERVICE, {
|
|
1177
|
+
label: "conformance",
|
|
1178
|
+
expiresInDays: 1
|
|
1179
|
+
});
|
|
1180
|
+
const cliToken = await deviceLogin(deployment, leaver.browser);
|
|
1181
|
+
return {
|
|
1182
|
+
detail: "a reader, a leaver in a browser and the CLI, a bulk reader, a service with a token, and a stranger",
|
|
1183
|
+
value: {
|
|
1184
|
+
admin,
|
|
1185
|
+
reader,
|
|
1186
|
+
leaver: {
|
|
1187
|
+
...leaver,
|
|
1188
|
+
cli: bearer(deployment.origin, cliToken),
|
|
1189
|
+
cliToken
|
|
1190
|
+
},
|
|
1191
|
+
service: {
|
|
1192
|
+
member: SERVICE,
|
|
1193
|
+
token: issued.token,
|
|
1194
|
+
api: bearer(deployment.origin, issued.token)
|
|
1195
|
+
},
|
|
1196
|
+
stranger: {
|
|
1197
|
+
email: "stranger@conformance.example",
|
|
1198
|
+
browser: new Browser(deployment.origin)
|
|
1199
|
+
},
|
|
1200
|
+
bulk
|
|
1201
|
+
}
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
/** `coffre login`: a device code, approved in a signed-in browser, for a CLI session. */
|
|
1205
|
+
async function deviceLogin(deployment, browser) {
|
|
1206
|
+
const json = { "content-type": "application/json" };
|
|
1207
|
+
const started = await fetch(`${deployment.origin}/api/auth/device`, {
|
|
1208
|
+
method: "POST",
|
|
1209
|
+
headers: json,
|
|
1210
|
+
body: JSON.stringify({ client_label: "coffre-conformance" })
|
|
1211
|
+
});
|
|
1212
|
+
expect(started.ok, `the device login did not start: ${started.status}`, await started.clone().text());
|
|
1213
|
+
const device = await started.json();
|
|
1214
|
+
await browser.client().deviceLogins.decide(device.user_code, true);
|
|
1215
|
+
const polled = await fetch(`${deployment.origin}/api/auth/device/token`, {
|
|
1216
|
+
method: "POST",
|
|
1217
|
+
headers: json,
|
|
1218
|
+
body: JSON.stringify({ device_code: device.device_code })
|
|
1219
|
+
});
|
|
1220
|
+
const session = await polled.json();
|
|
1221
|
+
expect(polled.ok && session.access_token, `the approved device login gave no token: ${polled.status}`, session);
|
|
1222
|
+
return session.access_token;
|
|
1223
|
+
}
|
|
1224
|
+
//#endregion
|
|
1225
|
+
//#region src/checks/storage.ts
|
|
1226
|
+
const TABLES = [
|
|
1227
|
+
"projects",
|
|
1228
|
+
"environments",
|
|
1229
|
+
"secrets",
|
|
1230
|
+
"secret_versions",
|
|
1231
|
+
"vault_members",
|
|
1232
|
+
"vault_grants",
|
|
1233
|
+
"audit_log",
|
|
1234
|
+
"audit_chain_head",
|
|
1235
|
+
"identities",
|
|
1236
|
+
"credentials",
|
|
1237
|
+
"device_authorizations",
|
|
1238
|
+
"syncs",
|
|
1239
|
+
"sync_keys"
|
|
1240
|
+
];
|
|
1241
|
+
/** Inspect every table, including any a deployment added. Missing shared tables fail. */
|
|
1242
|
+
async function tables(sql) {
|
|
1243
|
+
const names = (await sql.query(sql.engine === "postgres" ? `SELECT table_name AS name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'` : `SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`)).map((row) => row.name);
|
|
1244
|
+
const missing = TABLES.filter((name) => !names.includes(name));
|
|
1245
|
+
expect(missing.length === 0, "shared tables could not be inspected", missing);
|
|
1246
|
+
return names;
|
|
1247
|
+
}
|
|
1248
|
+
function quoted(name) {
|
|
1249
|
+
return `"${name.replaceAll("\"", "\"\"")}"`;
|
|
1250
|
+
}
|
|
1251
|
+
/** Both drivers return binary columns as bytes; casting records to text hides bytea. */
|
|
1252
|
+
async function scanTables(sql, names, look) {
|
|
1253
|
+
for (const name of names) for (const row of await sql.query(`SELECT * FROM ${quoted(name)}`)) for (const value of Object.values(row)) look(`the database's ${name} table`, value instanceof Uint8Array ? value : JSON.stringify(value) ?? "");
|
|
1254
|
+
}
|
|
1255
|
+
/** Parameters use one spelling in the checks, whatever the database. */
|
|
1256
|
+
function query(sql, statement, params = []) {
|
|
1257
|
+
return sql.query(sql.engine === "sqlite" ? statement.replace(/\$\d+/g, "?") : statement, params);
|
|
1258
|
+
}
|
|
1259
|
+
/** Insert a saved or forged row as it is, but for its generated columns. */
|
|
1260
|
+
async function insertRow(sql, table, row) {
|
|
1261
|
+
const fields = Object.keys(row).filter((field) => field !== "active_subject");
|
|
1262
|
+
await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(", ")}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(", ")})`, fields.map((field) => row[field]));
|
|
1263
|
+
}
|
|
1264
|
+
/** Put a saved row back without changing its bytes or its generated columns. */
|
|
1265
|
+
async function restoreRow(sql, table, row, key) {
|
|
1266
|
+
const fields = Object.keys(row).filter((field) => field !== key && field !== "active_subject");
|
|
1267
|
+
await query(sql, `UPDATE ${quoted(table)} SET ${fields.map((field, i) => `${quoted(field)} = $${i + 1}`).join(", ")} WHERE ${quoted(key)} = $${fields.length + 1}`, [...fields.map((field) => row[field]), row[key]]);
|
|
1268
|
+
}
|
|
1269
|
+
/** Keep a deliberate rewrite, and its restoration, invisible until each is complete. */
|
|
1270
|
+
async function transaction(sql, work) {
|
|
1271
|
+
await sql.exec("BEGIN");
|
|
1272
|
+
try {
|
|
1273
|
+
const result = await work();
|
|
1274
|
+
await sql.exec("COMMIT");
|
|
1275
|
+
return result;
|
|
1276
|
+
} catch (error) {
|
|
1277
|
+
await sql.exec("ROLLBACK");
|
|
1278
|
+
throw error;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* `work` with the audit log's append-only triggers lifted, as only its owner
|
|
1283
|
+
* can: Postgres disables them for the session, SQLite drops them and makes
|
|
1284
|
+
* them again after.
|
|
1285
|
+
*/
|
|
1286
|
+
async function appendOnlyLifted(sql, work) {
|
|
1287
|
+
if (sql.engine === "postgres") {
|
|
1288
|
+
await sql.exec("ALTER TABLE audit_log DISABLE TRIGGER USER");
|
|
1289
|
+
try {
|
|
1290
|
+
return await work();
|
|
1291
|
+
} finally {
|
|
1292
|
+
await sql.exec("ALTER TABLE audit_log ENABLE TRIGGER USER");
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
const triggers = await sql.query(`SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'audit_log'`);
|
|
1296
|
+
for (const trigger of triggers) await sql.exec(`DROP TRIGGER ${trigger.name}`);
|
|
1297
|
+
try {
|
|
1298
|
+
return await work();
|
|
1299
|
+
} finally {
|
|
1300
|
+
for (const trigger of triggers) await sql.exec(trigger.sql);
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
const LOG_REFUSAL = "coffre-conformance: the log refuses writes";
|
|
1304
|
+
/**
|
|
1305
|
+
* `work` with the audit log refusing appends, of one action or all, as a
|
|
1306
|
+
* full disk or a lost connection would, without changing the deployment's
|
|
1307
|
+
* code: a trigger, dropped after.
|
|
1308
|
+
*/
|
|
1309
|
+
async function logRefuses(sql, work, action) {
|
|
1310
|
+
const when = action === void 0 ? "" : ` WHEN (NEW.action = '${action}')`;
|
|
1311
|
+
const create = sql.engine === "postgres" ? `CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
1312
|
+
BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
|
|
1313
|
+
CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();` : `CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log${when}
|
|
1314
|
+
BEGIN SELECT RAISE(ABORT, '${LOG_REFUSAL}'); END;`;
|
|
1315
|
+
await sql.exec(create);
|
|
1316
|
+
try {
|
|
1317
|
+
return await work();
|
|
1318
|
+
} finally {
|
|
1319
|
+
await sql.exec(sql.engine === "postgres" ? "DROP TRIGGER conformance_refuse ON audit_log; DROP FUNCTION conformance_refuse();" : "DROP TRIGGER conformance_refuse;");
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
/** The log's fields in the order its format (`coffre.audit.v2`) encodes them. */
|
|
1323
|
+
const ENTRY_FIELDS = [
|
|
1324
|
+
"seq",
|
|
1325
|
+
"author",
|
|
1326
|
+
"key_id",
|
|
1327
|
+
"occurred_at",
|
|
1328
|
+
"actor",
|
|
1329
|
+
"action",
|
|
1330
|
+
"decision",
|
|
1331
|
+
"code",
|
|
1332
|
+
"subject_principal",
|
|
1333
|
+
"project_id",
|
|
1334
|
+
"environment_id",
|
|
1335
|
+
"secret_id",
|
|
1336
|
+
"secret_version_id",
|
|
1337
|
+
"operation_id",
|
|
1338
|
+
"request_id",
|
|
1339
|
+
"source_ip",
|
|
1340
|
+
"related_seq",
|
|
1341
|
+
"metadata"
|
|
1342
|
+
];
|
|
1343
|
+
/**
|
|
1344
|
+
* An entry's fields as its MAC and hash cover them, written here from the
|
|
1345
|
+
* format rather than taken from coffre, so a check does not trust the code
|
|
1346
|
+
* it checks: each field length-prefixed in order, null as length -1.
|
|
1347
|
+
*/
|
|
1348
|
+
function entryFields(row) {
|
|
1349
|
+
return Buffer.concat(ENTRY_FIELDS.flatMap((field) => {
|
|
1350
|
+
const header = Buffer.alloc(4);
|
|
1351
|
+
const value = row[field];
|
|
1352
|
+
if (value === null || value === void 0) {
|
|
1353
|
+
header.writeInt32BE(-1);
|
|
1354
|
+
return [header];
|
|
1355
|
+
}
|
|
1356
|
+
const bytes = Buffer.from(String(value));
|
|
1357
|
+
header.writeInt32BE(bytes.length);
|
|
1358
|
+
return [header, bytes];
|
|
1359
|
+
}));
|
|
1360
|
+
}
|
|
1361
|
+
//#endregion
|
|
1362
|
+
//#region src/checks/members.ts
|
|
1363
|
+
async function member(deployment, { admin }, name) {
|
|
1364
|
+
const email = `${name}@conformance.example`;
|
|
1365
|
+
const principal = `user:${email}`;
|
|
1366
|
+
await admin.api.members.add(principal);
|
|
1367
|
+
await admin.api.access.set(principal, { [DEV]: "viewer" });
|
|
1368
|
+
const browser = new Browser(deployment.origin);
|
|
1369
|
+
expect((await signIn(deployment, browser, email)).ok, "the fixture member could not sign in");
|
|
1370
|
+
return {
|
|
1371
|
+
email,
|
|
1372
|
+
member: principal,
|
|
1373
|
+
browser,
|
|
1374
|
+
api: browser.client()
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
/** The row is refused at use, not only when someone explicitly verifies the log. */
|
|
1378
|
+
async function memberTampering(deployment, people, kind) {
|
|
1379
|
+
const person = await member(deployment, people, `tamper-${kind}`);
|
|
1380
|
+
const { admin } = people;
|
|
1381
|
+
const path = kind === "grant" ? PROD : DEV;
|
|
1382
|
+
const protectedValues = Object.values((await admin.api.secrets.reveal(path)).values);
|
|
1383
|
+
await using(deployment.database(), async (sql) => {
|
|
1384
|
+
const [before] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
|
|
1385
|
+
const grants = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
|
|
1386
|
+
expect(before !== void 0 && grants.length === 1, "the fixture has no member row and grant");
|
|
1387
|
+
let restored = before;
|
|
1388
|
+
let held = grants;
|
|
1389
|
+
if (kind === "stale") {
|
|
1390
|
+
await admin.api.access.set(person.member, { [DEV]: "developer" });
|
|
1391
|
+
[restored] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
|
|
1392
|
+
held = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
|
|
1393
|
+
await restoreRow(sql, "vault_members", before, "principal");
|
|
1394
|
+
await query(sql, "UPDATE vault_grants SET role = $1, granted_at = $2, granted_by = $3 WHERE principal = $4", [
|
|
1395
|
+
grants[0].role,
|
|
1396
|
+
grants[0].granted_at,
|
|
1397
|
+
grants[0].granted_by,
|
|
1398
|
+
person.member
|
|
1399
|
+
]);
|
|
1400
|
+
} else if (kind === "member") await query(sql, "UPDATE vault_members SET owner = $1 WHERE principal = $2", [sql.engine === "sqlite" ? 1 : true, person.member]);
|
|
1401
|
+
else {
|
|
1402
|
+
const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
|
|
1403
|
+
WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
|
|
1404
|
+
expect(place !== void 0, "no prod environment to forge a grant on");
|
|
1405
|
+
await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by) VALUES ($1, $2, 'viewer', $3, $4)`, [
|
|
1406
|
+
person.member,
|
|
1407
|
+
place.id,
|
|
1408
|
+
Date.now(),
|
|
1409
|
+
person.member
|
|
1410
|
+
]);
|
|
1411
|
+
}
|
|
1412
|
+
try {
|
|
1413
|
+
const response = await person.browser.send("POST", "/api/reveals", { path });
|
|
1414
|
+
const text = await response.text();
|
|
1415
|
+
expect(response.status === 401 && JSON.parse(text).error === "unauthenticated", "the tampered credential was not refused", text);
|
|
1416
|
+
expect(!protectedValues.some((value) => text.includes(value)), "the tampered refusal carried a value", text);
|
|
1417
|
+
const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
|
|
1418
|
+
AND subject_principal = $1 AND code = $2`, [person.member, kind === "stale" ? "stale" : "mac"]);
|
|
1419
|
+
expect(reports.length > 0, "the tampering was not logged at use", reports);
|
|
1420
|
+
expect((await admin.api.members.get(person.member)).status === "tampered", "the refused member is not marked tampered");
|
|
1421
|
+
const verified = await admin.api.audit.verify();
|
|
1422
|
+
expect(!verified.ok && verified.author === "vault" && verified.failedAtSeq === null, "the tampered member verifies", verified);
|
|
1423
|
+
} finally {
|
|
1424
|
+
await restoreRow(sql, "vault_members", restored, "principal");
|
|
1425
|
+
await query(sql, "DELETE FROM vault_grants WHERE principal = $1", [person.member]);
|
|
1426
|
+
for (const grant of held) await insertRow(sql, "vault_grants", grant);
|
|
1427
|
+
}
|
|
1428
|
+
});
|
|
1429
|
+
expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "restoring the member did not restore legitimate use");
|
|
1430
|
+
expect((await admin.api.audit.verify()).ok, "the restored member did not verify");
|
|
1431
|
+
return `${kind === "stale" ? "a genuine older member row, put back," : `a forged ${kind}`} refused at use as tampered, logged, then restored`;
|
|
1432
|
+
}
|
|
1433
|
+
/** Access changes have one author, the vault that commits the changed rows. */
|
|
1434
|
+
async function accessAuthorship(deployment, people) {
|
|
1435
|
+
const person = await member(deployment, people, "access-actions");
|
|
1436
|
+
const { admin } = people;
|
|
1437
|
+
await admin.api.access.set(person.member, { [DEV]: null });
|
|
1438
|
+
await admin.api.members.remove(person.member);
|
|
1439
|
+
await admin.api.members.add(person.member);
|
|
1440
|
+
await using(deployment.database(), async (sql) => {
|
|
1441
|
+
const access = await sql.query(`SELECT author, action, subject_principal FROM audit_log
|
|
1442
|
+
WHERE decision = 'allow' AND (action LIKE 'access.%' OR action LIKE 'member.%' OR action LIKE 'directory.%' OR action LIKE 'grant.%' OR action LIKE 'principal.%')`);
|
|
1443
|
+
expect(access.length > 0 && access.every((entry) => entry.author === "vault"), "an access change was written by the app", access.filter((entry) => entry.author !== "vault"));
|
|
1444
|
+
const actions = access.filter((entry) => entry.subject_principal === person.member).map((entry) => entry.action);
|
|
1445
|
+
for (const action of [
|
|
1446
|
+
"member.add",
|
|
1447
|
+
"member.remove",
|
|
1448
|
+
"member.restore",
|
|
1449
|
+
"access.grant",
|
|
1450
|
+
"access.revoke"
|
|
1451
|
+
]) expect(actions.includes(action), `${action} left no vault entry`, actions);
|
|
1452
|
+
});
|
|
1453
|
+
return "admission, grant, revoke, removal and re-admission are only vault entries";
|
|
1454
|
+
}
|
|
1455
|
+
/** A failure to append may not commit an access change. */
|
|
1456
|
+
async function noAuditNoAccess(deployment, people) {
|
|
1457
|
+
const person = await member(deployment, people, "no-audit");
|
|
1458
|
+
const { admin } = people;
|
|
1459
|
+
await using(deployment.database(), async (sql) => {
|
|
1460
|
+
const snapshot = async () => ({
|
|
1461
|
+
members: await sql.query("SELECT * FROM vault_members ORDER BY principal"),
|
|
1462
|
+
grants: await sql.query("SELECT * FROM vault_grants ORDER BY principal, project_id, environment_id")
|
|
1463
|
+
});
|
|
1464
|
+
const before = await snapshot();
|
|
1465
|
+
await logRefuses(sql, async () => {
|
|
1466
|
+
for (const change of [
|
|
1467
|
+
() => admin.api.access.set(person.member, { [DEV]: "developer" }),
|
|
1468
|
+
() => admin.api.members.remove(person.member),
|
|
1469
|
+
() => admin.api.members.add("user:no-audit-new@conformance.example")
|
|
1470
|
+
]) {
|
|
1471
|
+
expect(!await change().then(() => true, () => false), "an access change succeeded while the log refused writes");
|
|
1472
|
+
expect(JSON.stringify(await snapshot()) === JSON.stringify(before), "an access change committed without its entry");
|
|
1473
|
+
}
|
|
1474
|
+
});
|
|
1475
|
+
});
|
|
1476
|
+
expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "legitimate reads did not recover");
|
|
1477
|
+
return "grant, removal and admission commit nothing while the log refuses their entries";
|
|
1478
|
+
}
|
|
1479
|
+
/** Hold the head after the vault checked a member, then forge a grant before it seals them. */
|
|
1480
|
+
async function sealingRace(deployment, people) {
|
|
1481
|
+
if (deployment.vaultRuntime === null) throw new Skip("SQLite prevents a write beside a decision; the vault suite covers review R2 on Postgres");
|
|
1482
|
+
const [login] = await using(deployment.vaultRuntime(), (sql) => sql.query("SELECT current_user AS name"));
|
|
1483
|
+
const person = await member(deployment, people, "seal-race");
|
|
1484
|
+
const { admin } = people;
|
|
1485
|
+
try {
|
|
1486
|
+
await using(deployment.database(), (holder) => using(deployment.database(), async (sql) => {
|
|
1487
|
+
const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
|
|
1488
|
+
WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
|
|
1489
|
+
const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
|
|
1490
|
+
const [owner] = await holder.query("SELECT pg_backend_pid() AS pid");
|
|
1491
|
+
expect(place !== void 0, "no prod environment for the sealing race");
|
|
1492
|
+
await holder.exec("BEGIN");
|
|
1493
|
+
let pending;
|
|
1494
|
+
try {
|
|
1495
|
+
await holder.query("SELECT 1 FROM audit_chain_head FOR UPDATE");
|
|
1496
|
+
pending = admin.api.access.set(person.member, { [DEV]: "developer" }).then((value) => ({ value }), (error) => ({ error }));
|
|
1497
|
+
await until("the vault decision waiting on our head", async () => {
|
|
1498
|
+
return (await query(sql, `SELECT pid FROM pg_stat_activity WHERE datname = current_database()
|
|
1499
|
+
AND usename = $1 AND $2 = ANY(pg_blocking_pids(pid))`, [login.name, owner.pid])).length > 0;
|
|
1500
|
+
}, 10);
|
|
1501
|
+
await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by)
|
|
1502
|
+
VALUES ($1, $2, 'viewer', $3, $4)`, [
|
|
1503
|
+
person.member,
|
|
1504
|
+
place.id,
|
|
1505
|
+
Date.now(),
|
|
1506
|
+
person.member
|
|
1507
|
+
]);
|
|
1508
|
+
} finally {
|
|
1509
|
+
await holder.exec("ROLLBACK");
|
|
1510
|
+
if (pending !== void 0) await pending;
|
|
1511
|
+
}
|
|
1512
|
+
expect(pending !== void 0, "the access decision was not started");
|
|
1513
|
+
const result = await pending;
|
|
1514
|
+
expect("error" in result && result.error instanceof CoffreError && result.error.status === 403 && result.error.code === "vault_refused" && result.error.reason === "tampered", "a grant inserted during the decision was not refused as tampered", result);
|
|
1515
|
+
const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
|
|
1516
|
+
AND subject_principal = $1 AND code = 'mac' AND seq >= $2`, [person.member, head.next_seq]);
|
|
1517
|
+
expect(reports.length > 0, "the grant inserted during the decision was not reported", reports);
|
|
1518
|
+
const response = await person.browser.send("POST", "/api/reveals", { path: PROD });
|
|
1519
|
+
const body = await response.json();
|
|
1520
|
+
expect(response.status === 401 && body.error === "unauthenticated", "the forged grant let its member read", body);
|
|
1521
|
+
}));
|
|
1522
|
+
} finally {
|
|
1523
|
+
await admin.api.members.remove(person.member);
|
|
1524
|
+
}
|
|
1525
|
+
expect((await admin.api.audit.verify()).ok, "the log did not verify after recovering the sealing-race member");
|
|
1526
|
+
return "a grant inserted while the vault waited on the head was refused as tampered and logged; removal recovered the member";
|
|
1527
|
+
}
|
|
1528
|
+
//#endregion
|
|
1529
|
+
//#region src/checks/signin.ts
|
|
1530
|
+
const hash = (value) => createHash("sha256").update(value).digest();
|
|
1531
|
+
async function reported(deployment, id) {
|
|
1532
|
+
const event = new RegExp(`auth_row_tampered[^}]*${id}`);
|
|
1533
|
+
await until(`the sign-in row failure report for ${id}`, async () => event.test(deployment.output()), 10);
|
|
1534
|
+
}
|
|
1535
|
+
/** Knowing the database password and a chosen token must not mint a credential. */
|
|
1536
|
+
async function forgedCredential(deployment, people) {
|
|
1537
|
+
const person = await member(deployment, people, "forged-credential");
|
|
1538
|
+
const token = `coffre_cli_${randomBytes(32).toString("base64url")}`;
|
|
1539
|
+
await using(deployment.database(), async (sql) => {
|
|
1540
|
+
const [row] = await query(sql, "SELECT * FROM credentials WHERE principal = $1 AND revoked_at IS NULL LIMIT 1", [person.member]);
|
|
1541
|
+
expect(row !== void 0, "no genuine credential to copy");
|
|
1542
|
+
const forged = {
|
|
1543
|
+
...row,
|
|
1544
|
+
id: randomUUID(),
|
|
1545
|
+
kind: "cli",
|
|
1546
|
+
token_hash: hash(token),
|
|
1547
|
+
identity_id: null
|
|
1548
|
+
};
|
|
1549
|
+
await insertRow(sql, "credentials", forged);
|
|
1550
|
+
try {
|
|
1551
|
+
const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } });
|
|
1552
|
+
expect(response.status === 401, "an owner-forged credential authenticated", await response.text());
|
|
1553
|
+
await reported(deployment, forged.id);
|
|
1554
|
+
} finally {
|
|
1555
|
+
await query(sql, "DELETE FROM credentials WHERE id = $1", [forged.id]);
|
|
1556
|
+
}
|
|
1557
|
+
});
|
|
1558
|
+
expect((await person.api.me()).principal.id === person.email, "the genuine credential stopped working");
|
|
1559
|
+
return "a chosen token in an owner-inserted credential cannot authenticate, and is reported";
|
|
1560
|
+
}
|
|
1561
|
+
/** An account binding inserted by the owner must not sign someone in as a member. */
|
|
1562
|
+
async function forgedIdentity(deployment, people) {
|
|
1563
|
+
const person = await member(deployment, people, "forged-identity");
|
|
1564
|
+
const email = "identity-forger@conformance.example";
|
|
1565
|
+
const browser = new Browser(deployment.origin);
|
|
1566
|
+
await using(deployment.database(), async (sql) => {
|
|
1567
|
+
const [row] = await query(sql, "SELECT * FROM identities WHERE principal = $1 AND revoked_at IS NULL LIMIT 1", [person.member]);
|
|
1568
|
+
expect(row !== void 0, "no genuine identity to copy");
|
|
1569
|
+
const forged = {
|
|
1570
|
+
...row,
|
|
1571
|
+
id: randomUUID(),
|
|
1572
|
+
subject: String(defaultGitHubAccount(email).id)
|
|
1573
|
+
};
|
|
1574
|
+
await insertRow(sql, "identities", forged);
|
|
1575
|
+
try {
|
|
1576
|
+
await signIn(deployment, browser, email).catch(() => {});
|
|
1577
|
+
expect((await browser.fetch("/api/me")).status === 401, "an owner-forged identity minted a session");
|
|
1578
|
+
await reported(deployment, forged.id);
|
|
1579
|
+
} finally {
|
|
1580
|
+
await query(sql, "DELETE FROM credentials WHERE identity_id = $1", [forged.id]);
|
|
1581
|
+
await query(sql, "DELETE FROM identities WHERE id = $1", [forged.id]);
|
|
1582
|
+
}
|
|
1583
|
+
});
|
|
1584
|
+
expect((await person.api.me()).principal.id === person.email, "the genuine identity stopped working");
|
|
1585
|
+
return "an owner-inserted account binding cannot mint a session, and is reported";
|
|
1586
|
+
}
|
|
1587
|
+
async function startDevice(deployment) {
|
|
1588
|
+
const response = await fetch(`${deployment.origin}/api/auth/device`, {
|
|
1589
|
+
method: "POST",
|
|
1590
|
+
headers: { "content-type": "application/json" },
|
|
1591
|
+
body: "{}"
|
|
1592
|
+
});
|
|
1593
|
+
expect(response.ok, "the device fixture did not start", await response.clone().text());
|
|
1594
|
+
return response.json();
|
|
1595
|
+
}
|
|
1596
|
+
function pollDevice(deployment, code) {
|
|
1597
|
+
return fetch(`${deployment.origin}/api/auth/device/token`, {
|
|
1598
|
+
method: "POST",
|
|
1599
|
+
headers: { "content-type": "application/json" },
|
|
1600
|
+
body: JSON.stringify({ device_code: code })
|
|
1601
|
+
});
|
|
1602
|
+
}
|
|
1603
|
+
/** An authentic pending row, changed to approved around the app, grants nothing. */
|
|
1604
|
+
async function forgedApproval(deployment, people) {
|
|
1605
|
+
const person = await member(deployment, people, "forged-approval");
|
|
1606
|
+
const device = await startDevice(deployment);
|
|
1607
|
+
await using(deployment.database(), async (sql) => {
|
|
1608
|
+
const [row] = await query(sql, "SELECT * FROM device_authorizations WHERE user_code = $1", [device.user_code]);
|
|
1609
|
+
const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [person.member]);
|
|
1610
|
+
expect(row !== void 0, "no device row to forge");
|
|
1611
|
+
await query(sql, `UPDATE device_authorizations SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
|
|
1612
|
+
sql.engine === "sqlite" ? Date.now() : /* @__PURE__ */ new Date(),
|
|
1613
|
+
person.member,
|
|
1614
|
+
standing.generation,
|
|
1615
|
+
row.id
|
|
1616
|
+
]);
|
|
1617
|
+
try {
|
|
1618
|
+
const response = await pollDevice(deployment, device.device_code);
|
|
1619
|
+
const body = await response.text();
|
|
1620
|
+
expect(!response.ok && !body.includes("access_token"), "an owner-forged approval minted a session", body);
|
|
1621
|
+
await reported(deployment, String(row.id));
|
|
1622
|
+
} finally {
|
|
1623
|
+
await query(sql, "DELETE FROM device_authorizations WHERE id = $1", [row.id]);
|
|
1624
|
+
}
|
|
1625
|
+
});
|
|
1626
|
+
const control = await startDevice(deployment);
|
|
1627
|
+
await person.api.deviceLogins.decide(control.user_code, true);
|
|
1628
|
+
expect((await pollDevice(deployment, control.device_code)).ok, "a genuine approval no longer works");
|
|
1629
|
+
return "an owner-edited device approval grants nothing and is reported; a genuine approval works";
|
|
1630
|
+
}
|
|
1631
|
+
/** Changing an old credential's generation cannot bring its chosen token back. */
|
|
1632
|
+
async function editedGeneration(deployment, people) {
|
|
1633
|
+
const principal = "token:generation-mac";
|
|
1634
|
+
const { admin } = people;
|
|
1635
|
+
await admin.api.members.add(principal);
|
|
1636
|
+
await admin.api.access.set(principal, { "conformance/dev": "viewer" });
|
|
1637
|
+
const issued = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
|
|
1638
|
+
await using(deployment.database(), async (sql) => {
|
|
1639
|
+
const [old] = await query(sql, "SELECT * FROM credentials WHERE token_hash = $1", [hash(issued.token)]);
|
|
1640
|
+
expect(old !== void 0, "no old token row to save");
|
|
1641
|
+
await admin.api.members.remove(principal);
|
|
1642
|
+
await admin.api.members.add(principal);
|
|
1643
|
+
await admin.api.access.set(principal, { "conformance/dev": "viewer" });
|
|
1644
|
+
const [current] = await query(sql, "SELECT * FROM credentials WHERE id = $1", [old.id]);
|
|
1645
|
+
const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [principal]);
|
|
1646
|
+
await restoreRow(sql, "credentials", {
|
|
1647
|
+
...old,
|
|
1648
|
+
generation: standing.generation
|
|
1649
|
+
}, "id");
|
|
1650
|
+
try {
|
|
1651
|
+
const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${issued.token}` } });
|
|
1652
|
+
expect(response.status === 401, "editing the credential generation revived the token", await response.text());
|
|
1653
|
+
await reported(deployment, String(old.id));
|
|
1654
|
+
} finally {
|
|
1655
|
+
await restoreRow(sql, "credentials", current, "id");
|
|
1656
|
+
}
|
|
1657
|
+
});
|
|
1658
|
+
const fresh = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
|
|
1659
|
+
expect((await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${fresh.token}` } })).ok, "a newly issued token does not work");
|
|
1660
|
+
return "an old token with its generation edited to the current membership is refused and reported";
|
|
1661
|
+
}
|
|
1662
|
+
//#endregion
|
|
1663
|
+
//#region src/checks/access.ts
|
|
1664
|
+
async function membersOnly(deployment, { stranger }) {
|
|
1665
|
+
const attempt = await signIn(deployment, stranger.browser, stranger.email);
|
|
1666
|
+
expect(!attempt.ok, "someone never admitted signed in");
|
|
1667
|
+
expect(!stranger.browser.hasCookies, "the refused sign-in left the browser a cookie");
|
|
1668
|
+
expect((await stranger.browser.fetch("/api/me")).status === 401, "the refused sign-in left a session");
|
|
1669
|
+
await refused("a made-up token read secrets", bearer(deployment.origin, "coffre_cli_conformanceforgedtokenconformanceforged").secrets.list(DEV));
|
|
1670
|
+
for (const [method, path, body] of [
|
|
1671
|
+
[
|
|
1672
|
+
"GET",
|
|
1673
|
+
`/api/secrets/${DEV}`,
|
|
1674
|
+
void 0
|
|
1675
|
+
],
|
|
1676
|
+
[
|
|
1677
|
+
"POST",
|
|
1678
|
+
"/api/reveals",
|
|
1679
|
+
{ path: DEV }
|
|
1680
|
+
],
|
|
1681
|
+
[
|
|
1682
|
+
"PATCH",
|
|
1683
|
+
`/api/secrets/${DEV}`,
|
|
1684
|
+
{ API_KEY: "from nobody" }
|
|
1685
|
+
],
|
|
1686
|
+
[
|
|
1687
|
+
"GET",
|
|
1688
|
+
"/api/audit",
|
|
1689
|
+
void 0
|
|
1690
|
+
]
|
|
1691
|
+
]) {
|
|
1692
|
+
const response = await fetch(`${deployment.origin}${path}`, {
|
|
1693
|
+
method,
|
|
1694
|
+
headers: {
|
|
1695
|
+
origin: deployment.origin,
|
|
1696
|
+
"content-type": "application/json"
|
|
1697
|
+
},
|
|
1698
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
1699
|
+
});
|
|
1700
|
+
expect(response.status === 401, `${method} ${path} answered ${response.status} to no one`);
|
|
1701
|
+
}
|
|
1702
|
+
return `a stranger's sign-in is ${attempt.ok ? "let" : attempt.error}; no one and a made-up token get 401`;
|
|
1703
|
+
}
|
|
1704
|
+
async function grantScoping({ reader, service, bulk }, canaries) {
|
|
1705
|
+
const read = await reader.api.secrets.reveal(DEV);
|
|
1706
|
+
expect(same(read.values, valuesIn(canaries, DEV)), "a viewer on dev did not get dev's values", read.values);
|
|
1707
|
+
await refused("a viewer on dev revealed prod", reader.api.secrets.reveal(PROD));
|
|
1708
|
+
await refused("a viewer on dev listed prod", reader.api.secrets.list(PROD));
|
|
1709
|
+
await refused("a viewer wrote a value", reader.api.secrets.set(DEV, { API_KEY: "from the reader" }));
|
|
1710
|
+
await refused("a viewer granted themselves more", reader.api.access.set(reader.member, { [PROD]: "owner" }));
|
|
1711
|
+
await refused("a viewer added a member", reader.api.members.add("user:friend@conformance.example"));
|
|
1712
|
+
await refused("a viewer issued a service token", reader.api.tokens.issue(service.member, { expiresInDays: 1 }));
|
|
1713
|
+
const places = (await reader.api.me()).environments.map((place) => `${place.project}/${place.environment}`);
|
|
1714
|
+
expect(places.length === 1 && places[0] === DEV, "a viewer on dev is told of other places", places);
|
|
1715
|
+
const served = await service.api.secrets.reveal(DEV);
|
|
1716
|
+
expect(same(served.values, valuesIn(canaries, DEV)), "a service viewer on dev did not get dev's values", served.values);
|
|
1717
|
+
await refused("a service viewer on dev revealed prod", service.api.secrets.reveal(PROD));
|
|
1718
|
+
await refused("a viewer on bulk revealed dev", bulk.api.secrets.reveal(DEV));
|
|
1719
|
+
return "a viewer on dev reads dev and nothing else, in a browser or with a token, and changes nothing";
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* A change sent with the session cookie from another site's page, or from
|
|
1723
|
+
* no page coffre can vouch for, is refused (CSRF), and so is a reveal.
|
|
1724
|
+
*/
|
|
1725
|
+
async function crossSite({ admin }, canaries) {
|
|
1726
|
+
for (const headers of [
|
|
1727
|
+
{
|
|
1728
|
+
origin: "https://attacker.example",
|
|
1729
|
+
"sec-fetch-site": "cross-site"
|
|
1730
|
+
},
|
|
1731
|
+
{ origin: "https://attacker.example" },
|
|
1732
|
+
{ "sec-fetch-site": "same-site" },
|
|
1733
|
+
{}
|
|
1734
|
+
]) for (const [method, path, body] of [
|
|
1735
|
+
[
|
|
1736
|
+
"PATCH",
|
|
1737
|
+
`/api/secrets/${DEV}`,
|
|
1738
|
+
{ API_KEY: "from another site" }
|
|
1739
|
+
],
|
|
1740
|
+
[
|
|
1741
|
+
"POST",
|
|
1742
|
+
"/api/reveals",
|
|
1743
|
+
{ path: DEV }
|
|
1744
|
+
],
|
|
1745
|
+
[
|
|
1746
|
+
"PUT",
|
|
1747
|
+
"/api/members/user:mallory@attacker.example",
|
|
1748
|
+
{}
|
|
1749
|
+
],
|
|
1750
|
+
[
|
|
1751
|
+
"POST",
|
|
1752
|
+
"/auth/signout",
|
|
1753
|
+
void 0
|
|
1754
|
+
]
|
|
1755
|
+
]) {
|
|
1756
|
+
const response = await admin.browser.fetch(path, {
|
|
1757
|
+
method,
|
|
1758
|
+
headers: {
|
|
1759
|
+
...headers,
|
|
1760
|
+
"content-type": "application/json"
|
|
1761
|
+
},
|
|
1762
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
1763
|
+
});
|
|
1764
|
+
const text = await response.text();
|
|
1765
|
+
expect(response.status === 403, `${method} ${path} with ${JSON.stringify(headers)} answered ${response.status}`, text);
|
|
1766
|
+
expect(!Object.values(canaries).some((value) => text.includes(value)), `${method} ${path} from another site carried a value`);
|
|
1767
|
+
}
|
|
1768
|
+
const after = await admin.api.secrets.reveal(DEV);
|
|
1769
|
+
expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`], "a value changed from another site", after.values);
|
|
1770
|
+
expect(!(await admin.api.members.list()).members.some((entry) => entry.member.includes("mallory")), "a member was added from another site");
|
|
1771
|
+
return "writes, reveals and sign-out with the cookie, from another site or no page at all: 403";
|
|
1772
|
+
}
|
|
1773
|
+
async function offboarding(deployment, { admin, leaver, service }, canaries) {
|
|
1774
|
+
const device = await startDevice(deployment);
|
|
1775
|
+
await leaver.api.deviceLogins.decide(device.user_code, true);
|
|
1776
|
+
await leaver.cli.secrets.reveal(DEV);
|
|
1777
|
+
const { report } = await admin.api.members.remove(leaver.member);
|
|
1778
|
+
const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
|
|
1779
|
+
expect(same(exposed, Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort()), "the offboarding report does not name what they read", report.exposed);
|
|
1780
|
+
expect((await leaver.browser.fetch("/api/me")).status === 401, "a removed member's browser session still works");
|
|
1781
|
+
await refused("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
|
|
1782
|
+
expect(!(await signIn(deployment, leaver.browser, leaver.email)).ok, "a removed member signed in again");
|
|
1783
|
+
await admin.api.members.remove(service.member);
|
|
1784
|
+
await refused("a removed service's token read secrets", service.api.secrets.reveal(DEV));
|
|
1785
|
+
await admin.api.members.add(leaver.member);
|
|
1786
|
+
await admin.api.access.set(leaver.member, { [DEV]: "developer" });
|
|
1787
|
+
await admin.api.members.add(service.member);
|
|
1788
|
+
await admin.api.access.set(service.member, { [DEV]: "viewer" });
|
|
1789
|
+
expect((await leaver.browser.fetch("/api/me")).status === 401, "re-admission revived the browser session");
|
|
1790
|
+
await refused("re-admission revived the CLI session", leaver.cli.secrets.reveal(DEV));
|
|
1791
|
+
await refused("re-admission revived the service token", service.api.secrets.reveal(DEV));
|
|
1792
|
+
expect(!(await pollDevice(deployment, device.device_code)).ok, "re-admission revived the old device approval");
|
|
1793
|
+
expect((await signIn(deployment, leaver.browser, leaver.email)).ok, "a fresh provider callback could not sign the re-admitted member in");
|
|
1794
|
+
await admin.api.members.remove(leaver.member);
|
|
1795
|
+
await admin.api.members.remove(service.member);
|
|
1796
|
+
return `the report names ${exposed.length} values; re-admission revives no old session, token or approval; a fresh provider callback works`;
|
|
1797
|
+
}
|
|
1798
|
+
/** Reading more values at once than the vault allows in its window is refused. */
|
|
1799
|
+
async function bulkLimit$1({ admin, bulk }, limit) {
|
|
1800
|
+
const values = {};
|
|
1801
|
+
for (let i = 0; i <= limit; i++) values[`BULK_${i}`] = `bulk-${i}`;
|
|
1802
|
+
await admin.api.secrets.set(BULK, values);
|
|
1803
|
+
const refusal = await refused(`${limit + 1} values were revealed at once`, bulk.api.secrets.reveal(BULK));
|
|
1804
|
+
expect(refusal.status === 403 && refusal.code === "bulk_limit" && refusal.reason === "bulk_limit", "the bulk read was not refused as bulk_limit", refusal);
|
|
1805
|
+
const one = await bulk.api.secrets.reveal(`${BULK}/BULK_0`);
|
|
1806
|
+
expect(one.values.BULK_0 === "bulk-0", "a viewer on bulk could not read one of its values", one.values);
|
|
1807
|
+
return `${limit + 1} values at once: ${refusal.status} ${refusal.reason ?? refusal.code}; one still opens`;
|
|
1808
|
+
}
|
|
1809
|
+
function same(a, b) {
|
|
1810
|
+
return JSON.stringify(sorted(a)) === JSON.stringify(sorted(b));
|
|
1811
|
+
}
|
|
1812
|
+
function sorted(value) {
|
|
1813
|
+
if (Array.isArray(value) || value === null || typeof value !== "object") return value;
|
|
1814
|
+
return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1));
|
|
1815
|
+
}
|
|
1816
|
+
//#endregion
|
|
1817
|
+
//#region src/checks/audit.ts
|
|
1818
|
+
/** One read per returned value, committed by the vault under the response's operation. */
|
|
1819
|
+
async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
|
|
1820
|
+
const path = purpose === "reveal" ? `${DEV}/API_KEY` : DEV;
|
|
1821
|
+
const { operationId, values } = await reader.api.secrets.reveal(path);
|
|
1822
|
+
expect(typeof operationId === "string" && operationId.length > 0, "the read returned no operation id");
|
|
1823
|
+
const expected = purpose === "reveal" ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
|
|
1824
|
+
expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), "the read returned different values", values);
|
|
1825
|
+
const bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.operationId === operationId);
|
|
1826
|
+
const keys = bundle.map((entry) => entry.key).sort();
|
|
1827
|
+
expect(JSON.stringify(keys) === JSON.stringify(Object.keys(expected).sort()), "the read is not logged once per value", bundle);
|
|
1828
|
+
expect(bundle.every((entry) => entry.author === "vault" && entry.action === "secret.read" && entry.decision === "allow"), "the read's entries are not the vault's allowed reads", bundle);
|
|
1829
|
+
expect(bundle.every((entry) => entry.metadata.purpose === purpose && entry.requestId !== null && entry.requestId === bundle[0].requestId), "the reads do not name one request and its purpose", bundle);
|
|
1830
|
+
const { keys: listed } = await admin.api.secrets.list(DEV);
|
|
1831
|
+
for (const entry of bundle) expect(entry.version === listed.find((key) => key.key === entry.key)?.version, `the entry for ${String(entry.key)} names another version`, entry);
|
|
1832
|
+
await using(deployment.database(), async (sql) => {
|
|
1833
|
+
const rows = await query(sql, `SELECT l.author, l.action, l.decision, l.request_id, l.metadata, s.key, s.current_version
|
|
1834
|
+
FROM audit_log l LEFT JOIN secrets s ON s.id = l.secret_id WHERE l.operation_id = $1`, [operationId]);
|
|
1835
|
+
expect(JSON.stringify(rows.map((row) => row.key).sort()) === JSON.stringify(Object.keys(expected).sort()), "the committed read is not once per value", rows);
|
|
1836
|
+
for (const row of rows) {
|
|
1837
|
+
const metadata = JSON.parse(row.metadata);
|
|
1838
|
+
expect(row.author === "vault" && row.action === "secret.read" && row.decision === "allow", "the committed read is not the vault release", row);
|
|
1839
|
+
expect(row.request_id !== null && row.request_id === rows[0].request_id && metadata.purpose === purpose, "the committed reads have different requests or purposes", rows);
|
|
1840
|
+
expect(metadata.version === row.current_version, "the committed read names another version", row);
|
|
1841
|
+
}
|
|
1842
|
+
});
|
|
1843
|
+
return `${bundle.length} values, each read once by the vault for ${purpose}, under ${operationId}`;
|
|
1844
|
+
}
|
|
1845
|
+
/** Full verification must reach the stored head, not just some intact prefix. */
|
|
1846
|
+
async function verification(deployment, { admin }) {
|
|
1847
|
+
const result = await admin.api.audit.verify();
|
|
1848
|
+
expect(result.ok, "the full log does not verify", result);
|
|
1849
|
+
await using(deployment.database(), async (sql) => {
|
|
1850
|
+
const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
|
|
1851
|
+
const [last] = await sql.query("SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1");
|
|
1852
|
+
expect(head !== void 0 && last !== void 0, "the head or newest entry is missing");
|
|
1853
|
+
expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, "the head does not name the newest entry");
|
|
1854
|
+
expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), "the head has another hash");
|
|
1855
|
+
expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), "verification stopped before the head", {
|
|
1856
|
+
result,
|
|
1857
|
+
head: head.next_seq
|
|
1858
|
+
});
|
|
1859
|
+
expect(BigInt(result.entries) === BigInt(head.next_seq), "verification did not count every entry", result);
|
|
1860
|
+
});
|
|
1861
|
+
return `${result.entries} entries verified through the head at ${result.through}`;
|
|
1862
|
+
}
|
|
1863
|
+
/**
|
|
1864
|
+
* Each heartbeat has the vault sign the log up to its last entry, in an
|
|
1865
|
+
* entry of its own, after checking that the prefix it signed last is still
|
|
1866
|
+
* there. Two, so the second covers the first; then the log is verified from
|
|
1867
|
+
* its first entry, every checkpoint with it.
|
|
1868
|
+
*/
|
|
1869
|
+
async function checkpoints(deployment, { admin }) {
|
|
1870
|
+
const before = await admin.api.audit.verify();
|
|
1871
|
+
expect(before.ok, "the log does not verify", before);
|
|
1872
|
+
await deployment.scheduled();
|
|
1873
|
+
await deployment.scheduled();
|
|
1874
|
+
const after = await admin.api.audit.verify();
|
|
1875
|
+
expect(after.ok && after.checkpoint !== null, "the audit log is not verified and checkpointed", after);
|
|
1876
|
+
expect(after.checkpoint.seq > (before.through ?? -1), "the checkpoint does not cover what was written before it", {
|
|
1877
|
+
before,
|
|
1878
|
+
after
|
|
1879
|
+
});
|
|
1880
|
+
const { entries } = await admin.api.audit.list({
|
|
1881
|
+
detail: "1",
|
|
1882
|
+
limit: 20
|
|
1883
|
+
});
|
|
1884
|
+
const signed = entries.filter((entry) => entry.action === "audit.checkpoint" && entry.decision === "allow");
|
|
1885
|
+
expect(signed.length >= 2 && signed.every((entry) => entry.author === "vault" && entry.detail), "the checkpoints are not the vault's entries, hidden as detail", signed);
|
|
1886
|
+
const [newest, previous] = signed;
|
|
1887
|
+
expect(newest.metadata.seq === after.checkpoint.seq, "the newest checkpoint entry is not the one verified", {
|
|
1888
|
+
signed,
|
|
1889
|
+
after
|
|
1890
|
+
});
|
|
1891
|
+
expect(previous.seq <= after.checkpoint.seq, "the newest checkpoint does not cover the one before it", signed);
|
|
1892
|
+
return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
|
|
1893
|
+
}
|
|
1894
|
+
/**
|
|
1895
|
+
* Every version the app says it stored names the vault's entry for the key
|
|
1896
|
+
* it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore:
|
|
1897
|
+
* the same member, request and operation, the same secret and version. And
|
|
1898
|
+
* every value read is the vault's to log: the app keeps no reads of its own.
|
|
1899
|
+
*/
|
|
1900
|
+
async function writesAgree(deployment) {
|
|
1901
|
+
return using(deployment.database(), async (sql) => {
|
|
1902
|
+
const entries = await sql.query("SELECT * FROM audit_log ORDER BY seq");
|
|
1903
|
+
const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
|
|
1904
|
+
const writes = entries.filter((entry) => entry.author === "app" && entry.decision === "allow" && (entry.action === "secret.write" || entry.action === "secret.restore"));
|
|
1905
|
+
for (const entry of entries.filter((row) => row.author === "app" && row.decision === "allow")) expect(entry.action !== "secret.read", "the app logged an allowed read of a value", entry);
|
|
1906
|
+
for (const entry of writes) {
|
|
1907
|
+
const seal = entry.action === "secret.write" ? "key.wrap" : "key.rewrap";
|
|
1908
|
+
const key = entry.related_seq === null ? void 0 : bySeq.get(String(entry.related_seq));
|
|
1909
|
+
expect(key !== void 0 && key.author === "vault" && key.action === seal && key.decision === "allow", `a ${entry.action} names no ${seal} of the vault's`, {
|
|
1910
|
+
entry,
|
|
1911
|
+
key
|
|
1912
|
+
});
|
|
1913
|
+
for (const field of [
|
|
1914
|
+
"actor",
|
|
1915
|
+
"request_id",
|
|
1916
|
+
"operation_id",
|
|
1917
|
+
"project_id",
|
|
1918
|
+
"environment_id"
|
|
1919
|
+
]) expect(entry[field] === key[field], `the write and its seal have different ${field}`, {
|
|
1920
|
+
entry,
|
|
1921
|
+
key
|
|
1922
|
+
});
|
|
1923
|
+
expect(entry.operation_id !== null && entry.request_id !== null, "the committed write has no operation or request", entry);
|
|
1924
|
+
const metadata = JSON.parse(entry.metadata);
|
|
1925
|
+
const sealed = JSON.parse(key.metadata);
|
|
1926
|
+
expect((sealed.secretId ?? key.secret_id) === entry.secret_id && sealed.version === metadata.version && sealed.subject.split("/").at(-1) === metadata.key, "the write names a seal of another secret or version", {
|
|
1927
|
+
entry,
|
|
1928
|
+
key
|
|
1929
|
+
});
|
|
1930
|
+
}
|
|
1931
|
+
const versions = await query(sql, `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
|
|
1932
|
+
JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT]);
|
|
1933
|
+
expect(versions.length > 0, "no stored versions were inspected");
|
|
1934
|
+
for (const version of versions) {
|
|
1935
|
+
const matching = writes.filter((entry) => entry.secret_id === version.secret_id && JSON.parse(entry.metadata).version === version.version);
|
|
1936
|
+
expect(matching.length === 1, "a stored version has no unique app write entry", {
|
|
1937
|
+
version,
|
|
1938
|
+
matching
|
|
1939
|
+
});
|
|
1940
|
+
}
|
|
1941
|
+
return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
|
|
1942
|
+
});
|
|
1943
|
+
}
|
|
1944
|
+
/**
|
|
1945
|
+
* No value leaves without its entry: with the audit log refusing writes (a
|
|
1946
|
+
* trigger, as a full disk or a lost connection would), the vault cannot log
|
|
1947
|
+
* the read, so a reveal fails and carries nothing.
|
|
1948
|
+
*/
|
|
1949
|
+
async function noAuditNoValue(deployment, { admin }, canaries) {
|
|
1950
|
+
let status = 0;
|
|
1951
|
+
await using(deployment.database(), (sql) => logRefuses(sql, async () => {
|
|
1952
|
+
const before = deployment.output().split(LOG_REFUSAL).length;
|
|
1953
|
+
const response = await admin.browser.send("POST", "/api/reveals", { path: DEV });
|
|
1954
|
+
const text = await response.text();
|
|
1955
|
+
status = response.status;
|
|
1956
|
+
const body = JSON.parse(text);
|
|
1957
|
+
expect(status === 500 && body.error === "internal_error" && body.reason === void 0, "the unlogged reveal did not fail as internal_error", {
|
|
1958
|
+
status,
|
|
1959
|
+
body
|
|
1960
|
+
});
|
|
1961
|
+
expect(body.message === "something went wrong; see the server log", "the audit failure exposed another reason", body);
|
|
1962
|
+
expect(!Object.values(canaries).some((value) => text.includes(value)), "a reveal that could not be logged carried a value", text);
|
|
1963
|
+
await until("the injected audit refusal in the processes' output", async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
|
|
1964
|
+
}, "secret.read"));
|
|
1965
|
+
expect((await admin.api.secrets.reveal(DEV)).values.API_KEY === canaries[`${DEV}/API_KEY`], "reveals did not come back once the log took writes again");
|
|
1966
|
+
return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
|
|
1967
|
+
}
|
|
1968
|
+
/** A publicly chained entry still needs the vault's MAC. */
|
|
1969
|
+
async function forgedVaultEntry(deployment, { admin }) {
|
|
1970
|
+
const verify = () => admin.api.audit.verify();
|
|
1971
|
+
expect((await verify()).ok, "the log does not verify before the forged entry");
|
|
1972
|
+
await using(deployment.database(), async (sql) => {
|
|
1973
|
+
const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
|
|
1974
|
+
const [writer] = await sql.query("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
|
|
1975
|
+
expect(writer !== void 0, "no vault key id to copy");
|
|
1976
|
+
const seq = BigInt(head.next_seq);
|
|
1977
|
+
const forged = {
|
|
1978
|
+
seq,
|
|
1979
|
+
author: "vault",
|
|
1980
|
+
key_id: writer.key_id,
|
|
1981
|
+
occurred_at: Date.now(),
|
|
1982
|
+
actor: "user:forger@conformance.example",
|
|
1983
|
+
action: "secret.read",
|
|
1984
|
+
decision: "allow",
|
|
1985
|
+
metadata: "{}"
|
|
1986
|
+
};
|
|
1987
|
+
const prevHash = Buffer.from(head.head_hash);
|
|
1988
|
+
const mac = Buffer.alloc(32, 65);
|
|
1989
|
+
const hash = createHash("sha256").update("coffre.audit.chain.v2").update(prevHash).update(entryFields(forged)).update(mac).digest();
|
|
1990
|
+
await insertRow(sql, "audit_log", {
|
|
1991
|
+
...forged,
|
|
1992
|
+
prev_hash: prevHash,
|
|
1993
|
+
mac,
|
|
1994
|
+
hash
|
|
1995
|
+
});
|
|
1996
|
+
await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq + 1n, hash]);
|
|
1997
|
+
let inserted;
|
|
1998
|
+
try {
|
|
1999
|
+
inserted = await verify();
|
|
2000
|
+
} finally {
|
|
2001
|
+
await appendOnlyLifted(sql, () => query(sql, "DELETE FROM audit_log WHERE seq = $1", [seq]));
|
|
2002
|
+
await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq, prevHash]);
|
|
2003
|
+
}
|
|
2004
|
+
expect(!inserted.ok && inserted.author === "vault" && inserted.failedAtSeq === Number(seq), "an entry in the vault's name, chained but not by the vault, verifies", inserted);
|
|
2005
|
+
const removed = await verify();
|
|
2006
|
+
expect(removed.ok, "the log did not verify once the entry was gone", removed);
|
|
2007
|
+
});
|
|
2008
|
+
return "a publicly chained vault entry with a made-up MAC was refused at its sequence";
|
|
2009
|
+
}
|
|
2010
|
+
/** Rewrite either author's entry and require a fault at that exact sequence. */
|
|
2011
|
+
async function rewrittenEntry(deployment, { admin }, author) {
|
|
2012
|
+
expect((await admin.api.audit.verify()).ok, "the log does not verify before the rewrite");
|
|
2013
|
+
let seq = 0;
|
|
2014
|
+
await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
|
|
2015
|
+
const [row] = await query(sql, "SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1", [author]);
|
|
2016
|
+
expect(row !== void 0, `no ${author} entry to rewrite`);
|
|
2017
|
+
seq = Number(row.seq);
|
|
2018
|
+
await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", ["user:nobody@conformance.example", row.seq]);
|
|
2019
|
+
try {
|
|
2020
|
+
const result = await admin.api.audit.verify();
|
|
2021
|
+
expect(!result.ok && result.failedAtSeq === Number(row.seq), "a rewritten entry was not caught at its sequence", result);
|
|
2022
|
+
} finally {
|
|
2023
|
+
await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", [row.actor, row.seq]);
|
|
2024
|
+
}
|
|
2025
|
+
}));
|
|
2026
|
+
expect((await admin.api.audit.verify()).ok, "the restored entry did not verify");
|
|
2027
|
+
return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
|
|
2028
|
+
}
|
|
2029
|
+
/** Remove an unreferenced entry, then put its exact bytes back. */
|
|
2030
|
+
async function missingEntry(deployment, { admin }, position) {
|
|
2031
|
+
expect((await admin.api.audit.verify()).ok, "the log does not verify before deletion");
|
|
2032
|
+
let seq = 0;
|
|
2033
|
+
await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
|
|
2034
|
+
const [row] = await sql.query(`SELECT * FROM audit_log WHERE ${position !== "middle" ? `seq = ${position === "first" ? 0 : 1e3}` : "seq > 0 AND action = 'audit.heartbeat' AND NOT EXISTS (SELECT 1 FROM audit_log linked WHERE linked.related_seq = audit_log.seq)"} ORDER BY seq LIMIT 1`);
|
|
2035
|
+
expect(row !== void 0, "no unreferenced entry to delete");
|
|
2036
|
+
seq = Number(row.seq);
|
|
2037
|
+
const linked = await query(sql, "SELECT * FROM audit_log WHERE related_seq = $1", [row.seq]);
|
|
2038
|
+
await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1", [row.seq]);
|
|
2039
|
+
await query(sql, "DELETE FROM audit_log WHERE seq = $1", [row.seq]);
|
|
2040
|
+
try {
|
|
2041
|
+
const result = await admin.api.audit.verify();
|
|
2042
|
+
expect(!result.ok, "a log with an entry missing verifies", result);
|
|
2043
|
+
if (position !== "middle") expect(result.reason.includes(`sequence gap: expected seq ${position === "first" ? 0 : 1e3}`), "the sequence gap was not reported", result);
|
|
2044
|
+
} finally {
|
|
2045
|
+
await insertRow(sql, "audit_log", row);
|
|
2046
|
+
for (const entry of linked) await query(sql, "UPDATE audit_log SET related_seq = $1 WHERE seq = $2", [entry.related_seq, entry.seq]);
|
|
2047
|
+
}
|
|
2048
|
+
}));
|
|
2049
|
+
expect((await admin.api.audit.verify()).ok, "the restored log did not verify");
|
|
2050
|
+
const which = {
|
|
2051
|
+
first: "the first",
|
|
2052
|
+
batch: "the first past a page of 1,000",
|
|
2053
|
+
middle: "a heartbeat"
|
|
2054
|
+
}[position];
|
|
2055
|
+
return `entry ${seq}, ${which}, deleted: caught, then put back`;
|
|
2056
|
+
}
|
|
2057
|
+
/** Last: keep the head, delete its newest entries, and require verification to notice. */
|
|
2058
|
+
async function deletedTail(deployment, { admin }) {
|
|
2059
|
+
const intact = await admin.api.audit.verify();
|
|
2060
|
+
expect(intact.ok && intact.checkpoint !== null, "the log is not intact and checkpointed");
|
|
2061
|
+
const signed = intact.checkpoint.seq;
|
|
2062
|
+
await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
|
|
2063
|
+
await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq >= $1", [signed]);
|
|
2064
|
+
await query(sql, "DELETE FROM audit_log WHERE seq >= $1", [signed]);
|
|
2065
|
+
}));
|
|
2066
|
+
const result = await admin.api.audit.verify();
|
|
2067
|
+
expect(!result.ok && result.author === "app" && result.reason.includes("removed from the end"), "the log verifies with its newest entries deleted", result);
|
|
2068
|
+
return "the newest entries deleted, with the head left alone, were caught";
|
|
2069
|
+
}
|
|
2070
|
+
/** The whole log, detail included, newest first. */
|
|
2071
|
+
async function everyAuditEntry(api) {
|
|
2072
|
+
const all = [];
|
|
2073
|
+
for (let before;;) {
|
|
2074
|
+
const { entries } = await api.audit.list({
|
|
2075
|
+
before,
|
|
2076
|
+
limit: 500,
|
|
2077
|
+
detail: "1"
|
|
2078
|
+
});
|
|
2079
|
+
all.push(...entries);
|
|
2080
|
+
if (entries.length < 500) return all;
|
|
2081
|
+
before = entries.at(-1).seq;
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
//#endregion
|
|
2085
|
+
//#region src/checks/checkpoints.ts
|
|
2086
|
+
/** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
|
|
2087
|
+
async function earlierCheckpoint(deployment, { admin }) {
|
|
2088
|
+
expect((await admin.api.audit.verify()).ok, "the log does not verify before checkpoint tampering");
|
|
2089
|
+
await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
|
|
2090
|
+
const rows = await sql.query("SELECT * FROM audit_log ORDER BY seq");
|
|
2091
|
+
const signed = rows.filter((row) => row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow");
|
|
2092
|
+
expect(signed.length >= 2, "two checkpoints are needed");
|
|
2093
|
+
const [head] = await sql.query("SELECT * FROM audit_chain_head");
|
|
2094
|
+
const seed = Buffer.from(KEYS.SIGNING_KEY, "base64");
|
|
2095
|
+
const signingKey = createPrivateKey({
|
|
2096
|
+
key: Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), seed]),
|
|
2097
|
+
format: "der",
|
|
2098
|
+
type: "pkcs8"
|
|
2099
|
+
});
|
|
2100
|
+
const keys = {
|
|
2101
|
+
app: Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.AUDIT_CHAIN_KEY, "base64"), Buffer.alloc(0), "coffre.audit.app.v2", 32)),
|
|
2102
|
+
vault: Buffer.from(hkdfSync("sha256", seed, Buffer.alloc(0), "coffre.audit.vault.v2", 32))
|
|
2103
|
+
};
|
|
2104
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
2105
|
+
let previous = Buffer.alloc(32);
|
|
2106
|
+
try {
|
|
2107
|
+
await transaction(sql, async () => {
|
|
2108
|
+
for (const original of rows) {
|
|
2109
|
+
const row = { ...original };
|
|
2110
|
+
if (row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow") {
|
|
2111
|
+
const checkpoint = JSON.parse(String(row.metadata));
|
|
2112
|
+
if (row.seq === signed[0].seq) checkpoint.signature = Buffer.alloc(64).toString("base64");
|
|
2113
|
+
else {
|
|
2114
|
+
checkpoint.hash = hashes.get(BigInt(checkpoint.seq));
|
|
2115
|
+
checkpoint.signature = sign(null, Buffer.from(`coffre.checkpoint.v3|${checkpoint.seq}|${checkpoint.hash}|${checkpoint.signedAt}`), signingKey).toString("base64");
|
|
2116
|
+
}
|
|
2117
|
+
row.metadata = JSON.stringify(checkpoint);
|
|
2118
|
+
}
|
|
2119
|
+
const fields = entryFields(row);
|
|
2120
|
+
row.prev_hash = previous;
|
|
2121
|
+
row.mac = createHmac("sha256", keys[row.author]).update("coffre.audit.mac.v2").update(previous).update(fields).digest();
|
|
2122
|
+
previous = createHash("sha256").update("coffre.audit.chain.v2").update(previous).update(fields).update(row.mac).digest();
|
|
2123
|
+
row.hash = previous;
|
|
2124
|
+
hashes.set(BigInt(String(row.seq)), previous.toString("hex"));
|
|
2125
|
+
await restoreRow(sql, "audit_log", row, "seq");
|
|
2126
|
+
}
|
|
2127
|
+
await restoreRow(sql, "audit_chain_head", {
|
|
2128
|
+
...head,
|
|
2129
|
+
head_hash: previous
|
|
2130
|
+
}, "only_row");
|
|
2131
|
+
});
|
|
2132
|
+
const result = await admin.api.audit.verify();
|
|
2133
|
+
expect(!result.ok && result.author === "vault" && result.failedAtSeq === Number(signed[0].seq) && result.reason === "a checkpoint the vault did not sign", "a valid later checkpoint hid an invalid earlier one", result);
|
|
2134
|
+
} finally {
|
|
2135
|
+
await transaction(sql, async () => {
|
|
2136
|
+
for (const row of rows) await restoreRow(sql, "audit_log", row, "seq");
|
|
2137
|
+
await restoreRow(sql, "audit_chain_head", head, "only_row");
|
|
2138
|
+
});
|
|
2139
|
+
keys.app.fill(0);
|
|
2140
|
+
keys.vault.fill(0);
|
|
2141
|
+
seed.fill(0);
|
|
2142
|
+
}
|
|
2143
|
+
}));
|
|
2144
|
+
expect((await admin.api.audit.verify()).ok, "restoring the signed prefixes did not restore verification");
|
|
2145
|
+
return "an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint";
|
|
2146
|
+
}
|
|
2147
|
+
//#endregion
|
|
2148
|
+
//#region src/checks/logins.ts
|
|
2149
|
+
const ZEROS = "decode(repeat('00', 32), 'hex')";
|
|
2150
|
+
const appendAs = (author) => `INSERT INTO audit_log
|
|
2151
|
+
(seq, author, key_id, occurred_at, actor, action, decision, prev_hash, mac, hash)
|
|
2152
|
+
VALUES (9000000000, '${author}', '${author}:0', 0, 'system:conformance', 'probe', 'allow', ${ZEROS}, ${ZEROS}, ${ZEROS})`;
|
|
2153
|
+
const IMMUTABLE = [
|
|
2154
|
+
"UPDATE audit_log SET action = 'rewritten' WHERE author = 'app'",
|
|
2155
|
+
"UPDATE audit_log SET action = 'rewritten' WHERE author = 'vault'",
|
|
2156
|
+
"DELETE FROM audit_log",
|
|
2157
|
+
"TRUNCATE audit_log",
|
|
2158
|
+
"DROP TABLE audit_log",
|
|
2159
|
+
"UPDATE secret_versions SET id = id",
|
|
2160
|
+
"DELETE FROM secret_versions",
|
|
2161
|
+
"DELETE FROM vault_members",
|
|
2162
|
+
"CREATE TABLE conformance_probe (id integer)",
|
|
2163
|
+
"CREATE TEMP TABLE conformance_probe (id integer)",
|
|
2164
|
+
"ALTER TABLE audit_log DISABLE TRIGGER USER",
|
|
2165
|
+
"SET session_replication_role = 'replica'",
|
|
2166
|
+
"SET ROLE coffre_owner"
|
|
2167
|
+
];
|
|
2168
|
+
/** A column of each app table, for an update that would change nothing, if it were allowed. */
|
|
2169
|
+
const APP_COLUMNS = {
|
|
2170
|
+
projects: "id",
|
|
2171
|
+
environments: "id",
|
|
2172
|
+
secrets: "id",
|
|
2173
|
+
secret_versions: "id",
|
|
2174
|
+
identities: "id",
|
|
2175
|
+
credentials: "id",
|
|
2176
|
+
device_authorizations: "id",
|
|
2177
|
+
syncs: "id",
|
|
2178
|
+
sync_keys: "key"
|
|
2179
|
+
};
|
|
2180
|
+
async function refuses(sql, statements) {
|
|
2181
|
+
for (const statement of statements) {
|
|
2182
|
+
await sql.exec("BEGIN");
|
|
2183
|
+
let error;
|
|
2184
|
+
try {
|
|
2185
|
+
await sql.exec(statement);
|
|
2186
|
+
} catch (failure) {
|
|
2187
|
+
error = failure;
|
|
2188
|
+
} finally {
|
|
2189
|
+
await sql.exec("ROLLBACK");
|
|
2190
|
+
}
|
|
2191
|
+
expect(error !== void 0, `the login could: ${statement}`);
|
|
2192
|
+
expect(error.code === "42501", `${statement} failed for a reason other than privilege`, error);
|
|
2193
|
+
}
|
|
2194
|
+
return statements.length;
|
|
2195
|
+
}
|
|
2196
|
+
/** Neither table nor column grants may let a login write outside its component. */
|
|
2197
|
+
async function noWrites(sql, names) {
|
|
2198
|
+
const columns = await sql.query(`SELECT table_name, column_name,
|
|
2199
|
+
has_column_privilege(current_user, quote_ident(table_name), column_name, 'INSERT') AS insertable,
|
|
2200
|
+
has_column_privilege(current_user, quote_ident(table_name), column_name, 'UPDATE') AS updatable,
|
|
2201
|
+
has_table_privilege(current_user, quote_ident(table_name), 'DELETE') AS deletable,
|
|
2202
|
+
has_table_privilege(current_user, quote_ident(table_name), 'TRUNCATE') AS truncatable
|
|
2203
|
+
FROM information_schema.columns WHERE table_schema = 'public'`);
|
|
2204
|
+
for (const name of names) {
|
|
2205
|
+
const inspected = columns.filter((column) => column.table_name === name);
|
|
2206
|
+
const [table] = await sql.query(`SELECT
|
|
2207
|
+
has_table_privilege(current_user, $1, 'INSERT, UPDATE, DELETE, TRUNCATE') AS writable`, [name]);
|
|
2208
|
+
expect(!table.writable, `the login has a table write privilege on ${name}`);
|
|
2209
|
+
expect(inspected.every((column) => !column.insertable && !column.updatable && !column.deletable && !column.truncatable), `the login has a column write privilege on ${name}`, inspected);
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
async function appLogin(deployment) {
|
|
2213
|
+
if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
|
|
2214
|
+
expect(deployment.runtime !== null, "the app runtime login is missing");
|
|
2215
|
+
const protectedTables = (await using(deployment.database(), tables)).filter((name) => name.startsWith("vault_"));
|
|
2216
|
+
return using(deployment.runtime(), async (sql) => {
|
|
2217
|
+
await noWrites(sql, protectedTables);
|
|
2218
|
+
const statements = [
|
|
2219
|
+
...IMMUTABLE,
|
|
2220
|
+
appendAs("vault"),
|
|
2221
|
+
"SET ROLE coffre_vault",
|
|
2222
|
+
"DELETE FROM secrets"
|
|
2223
|
+
];
|
|
2224
|
+
for (const table of protectedTables) statements.push(`INSERT INTO ${table} SELECT * FROM ${table} LIMIT 1`, `UPDATE ${table} SET principal = principal`, `DELETE FROM ${table}`, `TRUNCATE ${table}`);
|
|
2225
|
+
return `${await refuses(sql, statements)} writes and bypasses refused; no member or grant write privileges`;
|
|
2226
|
+
});
|
|
2227
|
+
}
|
|
2228
|
+
async function vaultLogin(deployment) {
|
|
2229
|
+
if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
|
|
2230
|
+
expect(deployment.vaultRuntime !== null, "the vault runtime login is missing");
|
|
2231
|
+
const appTables = (await using(deployment.database(), tables)).filter((name) => ![
|
|
2232
|
+
"vault_members",
|
|
2233
|
+
"vault_grants",
|
|
2234
|
+
"audit_log",
|
|
2235
|
+
"audit_chain_head"
|
|
2236
|
+
].includes(name));
|
|
2237
|
+
return using(deployment.vaultRuntime(), async (sql) => {
|
|
2238
|
+
await noWrites(sql, appTables);
|
|
2239
|
+
const [head] = await sql.query(`SELECT
|
|
2240
|
+
has_table_privilege(current_user, 'audit_chain_head', 'INSERT') AS insertable,
|
|
2241
|
+
has_table_privilege(current_user, 'audit_chain_head', 'DELETE') AS deletable,
|
|
2242
|
+
has_table_privilege(current_user, 'audit_chain_head', 'TRUNCATE') AS truncatable,
|
|
2243
|
+
has_column_privilege(current_user, 'audit_chain_head', 'only_row', 'UPDATE') AS identity_updatable`);
|
|
2244
|
+
expect(!head.insertable && !head.deletable && !head.truncatable && !head.identity_updatable, "the vault can replace the head row", head);
|
|
2245
|
+
const statements = [
|
|
2246
|
+
...IMMUTABLE,
|
|
2247
|
+
appendAs("app"),
|
|
2248
|
+
"SET ROLE coffre_app",
|
|
2249
|
+
"UPDATE audit_chain_head SET only_row = only_row",
|
|
2250
|
+
"DELETE FROM audit_chain_head",
|
|
2251
|
+
"TRUNCATE audit_chain_head",
|
|
2252
|
+
"UPDATE vault_members SET principal = principal",
|
|
2253
|
+
"UPDATE vault_grants SET role = role"
|
|
2254
|
+
];
|
|
2255
|
+
for (const table of appTables) {
|
|
2256
|
+
const column = APP_COLUMNS[table];
|
|
2257
|
+
if (column !== void 0) statements.push(`UPDATE ${quoted(table)} SET ${column} = ${column}`);
|
|
2258
|
+
}
|
|
2259
|
+
return `${await refuses(sql, statements)} writes and bypasses refused; app tables have no write privileges`;
|
|
2260
|
+
});
|
|
2261
|
+
}
|
|
2262
|
+
//#endregion
|
|
2263
|
+
//#region src/checks/readiness.ts
|
|
2264
|
+
async function red(deployment) {
|
|
2265
|
+
await until("readiness red", async () => (await fetch(`${deployment.origin}/readyz`)).status === 503, 10);
|
|
2266
|
+
const body = await (await fetch(`${deployment.origin}/readyz`)).json();
|
|
2267
|
+
expect(body.ok === false && body.checkpointed === false, "readiness did not report the missing checkpoint", body);
|
|
2268
|
+
}
|
|
2269
|
+
async function refusedCheckpoint(deployment) {
|
|
2270
|
+
expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before the checkpoint refusal");
|
|
2271
|
+
await using(deployment.database(), (sql) => logRefuses(sql, async () => {
|
|
2272
|
+
const [before] = await sql.query("SELECT seq FROM audit_log WHERE action = 'audit.heartbeat' ORDER BY seq DESC LIMIT 1");
|
|
2273
|
+
await deployment.scheduled({ allowFailure: true });
|
|
2274
|
+
await until("the new heartbeat", async () => {
|
|
2275
|
+
const [after] = await sql.query("SELECT seq FROM audit_log WHERE action = 'audit.heartbeat' ORDER BY seq DESC LIMIT 1");
|
|
2276
|
+
return BigInt(after.seq) > BigInt(before.seq);
|
|
2277
|
+
}, 10);
|
|
2278
|
+
await red(deployment);
|
|
2279
|
+
}, "audit.checkpoint"));
|
|
2280
|
+
await deployment.scheduled();
|
|
2281
|
+
await until("readiness recovered", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
|
|
2282
|
+
return "a committed heartbeat with a refused checkpoint turns readiness red, then recovers";
|
|
2283
|
+
}
|
|
2284
|
+
async function missingCheckpoint(deployment) {
|
|
2285
|
+
expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before removing checkpoints");
|
|
2286
|
+
await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
|
|
2287
|
+
const rows = await sql.query("SELECT * FROM audit_log WHERE action = 'audit.checkpoint' ORDER BY seq");
|
|
2288
|
+
expect(rows.length > 0, "no checkpoints to remove");
|
|
2289
|
+
await sql.exec("DELETE FROM audit_log WHERE action = 'audit.checkpoint'");
|
|
2290
|
+
try {
|
|
2291
|
+
await red(deployment);
|
|
2292
|
+
} finally {
|
|
2293
|
+
for (const row of rows) await insertRow(sql, "audit_log", row);
|
|
2294
|
+
}
|
|
2295
|
+
}));
|
|
2296
|
+
await until("readiness recovered", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
|
|
2297
|
+
return "a recent heartbeat without a checkpoint is red; restoring the checkpoint recovers";
|
|
2298
|
+
}
|
|
2299
|
+
/** A hole before the latest signed prefix must stop the next scheduled checkpoint. */
|
|
2300
|
+
async function middleCut(deployment, { admin }) {
|
|
2301
|
+
expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before the middle cut");
|
|
2302
|
+
let seq = 0;
|
|
2303
|
+
await using(deployment.database(), async (sql) => {
|
|
2304
|
+
const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
|
|
2305
|
+
const [latest] = await sql.query("SELECT seq, metadata FROM audit_log WHERE author = 'vault' AND action = 'audit.checkpoint' AND decision = 'allow' ORDER BY seq DESC LIMIT 1");
|
|
2306
|
+
expect(latest !== void 0, "no signed prefix to cut from");
|
|
2307
|
+
const [heartbeat] = await query(sql, `SELECT h.seq FROM audit_log h JOIN audit_log c ON c.seq = h.seq + 1
|
|
2308
|
+
WHERE h.action = 'audit.heartbeat' AND h.seq > 0 AND c.author = 'vault' AND c.action = 'audit.checkpoint'
|
|
2309
|
+
AND c.decision = 'allow' AND c.seq < $1 AND NOT EXISTS
|
|
2310
|
+
(SELECT 1 FROM audit_log linked WHERE linked.related_seq IN (h.seq, c.seq)) ORDER BY h.seq LIMIT 1`, [JSON.parse(latest.metadata).seq]);
|
|
2311
|
+
expect(heartbeat !== void 0, "no older heartbeat and checkpoint to cut");
|
|
2312
|
+
seq = Number(heartbeat.seq);
|
|
2313
|
+
const kept = await query(sql, "SELECT * FROM audit_log WHERE seq >= $1 AND seq <= $2 ORDER BY seq", [seq, seq + 1]);
|
|
2314
|
+
expect(kept.length === 2, "the middle cut needs two adjacent entries");
|
|
2315
|
+
await appendOnlyLifted(sql, async () => {
|
|
2316
|
+
await query(sql, "DELETE FROM audit_log WHERE seq >= $1 AND seq <= $2", [seq, seq + 1]);
|
|
2317
|
+
try {
|
|
2318
|
+
await deployment.scheduled({ allowFailure: true });
|
|
2319
|
+
let checkpoint;
|
|
2320
|
+
await until("the checkpoint after the cut", async () => {
|
|
2321
|
+
[checkpoint] = await query(sql, `SELECT decision, code, metadata FROM audit_log
|
|
2322
|
+
WHERE author = 'vault' AND action = 'audit.checkpoint' AND seq >= $1 ORDER BY seq LIMIT 1`, [head.next_seq]);
|
|
2323
|
+
return checkpoint !== void 0;
|
|
2324
|
+
}, 10);
|
|
2325
|
+
expect(checkpoint?.decision === "deny" && checkpoint.code === "log_broken", "the checkpoint signed over a middle cut", checkpoint);
|
|
2326
|
+
const detail = JSON.parse(checkpoint.metadata);
|
|
2327
|
+
expect(detail.reason.includes(`sequence gap: expected seq ${seq}`), "the checkpoint did not name the middle gap", detail);
|
|
2328
|
+
await red(deployment);
|
|
2329
|
+
} finally {
|
|
2330
|
+
await transaction(sql, async () => {
|
|
2331
|
+
for (const row of kept) await insertRow(sql, "audit_log", row);
|
|
2332
|
+
});
|
|
2333
|
+
}
|
|
2334
|
+
});
|
|
2335
|
+
});
|
|
2336
|
+
await deployment.scheduled();
|
|
2337
|
+
await until("readiness recovered after the middle cut", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
|
|
2338
|
+
expect((await admin.api.audit.verify()).ok, "the restored middle cut did not verify");
|
|
2339
|
+
return `entries ${seq} and ${seq + 1}, before the latest signed prefix, cut: checkpoint log_broken, readiness red; restored and recovered`;
|
|
2340
|
+
}
|
|
2341
|
+
//#endregion
|
|
2342
|
+
//#region src/checks/canaries.ts
|
|
2343
|
+
function calls(people) {
|
|
2344
|
+
const places = [
|
|
2345
|
+
DEV,
|
|
2346
|
+
PROD,
|
|
2347
|
+
BULK
|
|
2348
|
+
].map((path) => {
|
|
2349
|
+
const [project, environment] = path.split("/");
|
|
2350
|
+
return {
|
|
2351
|
+
project,
|
|
2352
|
+
environment
|
|
2353
|
+
};
|
|
2354
|
+
});
|
|
2355
|
+
return getCalls({
|
|
2356
|
+
places,
|
|
2357
|
+
secrets: places.slice(0, 2).map((place) => ({
|
|
2358
|
+
...place,
|
|
2359
|
+
key: "API_KEY"
|
|
2360
|
+
})),
|
|
2361
|
+
members: [...[
|
|
2362
|
+
people.admin,
|
|
2363
|
+
people.reader,
|
|
2364
|
+
people.leaver,
|
|
2365
|
+
people.bulk
|
|
2366
|
+
].map((person) => person.member), SERVICE],
|
|
2367
|
+
services: [SERVICE]
|
|
2368
|
+
});
|
|
2369
|
+
}
|
|
2370
|
+
function pages(people) {
|
|
2371
|
+
const users = [
|
|
2372
|
+
people.admin,
|
|
2373
|
+
people.reader,
|
|
2374
|
+
people.leaver,
|
|
2375
|
+
people.bulk
|
|
2376
|
+
];
|
|
2377
|
+
return [
|
|
2378
|
+
"/",
|
|
2379
|
+
"/login",
|
|
2380
|
+
"/unregistered",
|
|
2381
|
+
"/account",
|
|
2382
|
+
"/access",
|
|
2383
|
+
"/audit",
|
|
2384
|
+
"/settings",
|
|
2385
|
+
"/auth/device",
|
|
2386
|
+
"/projects",
|
|
2387
|
+
`/projects/${PROJECT}`,
|
|
2388
|
+
...[
|
|
2389
|
+
DEV,
|
|
2390
|
+
PROD,
|
|
2391
|
+
BULK
|
|
2392
|
+
].map((path) => `/projects/${path}`),
|
|
2393
|
+
"/users",
|
|
2394
|
+
...users.map((person) => `/users/${encodeURIComponent(person.email)}`),
|
|
2395
|
+
"/tokens",
|
|
2396
|
+
`/tokens/${SERVICE.slice(6)}`
|
|
2397
|
+
];
|
|
2398
|
+
}
|
|
2399
|
+
async function canaryScan(deployment, people, canaries) {
|
|
2400
|
+
const values = Object.values(canaries);
|
|
2401
|
+
const leaks = [];
|
|
2402
|
+
const look = (where, content) => {
|
|
2403
|
+
const bytes = Buffer.from(content);
|
|
2404
|
+
const found = values.filter((value) => bytes.includes(value));
|
|
2405
|
+
if (found.length > 0) leaks.push(`${where}: ${found.length} value${found.length === 1 ? "" : "s"}`);
|
|
2406
|
+
};
|
|
2407
|
+
const token = (value) => (url) => fetch(url, { headers: { authorization: `Bearer ${value}` } });
|
|
2408
|
+
const browsers = {
|
|
2409
|
+
"the root admin": people.admin.browser,
|
|
2410
|
+
"the reader": people.reader.browser,
|
|
2411
|
+
"the bulk reader": people.bulk.browser,
|
|
2412
|
+
"the leaver, removed": people.leaver.browser,
|
|
2413
|
+
"the stranger": people.stranger.browser
|
|
2414
|
+
};
|
|
2415
|
+
const callers = [
|
|
2416
|
+
["no one", (url) => fetch(url, { redirect: "manual" })],
|
|
2417
|
+
...Object.entries(browsers).map(([name, browser]) => [name, (url) => browser.fetch(url)]),
|
|
2418
|
+
["the leaver's CLI, removed", token(people.leaver.cliToken)],
|
|
2419
|
+
["the service, removed", token(people.service.token)]
|
|
2420
|
+
];
|
|
2421
|
+
let answers = 0;
|
|
2422
|
+
const routes = calls(people);
|
|
2423
|
+
for (const { key, url } of getUrls(deployment.origin, routes)) for (const [name, fetchAs] of callers) {
|
|
2424
|
+
look(`${key} as ${name}`, await (await fetchAs(url)).text());
|
|
2425
|
+
answers++;
|
|
2426
|
+
}
|
|
2427
|
+
const paths = pages(people);
|
|
2428
|
+
for (const path of paths) for (const [name, fetchAs] of callers.slice(0, 1 + Object.keys(browsers).length)) {
|
|
2429
|
+
look(`the page ${path} as ${name}`, await (await fetchAs(`${deployment.origin}${path}`)).text());
|
|
2430
|
+
answers++;
|
|
2431
|
+
}
|
|
2432
|
+
const stored = [];
|
|
2433
|
+
await using(deployment.database(), async (sql) => {
|
|
2434
|
+
const names = await tables(sql);
|
|
2435
|
+
const control = canary();
|
|
2436
|
+
await sql.exec(`CREATE TABLE conformance_canary_probe (value ${sql.engine === "postgres" ? "bytea" : "BLOB"})`);
|
|
2437
|
+
try {
|
|
2438
|
+
await sql.query(`INSERT INTO conformance_canary_probe VALUES (${sql.engine === "postgres" ? "$1" : "?"})`, [Buffer.from(control)]);
|
|
2439
|
+
let found = false;
|
|
2440
|
+
await scanTables(sql, ["conformance_canary_probe"], (_, bytes) => {
|
|
2441
|
+
found ||= Buffer.from(bytes).includes(control);
|
|
2442
|
+
});
|
|
2443
|
+
expect(found, "the database scanner missed its planted binary canary");
|
|
2444
|
+
} finally {
|
|
2445
|
+
await sql.exec("DROP TABLE conformance_canary_probe");
|
|
2446
|
+
}
|
|
2447
|
+
await scanTables(sql, names, look);
|
|
2448
|
+
stored.push(`${names.length} tables, with a binary positive control`);
|
|
2449
|
+
});
|
|
2450
|
+
if (deployment.databaseFile !== null) {
|
|
2451
|
+
look("the database's file", files(deployment.databaseFile));
|
|
2452
|
+
stored.push("the database file and WAL");
|
|
2453
|
+
}
|
|
2454
|
+
look("the processes' output", deployment.output());
|
|
2455
|
+
stored.push("the processes' output");
|
|
2456
|
+
expect(leaks.length === 0, "a value was found outside a reveal", leaks);
|
|
2457
|
+
return `${answers} answers from ${Object.keys(routes).length} GET routes and ${paths.length} pages as ${callers.length} callers, and ${stored.join(", ")}: no value`;
|
|
2458
|
+
}
|
|
2459
|
+
/** A SQLite file and its write-ahead log, as bytes: what anyone with the disk would read. */
|
|
2460
|
+
function files(path) {
|
|
2461
|
+
expect(existsSync(path), `the store file could not be found: ${path}`);
|
|
2462
|
+
return Buffer.concat([path, `${path}-wal`].filter((file) => existsSync(file)).map((file) => readFileSync(file)));
|
|
2463
|
+
}
|
|
2464
|
+
//#endregion
|
|
2465
|
+
//#region src/suite.ts
|
|
2466
|
+
/** The names of the checks that failed. */
|
|
2467
|
+
async function conform(deployment, options) {
|
|
2468
|
+
const report = new Report();
|
|
2469
|
+
await report.check("health", {}, () => health(deployment));
|
|
2470
|
+
await anonymousChecks(report, deployment.origin, { health: false });
|
|
2471
|
+
const admin = await report.check("sign-in", {}, () => signInAdmin(deployment));
|
|
2472
|
+
const canaries = await report.check("setup", { admin }, ({ admin }) => setUp(admin));
|
|
2473
|
+
const people = await report.check("personas", {
|
|
2474
|
+
admin,
|
|
2475
|
+
canaries
|
|
2476
|
+
}, ({ admin }) => personas(deployment, admin));
|
|
2477
|
+
const all = {
|
|
2478
|
+
people,
|
|
2479
|
+
canaries
|
|
2480
|
+
};
|
|
2481
|
+
await report.check("members only", { people }, ({ people }) => membersOnly(deployment, people));
|
|
2482
|
+
await report.check("grant scoping", all, ({ people, canaries }) => grantScoping(people, canaries));
|
|
2483
|
+
await report.check("reveals audited", all, ({ people, canaries }) => revealAudited(deployment, people, canaries, "reveal"));
|
|
2484
|
+
await report.check("runs audited", all, ({ people, canaries }) => revealAudited(deployment, people, canaries, "run"));
|
|
2485
|
+
await report.check("cross-site", all, ({ people, canaries }) => crossSite(people, canaries));
|
|
2486
|
+
const live = await report.check("live setup", {
|
|
2487
|
+
admin,
|
|
2488
|
+
canaries
|
|
2489
|
+
}, ({ admin, canaries }) => setUpLive(admin, canaries));
|
|
2490
|
+
await tokenChecks(report, deployment.origin, live ?? {});
|
|
2491
|
+
await report.check("offboarding", all, ({ people, canaries }) => offboarding(deployment, people, canaries));
|
|
2492
|
+
await report.check("bulk limit", { people }, ({ people }) => bulkLimit$1(people, options.bulkLimit));
|
|
2493
|
+
await report.check("checkpoints", { people }, ({ people }) => checkpoints(deployment, people));
|
|
2494
|
+
await report.check("keys behind writes", { people }, ({ people }) => writesAgree(deployment));
|
|
2495
|
+
await report.check("no audit, no value", all, ({ people, canaries }) => noAuditNoValue(deployment, people, canaries));
|
|
2496
|
+
await report.check("access authorship", { people }, ({ people }) => accessAuthorship(deployment, people));
|
|
2497
|
+
await report.check("no audit, no access", { people }, ({ people }) => noAuditNoAccess(deployment, people));
|
|
2498
|
+
await report.check("full verification", { people }, ({ people }) => verification(deployment, people));
|
|
2499
|
+
await report.check("checkpoint refused", {}, () => refusedCheckpoint(deployment));
|
|
2500
|
+
await report.check("checkpoint missing", {}, () => missingCheckpoint(deployment));
|
|
2501
|
+
await report.check("app login", {}, () => appLogin(deployment));
|
|
2502
|
+
await report.check("vault login", {}, () => vaultLogin(deployment));
|
|
2503
|
+
await report.check("forged grant", { people }, ({ people }) => memberTampering(deployment, people, "grant"));
|
|
2504
|
+
await report.check("forged member", { people }, ({ people }) => memberTampering(deployment, people, "member"));
|
|
2505
|
+
await report.check("stale member", { people }, ({ people }) => memberTampering(deployment, people, "stale"));
|
|
2506
|
+
await report.check("sealing race", { people }, ({ people }) => sealingRace(deployment, people));
|
|
2507
|
+
await report.check("forged credential", { people }, ({ people }) => forgedCredential(deployment, people));
|
|
2508
|
+
await report.check("forged identity", { people }, ({ people }) => forgedIdentity(deployment, people));
|
|
2509
|
+
await report.check("forged approval", { people }, ({ people }) => forgedApproval(deployment, people));
|
|
2510
|
+
await report.check("edited generation", { people }, ({ people }) => editedGeneration(deployment, people));
|
|
2511
|
+
await report.check("canary scan", all, ({ people, canaries }) => canaryScan(deployment, people, canaries));
|
|
2512
|
+
for (const author of ["app", "vault"]) await report.check(`${author} rewritten`, { people }, ({ people }) => rewrittenEntry(deployment, people, author));
|
|
2513
|
+
await report.check("vault forged", { people }, ({ people }) => forgedVaultEntry(deployment, people));
|
|
2514
|
+
await report.check("middle gap", { people }, ({ people }) => missingEntry(deployment, people, "middle"));
|
|
2515
|
+
await report.check("middle cut", { people }, ({ people }) => middleCut(deployment, people));
|
|
2516
|
+
await report.check("first gap", { people }, ({ people }) => missingEntry(deployment, people, "first"));
|
|
2517
|
+
await report.check("batch gap", { people }, ({ people }) => missingEntry(deployment, people, "batch"));
|
|
2518
|
+
await report.check("earlier checkpoint", { people }, ({ people }) => earlierCheckpoint(deployment, people));
|
|
2519
|
+
await report.check("tail deleted", { people }, ({ people }) => deletedTail(deployment, people));
|
|
2520
|
+
return report.failed;
|
|
2521
|
+
}
|
|
2522
|
+
/**
|
|
2523
|
+
* What can be checked of an instance someone else runs: as no one, and with
|
|
2524
|
+
* a token that reads a canary when given one. Nothing is written but the
|
|
2525
|
+
* audit log's entries for the token's reads.
|
|
2526
|
+
*/
|
|
2527
|
+
async function probe(origin, live) {
|
|
2528
|
+
const report = new Report();
|
|
2529
|
+
await anonymousChecks(report, origin, { health: true });
|
|
2530
|
+
if (live.token !== void 0) await tokenChecks(report, origin, live);
|
|
2531
|
+
return report.failed;
|
|
2532
|
+
}
|
|
2533
|
+
//#endregion
|
|
2534
|
+
//#region src/main.ts
|
|
2535
|
+
const USAGE = `usage:
|
|
2536
|
+
coffre-conformance workers [<dir>] --postgres <owner URL> --runtime <coffre_runtime URL>
|
|
2537
|
+
--vault-runtime <coffre_vault_runtime URL> [options]
|
|
2538
|
+
coffre-conformance node [<dir>] [options]
|
|
2539
|
+
coffre-conformance probe <url> [--token <service token> --canary <project>/<env>/<KEY>[=<value>]]
|
|
2540
|
+
|
|
2541
|
+
<dir> the deployment, as \`coffre init\` wrote it (default: here)
|
|
2542
|
+
--port <n> coffre's port; the IdP gets the next, wrangler's inspector the one after (3082)
|
|
2543
|
+
--bulk-limit <n> the vault's bulkLimit count, when not the default (1000)
|
|
2544
|
+
--postgres <url> workers: a Postgres login that may create databases; one is made for the run
|
|
2545
|
+
--runtime <url> workers: the same server as coffre_runtime, the login the app runs as
|
|
2546
|
+
--vault-runtime <url>
|
|
2547
|
+
workers: the same server as coffre_vault_runtime, the login the vault runs as
|
|
2548
|
+
|
|
2549
|
+
probe checks a running instance from outside, and writes nothing to it:
|
|
2550
|
+
as no one health, headers, every route refusing no one, changes from
|
|
2551
|
+
another site refused, /api/auth, nothing like a value shown
|
|
2552
|
+
with --token a service token that reads the canary's environment and
|
|
2553
|
+
holds auditor on its project: its value nowhere but its
|
|
2554
|
+
reveal, the reveal audited, nothing else in reach
|
|
2555
|
+
--token or COFFRE_TOKEN
|
|
2556
|
+
--canary the value after =, or in COFFRE_CONFORMANCE_CANARY, or on
|
|
2557
|
+
stdin, so it stays out of the shell's history
|
|
2558
|
+
|
|
2559
|
+
Each run with a token adds a few entries to the instance's audit log, which
|
|
2560
|
+
is append-only, so they stay: the canary's read, and the reads it was refused.
|
|
2561
|
+
It changes nothing else.`;
|
|
2562
|
+
const { values, positionals } = parseArgs({
|
|
2563
|
+
allowPositionals: true,
|
|
2564
|
+
options: {
|
|
2565
|
+
port: {
|
|
2566
|
+
type: "string",
|
|
2567
|
+
default: "3082"
|
|
2568
|
+
},
|
|
2569
|
+
"bulk-limit": {
|
|
2570
|
+
type: "string",
|
|
2571
|
+
default: "1000"
|
|
2572
|
+
},
|
|
2573
|
+
postgres: { type: "string" },
|
|
2574
|
+
runtime: { type: "string" },
|
|
2575
|
+
"vault-runtime": { type: "string" },
|
|
2576
|
+
token: { type: "string" },
|
|
2577
|
+
canary: { type: "string" },
|
|
2578
|
+
help: {
|
|
2579
|
+
type: "boolean",
|
|
2580
|
+
short: "h"
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
});
|
|
2584
|
+
const [command, target] = positionals;
|
|
2585
|
+
if (values.help || command === void 0) exit(values.help ? 0 : 2, USAGE);
|
|
2586
|
+
if (command === "probe") {
|
|
2587
|
+
if (target === void 0) exit(2, USAGE);
|
|
2588
|
+
const origin = new URL(target).origin;
|
|
2589
|
+
const token = values.token ?? process.env.COFFRE_TOKEN;
|
|
2590
|
+
let canary;
|
|
2591
|
+
if (token !== void 0) {
|
|
2592
|
+
if (values.canary === void 0) exit(2, "with a token, name its canary: --canary <project>/<environment>/<KEY>[=<value>]");
|
|
2593
|
+
try {
|
|
2594
|
+
canary = parseCanary(values.canary, values.canary.includes("=") ? void 0 : await canaryValue());
|
|
2595
|
+
} catch (error) {
|
|
2596
|
+
exit(2, error instanceof Error ? error.message : String(error));
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2599
|
+
console.log(`coffre-conformance: probing ${origin}${token === void 0 ? ", as no one" : ", as no one and with a token"}`);
|
|
2600
|
+
if (token !== void 0) console.log(" (the token's reads add a few entries to the instance's audit log, for good)");
|
|
2601
|
+
const failed = await probe(origin, {
|
|
2602
|
+
token,
|
|
2603
|
+
canary
|
|
2604
|
+
});
|
|
2605
|
+
exit(failed.length === 0 ? 0 : 1, summary(failed));
|
|
2606
|
+
}
|
|
2607
|
+
/** The canary's value from the environment, or the first line of stdin when it is piped. */
|
|
2608
|
+
async function canaryValue() {
|
|
2609
|
+
const given = process.env.COFFRE_CONFORMANCE_CANARY;
|
|
2610
|
+
if (given !== void 0 && given !== "") return given;
|
|
2611
|
+
if (process.stdin.isTTY) return void 0;
|
|
2612
|
+
let text = "";
|
|
2613
|
+
for await (const chunk of process.stdin) text += String(chunk);
|
|
2614
|
+
return text.split(/\r?\n/)[0];
|
|
2615
|
+
}
|
|
2616
|
+
if (command !== "workers" && command !== "node") exit(2, USAGE);
|
|
2617
|
+
const kind = command;
|
|
2618
|
+
const dir = resolve(target ?? ".");
|
|
2619
|
+
const port = Number(values.port);
|
|
2620
|
+
const bulkLimit = Number(values["bulk-limit"]);
|
|
2621
|
+
if (!Number.isInteger(port) || !Number.isInteger(bulkLimit) || bulkLimit < 1) exit(2, USAGE);
|
|
2622
|
+
console.log(`coffre-conformance: ${kind} from ${dir}`);
|
|
2623
|
+
let deployment;
|
|
2624
|
+
try {
|
|
2625
|
+
deployment = await boot(kind, dir, {
|
|
2626
|
+
port,
|
|
2627
|
+
postgres: values.postgres,
|
|
2628
|
+
runtime: values.runtime,
|
|
2629
|
+
vaultRuntime: values["vault-runtime"]
|
|
2630
|
+
});
|
|
2631
|
+
} catch (error) {
|
|
2632
|
+
const detail = error instanceof Failure && error.detail !== void 0 ? `\n${String(error.detail)}` : "";
|
|
2633
|
+
exit(1, `the deployment did not start: ${error instanceof Error ? error.message : String(error)}${detail}`);
|
|
2634
|
+
}
|
|
2635
|
+
console.log(` up on ${deployment.origin}`);
|
|
2636
|
+
let failed;
|
|
2637
|
+
try {
|
|
2638
|
+
failed = await conform(deployment, { bulkLimit });
|
|
2639
|
+
if (failed.length > 0) console.error(`\nThe processes' output, last lines:\n${deployment.output(4e3)}`);
|
|
2640
|
+
} finally {
|
|
2641
|
+
await deployment.stop();
|
|
2642
|
+
}
|
|
2643
|
+
exit(failed.length === 0 ? 0 : 1, summary(failed));
|
|
2644
|
+
function summary(failed) {
|
|
2645
|
+
return failed.length === 0 ? "conformant" : `NOT conformant: ${failed.join(", ")}`;
|
|
2646
|
+
}
|
|
2647
|
+
function exit(code, message) {
|
|
2648
|
+
(code === 0 ? console.log : console.error)(message);
|
|
2649
|
+
process.exit(code);
|
|
2650
|
+
}
|
|
2651
|
+
//#endregion
|
|
2652
|
+
export {};
|