@fuzdev/fuz_app 0.186.0 → 0.187.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/dist/auth/CLAUDE.md +10 -5
- package/dist/auth/account_queries.d.ts +8 -5
- package/dist/auth/account_queries.d.ts.map +1 -1
- package/dist/auth/account_queries.js +8 -5
- package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
- package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_action_specs.js +2 -1
- package/dist/auth/role_grant_offer_actions.d.ts +12 -0
- package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_actions.js +48 -20
- package/dist/auth/role_grant_offer_queries.d.ts +16 -3
- package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_queries.js +24 -4
- package/dist/auth/role_grant_queries.d.ts +102 -11
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +129 -15
- package/dist/db/migrate.d.ts +46 -22
- package/dist/db/migrate.d.ts.map +1 -1
- package/dist/db/migrate.js +103 -85
- package/dist/server/app_backend.d.ts.map +1 -1
- package/dist/server/app_backend.js +1 -4
- package/dist/testing/CLAUDE.md +35 -0
- package/dist/testing/cross_backend/backend_both_live.d.ts +47 -0
- package/dist/testing/cross_backend/backend_both_live.d.ts.map +1 -0
- package/dist/testing/cross_backend/backend_both_live.js +566 -0
- package/dist/testing/cross_backend/backend_swap.d.ts.map +1 -1
- package/dist/testing/cross_backend/backend_swap.js +37 -201
- package/dist/testing/cross_backend/shared_database_backends.d.ts +175 -0
- package/dist/testing/cross_backend/shared_database_backends.d.ts.map +1 -0
- package/dist/testing/cross_backend/shared_database_backends.js +298 -0
- package/dist/testing/cross_backend/spine_suites.d.ts +1 -1
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
import '../assert_dev_env.js';
|
|
2
|
+
/**
|
|
3
|
+
* Both spines live: two backends serving one Postgres database at once. The
|
|
4
|
+
* backend swap (`backend_swap.ts`) runs them one at a time; here a credential
|
|
5
|
+
* minted on one is used, revoked and refused across the other while both
|
|
6
|
+
* run, and the per-process state each keeps is pinned as the declared limit
|
|
7
|
+
* it is.
|
|
8
|
+
*
|
|
9
|
+
* Per order, over a database emptied first, never reset in between
|
|
10
|
+
* (`_testing_reset` would drop what the other spine holds):
|
|
11
|
+
*
|
|
12
|
+
* - **Boot state** — the second spawned before the first's bootstrap. Its
|
|
13
|
+
* `/status` still advertises bootstrap and its daemon token resolves no
|
|
14
|
+
* keeper, both read once at its boot. A `POST /bootstrap` on it is refused
|
|
15
|
+
* by the database's lock, which also closes its window; its daemon token
|
|
16
|
+
* still resolves no keeper until it restarts. Both stop before the next
|
|
17
|
+
* phase.
|
|
18
|
+
* - **Boot** — the first spawned, its keeper bootstrapped, then the second
|
|
19
|
+
* spawned over that database, so its daemon leg resolves the keeper at
|
|
20
|
+
* assembly (the spawn order the per-process keeper cache forces).
|
|
21
|
+
* - **Live cross-spine credential honor** — mint on the first, prove it on
|
|
22
|
+
* both, revoke on the second, refused on the first at once: a session
|
|
23
|
+
* logged out and one revoked by id, an API token revoked, a password
|
|
24
|
+
* changed, an account deleted; and an offer the first made accepted on the
|
|
25
|
+
* second, after which the first's admin gate passes. Each revoke is
|
|
26
|
+
* bracketed by per-table content digests read after both backends drain,
|
|
27
|
+
* so the tables it changed are exactly the ones it should, and every test
|
|
28
|
+
* ends with both backends' invariant check.
|
|
29
|
+
* - **Declared limits** — what each process keeps to itself, pinned by name
|
|
30
|
+
* (`docs/security.md` § Single-Process Architecture): a socket open on the
|
|
31
|
+
* first outlives the second's password change, logout and token revoke,
|
|
32
|
+
* and keeps answering per-message calls; a role-gated socket on the first
|
|
33
|
+
* outlives the second's role revoke and keeps receiving the account's
|
|
34
|
+
* pushes; an offer made on either notifies no socket on the other. Each
|
|
35
|
+
* assertion holds today's behavior, so a cross-process close or
|
|
36
|
+
* notification that lands within `STAYS_OPEN_MS` fails it loudly — then
|
|
37
|
+
* the limit is lifted and the test flipped.
|
|
38
|
+
* - **Close** — both drained, invariants checked on both, then each torn down
|
|
39
|
+
* (exit `0`, no unexpected error in its log, `spawn_backend`).
|
|
40
|
+
*
|
|
41
|
+
* **What the configs must share** is the swap's
|
|
42
|
+
* (`to_shared_database_problems`): the database (`DATABASE_URL`, exact
|
|
43
|
+
* strings), the cookie name and `SECRET_FUZ_COOKIE_KEYS`, a startup reset
|
|
44
|
+
* off, and the drain and invariant backdoors. Their daemon-token, bootstrap
|
|
45
|
+
* token and fact directories stay their own (each config's paths). The
|
|
46
|
+
* socket cases run when the first backend declares `ws` and
|
|
47
|
+
* `ws_account_actions` (and `ws_role_gated` for the gated case; `ws` on both
|
|
48
|
+
* for the notification case).
|
|
49
|
+
*
|
|
50
|
+
* @module
|
|
51
|
+
*/
|
|
52
|
+
import { afterAll, assert, beforeAll, describe, test } from 'vitest';
|
|
53
|
+
import { SessionListOutput, SessionRevokeOutput, TokenCreateOutput, TokenRevokeOutput } from '../../auth/account_action_specs.js';
|
|
54
|
+
import { AccountDeleteOutput, InviteCreateOutput } from '../../auth/admin_action_specs.js';
|
|
55
|
+
import { RoleGrantAssignOutput, RoleGrantOfferAcceptOutput, RoleGrantOfferCreateOutput, RoleGrantOfferHistoryOutput, RoleGrantOfferListOutput, RoleGrantRevokeOutput } from '../../auth/role_grant_offer_action_specs.js';
|
|
56
|
+
import { ROLE_GRANT_OFFER_RECEIVED_NOTIFICATION_METHOD } from '../../auth/role_grant_offer_notifications.js';
|
|
57
|
+
import { ROLE_ADMIN } from '../../auth/role_schema.js';
|
|
58
|
+
import { create_db } from '../../db/create_db.js';
|
|
59
|
+
import { ERROR_ALREADY_BOOTSTRAPPED, ERROR_AUTHENTICATION_REQUIRED, ERROR_INSUFFICIENT_PERMISSIONS, ERROR_INVALID_CREDENTIALS } from '../../http/error_schemas.js';
|
|
60
|
+
import { bootstrap } from '../transports/bootstrap.js';
|
|
61
|
+
import { is_notification_with } from '../transports/ws_client.js';
|
|
62
|
+
import { create_admitted_ws_transport } from '../transports/ws_transport.js';
|
|
63
|
+
import { test_if } from './capabilities.js';
|
|
64
|
+
import { error_reason } from './cross_rpc.js';
|
|
65
|
+
import { spawn_backend } from './spawn_backend.js';
|
|
66
|
+
import { assert_backend_invariants, backend_daemon_rpc, backend_post_json, backend_rpc_as, backend_transport, drain_backend_effects, passes_admin_gate, read_shared_database_snapshot, teardown_live_backend, to_changed_tables, to_shared_database_problems, verified_account_id, with_live_backend } from './shared_database_backends.js';
|
|
67
|
+
import { SPINE_ACCOUNT_LOGIN_PATH, SPINE_ACCOUNT_LOGOUT_PATH, SPINE_ACCOUNT_PASSWORD_PATH, SPINE_ACCOUNT_SIGNUP_PATH, SPINE_ACCOUNT_STATUS_PATH, SPINE_PARTICIPANT_ROLE, SPINE_ROLE_GATED_WS_PATH } from './spine_surface_constants.js';
|
|
68
|
+
const MEMBER_PASSWORD = 'both-live-password-before';
|
|
69
|
+
const MEMBER_PASSWORD_CHANGED = 'both-live-password-after';
|
|
70
|
+
/**
|
|
71
|
+
* How long a socket that must stay open, or must hear nothing, is watched —
|
|
72
|
+
* long past the same-process close the other suites see in milliseconds.
|
|
73
|
+
*/
|
|
74
|
+
const STAYS_OPEN_MS = 1500;
|
|
75
|
+
/** What a spawning test may take past its backends' startup windows (ms). */
|
|
76
|
+
const BOTH_LIVE_PHASE_WORK_MS = 30_000;
|
|
77
|
+
/** The tables a step writes, by the spine-owned table names. */
|
|
78
|
+
const TABLE_AUDIT_LOG = 'public.audit_log';
|
|
79
|
+
const TABLE_AUTH_SESSION = 'public.auth_session';
|
|
80
|
+
const TABLE_API_TOKEN = 'public.api_token';
|
|
81
|
+
const TABLE_ACCOUNT = 'public.account';
|
|
82
|
+
const TABLE_ACTOR = 'public.actor';
|
|
83
|
+
const TABLE_ROLE_GRANT = 'public.role_grant';
|
|
84
|
+
const TABLE_ROLE_GRANT_OFFER = 'public.role_grant_offer';
|
|
85
|
+
/** The `Cookie` pair of each `Set-Cookie` line, for a socket upgrade. */
|
|
86
|
+
const to_cookie_pairs = (set_cookies) => set_cookies.map((line) => line.split(';')[0].trim());
|
|
87
|
+
/** Log `username` in on `config`'s backend, asserting it succeeds; its `Set-Cookie` lines. */
|
|
88
|
+
const login = async (config, username, password) => {
|
|
89
|
+
const res = await backend_post_json(backend_transport(config), SPINE_ACCOUNT_LOGIN_PATH, {
|
|
90
|
+
username,
|
|
91
|
+
password
|
|
92
|
+
});
|
|
93
|
+
assert.strictEqual(res.status, 200, `${config.name}: ${username} logs in`);
|
|
94
|
+
assert.strictEqual(res.set_cookies.length, 1, 'a login sets exactly the session cookie');
|
|
95
|
+
return res.set_cookies;
|
|
96
|
+
};
|
|
97
|
+
/** A login on `config` refused 401 `invalid_credentials`. */
|
|
98
|
+
const assert_login_refused = async (config, username, password, message) => {
|
|
99
|
+
const res = await backend_post_json(backend_transport(config), SPINE_ACCOUNT_LOGIN_PATH, {
|
|
100
|
+
username,
|
|
101
|
+
password
|
|
102
|
+
});
|
|
103
|
+
assert.strictEqual(res.status, 401, `${config.name}: ${message}`);
|
|
104
|
+
assert.strictEqual(res.body.error, ERROR_INVALID_CREDENTIALS);
|
|
105
|
+
};
|
|
106
|
+
/** The keeper invites `username` on the first backend, which signs it up. */
|
|
107
|
+
const create_member = async (live, username) => {
|
|
108
|
+
const { config } = live.a;
|
|
109
|
+
await backend_rpc_as(config, live.keeper_cookies, 'invite_create', { username }, InviteCreateOutput);
|
|
110
|
+
const signup = await backend_post_json(backend_transport(config), SPINE_ACCOUNT_SIGNUP_PATH, {
|
|
111
|
+
username,
|
|
112
|
+
password: MEMBER_PASSWORD
|
|
113
|
+
});
|
|
114
|
+
assert.strictEqual(signup.status, 200, `signup: ${JSON.stringify(signup.body)}`);
|
|
115
|
+
const body = signup.body;
|
|
116
|
+
return { username, account_id: body.account.id, actor_id: body.actor.id };
|
|
117
|
+
};
|
|
118
|
+
/** Mint a full eternal API token on `config` over a session. */
|
|
119
|
+
const create_token = (config, cookies) => backend_rpc_as(config, cookies, 'account_token_create', { scope: { kind: 'full' }, lifetime: { kind: 'eternal' } }, TokenCreateOutput);
|
|
120
|
+
/** Assert a credential authenticates as `account_id` on both backends. */
|
|
121
|
+
const assert_live_on_both = async (live, credential, account_id, what) => {
|
|
122
|
+
for (const { config } of [live.a, live.b]) {
|
|
123
|
+
assert.strictEqual(await verified_account_id(config, credential), account_id, `${config.name}: ${what} authenticates`);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
/** Assert a credential is refused 401 on `handle`'s backend. */
|
|
127
|
+
const assert_refused = async (handle, credential, what) => {
|
|
128
|
+
assert.isNull(await verified_account_id(handle.config, credential), `${handle.config.name}: ${what} is refused`);
|
|
129
|
+
};
|
|
130
|
+
const drain_both = async (live) => {
|
|
131
|
+
await drain_backend_effects(live.a);
|
|
132
|
+
await drain_backend_effects(live.b);
|
|
133
|
+
};
|
|
134
|
+
/** Drain both backends, then read the snapshot — nothing detached lands after it. */
|
|
135
|
+
const drained_snapshot = async (live, db) => {
|
|
136
|
+
await drain_both(live);
|
|
137
|
+
return read_shared_database_snapshot(db);
|
|
138
|
+
};
|
|
139
|
+
const assert_invariants_both = async (live) => {
|
|
140
|
+
await assert_backend_invariants(live.a);
|
|
141
|
+
await assert_backend_invariants(live.b);
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Assert the tables that changed between two drained snapshots are exactly
|
|
145
|
+
* `expected` — a step that wrote less missed its write, one that wrote more
|
|
146
|
+
* wrote what it shouldn't.
|
|
147
|
+
*/
|
|
148
|
+
const assert_changed_tables = (before, after, expected, step) => {
|
|
149
|
+
assert.deepStrictEqual(to_changed_tables(before, after), [...expected].sort(), `the tables ${step} changed`);
|
|
150
|
+
};
|
|
151
|
+
/** Open an admitted socket on `config`'s `ws_path` (or `path`) on a session. */
|
|
152
|
+
const open_session_socket = (config, cookies, path = config.ws_path) => create_admitted_ws_transport({
|
|
153
|
+
base_url: config.base_url,
|
|
154
|
+
ws_path: path,
|
|
155
|
+
cookies: to_cookie_pairs(cookies)
|
|
156
|
+
});
|
|
157
|
+
/** Open an admitted socket on `config`'s `ws_path` on a bearer token (Origin-free). */
|
|
158
|
+
const open_bearer_socket = (config, token) => create_admitted_ws_transport({
|
|
159
|
+
base_url: config.base_url,
|
|
160
|
+
ws_path: config.ws_path,
|
|
161
|
+
cookies: [],
|
|
162
|
+
origin: null,
|
|
163
|
+
headers: { authorization: `Bearer ${token}` }
|
|
164
|
+
});
|
|
165
|
+
/**
|
|
166
|
+
* Assert `socket` stays open past `STAYS_OPEN_MS` and still answers a
|
|
167
|
+
* per-message `account_verify` as `account_id` — dispatch re-reads the actor
|
|
168
|
+
* and its grants, never the session or token the socket opened on.
|
|
169
|
+
*/
|
|
170
|
+
const assert_socket_still_serves = async (socket, account_id, limit) => {
|
|
171
|
+
assert.isFalse(await socket.wait_for_close(STAYS_OPEN_MS), `${limit}: the socket closed (${socket.close_code}) — a cross-process close landed? ` +
|
|
172
|
+
'lift the declared limit and flip this test');
|
|
173
|
+
const verified = await socket.request('still_serves', 'account_verify', undefined);
|
|
174
|
+
assert.strictEqual(verified.id, account_id, `${limit}: the socket still acts as the account`);
|
|
175
|
+
};
|
|
176
|
+
/** Close sockets, ignoring a socket the server already closed. */
|
|
177
|
+
const close_sockets = async (sockets) => {
|
|
178
|
+
for (const socket of sockets) {
|
|
179
|
+
await socket?.close().catch(() => { });
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
/** The anonymous `/status` answer: its status and `bootstrap_available`. */
|
|
183
|
+
const read_status = async (config) => {
|
|
184
|
+
const res = await backend_transport(config)(SPINE_ACCOUNT_STATUS_PATH, { method: 'GET' });
|
|
185
|
+
const body = (await res.json());
|
|
186
|
+
return { status: res.status, bootstrap_available: body.bootstrap_available };
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Throw naming each way `order`'s configs can't run at once over
|
|
190
|
+
* `database_url` (`to_shared_database_problems`, and `offer_role` not `admin`).
|
|
191
|
+
*/
|
|
192
|
+
const assert_both_live = (order, database_url, offer_role) => {
|
|
193
|
+
const problems = [];
|
|
194
|
+
if (offer_role === ROLE_ADMIN) {
|
|
195
|
+
problems.push(`offer_role is ${ROLE_ADMIN}, the role the gated socket's grant, the accepted offer and the reverse notification offer use`);
|
|
196
|
+
}
|
|
197
|
+
problems.push(...to_shared_database_problems([order.first, order.second], database_url));
|
|
198
|
+
if (problems.length > 0) {
|
|
199
|
+
throw new Error(`backend both live '${order.label}': ${problems.join('; ')}`);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Describe both spines live over each order (§module doc): per order, the
|
|
204
|
+
* boot-state limits, the boot, the live cross-spine credential cases, the
|
|
205
|
+
* declared per-process limits, and the close, run in turn. Each test after the
|
|
206
|
+
* boot fails naming it when the boot did not complete.
|
|
207
|
+
*
|
|
208
|
+
* @param options - the orders, the shared database, and how to empty it
|
|
209
|
+
*/
|
|
210
|
+
export const describe_backend_both_live_tests = (options) => {
|
|
211
|
+
const { orders, database_url, empty_database, offer_role = SPINE_PARTICIPANT_ROLE, gated_ws_path = SPINE_ROLE_GATED_WS_PATH } = options;
|
|
212
|
+
describe('backend both live', () => {
|
|
213
|
+
let db = null;
|
|
214
|
+
let close_db = null;
|
|
215
|
+
beforeAll(async () => {
|
|
216
|
+
const created = await create_db(database_url);
|
|
217
|
+
db = created.db;
|
|
218
|
+
close_db = created.close;
|
|
219
|
+
});
|
|
220
|
+
afterAll(async () => {
|
|
221
|
+
await close_db?.();
|
|
222
|
+
});
|
|
223
|
+
const get_db = () => {
|
|
224
|
+
assert.ok(db, 'the snapshot connection is open');
|
|
225
|
+
return db;
|
|
226
|
+
};
|
|
227
|
+
for (const order of orders) {
|
|
228
|
+
const { label, first, second } = order;
|
|
229
|
+
const spawn_timeout = first.startup_timeout_ms + second.startup_timeout_ms + BOTH_LIVE_PHASE_WORK_MS;
|
|
230
|
+
const first_live = { handle: null };
|
|
231
|
+
const second_live = { handle: null };
|
|
232
|
+
const state = {};
|
|
233
|
+
const get_live = () => {
|
|
234
|
+
const a = first_live.handle;
|
|
235
|
+
const b = second_live.handle;
|
|
236
|
+
assert.ok(a && b && state.keeper, 'both backends booted (the boot test passed)');
|
|
237
|
+
return { a, b, keeper_id: state.keeper.id, keeper_cookies: state.keeper.cookies };
|
|
238
|
+
};
|
|
239
|
+
const ws_first = first.capabilities.ws && first.capabilities.ws_account_actions;
|
|
240
|
+
describe(label, () => {
|
|
241
|
+
afterAll(async () => {
|
|
242
|
+
await teardown_live_backend(second_live);
|
|
243
|
+
await teardown_live_backend(first_live);
|
|
244
|
+
});
|
|
245
|
+
// Declared limit (docs/security.md § Single-Process Architecture):
|
|
246
|
+
// `/status`'s `bootstrap_available` and the daemon leg's keeper are
|
|
247
|
+
// read once per process, at boot — the reason the boot below spawns
|
|
248
|
+
// the second backend only after the first's bootstrap.
|
|
249
|
+
test(`declared limit: ${second.name} booted before ${first.name}'s bootstrap keeps its boot-time bootstrap state`, async () => {
|
|
250
|
+
assert_both_live(order, database_url, offer_role);
|
|
251
|
+
await empty_database();
|
|
252
|
+
await with_live_backend(first, first_live, (a) => with_live_backend(second, second_live, async (b) => {
|
|
253
|
+
const before = await read_status(b.config);
|
|
254
|
+
assert.strictEqual(before.status, 401);
|
|
255
|
+
assert.strictEqual(before.bootstrap_available, true, `${b.config.name} advertises bootstrap over the empty database`);
|
|
256
|
+
await bootstrap({ transport: backend_transport(a.config), config: a.config });
|
|
257
|
+
const own = await read_status(a.config);
|
|
258
|
+
assert.notStrictEqual(own.bootstrap_available, true, `${a.config.name} closed bootstrap on its own`);
|
|
259
|
+
const stale = await read_status(b.config);
|
|
260
|
+
assert.strictEqual(stale.bootstrap_available, true, `declared limit: ${b.config.name}'s /status still advertises bootstrap ` +
|
|
261
|
+
"after the other spine's — a re-read landed? lift the limit and flip this");
|
|
262
|
+
const keeperless = await backend_daemon_rpc(b, '_testing_drain_effects');
|
|
263
|
+
assert.strictEqual(keeperless.status, 401, `declared limit: ${b.config.name}'s daemon token resolves no keeper ` +
|
|
264
|
+
'until it restarts — a re-resolve landed? lift the limit and flip this');
|
|
265
|
+
assert.strictEqual(error_reason(keeperless), ERROR_AUTHENTICATION_REQUIRED);
|
|
266
|
+
// the window it advertises is closed where it counts: the lock
|
|
267
|
+
const rebootstrap = await backend_post_json(backend_transport(b.config), b.config.bootstrap_path, {
|
|
268
|
+
token: b.config.bootstrap.token,
|
|
269
|
+
username: b.config.bootstrap.username,
|
|
270
|
+
password: b.config.bootstrap.password
|
|
271
|
+
});
|
|
272
|
+
assert.strictEqual(rebootstrap.status, 403, 'the stale window bootstraps nothing');
|
|
273
|
+
assert.strictEqual(rebootstrap.body.error, ERROR_ALREADY_BOOTSTRAPPED);
|
|
274
|
+
const closed = await read_status(b.config);
|
|
275
|
+
assert.notStrictEqual(closed.bootstrap_available, true, `${b.config.name}: the lock refusal closes its window`);
|
|
276
|
+
// the second's daemon leg can't drain it (the limit above);
|
|
277
|
+
// its teardown awaits what it holds
|
|
278
|
+
await drain_backend_effects(a);
|
|
279
|
+
await assert_backend_invariants(a);
|
|
280
|
+
}));
|
|
281
|
+
}, spawn_timeout);
|
|
282
|
+
test(`${first.name} bootstraps over an empty database and ${second.name} boots over it`, async () => {
|
|
283
|
+
assert_both_live(order, database_url, offer_role);
|
|
284
|
+
await empty_database();
|
|
285
|
+
const a = await spawn_backend(first);
|
|
286
|
+
first_live.handle = a;
|
|
287
|
+
const keeper = await bootstrap({
|
|
288
|
+
transport: backend_transport(a.config),
|
|
289
|
+
config: a.config
|
|
290
|
+
});
|
|
291
|
+
await drain_backend_effects(a);
|
|
292
|
+
const b = await spawn_backend(second);
|
|
293
|
+
second_live.handle = b;
|
|
294
|
+
state.keeper = { id: keeper.account.id, cookies: keeper.cookies };
|
|
295
|
+
const live = get_live();
|
|
296
|
+
// the second's boot resolved the keeper the first bootstrapped
|
|
297
|
+
await drain_backend_effects(b);
|
|
298
|
+
const status = await read_status(b.config);
|
|
299
|
+
assert.strictEqual(status.status, 401);
|
|
300
|
+
assert.notStrictEqual(status.bootstrap_available, true, 'bootstrap is closed');
|
|
301
|
+
await assert_live_on_both(live, { cookies: keeper.cookies }, keeper.account.id, "the keeper's cookie");
|
|
302
|
+
await assert_invariants_both(live);
|
|
303
|
+
}, spawn_timeout);
|
|
304
|
+
describe('live cross-spine credential honor', () => {
|
|
305
|
+
test(`a session minted on ${first.name}, logged out on ${second.name}`, async () => {
|
|
306
|
+
const live = get_live();
|
|
307
|
+
const member = await create_member(live, 'live_logout');
|
|
308
|
+
const cookies = await login(first, member.username, MEMBER_PASSWORD);
|
|
309
|
+
const spare = await login(first, member.username, MEMBER_PASSWORD);
|
|
310
|
+
await assert_live_on_both(live, { cookies }, member.account_id, 'the session');
|
|
311
|
+
const before = await drained_snapshot(live, get_db());
|
|
312
|
+
const logout = await backend_post_json(backend_transport(second, cookies), SPINE_ACCOUNT_LOGOUT_PATH, {});
|
|
313
|
+
assert.strictEqual(logout.status, 200, `${second.name} logs the session out`);
|
|
314
|
+
await assert_refused(live.a, { cookies }, 'the logged-out session');
|
|
315
|
+
await assert_refused(live.b, { cookies }, 'the logged-out session');
|
|
316
|
+
// logout ends one session, not the account's others
|
|
317
|
+
await assert_live_on_both(live, { cookies: spare }, member.account_id, 'the spare session');
|
|
318
|
+
const after = await drained_snapshot(live, get_db());
|
|
319
|
+
assert_changed_tables(before, after, [TABLE_AUTH_SESSION, TABLE_AUDIT_LOG], 'logout');
|
|
320
|
+
await assert_invariants_both(live);
|
|
321
|
+
});
|
|
322
|
+
test(`a session minted on ${first.name}, revoked by id on ${second.name}`, async () => {
|
|
323
|
+
const live = get_live();
|
|
324
|
+
const member = await create_member(live, 'live_session_revoke');
|
|
325
|
+
const target = await login(first, member.username, MEMBER_PASSWORD);
|
|
326
|
+
const revoker = await login(second, member.username, MEMBER_PASSWORD);
|
|
327
|
+
await assert_live_on_both(live, { cookies: target }, member.account_id, 'the target');
|
|
328
|
+
const { sessions } = await backend_rpc_as(first, target, 'account_session_list', undefined, SessionListOutput);
|
|
329
|
+
const current = sessions.find((s) => s.is_current);
|
|
330
|
+
assert.ok(current, 'the session list names the caller');
|
|
331
|
+
const before = await drained_snapshot(live, get_db());
|
|
332
|
+
const revoked = await backend_rpc_as(second, revoker, 'account_session_revoke', { session_id: current.id }, SessionRevokeOutput);
|
|
333
|
+
assert.isTrue(revoked.revoked, `${second.name} revokes the session`);
|
|
334
|
+
await assert_refused(live.a, { cookies: target }, 'the revoked session');
|
|
335
|
+
await assert_refused(live.b, { cookies: target }, 'the revoked session');
|
|
336
|
+
await assert_live_on_both(live, { cookies: revoker }, member.account_id, 'the revoking session');
|
|
337
|
+
const after = await drained_snapshot(live, get_db());
|
|
338
|
+
assert_changed_tables(before, after, [TABLE_AUTH_SESSION, TABLE_AUDIT_LOG], 'the session revoke');
|
|
339
|
+
await assert_invariants_both(live);
|
|
340
|
+
});
|
|
341
|
+
test(`an API token minted on ${first.name}, revoked on ${second.name}`, async () => {
|
|
342
|
+
const live = get_live();
|
|
343
|
+
const member = await create_member(live, 'live_token');
|
|
344
|
+
const cookies = await login(first, member.username, MEMBER_PASSWORD);
|
|
345
|
+
const token = await create_token(first, cookies);
|
|
346
|
+
const spare = await create_token(first, cookies);
|
|
347
|
+
await assert_live_on_both(live, { bearer: token.token }, member.account_id, 'the token');
|
|
348
|
+
const before = await drained_snapshot(live, get_db());
|
|
349
|
+
const revoked = await backend_rpc_as(second, cookies, 'account_token_revoke', { token_id: token.id }, TokenRevokeOutput);
|
|
350
|
+
assert.isTrue(revoked.revoked, `${second.name} revokes the token`);
|
|
351
|
+
await assert_refused(live.a, { bearer: token.token }, 'the revoked token');
|
|
352
|
+
await assert_refused(live.b, { bearer: token.token }, 'the revoked token');
|
|
353
|
+
// the revoke ends one token, not the account's others
|
|
354
|
+
await assert_live_on_both(live, { bearer: spare.token }, member.account_id, 'the spare token');
|
|
355
|
+
await assert_live_on_both(live, { cookies }, member.account_id, 'the session');
|
|
356
|
+
const after = await drained_snapshot(live, get_db());
|
|
357
|
+
assert_changed_tables(before, after, [TABLE_API_TOKEN, TABLE_AUDIT_LOG], 'the revoke');
|
|
358
|
+
await assert_invariants_both(live);
|
|
359
|
+
});
|
|
360
|
+
test(`a password changed on ${second.name} ends ${first.name}'s credentials`, async () => {
|
|
361
|
+
const live = get_live();
|
|
362
|
+
const member = await create_member(live, 'live_password');
|
|
363
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
364
|
+
const token = await create_token(first, session);
|
|
365
|
+
const changer = await login(second, member.username, MEMBER_PASSWORD);
|
|
366
|
+
await assert_live_on_both(live, { cookies: session }, member.account_id, 'the session');
|
|
367
|
+
await assert_live_on_both(live, { bearer: token.token }, member.account_id, 'the token');
|
|
368
|
+
const before = await drained_snapshot(live, get_db());
|
|
369
|
+
const changed = await backend_post_json(backend_transport(second, changer), SPINE_ACCOUNT_PASSWORD_PATH, { current_password: MEMBER_PASSWORD, new_password: MEMBER_PASSWORD_CHANGED });
|
|
370
|
+
assert.strictEqual(changed.status, 200, `password change: ${JSON.stringify(changed.body)}`);
|
|
371
|
+
await assert_refused(live.a, { cookies: session }, "the first spine's session");
|
|
372
|
+
await assert_refused(live.a, { bearer: token.token }, "the first spine's token");
|
|
373
|
+
await assert_refused(live.b, { cookies: session }, "the first spine's session");
|
|
374
|
+
await assert_refused(live.b, { bearer: token.token }, "the first spine's token");
|
|
375
|
+
await assert_refused(live.a, { cookies: changer }, 'the changing session');
|
|
376
|
+
await assert_login_refused(first, member.username, MEMBER_PASSWORD, 'the old password is refused');
|
|
377
|
+
const after = await drained_snapshot(live, get_db());
|
|
378
|
+
assert_changed_tables(before, after, [TABLE_ACCOUNT, TABLE_AUTH_SESSION, TABLE_API_TOKEN, TABLE_AUDIT_LOG], 'the password change');
|
|
379
|
+
await login(first, member.username, MEMBER_PASSWORD_CHANGED);
|
|
380
|
+
await assert_invariants_both(live);
|
|
381
|
+
});
|
|
382
|
+
test(`an offer made on ${first.name}, accepted on ${second.name}`, async () => {
|
|
383
|
+
const live = get_live();
|
|
384
|
+
const member = await create_member(live, 'live_offer');
|
|
385
|
+
const cookies = await login(first, member.username, MEMBER_PASSWORD);
|
|
386
|
+
const { offer } = await backend_rpc_as(first, live.keeper_cookies, 'role_grant_offer_create', { to_account_id: member.account_id, role: ROLE_ADMIN }, RoleGrantOfferCreateOutput);
|
|
387
|
+
const { offers } = await backend_rpc_as(second, cookies, 'role_grant_offer_list', undefined, RoleGrantOfferListOutput);
|
|
388
|
+
assert.deepStrictEqual(offers.map((o) => o.id), [offer.id], `${second.name} lists the offer`);
|
|
389
|
+
assert.isFalse(await passes_admin_gate(first, cookies), 'no admin before the accept');
|
|
390
|
+
const before = await drained_snapshot(live, get_db());
|
|
391
|
+
const accepted = await backend_rpc_as(second, cookies, 'role_grant_offer_accept', { offer_id: offer.id }, RoleGrantOfferAcceptOutput);
|
|
392
|
+
assert.isTrue(await passes_admin_gate(first, cookies), `${first.name} honors the grant at once`);
|
|
393
|
+
const history = await backend_rpc_as(first, live.keeper_cookies, 'role_grant_offer_history', {}, RoleGrantOfferHistoryOutput);
|
|
394
|
+
const read = history.offers.find((o) => o.id === offer.id);
|
|
395
|
+
assert.ok(read, "the keeper's history lists the offer");
|
|
396
|
+
assert.strictEqual(read.resulting_role_grant_id, accepted.role_grant_id);
|
|
397
|
+
const after = await drained_snapshot(live, get_db());
|
|
398
|
+
assert_changed_tables(before, after, [TABLE_ROLE_GRANT, TABLE_ROLE_GRANT_OFFER, TABLE_AUDIT_LOG], 'the accept');
|
|
399
|
+
await assert_invariants_both(live);
|
|
400
|
+
});
|
|
401
|
+
test(`an account deleted on ${second.name} ends ${first.name}'s credentials`, async () => {
|
|
402
|
+
const live = get_live();
|
|
403
|
+
const member = await create_member(live, 'live_delete');
|
|
404
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
405
|
+
const token = await create_token(first, session);
|
|
406
|
+
const deleter = await login(second, member.username, MEMBER_PASSWORD);
|
|
407
|
+
await assert_live_on_both(live, { cookies: session }, member.account_id, 'the session');
|
|
408
|
+
await assert_live_on_both(live, { bearer: token.token }, member.account_id, 'the token');
|
|
409
|
+
const before = await drained_snapshot(live, get_db());
|
|
410
|
+
const deleted = await backend_rpc_as(second, deleter, 'account_delete', {}, AccountDeleteOutput);
|
|
411
|
+
assert.isTrue(deleted.deleted, `${second.name} deletes the account`);
|
|
412
|
+
await assert_refused(live.a, { cookies: session }, "the deleted account's session");
|
|
413
|
+
await assert_refused(live.a, { bearer: token.token }, "the deleted account's token");
|
|
414
|
+
await assert_refused(live.b, { cookies: session }, "the deleted account's session");
|
|
415
|
+
await assert_refused(live.b, { bearer: token.token }, "the deleted account's token");
|
|
416
|
+
await assert_login_refused(first, member.username, MEMBER_PASSWORD, "the deleted account's password is refused");
|
|
417
|
+
const after = await drained_snapshot(live, get_db());
|
|
418
|
+
assert_changed_tables(before, after, [TABLE_ACCOUNT, TABLE_ACTOR, TABLE_AUTH_SESSION, TABLE_API_TOKEN, TABLE_AUDIT_LOG], 'the delete');
|
|
419
|
+
await assert_invariants_both(live);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
// Each case pins a declared limit (docs/security.md § Single-Process
|
|
423
|
+
// Architecture): a revocation closes the connections of its own
|
|
424
|
+
// process only, and a notification reaches its own process's
|
|
425
|
+
// sockets only. HTTP on both spines proves each revocation landed,
|
|
426
|
+
// so the socket outliving it is the limit, not a vacuous revoke.
|
|
427
|
+
describe('declared limits', () => {
|
|
428
|
+
test_if(ws_first, `declared limit: a socket on ${first.name} outlives a password change on ${second.name}`, async () => {
|
|
429
|
+
const live = get_live();
|
|
430
|
+
const member = await create_member(live, 'limit_password');
|
|
431
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
432
|
+
const changer = await login(second, member.username, MEMBER_PASSWORD);
|
|
433
|
+
let socket = null;
|
|
434
|
+
try {
|
|
435
|
+
socket = await open_session_socket(first, session);
|
|
436
|
+
const changed = await backend_post_json(backend_transport(second, changer), SPINE_ACCOUNT_PASSWORD_PATH, { current_password: MEMBER_PASSWORD, new_password: MEMBER_PASSWORD_CHANGED });
|
|
437
|
+
assert.strictEqual(changed.status, 200);
|
|
438
|
+
await assert_refused(live.a, { cookies: session }, 'the old session');
|
|
439
|
+
await assert_refused(live.b, { cookies: session }, 'the old session');
|
|
440
|
+
await assert_socket_still_serves(socket, member.account_id, 'password change across processes');
|
|
441
|
+
}
|
|
442
|
+
finally {
|
|
443
|
+
await close_sockets([socket]);
|
|
444
|
+
}
|
|
445
|
+
await drain_both(live);
|
|
446
|
+
await assert_invariants_both(live);
|
|
447
|
+
});
|
|
448
|
+
test_if(ws_first, `declared limit: a socket on ${first.name} outlives its session's logout on ${second.name}`, async () => {
|
|
449
|
+
const live = get_live();
|
|
450
|
+
const member = await create_member(live, 'limit_logout');
|
|
451
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
452
|
+
let socket = null;
|
|
453
|
+
try {
|
|
454
|
+
socket = await open_session_socket(first, session);
|
|
455
|
+
const logout = await backend_post_json(backend_transport(second, session), SPINE_ACCOUNT_LOGOUT_PATH, {});
|
|
456
|
+
assert.strictEqual(logout.status, 200);
|
|
457
|
+
await assert_refused(live.a, { cookies: session }, 'the logged-out session');
|
|
458
|
+
await assert_refused(live.b, { cookies: session }, 'the logged-out session');
|
|
459
|
+
await assert_socket_still_serves(socket, member.account_id, 'logout across processes');
|
|
460
|
+
}
|
|
461
|
+
finally {
|
|
462
|
+
await close_sockets([socket]);
|
|
463
|
+
}
|
|
464
|
+
await drain_both(live);
|
|
465
|
+
await assert_invariants_both(live);
|
|
466
|
+
});
|
|
467
|
+
test_if(ws_first, `declared limit: a socket on ${first.name} outlives its token's revoke on ${second.name}`, async () => {
|
|
468
|
+
const live = get_live();
|
|
469
|
+
const member = await create_member(live, 'limit_token');
|
|
470
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
471
|
+
const token = await create_token(first, session);
|
|
472
|
+
let socket = null;
|
|
473
|
+
try {
|
|
474
|
+
socket = await open_bearer_socket(first, token.token);
|
|
475
|
+
const revoked = await backend_rpc_as(second, session, 'account_token_revoke', { token_id: token.id }, TokenRevokeOutput);
|
|
476
|
+
assert.isTrue(revoked.revoked);
|
|
477
|
+
await assert_refused(live.a, { bearer: token.token }, 'the revoked token');
|
|
478
|
+
await assert_refused(live.b, { bearer: token.token }, 'the revoked token');
|
|
479
|
+
await assert_socket_still_serves(socket, member.account_id, 'token revoke across processes');
|
|
480
|
+
}
|
|
481
|
+
finally {
|
|
482
|
+
await close_sockets([socket]);
|
|
483
|
+
}
|
|
484
|
+
await drain_both(live);
|
|
485
|
+
await assert_invariants_both(live);
|
|
486
|
+
});
|
|
487
|
+
test_if(ws_first && first.capabilities.ws_role_gated, `declared limit: a role-gated socket on ${first.name} outlives the role's revoke on ${second.name}`, async () => {
|
|
488
|
+
const live = get_live();
|
|
489
|
+
const member = await create_member(live, 'limit_role');
|
|
490
|
+
const { role_grant_id } = await backend_rpc_as(first, live.keeper_cookies, 'role_grant_assign', { to_account_id: member.account_id, role: ROLE_ADMIN }, RoleGrantAssignOutput);
|
|
491
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
492
|
+
let socket = null;
|
|
493
|
+
try {
|
|
494
|
+
// admitted at the gate: the grant is live on the first
|
|
495
|
+
socket = await open_session_socket(first, session, gated_ws_path);
|
|
496
|
+
await socket.request('admin_before', 'invite_list', undefined);
|
|
497
|
+
const revoked = await backend_rpc_as(second, live.keeper_cookies, 'role_grant_revoke', { actor_id: member.actor_id, role_grant_id }, RoleGrantRevokeOutput);
|
|
498
|
+
assert.isTrue(revoked.revoked);
|
|
499
|
+
assert.isFalse(await passes_admin_gate(first, session), 'the role is gone');
|
|
500
|
+
assert.isFalse(await passes_admin_gate(second, session), 'the role is gone');
|
|
501
|
+
assert.isFalse(await socket.wait_for_close(STAYS_OPEN_MS), `declared limit: the gated socket closed (${socket.close_code}) on the ` +
|
|
502
|
+
"other process's role revoke — lift the limit and flip this test");
|
|
503
|
+
// per-message dispatch re-reads the grants: the socket keeps
|
|
504
|
+
// the gate's pushes, not the role's actions
|
|
505
|
+
const refused = await socket.request('admin_after', 'invite_list', undefined).then(() => null, (err) => err);
|
|
506
|
+
assert.ok(refused instanceof Error, 'the revoked role still authorizes a per-message call');
|
|
507
|
+
assert.include(refused.message, `"reason":"${ERROR_INSUFFICIENT_PERMISSIONS}"`, 'refused for the role');
|
|
508
|
+
const { offer } = await backend_rpc_as(first, live.keeper_cookies, 'role_grant_offer_create', { to_account_id: member.account_id, role: offer_role }, RoleGrantOfferCreateOutput);
|
|
509
|
+
await socket.wait_for(is_notification_with(ROLE_GRANT_OFFER_RECEIVED_NOTIFICATION_METHOD, (p) => p.offer.id === offer.id), 5000);
|
|
510
|
+
}
|
|
511
|
+
finally {
|
|
512
|
+
await close_sockets([socket]);
|
|
513
|
+
}
|
|
514
|
+
await drain_both(live);
|
|
515
|
+
await assert_invariants_both(live);
|
|
516
|
+
});
|
|
517
|
+
test_if(ws_first && second.capabilities.ws && second.capabilities.ws_account_actions, `declared limit: an offer made on either of ${first.name} and ${second.name} notifies no socket on the other`, async () => {
|
|
518
|
+
const live = get_live();
|
|
519
|
+
const member = await create_member(live, 'limit_notify');
|
|
520
|
+
const session = await login(first, member.username, MEMBER_PASSWORD);
|
|
521
|
+
let near = null;
|
|
522
|
+
let far = null;
|
|
523
|
+
try {
|
|
524
|
+
near = await open_session_socket(first, session);
|
|
525
|
+
far = await open_session_socket(second, session);
|
|
526
|
+
const { offer } = await backend_rpc_as(first, live.keeper_cookies, 'role_grant_offer_create', { to_account_id: member.account_id, role: offer_role }, RoleGrantOfferCreateOutput);
|
|
527
|
+
const is_this_offer = is_notification_with(ROLE_GRANT_OFFER_RECEIVED_NOTIFICATION_METHOD, (p) => p.offer.id === offer.id);
|
|
528
|
+
// the control: the offering process's socket hears it
|
|
529
|
+
await near
|
|
530
|
+
.wait_for(is_this_offer, 5000)
|
|
531
|
+
.catch(() => assert.fail(`${first.name}'s own socket hears its offer (the near control)`));
|
|
532
|
+
const heard = await far.wait_for(is_this_offer, STAYS_OPEN_MS).then(() => true, () => false);
|
|
533
|
+
assert.isFalse(heard, `declared limit: ${second.name}'s socket heard ${first.name}'s offer — ` +
|
|
534
|
+
'a cross-process notification landed? lift the limit and flip this test');
|
|
535
|
+
// the far control, and the reverse direction: an offer made on
|
|
536
|
+
// the second reaches its own socket and not the first's (a
|
|
537
|
+
// different role, so it is a new offer, not the pending one)
|
|
538
|
+
const { offer: far_offer } = await backend_rpc_as(second, live.keeper_cookies, 'role_grant_offer_create', { to_account_id: member.account_id, role: ROLE_ADMIN }, RoleGrantOfferCreateOutput);
|
|
539
|
+
const is_far_offer = is_notification_with(ROLE_GRANT_OFFER_RECEIVED_NOTIFICATION_METHOD, (p) => p.offer.id === far_offer.id);
|
|
540
|
+
await far
|
|
541
|
+
.wait_for(is_far_offer, 5000)
|
|
542
|
+
.catch(() => assert.fail(`${second.name}'s own socket hears its offer (the far control)`));
|
|
543
|
+
const heard_reverse = await near.wait_for(is_far_offer, STAYS_OPEN_MS).then(() => true, () => false);
|
|
544
|
+
assert.isFalse(heard_reverse, `declared limit: ${first.name}'s socket heard ${second.name}'s offer — ` +
|
|
545
|
+
'a cross-process notification landed? lift the limit and flip this test');
|
|
546
|
+
}
|
|
547
|
+
finally {
|
|
548
|
+
await close_sockets([near, far]);
|
|
549
|
+
}
|
|
550
|
+
await drain_both(live);
|
|
551
|
+
await assert_invariants_both(live);
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
test('both drain, hold their invariants, and stop cleanly', async () => {
|
|
555
|
+
const live = get_live();
|
|
556
|
+
await drain_both(live);
|
|
557
|
+
await assert_invariants_both(live);
|
|
558
|
+
second_live.handle = null;
|
|
559
|
+
await live.b.teardown();
|
|
560
|
+
first_live.handle = null;
|
|
561
|
+
await live.a.teardown();
|
|
562
|
+
}, spawn_timeout);
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backend_swap.d.ts","sourceRoot":"","sources":["../../../src/lib/testing/cross_backend/backend_swap.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"backend_swap.d.ts","sourceRoot":"","sources":["../../../src/lib/testing/cross_backend/backend_swap.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AA2E9B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AA4BzD,6EAA6E;AAC7E,MAAM,WAAW,gBAAgB;IAChC,oCAAoC;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;CAC/B;AAED,iDAAiD;AACjD,MAAM,WAAW,sBAAsB;IACtC,kEAAkE;IAClE,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC;IACjD;;;OAGG;IACH,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;;;;OAKG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;;;OAIG;IACH,QAAQ,CAAC,cAAc,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7C;AAuWD;;;;;;GAMG;AACH,eAAO,MAAM,2BAA2B,GAAI,SAAS,sBAAsB,KAAG,IAmE7E,CAAC"}
|