@dreamforgestudiollc/emet 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/README.md +29 -6
- package/docs/BOOTSTRAP.md +2 -0
- package/docs/BOTS.md +8 -7
- package/docs/DEPLOY.md +6 -0
- package/docs/ERROR-CODES-SPEC.md +2 -2
- package/docs/ERROR-CODES.md +29 -0
- package/docs/GUIDED-SETUP.md +1 -1
- package/docs/PERMISSIONS.md +5 -5
- package/docs/SETUP.md +10 -4
- package/docs/SUBKEYS.md +4 -4
- package/package.json +2 -2
- package/server.json +8 -4
- package/src/aleph.js +34 -13
- package/src/annotations.js +6 -2
- package/src/approve-throttle.js +3 -0
- package/src/atlas-role.js +1 -1
- package/src/attribution.js +76 -28
- package/src/bound-door.js +170 -0
- package/src/break-glass.js +531 -0
- package/src/carry-forward-close.js +386 -0
- package/src/checkpoint.js +241 -0
- package/src/chooser.js +543 -0
- package/src/claimed-tag.js +140 -0
- package/src/conformance.js +269 -5
- package/src/deep-links.js +267 -0
- package/src/dispatch.js +112 -18
- package/src/error-codes.js +29 -0
- package/src/error-log.js +138 -26
- package/src/error-report.js +17 -5
- package/src/gaps.js +1 -1
- package/src/grants.js +230 -0
- package/src/http-remote.js +80 -26
- package/src/index.js +18 -2
- package/src/invites.js +47 -1
- package/src/layers.js +1 -1
- package/src/lessons-to-checks.js +1 -1
- package/src/logger.js +3 -1
- package/src/oauth-provider.js +89 -5
- package/src/owl.js +232 -0
- package/src/owner-approvals.js +24 -267
- package/src/owner-session.js +235 -0
- package/src/pages.js +1 -1
- package/src/persons.js +39 -6
- package/src/proof-run.js +70 -0
- package/src/redact.js +4 -0
- package/src/request-log.js +2 -2
- package/src/rotate.js +554 -0
- package/src/rune-dispatch.js +1 -1
- package/src/session-graph.js +44 -25
- package/src/session.js +52 -9
- package/src/setup.js +21 -12
- package/src/staged-write.js +522 -0
- package/src/startup-pages.js +2 -2
- package/src/startup-payload.js +3 -3
- package/src/subkeys.js +61 -0
- package/src/tools.js +290 -79
- package/src/transcript-session.js +23 -4
- package/src/validation.js +1 -1
- package/src/version.js +1 -1
- package/src/writer-approval.js +394 -0
- package/templates/AREA.md +1 -1
- package/templates/BOTHANDOFF.md +2 -2
- package/templates/GRANT.md +48 -0
- package/templates/INDEX.md +9 -7
- package/templates/PROCEDURE.md +1 -1
- package/templates/ROSTER.md +2 -2
- package/templates/SEED.md +2 -2
- package/templates/SESSIONDISPOSITION.md +59 -0
- package/templates/STAGING.md +1 -1
- package/templates/STANDARD.md +1 -1
- package/templates/THREAD.md +1 -1
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Break-glass recovery (R4b).
|
|
3
|
+
*
|
|
4
|
+
* One stable URL on Railway EMET: /owner/break-glass. The owner passphrase
|
|
5
|
+
* session is the only credential. A member or guest grant does not open it,
|
|
6
|
+
* and no AI client is required. Each authenticated use is logged and counted
|
|
7
|
+
* on emet_status.break_glass.
|
|
8
|
+
*
|
|
9
|
+
* Revoke ends one grant through MongoOAuthProvider.revokeGrantById. That
|
|
10
|
+
* door still deletes that one family. Rotate-all and per-member revoke are
|
|
11
|
+
* separate confirmed actions on this page. They stamp and retain. Memory
|
|
12
|
+
* rows already written stay.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import express from 'express';
|
|
16
|
+
import { COLLECTION_PREFIX } from './database.js';
|
|
17
|
+
import { BREAK_GLASS, grantView, logGrantId } from './grants.js';
|
|
18
|
+
import { timingSafeEqualStr } from './oauth-provider.js';
|
|
19
|
+
import { noteBreakGlassUse, recordAccessDecision, breakGlassMetric } from './deep-links.js';
|
|
20
|
+
import { AuditOperation } from './logger.js';
|
|
21
|
+
import {
|
|
22
|
+
noteRotationRefused, normalizeRotationRequest, describeRotation, commitRotation,
|
|
23
|
+
rotationForms, rotationConfirmForm, rotationTriggerAllowed
|
|
24
|
+
} from './rotate.js';
|
|
25
|
+
import {
|
|
26
|
+
esc, sendHtml, loginForm, mountOwnerLogin, createOwnerGate
|
|
27
|
+
} from './owner-session.js';
|
|
28
|
+
|
|
29
|
+
export { breakGlassMetric };
|
|
30
|
+
|
|
31
|
+
export const BREAK_GLASS_ACTION_MAX = 30;
|
|
32
|
+
const ACTION_WINDOW_MS = 15 * 60 * 1000;
|
|
33
|
+
const revocations = [];
|
|
34
|
+
|
|
35
|
+
export function retainedRevocations() {
|
|
36
|
+
return revocations.map((row) => ({ ...row }));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function _resetBreakGlassForTests() {
|
|
40
|
+
revocations.length = 0;
|
|
41
|
+
actionHits.clear();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const actionHits = new Map();
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The owner session is the credential. A guest or member grant is not.
|
|
48
|
+
* presentedDoor is ignored on purpose: this page does not read a bearer.
|
|
49
|
+
*/
|
|
50
|
+
export function breakGlassAuthorized(session, presentedDoor = null) {
|
|
51
|
+
if (presentedDoor === 'guest' || presentedDoor === 'member') return false;
|
|
52
|
+
return !!(session && session.csrf);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function allowAction(id, now, max) {
|
|
56
|
+
const t = now();
|
|
57
|
+
const kept = (actionHits.get(id) || []).filter((ts) => t - ts < ACTION_WINDOW_MS);
|
|
58
|
+
if (kept.length >= max) {
|
|
59
|
+
actionHits.set(id, kept);
|
|
60
|
+
const retryAfterS = Math.max(1, Math.ceil((kept[0] + ACTION_WINDOW_MS - t) / 1000));
|
|
61
|
+
return { allowed: false, retryAfterS };
|
|
62
|
+
}
|
|
63
|
+
kept.push(t);
|
|
64
|
+
actionHits.set(id, kept);
|
|
65
|
+
return { allowed: true, retryAfterS: 0 };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function audit(logger, details) {
|
|
69
|
+
if (logger && typeof logger.audit === 'function') {
|
|
70
|
+
logger.audit(AuditOperation.BREAK_GLASS, details);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function listLiveGrants(db, prefix) {
|
|
75
|
+
if (!db) return [];
|
|
76
|
+
const rows = await db.collection(`${prefix}oauth_tokens`).find({}).toArray();
|
|
77
|
+
const byFamily = new Map();
|
|
78
|
+
for (const row of rows) {
|
|
79
|
+
if (!row || row.superseded_by || row.retired === true) continue;
|
|
80
|
+
const view = grantView(row);
|
|
81
|
+
if (!view || !view.grant_id) continue;
|
|
82
|
+
const prev = byFamily.get(view.grant_id);
|
|
83
|
+
if (!prev || row.kind === 'access') byFamily.set(view.grant_id, view);
|
|
84
|
+
}
|
|
85
|
+
return [...byFamily.values()].sort((a, b) => String(a.grant_id).localeCompare(String(b.grant_id)));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function rememberRevocation(record) {
|
|
89
|
+
revocations.push(record);
|
|
90
|
+
if (revocations.length > 500) revocations.shift();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function revokeOneGrant(provider, grantId) {
|
|
94
|
+
const safe = logGrantId(grantId);
|
|
95
|
+
if (safe === 'none' || grantId === 'unknown' || grantId === '*' || grantId === 'all') {
|
|
96
|
+
return { ok: false, note: 'Name one grant. Rotate-all is a separate confirmed action.' };
|
|
97
|
+
}
|
|
98
|
+
if (!provider || typeof provider.revokeGrantById !== 'function') {
|
|
99
|
+
return { ok: false, note: 'The store is unavailable. Nothing was revoked.' };
|
|
100
|
+
}
|
|
101
|
+
const result = await provider.revokeGrantById(safe);
|
|
102
|
+
if (!result || !result.ok) {
|
|
103
|
+
if (result && result.reason === 'missing') return { ok: false, note: 'That grant is not in the live set. Nothing was revoked.' };
|
|
104
|
+
if (result && result.reason === 'unavailable') return { ok: false, note: 'The store is unavailable. Nothing was revoked.' };
|
|
105
|
+
return { ok: false, note: 'Name one grant. Rotate-all is a separate confirmed action.' };
|
|
106
|
+
}
|
|
107
|
+
rememberRevocation({
|
|
108
|
+
grant_id: result.grant_id,
|
|
109
|
+
at: new Date().toISOString(),
|
|
110
|
+
effect: 'revoked',
|
|
111
|
+
door: result.door,
|
|
112
|
+
retained: true,
|
|
113
|
+
rung: 'R4'
|
|
114
|
+
});
|
|
115
|
+
return { ok: true, grant_id: result.grant_id, door: result.door };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function grantRows(grants, kept, csrf) {
|
|
119
|
+
const live = grants.length
|
|
120
|
+
? `<table><thead><tr><th>Grant</th><th>Door</th><th>Client</th><th>Tag</th><th></th></tr></thead><tbody>
|
|
121
|
+
${grants.map((g) => `<tr><td><code>${esc(g.grant_id)}</code></td><td>${esc(g.door || '')}</td><td>${esc(g.client_id || '')}</td><td>${esc(g.source_tag || '')}</td><td>
|
|
122
|
+
<form method="post" action="/owner/break-glass/revoke">
|
|
123
|
+
<input type="hidden" name="csrf" value="${esc(csrf)}">
|
|
124
|
+
<input type="hidden" name="grant_id" value="${esc(g.grant_id)}">
|
|
125
|
+
<button type="submit">Revoke</button>
|
|
126
|
+
</form>
|
|
127
|
+
</td></tr>`).join('\n')}
|
|
128
|
+
</tbody></table>`
|
|
129
|
+
: '<p>No live grants.</p>';
|
|
130
|
+
const retained = kept.length
|
|
131
|
+
? `<ul>${kept.map((row) => `<li><code>${esc(row.grant_id)}</code> ${esc(row.effect)} ${esc(row.at)}</li>`).join('')}</ul>`
|
|
132
|
+
: '<p>No retained revocations in this process.</p>';
|
|
133
|
+
return `<h2>Live grants</h2>${live}<h2>Retained revocations</h2>${retained}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function panel(session, grants, kept, note, extraHtml = '') {
|
|
137
|
+
const csrf = session.csrf;
|
|
138
|
+
const body = grantRows(grants, kept, csrf);
|
|
139
|
+
const noteHtml = note ? `<p class="note">${esc(note)}</p>` : '';
|
|
140
|
+
return `${noteHtml}<h1>Break-glass</h1>
|
|
141
|
+
<p>Owner session only. Path <code>${esc(BREAK_GLASS.path)}</code>. This page does not accept a member or guest grant. Rotate-all is the confirmed section below.</p>
|
|
142
|
+
${body}
|
|
143
|
+
${rotationForms(csrf)}
|
|
144
|
+
<h2>Re-issue one door</h2>
|
|
145
|
+
<form method="post" action="/owner/break-glass/reissue">
|
|
146
|
+
<input type="hidden" name="csrf" value="${esc(csrf)}">
|
|
147
|
+
<label>Tag <input type="text" name="tag" maxlength="80"></label>
|
|
148
|
+
<label><input type="radio" name="door" value="guest" checked> Guest</label>
|
|
149
|
+
<label><input type="radio" name="door" value="member"> Member</label>
|
|
150
|
+
<label><input type="radio" name="door" value="owner"> Owner</label>
|
|
151
|
+
<button type="submit">Re-issue</button>
|
|
152
|
+
</form>
|
|
153
|
+
<h2>Clear a lockout</h2>
|
|
154
|
+
<form method="post" action="/owner/break-glass/clear">
|
|
155
|
+
<input type="hidden" name="csrf" value="${esc(csrf)}">
|
|
156
|
+
<label>Address <input type="text" name="ip" maxlength="80" placeholder="optional passphrase-form address"></label>
|
|
157
|
+
<button type="submit" name="scope" value="address">Clear that address</button>
|
|
158
|
+
<button type="submit" name="scope" value="tools">Clear tool lockout</button>
|
|
159
|
+
</form>
|
|
160
|
+
<p>The chooser is <a href="/owner/chooser">/owner/chooser</a>.</p>
|
|
161
|
+
${extraHtml || ''}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @param {import('express').Express} app
|
|
166
|
+
* @param {{ gate?: object, checkPassphrase?: Function, throttle?: object, ownerId?: string | Function, db?: object | Function, provider?: object, prefix?: string, rateLimiter?: object, logger?: object, now?: () => number, actionMax?: number }} options
|
|
167
|
+
*/
|
|
168
|
+
export function mountBreakGlass(app, options = {}) {
|
|
169
|
+
const now = options.now || Date.now;
|
|
170
|
+
const max = Number.isInteger(options.actionMax) && options.actionMax > 0 ? options.actionMax : BREAK_GLASS_ACTION_MAX;
|
|
171
|
+
const gate = options.gate || createOwnerGate({
|
|
172
|
+
checkPassphrase: options.checkPassphrase,
|
|
173
|
+
throttle: options.throttle,
|
|
174
|
+
now
|
|
175
|
+
});
|
|
176
|
+
mountOwnerLogin(app, gate);
|
|
177
|
+
const parseBody = express.urlencoded({ extended: false });
|
|
178
|
+
const prefix = typeof options.prefix === 'string' ? options.prefix : COLLECTION_PREFIX;
|
|
179
|
+
const dbOf = () => (typeof options.db === 'function' ? options.db() : options.db);
|
|
180
|
+
const depsOf = () => (typeof options.deps === 'function' ? options.deps() : (options.deps || null));
|
|
181
|
+
|
|
182
|
+
async function resolveOwner() {
|
|
183
|
+
try {
|
|
184
|
+
const id = await (typeof options.ownerId === 'function' ? options.ownerId() : options.ownerId);
|
|
185
|
+
return (typeof id === 'string' && id.trim()) || null;
|
|
186
|
+
} catch { return null; }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function refuseGrant(res) {
|
|
190
|
+
res.locals.reason = 'break_glass_owner_only';
|
|
191
|
+
sendHtml(res, 401, `${loginForm('/owner/break-glass')}<p>A member or guest grant cannot open this page.</p>`, 'EMET break-glass');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function render(req, res, note, count = true, extraHtml = '') {
|
|
195
|
+
const session = gate.sessionOf(req);
|
|
196
|
+
if (!breakGlassAuthorized(session)) {
|
|
197
|
+
refuseGrant(res);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (count) {
|
|
201
|
+
const limit = allowAction(session.id, now, max);
|
|
202
|
+
if (!limit.allowed) {
|
|
203
|
+
noteBreakGlassUse('rate_limited', { decider: 'owner', grant_id: 'none' });
|
|
204
|
+
res.locals.reason = 'break_glass_rate';
|
|
205
|
+
res.setHeader('Retry-After', String(limit.retryAfterS));
|
|
206
|
+
sendHtml(res, 429, '<p>Too many break-glass actions. Try again later.</p>', 'EMET break-glass');
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const personId = await resolveOwner();
|
|
210
|
+
noteBreakGlassUse('view', { decider: personId || 'owner', grant_id: 'none' });
|
|
211
|
+
audit(options.logger, { action: 'view', grant_id: 'none', decider: personId || 'owner' });
|
|
212
|
+
}
|
|
213
|
+
let grants = [];
|
|
214
|
+
let db = null;
|
|
215
|
+
try { db = dbOf() || null; } catch { db = null; }
|
|
216
|
+
try {
|
|
217
|
+
grants = await listLiveGrants(db, prefix);
|
|
218
|
+
} catch {
|
|
219
|
+
res.locals.reason = 'grant_store';
|
|
220
|
+
sendHtml(res, 503, '<p>The grant store is unavailable.</p>', 'EMET break-glass');
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
sendHtml(res, 200, panel(session, grants, retainedRevocations(), note, extraHtml), 'EMET break-glass');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function requireSession(req, res) {
|
|
227
|
+
const session = gate.sessionOf(req);
|
|
228
|
+
if (!breakGlassAuthorized(session)) {
|
|
229
|
+
refuseGrant(res);
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
const posted = (req.body && req.body.csrf) || '';
|
|
233
|
+
if (!timingSafeEqualStr(posted, session.csrf)) {
|
|
234
|
+
res.locals.reason = 'bad_csrf';
|
|
235
|
+
sendHtml(res, 403, '<p>The form expired. Reload the page and try again.</p>', 'EMET break-glass');
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
const limit = allowAction(session.id, now, max);
|
|
239
|
+
if (!limit.allowed) {
|
|
240
|
+
noteBreakGlassUse('rate_limited', { decider: 'owner', grant_id: 'none' });
|
|
241
|
+
res.locals.reason = 'break_glass_rate';
|
|
242
|
+
res.setHeader('Retry-After', String(limit.retryAfterS));
|
|
243
|
+
sendHtml(res, 429, '<p>Too many break-glass actions. Try again later.</p>', 'EMET break-glass');
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return session;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
app.get('/owner/break-glass', (req, res) => {
|
|
250
|
+
render(req, res).catch((e) => {
|
|
251
|
+
console.error('[emet] break-glass', e && e.message ? e.message : e);
|
|
252
|
+
if (!res.headersSent) {
|
|
253
|
+
res.locals.reason = 'break_glass_error';
|
|
254
|
+
sendHtml(res, 500, '<p>Something went wrong.</p>', 'EMET break-glass');
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
app.post('/owner/break-glass/rotate-all', parseBody, (req, res) => {
|
|
260
|
+
rotate(req, res, 'household').catch((e) => {
|
|
261
|
+
console.error('[emet] rotate-all', e && e.message ? e.message : e);
|
|
262
|
+
if (!res.headersSent) sendHtml(res, 500, '<p>Something went wrong. Nothing was revoked.</p>', 'EMET break-glass');
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
app.post('/owner/break-glass/revoke-member', parseBody, (req, res) => {
|
|
267
|
+
rotate(req, res, 'member').catch((e) => {
|
|
268
|
+
console.error('[emet] revoke-member', e && e.message ? e.message : e);
|
|
269
|
+
if (!res.headersSent) sendHtml(res, 500, '<p>Something went wrong. Nothing was revoked.</p>', 'EMET break-glass');
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
app.post('/owner/break-glass/revoke', parseBody, (req, res) => {
|
|
274
|
+
revoke(req, res).catch((e) => {
|
|
275
|
+
console.error('[emet] break-glass revoke', e && e.message ? e.message : e);
|
|
276
|
+
if (!res.headersSent) sendHtml(res, 500, '<p>Something went wrong. Nothing was revoked.</p>', 'EMET break-glass');
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
app.post('/owner/break-glass/reissue', parseBody, (req, res) => {
|
|
281
|
+
reissue(req, res).catch((e) => {
|
|
282
|
+
console.error('[emet] break-glass reissue', e && e.message ? e.message : e);
|
|
283
|
+
if (!res.headersSent) sendHtml(res, 500, '<p>Something went wrong. Nothing was issued.</p>', 'EMET break-glass');
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
app.post('/owner/break-glass/clear', parseBody, (req, res) => {
|
|
288
|
+
clearLockout(req, res).catch((e) => {
|
|
289
|
+
console.error('[emet] break-glass clear', e && e.message ? e.message : e);
|
|
290
|
+
if (!res.headersSent) sendHtml(res, 500, '<p>Something went wrong.</p>', 'EMET break-glass');
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
async function revoke(req, res) {
|
|
295
|
+
const session = requireSession(req, res);
|
|
296
|
+
if (!session) return;
|
|
297
|
+
const grantId = typeof (req.body && req.body.grant_id) === 'string' ? req.body.grant_id.trim() : '';
|
|
298
|
+
if (grantId === '*' || grantId === 'all' || grantId === '') {
|
|
299
|
+
const personId = await resolveOwner();
|
|
300
|
+
noteBreakGlassUse('revoke_refused', { decider: personId || 'owner', grant_id: 'none' });
|
|
301
|
+
res.locals.reason = 'not_rotate_all';
|
|
302
|
+
sendHtml(res, 400, '<p>Name one grant. Rotate-all is a separate confirmed action. Nothing was revoked.</p>', 'EMET break-glass');
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const personId = await resolveOwner();
|
|
306
|
+
let result;
|
|
307
|
+
try {
|
|
308
|
+
result = await revokeOneGrant(options.provider, grantId);
|
|
309
|
+
} catch {
|
|
310
|
+
res.locals.reason = 'grant_store';
|
|
311
|
+
sendHtml(res, 503, '<p>The grant store is unavailable. Nothing was revoked.</p>', 'EMET break-glass');
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
noteBreakGlassUse('revoke', { decider: personId || 'owner', grant_id: result.grant_id || grantId });
|
|
315
|
+
recordAccessDecision({
|
|
316
|
+
grant_id: result.grant_id || 'unknown',
|
|
317
|
+
correlation_id: 'none',
|
|
318
|
+
decider: personId || 'owner',
|
|
319
|
+
decision: result.ok ? 'revoke' : 'revoke_refused',
|
|
320
|
+
surface: 'break_glass',
|
|
321
|
+
door: result.door || 'guest',
|
|
322
|
+
tag: 'unknown',
|
|
323
|
+
kind: 'break_glass',
|
|
324
|
+
issued: false
|
|
325
|
+
});
|
|
326
|
+
audit(options.logger, { action: 'revoke', grant_id: logGrantId(result.grant_id || grantId), decider: personId || 'owner' });
|
|
327
|
+
if (!result.ok) {
|
|
328
|
+
res.locals.reason = 'revoke_refused';
|
|
329
|
+
await render(req, res, result.note, false);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
res.locals.reason = 'grant_revoked';
|
|
333
|
+
await render(req, res, `Revoked ${result.grant_id}. The revocation is retained.`, false);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function rotationAudit(details) {
|
|
337
|
+
if (options.logger && typeof options.logger.audit === 'function') {
|
|
338
|
+
options.logger.audit(AuditOperation.ROTATE_ALL, details);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async function rotate(req, res, kind) {
|
|
343
|
+
const session = requireSession(req, res);
|
|
344
|
+
if (!session) return;
|
|
345
|
+
const authorization = (req.headers && (req.headers.authorization || req.headers.Authorization)) || '';
|
|
346
|
+
if (!rotationTriggerAllowed({ session, authorization })) {
|
|
347
|
+
const personId = await resolveOwner();
|
|
348
|
+
noteRotationRefused('grant', personId);
|
|
349
|
+
res.locals.reason = 'rotate_grant_refused';
|
|
350
|
+
sendHtml(res, 403, '<p>An AI, member, or guest grant cannot rotate credentials. Nothing was revoked.</p>', 'EMET break-glass');
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const personId = await resolveOwner();
|
|
354
|
+
const scope = kind === 'member' ? 'member' : 'household';
|
|
355
|
+
const spec = normalizeRotationRequest({
|
|
356
|
+
scope,
|
|
357
|
+
tag: req.body && req.body.tag,
|
|
358
|
+
personId: req.body && req.body.person_id
|
|
359
|
+
});
|
|
360
|
+
if (!spec.ok) {
|
|
361
|
+
noteRotationRefused(spec.reason || 'one_subject', personId);
|
|
362
|
+
res.locals.reason = 'rotate_refused';
|
|
363
|
+
sendHtml(res, 400, '<p>Name one member or bot. Nothing was revoked.</p>', 'EMET break-glass');
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
let db = null;
|
|
367
|
+
try { db = dbOf() || null; } catch { db = null; }
|
|
368
|
+
if (!db) {
|
|
369
|
+
res.locals.reason = 'grant_store';
|
|
370
|
+
sendHtml(res, 503, '<p>The store is unavailable. Nothing was revoked.</p>', 'EMET break-glass');
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const expected = spec.scope === 'member' ? 'revoke-member' : 'rotate-all';
|
|
374
|
+
const confirm = req.body && req.body.confirm;
|
|
375
|
+
if (confirm !== expected) {
|
|
376
|
+
const picture = await describeRotation({
|
|
377
|
+
db, prefix, deps: depsOf(), scope: spec.scope, tag: spec.tag, personId: spec.personId
|
|
378
|
+
});
|
|
379
|
+
if (!picture.ok) {
|
|
380
|
+
const text = picture.reason === 'owner_is_not_a_member'
|
|
381
|
+
? 'The owner is not a member. Nothing was revoked.'
|
|
382
|
+
: 'Name one member or bot. Nothing was revoked.';
|
|
383
|
+
noteRotationRefused(picture.reason || 'refused', personId);
|
|
384
|
+
res.locals.reason = 'rotate_refused';
|
|
385
|
+
sendHtml(res, 400, `<p>${esc(text)}</p>`, 'EMET break-glass');
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const summary = `Review only. Nothing was revoked. Grants ${picture.grants}. Sub-keys ${picture.subkeys}. Sessions ${picture.sessions}. Subjects ${picture.subjects.length}.`;
|
|
389
|
+
const fields = spec.scope === 'member'
|
|
390
|
+
? {
|
|
391
|
+
tag: spec.tag || '',
|
|
392
|
+
person_id: spec.personId || '',
|
|
393
|
+
issue_invite: req.body && req.body.issue_invite === '1' ? '1' : ''
|
|
394
|
+
}
|
|
395
|
+
: {};
|
|
396
|
+
res.locals.reason = 'rotate_review';
|
|
397
|
+
await render(req, res, summary, false, rotationConfirmForm({
|
|
398
|
+
csrf: session.csrf,
|
|
399
|
+
action: spec.scope === 'member' ? '/owner/break-glass/revoke-member' : '/owner/break-glass/rotate-all',
|
|
400
|
+
confirm: expected,
|
|
401
|
+
fields
|
|
402
|
+
}));
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const result = await commitRotation({
|
|
406
|
+
db,
|
|
407
|
+
prefix,
|
|
408
|
+
deps: depsOf(),
|
|
409
|
+
scope: spec.scope,
|
|
410
|
+
tag: spec.tag,
|
|
411
|
+
personId: spec.personId,
|
|
412
|
+
issueInvite: spec.scope === 'household' || (req.body && req.body.issue_invite === '1'),
|
|
413
|
+
decider: personId || 'owner'
|
|
414
|
+
});
|
|
415
|
+
if (!result.ok) {
|
|
416
|
+
const text = result.reason === 'owner_is_not_a_member'
|
|
417
|
+
? 'The owner is not a member. Nothing was revoked.'
|
|
418
|
+
: 'Name one member or bot. Nothing was revoked.';
|
|
419
|
+
noteRotationRefused(result.reason || 'refused', personId);
|
|
420
|
+
res.locals.reason = 'rotate_refused';
|
|
421
|
+
sendHtml(res, 400, `<p>${esc(text)}</p>`, 'EMET break-glass');
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (spec.scope === 'household' && typeof gate.endOtherSessions === 'function') gate.endOtherSessions(session.id);
|
|
425
|
+
rotationAudit({
|
|
426
|
+
scope: result.scope,
|
|
427
|
+
decider: result.decider,
|
|
428
|
+
correlation_id: result.correlation_id,
|
|
429
|
+
revoked: result.revoked,
|
|
430
|
+
invites: result.invites_issued
|
|
431
|
+
});
|
|
432
|
+
const shown = result.invites.map((row) => `${row.display_name || row.tag || row.person_id || 'member'} ${row.door} ${row.code}`).join('; ');
|
|
433
|
+
const label = result.scope === 'household' ? 'Rotate-all' : 'Member revoke';
|
|
434
|
+
res.locals.reason = 'rotated';
|
|
435
|
+
await render(req, res, `${label} committed. correlation_id ${result.correlation_id}. revoked ${result.revoked}. invites ${result.invites_issued}. ${shown} Re-onboard: keyed link, owner sign-in, assign the tag, initialize via /owner/chooser.`, false);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
async function reissue(req, res) {
|
|
439
|
+
const session = requireSession(req, res);
|
|
440
|
+
if (!session) return;
|
|
441
|
+
const rawDoor = req.body && req.body.door;
|
|
442
|
+
const door = rawDoor === 'owner' || rawDoor === 'member' ? rawDoor : 'guest';
|
|
443
|
+
const tag = typeof (req.body && req.body.tag) === 'string' && req.body.tag.trim() ? req.body.tag.trim().slice(0, 80) : '';
|
|
444
|
+
const personId = await resolveOwner();
|
|
445
|
+
if (!personId) {
|
|
446
|
+
sendHtml(res, 400, '<p>The owner id is unavailable. Nothing was issued.</p>', 'EMET break-glass');
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (door !== 'owner' && !tag) {
|
|
450
|
+
sendHtml(res, 400, '<p>A tag is required for member or guest. Nothing was issued.</p>', 'EMET break-glass');
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
let note = 'Owner door is the passphrase consent on this host. This page did not mint a token.';
|
|
454
|
+
let issued = false;
|
|
455
|
+
if (door !== 'owner') {
|
|
456
|
+
let db = null;
|
|
457
|
+
try { db = dbOf() || null; } catch { db = null; }
|
|
458
|
+
if (!db) note = 'The store is unavailable. Nothing was issued.';
|
|
459
|
+
else {
|
|
460
|
+
try {
|
|
461
|
+
if (door === 'member') {
|
|
462
|
+
const { createMemberAccess } = await import('./invites.js');
|
|
463
|
+
const invite = await createMemberAccess(db, { created_by: personId, display_name: tag, source_tag: tag });
|
|
464
|
+
issued = true;
|
|
465
|
+
note = `Member code, shown once: ${invite.code}`;
|
|
466
|
+
} else {
|
|
467
|
+
const { createInvite } = await import('./invites.js');
|
|
468
|
+
const invite = await createInvite(db, { created_by: personId, display_name: tag, source_tag: tag });
|
|
469
|
+
issued = true;
|
|
470
|
+
note = `Guest code, shown once: ${invite.code}`;
|
|
471
|
+
}
|
|
472
|
+
} catch (e) {
|
|
473
|
+
if (e && e.code === 'INVITES_FROZEN') note = 'EMET Guest Access is off on this install. Nothing was issued.';
|
|
474
|
+
else if (e && e.code === 'MEMBER_ACCESS_FROZEN') note = 'EMET Member Access is off on this install. Nothing was issued.';
|
|
475
|
+
else note = 'That door did not issue a code. Nothing was issued.';
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
noteBreakGlassUse('reissue', { decider: personId, grant_id: 'none' });
|
|
480
|
+
recordAccessDecision({
|
|
481
|
+
grant_id: 'unknown',
|
|
482
|
+
correlation_id: 'none',
|
|
483
|
+
decider: personId,
|
|
484
|
+
decision: 'reissue',
|
|
485
|
+
surface: 'break_glass',
|
|
486
|
+
door,
|
|
487
|
+
tag: tag || 'unknown',
|
|
488
|
+
kind: 'break_glass',
|
|
489
|
+
issued
|
|
490
|
+
});
|
|
491
|
+
audit(options.logger, { action: 'reissue', door, decider: personId, issued });
|
|
492
|
+
res.locals.reason = issued ? 'reissued' : 'reissue_recorded';
|
|
493
|
+
await render(req, res, note, false);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
async function clearLockout(req, res) {
|
|
497
|
+
const session = requireSession(req, res);
|
|
498
|
+
if (!session) return;
|
|
499
|
+
const scope = req.body && req.body.scope;
|
|
500
|
+
const personId = await resolveOwner();
|
|
501
|
+
if (scope === 'tools') {
|
|
502
|
+
const n = options.rateLimiter && typeof options.rateLimiter.clearToolLockout === 'function'
|
|
503
|
+
? options.rateLimiter.clearToolLockout()
|
|
504
|
+
: 0;
|
|
505
|
+
noteBreakGlassUse('clear_tool_lockout', { decider: personId || 'owner', grant_id: 'none' });
|
|
506
|
+
recordAccessDecision({
|
|
507
|
+
grant_id: 'unknown', correlation_id: 'none', decider: personId || 'owner',
|
|
508
|
+
decision: 'clear_tool_lockout', surface: 'break_glass', kind: 'break_glass', issued: false
|
|
509
|
+
});
|
|
510
|
+
audit(options.logger, { action: 'clear_tool_lockout', decider: personId || 'owner', buckets: n });
|
|
511
|
+
res.locals.reason = 'tool_lockout_cleared';
|
|
512
|
+
await render(req, res, `Tool lockout cleared (${n}). Grants were not revoked.`, false);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const ip = typeof (req.body && req.body.ip) === 'string' ? req.body.ip.trim().slice(0, 80) : '';
|
|
516
|
+
if (!ip || /[\r\n]/.test(ip)) {
|
|
517
|
+
res.locals.reason = 'address_required';
|
|
518
|
+
sendHtml(res, 400, '<p>Name one address. Nothing was cleared.</p>', 'EMET break-glass');
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
if (options.throttle && typeof options.throttle.clearAddress === 'function') options.throttle.clearAddress(ip);
|
|
522
|
+
noteBreakGlassUse('clear_address', { decider: personId || 'owner', grant_id: 'none' });
|
|
523
|
+
recordAccessDecision({
|
|
524
|
+
grant_id: 'unknown', correlation_id: 'none', decider: personId || 'owner',
|
|
525
|
+
decision: 'clear_address', surface: 'break_glass', kind: 'break_glass', issued: false
|
|
526
|
+
});
|
|
527
|
+
audit(options.logger, { action: 'clear_address', decider: personId || 'owner' });
|
|
528
|
+
res.locals.reason = 'address_cleared';
|
|
529
|
+
await render(req, res, 'Passphrase lockout cleared for that address.', false);
|
|
530
|
+
}
|
|
531
|
+
}
|