brain-memory 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +153 -0
- package/README.md +4 -0
- package/SECURITY.md +61 -9
- package/bin/brain.js +24 -3
- package/bin/cloud-sync.js +49 -6
- package/bin/install.js +72 -14
- package/bin/memorize.js +50 -6
- package/bin/port.js +174 -0
- package/bin/recall.js +16 -6
- package/bin/reinforce.js +5 -0
- package/bin/session-start.js +30 -4
- package/bin/verify.js +42 -4
- package/commands/brain/memorize.md +8 -0
- package/commands/brain/sync.md +23 -0
- package/commands/brain/verify.md +10 -2
- package/hooks/citation-check.mjs +94 -0
- package/hooks/hooks.json +11 -0
- package/hooks/lib.mjs +6 -0
- package/hooks/prompt-recall.mjs +24 -3
- package/hooks/session-start.mjs +4 -2
- package/integrations/README.md +4 -0
- package/integrations/agentskills/brain-memory/SKILL.md +9 -5
- package/package.json +10 -5
- package/prompts/antigravity.md +4 -2
- package/prompts/claude.md +4 -2
- package/prompts/copilot.md +4 -2
- package/prompts/kilo.md +4 -2
- package/prompts/openai.md +4 -2
- package/prompts/opencode.md +4 -2
- package/src/adapters.js +240 -0
- package/src/anomaly.js +41 -1
- package/src/cloud-sync.js +123 -11
- package/src/content-lint.js +55 -2
- package/src/export-import.js +1 -1
- package/src/git-sync.js +1 -1
- package/src/index-manager.js +9 -5
- package/src/installer.js +35 -1
- package/src/pinning.js +20 -1
- package/src/provenance.js +34 -0
- package/src/quarantine.js +82 -5
- package/src/receipt.js +9 -4
- package/src/surfaced.js +188 -0
- package/store/CONTRACT.md +244 -0
- package/store/Dockerfile +24 -0
- package/store/README.md +43 -0
- package/store/SELF-HOSTING.md +200 -0
- package/store/bin/brain-store.js +189 -0
- package/store/conformance/conformance.test.js +341 -0
- package/store/conformance/helpers.js +199 -0
- package/store/deploy/Caddyfile +14 -0
- package/store/deploy/connector.Dockerfile +38 -0
- package/store/deploy/docker-compose.yml +83 -0
- package/store/deploy/env.example +19 -0
- package/store/lib/auth.js +174 -0
- package/store/lib/oidc.js +206 -0
- package/store/lib/storage.js +333 -0
- package/store/lib/tar.js +150 -0
- package/store/openapi.yaml +318 -0
- package/store/server.js +465 -0
- package/store/test/mock-issuer.js +104 -0
- package/store/test/oidc.test.js +222 -0
- package/store/test/store.test.js +445 -0
package/store/server.js
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* brain-store — reference implementation of the Brain store contract
|
|
5
|
+
*
|
|
6
|
+
* A small HTTP server that keeps each user's brain as an opaque archive. It
|
|
7
|
+
* never reads the memories inside one. See CONTRACT.md for the wire contract
|
|
8
|
+
* this implements, and conformance/ for the black-box suite that checks it.
|
|
9
|
+
*
|
|
10
|
+
* No runtime dependencies. One process per data directory.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const crypto = require('crypto');
|
|
14
|
+
const http = require('http');
|
|
15
|
+
const { UserStore } = require('./lib/auth');
|
|
16
|
+
const { Storage, PreconditionFailed, NotFound, UUID_RE } = require('./lib/storage');
|
|
17
|
+
const { inspectArchive, ArchiveError } = require('./lib/tar');
|
|
18
|
+
const { createVerifier, looksLikeJwt, OidcError } = require('./lib/oidc');
|
|
19
|
+
|
|
20
|
+
const VERSION = '0.1.0';
|
|
21
|
+
const CONTRACT = '1.1';
|
|
22
|
+
const EMPTY_PUSH_GUARD = 5; // refuse an empty archive over a brain with at least this many files
|
|
23
|
+
const JSON_BODY_LIMIT = 64 * 1024;
|
|
24
|
+
const MULTIPART_OVERHEAD = 64 * 1024;
|
|
25
|
+
|
|
26
|
+
class HttpError extends Error {
|
|
27
|
+
constructor(status, message, extra) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.status = status;
|
|
30
|
+
this.extra = extra || null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Small helpers
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
/** Fixed-window counter. Good enough to blunt token guessing and runaway clients. */
|
|
39
|
+
class RateLimiter {
|
|
40
|
+
constructor(max, windowMs) {
|
|
41
|
+
this.max = max;
|
|
42
|
+
this.windowMs = windowMs;
|
|
43
|
+
this.hits = new Map();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @returns {number} 0 when allowed, otherwise seconds until the window resets */
|
|
47
|
+
check(key) {
|
|
48
|
+
if (!this.max) return 0;
|
|
49
|
+
const now = Date.now();
|
|
50
|
+
if (this.hits.size > 10000) {
|
|
51
|
+
for (const [k, v] of this.hits) if (v.reset <= now) this.hits.delete(k);
|
|
52
|
+
}
|
|
53
|
+
let entry = this.hits.get(key);
|
|
54
|
+
if (!entry || entry.reset <= now) {
|
|
55
|
+
entry = { count: 0, reset: now + this.windowMs };
|
|
56
|
+
this.hits.set(key, entry);
|
|
57
|
+
}
|
|
58
|
+
entry.count++;
|
|
59
|
+
return entry.count > this.max ? Math.max(1, Math.ceil((entry.reset - now) / 1000)) : 0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function readBody(req, limit) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const declared = Number(req.headers['content-length']);
|
|
66
|
+
if (Number.isFinite(declared) && declared > limit) {
|
|
67
|
+
req.resume();
|
|
68
|
+
return reject(new HttpError(413, 'request body too large'));
|
|
69
|
+
}
|
|
70
|
+
const chunks = [];
|
|
71
|
+
let total = 0;
|
|
72
|
+
let over = false;
|
|
73
|
+
req.on('data', (chunk) => {
|
|
74
|
+
if (over) return;
|
|
75
|
+
total += chunk.length;
|
|
76
|
+
if (total > limit) {
|
|
77
|
+
over = true;
|
|
78
|
+
chunks.length = 0;
|
|
79
|
+
return reject(new HttpError(413, 'request body too large'));
|
|
80
|
+
}
|
|
81
|
+
chunks.push(chunk);
|
|
82
|
+
});
|
|
83
|
+
req.on('end', () => { if (!over) resolve(Buffer.concat(chunks)); });
|
|
84
|
+
req.on('error', reject);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Extract one file field from a multipart/form-data body. */
|
|
89
|
+
function multipartField(body, contentType, fieldName) {
|
|
90
|
+
const m = /boundary=(?:"([^"]+)"|([^;\s]+))/i.exec(contentType || '');
|
|
91
|
+
if (!m) return null;
|
|
92
|
+
const boundary = Buffer.from('--' + (m[1] || m[2]));
|
|
93
|
+
const delimiter = Buffer.concat([Buffer.from('\r\n'), boundary]);
|
|
94
|
+
|
|
95
|
+
let pos = body.indexOf(boundary);
|
|
96
|
+
while (pos !== -1) {
|
|
97
|
+
const afterBoundary = pos + boundary.length;
|
|
98
|
+
if (body[afterBoundary] === 0x2d && body[afterBoundary + 1] === 0x2d) return null; // closing marker
|
|
99
|
+
const headerStart = afterBoundary + 2;
|
|
100
|
+
const headerEnd = body.indexOf('\r\n\r\n', headerStart);
|
|
101
|
+
if (headerEnd === -1) return null;
|
|
102
|
+
const next = body.indexOf(delimiter, headerEnd + 4);
|
|
103
|
+
if (next === -1) return null;
|
|
104
|
+
const headers = body.subarray(headerStart, headerEnd).toString('latin1');
|
|
105
|
+
const name = /content-disposition:[^\r\n]*[;\s]name="([^"]*)"/i.exec(headers);
|
|
106
|
+
if (name && name[1] === fieldName) return body.subarray(headerEnd + 4, next);
|
|
107
|
+
pos = next + 2;
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Parse an If-Match / If-None-Match header into bare checksums. `*` is kept as-is. */
|
|
113
|
+
function parseEtags(header) {
|
|
114
|
+
if (!header) return null;
|
|
115
|
+
return String(header)
|
|
116
|
+
.split(',')
|
|
117
|
+
.map((part) => part.trim().replace(/^W\//, '').replace(/^"(.*)"$/, '$1'))
|
|
118
|
+
.filter(Boolean);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function bearer(req) {
|
|
122
|
+
const m = /^Bearer\s+(\S+)$/i.exec(req.headers.authorization || '');
|
|
123
|
+
return m ? m[1] : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function publicBrain(meta) {
|
|
127
|
+
return {
|
|
128
|
+
id: meta.id,
|
|
129
|
+
user_id: meta.user_id,
|
|
130
|
+
name: meta.name,
|
|
131
|
+
size_bytes: meta.size_bytes,
|
|
132
|
+
file_count: meta.file_count,
|
|
133
|
+
last_synced_at: meta.last_synced_at,
|
|
134
|
+
checksum: meta.checksum,
|
|
135
|
+
created_at: meta.created_at,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// The store
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {Object} opts
|
|
145
|
+
* @param {string} opts.dataDir
|
|
146
|
+
* @param {Buffer|null} [opts.encryptionKey] 32-byte key; enables encryption at rest
|
|
147
|
+
* @param {number} [opts.maxUploadBytes=52428800]
|
|
148
|
+
* @param {number} [opts.maxUserBytes=0] 0 = unlimited
|
|
149
|
+
* @param {number} [opts.maxBrains=10] per user
|
|
150
|
+
* @param {number} [opts.versionRetention=5]
|
|
151
|
+
* @param {number} [opts.requestsPerMinute=300] per user
|
|
152
|
+
* @param {number} [opts.uploadsPerMinute=30] per user
|
|
153
|
+
* @param {number} [opts.failedAuthPerMinute=30] per client address
|
|
154
|
+
* @param {boolean} [opts.trustProxy=false] take the client address from X-Forwarded-For
|
|
155
|
+
* @param {Object} [opts.oidc] accept OpenID Connect ID tokens as bearer credentials:
|
|
156
|
+
* { issuer, audience, allowedDomains?, allowedEmails? }
|
|
157
|
+
* @param {(entry: Object) => void} [opts.log] one call per request; never receives a token
|
|
158
|
+
*/
|
|
159
|
+
function createStore(opts) {
|
|
160
|
+
if (!opts || !opts.dataDir) throw new Error('dataDir is required');
|
|
161
|
+
|
|
162
|
+
const users = new UserStore(opts.dataDir);
|
|
163
|
+
const storage = new Storage(opts.dataDir, {
|
|
164
|
+
encryptionKey: opts.encryptionKey || null,
|
|
165
|
+
versionRetention: opts.versionRetention,
|
|
166
|
+
});
|
|
167
|
+
const maxUploadBytes = opts.maxUploadBytes || 50 * 1024 * 1024;
|
|
168
|
+
const maxUserBytes = opts.maxUserBytes || 0;
|
|
169
|
+
const maxBrains = opts.maxBrains == null ? 10 : opts.maxBrains;
|
|
170
|
+
const pick = (value, fallback) => (value == null ? fallback : value);
|
|
171
|
+
const limits = {
|
|
172
|
+
requests: new RateLimiter(pick(opts.requestsPerMinute, 300), 60000),
|
|
173
|
+
uploads: new RateLimiter(pick(opts.uploadsPerMinute, 30), 60000),
|
|
174
|
+
failedAuth: new RateLimiter(pick(opts.failedAuthPerMinute, 30), 60000),
|
|
175
|
+
};
|
|
176
|
+
const log = opts.log || null;
|
|
177
|
+
|
|
178
|
+
// OpenID Connect: a bearer that is a valid ID token from the configured issuer
|
|
179
|
+
// identifies a user, who is provisioned on first sight. Static tokens keep
|
|
180
|
+
// working alongside. Verified tokens are remembered briefly so a burst of
|
|
181
|
+
// requests costs one signature check, not one each.
|
|
182
|
+
const oidc = opts.oidc ? createVerifier(opts.oidc) : null;
|
|
183
|
+
const oidcSeen = new Map(); // sha256(token) -> { user, until }
|
|
184
|
+
|
|
185
|
+
async function authenticateOidc(token) {
|
|
186
|
+
const key = crypto.createHash('sha256').update(token).digest('hex');
|
|
187
|
+
const hit = oidcSeen.get(key);
|
|
188
|
+
const nowMs = Date.now();
|
|
189
|
+
if (hit && hit.until > nowMs) return hit.user;
|
|
190
|
+
const claims = await oidc.verify(token);
|
|
191
|
+
const { user, created } = users.upsertOidc(opts.oidc.issuer, claims);
|
|
192
|
+
if (created) storage.createBrain(user.id, 'default');
|
|
193
|
+
if (oidcSeen.size > 1000) oidcSeen.clear();
|
|
194
|
+
// Never trust the cached verdict past the token's own expiry.
|
|
195
|
+
oidcSeen.set(key, { user, until: Math.min(nowMs + 60000, claims.exp * 1000) });
|
|
196
|
+
return user;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function clientAddress(req) {
|
|
200
|
+
if (opts.trustProxy) {
|
|
201
|
+
const forwarded = String(req.headers['x-forwarded-for'] || '').split(',')[0].trim();
|
|
202
|
+
if (forwarded) return forwarded;
|
|
203
|
+
}
|
|
204
|
+
return req.socket.remoteAddress || 'unknown';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function send(res, status, body, headers) {
|
|
208
|
+
const payload = body == null ? null : Buffer.from(JSON.stringify(body));
|
|
209
|
+
res.writeHead(status, {
|
|
210
|
+
'Cache-Control': 'no-store',
|
|
211
|
+
'X-Content-Type-Options': 'nosniff',
|
|
212
|
+
...(payload ? { 'Content-Type': 'application/json', 'Content-Length': payload.length } : {}),
|
|
213
|
+
...(headers || {}),
|
|
214
|
+
});
|
|
215
|
+
res.end(payload);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function sendArchive(res, archive, filename, headers) {
|
|
219
|
+
res.writeHead(200, {
|
|
220
|
+
'Cache-Control': 'no-store',
|
|
221
|
+
'X-Content-Type-Options': 'nosniff',
|
|
222
|
+
'Content-Type': 'application/gzip',
|
|
223
|
+
'Content-Length': archive.length,
|
|
224
|
+
'Content-Disposition': `attachment; filename=${filename}`,
|
|
225
|
+
...(headers || {}),
|
|
226
|
+
});
|
|
227
|
+
res.end(archive);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function requireUser(req) {
|
|
231
|
+
const address = clientAddress(req);
|
|
232
|
+
const token = bearer(req);
|
|
233
|
+
let user = token ? users.authenticate(token) : null;
|
|
234
|
+
if (!user && oidc && looksLikeJwt(token)) {
|
|
235
|
+
try {
|
|
236
|
+
user = await authenticateOidc(token);
|
|
237
|
+
} catch (err) {
|
|
238
|
+
if (!(err instanceof OidcError)) throw err;
|
|
239
|
+
user = null; // an invalid ID token is just an invalid credential
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (!user) {
|
|
243
|
+
const wait = limits.failedAuth.check(address);
|
|
244
|
+
if (wait) throw new HttpError(429, 'too many failed attempts', { retryAfter: wait });
|
|
245
|
+
throw new HttpError(401, 'unauthorized');
|
|
246
|
+
}
|
|
247
|
+
req.storeUserId = user.id; // for the request log only
|
|
248
|
+
const wait = limits.requests.check(user.id);
|
|
249
|
+
if (wait) throw new HttpError(429, 'rate limit exceeded', { retryAfter: wait });
|
|
250
|
+
return user;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function requireBrain(user, brainId) {
|
|
254
|
+
if (!UUID_RE.test(brainId)) throw new HttpError(400, 'invalid brain ID');
|
|
255
|
+
const meta = storage.readMeta(user.id, brainId);
|
|
256
|
+
if (!meta) throw new HttpError(404, 'brain not found');
|
|
257
|
+
return meta;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Shared by upload and restore: validate, guard, commit. */
|
|
261
|
+
async function commit(user, brain, archive, req, { force }) {
|
|
262
|
+
let fileCount;
|
|
263
|
+
try {
|
|
264
|
+
({ fileCount } = await inspectArchive(archive));
|
|
265
|
+
} catch (err) {
|
|
266
|
+
if (err instanceof ArchiveError) throw new HttpError(400, 'invalid archive: ' + err.message);
|
|
267
|
+
throw err;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const ifMatch = parseEtags(req.headers['if-match']);
|
|
271
|
+
const ifNoneMatch = parseEtags(req.headers['if-none-match']);
|
|
272
|
+
|
|
273
|
+
return storage.commitArchive(user.id, brain.id, {
|
|
274
|
+
archive,
|
|
275
|
+
fileCount,
|
|
276
|
+
ifMatch,
|
|
277
|
+
ifNoneMatchAny: Boolean(ifNoneMatch && ifNoneMatch.includes('*')),
|
|
278
|
+
guard: (meta) => {
|
|
279
|
+
if (!force && fileCount === 0 && meta.file_count >= EMPTY_PUSH_GUARD) {
|
|
280
|
+
throw new HttpError(409,
|
|
281
|
+
`refused: this push has 0 memories but your brain has ${meta.file_count}. ` +
|
|
282
|
+
'Retry with ?force=true to overwrite.');
|
|
283
|
+
}
|
|
284
|
+
if (maxUserBytes) {
|
|
285
|
+
const projected = storage.userBytes(user.id) - meta.size_bytes + archive.length;
|
|
286
|
+
if (projected > maxUserBytes) throw new HttpError(413, 'storage quota exceeded');
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// -- routes ----------------------------------------------------------------
|
|
293
|
+
|
|
294
|
+
async function route(req, res, url) {
|
|
295
|
+
const { pathname } = url;
|
|
296
|
+
const method = req.method;
|
|
297
|
+
|
|
298
|
+
if (pathname === '/health' && method === 'GET') {
|
|
299
|
+
return send(res, 200, {
|
|
300
|
+
status: 'ok',
|
|
301
|
+
server: 'brain-store/' + VERSION,
|
|
302
|
+
contract: CONTRACT,
|
|
303
|
+
capabilities: ['static-token', 'conditional-sync', ...(oidc ? ['oidc'] : [])],
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (pathname === '/auth/me' && method === 'GET') {
|
|
308
|
+
const user = await requireUser(req);
|
|
309
|
+
return send(res, 200, { user, storage_used: storage.userBytes(user.id) });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (pathname.startsWith('/auth/')) {
|
|
313
|
+
throw new HttpError(404, 'not supported: this store issues static tokens. Log in with a token.');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (pathname === '/api/brains') {
|
|
317
|
+
const user = await requireUser(req);
|
|
318
|
+
if (method === 'GET') return send(res, 200, storage.listBrains(user.id).map(publicBrain));
|
|
319
|
+
if (method === 'POST') {
|
|
320
|
+
const raw = await readBody(req, JSON_BODY_LIMIT);
|
|
321
|
+
let name = 'default';
|
|
322
|
+
if (raw.length) {
|
|
323
|
+
try {
|
|
324
|
+
const parsed = JSON.parse(raw.toString('utf8'));
|
|
325
|
+
if (parsed && typeof parsed.name === 'string' && parsed.name.trim()) name = parsed.name.trim();
|
|
326
|
+
} catch {
|
|
327
|
+
throw new HttpError(400, 'invalid JSON body');
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (maxBrains && storage.listBrains(user.id).length >= maxBrains) {
|
|
331
|
+
throw new HttpError(403, 'brain limit reached');
|
|
332
|
+
}
|
|
333
|
+
return send(res, 201, publicBrain(storage.createBrain(user.id, name)));
|
|
334
|
+
}
|
|
335
|
+
throw new HttpError(405, 'method not allowed');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const m = /^\/api\/brains\/([^/]+)(?:\/(sync|versions)(?:\/([^/]+)\/restore)?)?$/.exec(pathname);
|
|
339
|
+
if (!m) throw new HttpError(404, 'not found');
|
|
340
|
+
const [, brainId, section, versionParam] = m;
|
|
341
|
+
|
|
342
|
+
const user = await requireUser(req);
|
|
343
|
+
const brain = requireBrain(user, brainId);
|
|
344
|
+
|
|
345
|
+
if (!section) {
|
|
346
|
+
if (method === 'GET') return send(res, 200, publicBrain(brain));
|
|
347
|
+
if (method === 'DELETE') {
|
|
348
|
+
storage.deleteBrain(user.id, brain.id);
|
|
349
|
+
return send(res, 204, null);
|
|
350
|
+
}
|
|
351
|
+
throw new HttpError(405, 'method not allowed');
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (section === 'sync' && !versionParam) {
|
|
355
|
+
if (method === 'GET') {
|
|
356
|
+
const version = url.searchParams.get('version');
|
|
357
|
+
if (version) {
|
|
358
|
+
return sendArchive(res, storage.readVersion(user.id, brain.id, version), version);
|
|
359
|
+
}
|
|
360
|
+
if (!brain.checksum) throw new HttpError(404, 'brain archive not found');
|
|
361
|
+
const etag = `"${brain.checksum}"`;
|
|
362
|
+
const headers = { ETag: etag, 'X-Checksum': brain.checksum };
|
|
363
|
+
const inm = parseEtags(req.headers['if-none-match']);
|
|
364
|
+
if (inm && (inm.includes('*') || inm.includes(brain.checksum))) {
|
|
365
|
+
res.writeHead(304, { 'Cache-Control': 'no-store', ...headers });
|
|
366
|
+
return res.end();
|
|
367
|
+
}
|
|
368
|
+
return sendArchive(res, storage.readArchive(user.id, brain.id), 'brain.tar.gz', headers);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (method === 'PUT') {
|
|
372
|
+
const wait = limits.uploads.check(user.id);
|
|
373
|
+
if (wait) throw new HttpError(429, 'upload rate limit exceeded', { retryAfter: wait });
|
|
374
|
+
|
|
375
|
+
const body = await readBody(req, maxUploadBytes + MULTIPART_OVERHEAD);
|
|
376
|
+
const archive = multipartField(body, req.headers['content-type'], 'brain');
|
|
377
|
+
if (!archive) throw new HttpError(400, 'missing brain file');
|
|
378
|
+
if (archive.length > maxUploadBytes) throw new HttpError(413, 'archive too large');
|
|
379
|
+
|
|
380
|
+
const meta = await commit(user, brain, archive, req, {
|
|
381
|
+
force: url.searchParams.get('force') === 'true',
|
|
382
|
+
});
|
|
383
|
+
return send(res, 200, {
|
|
384
|
+
size_bytes: meta.size_bytes,
|
|
385
|
+
file_count: meta.file_count,
|
|
386
|
+
checksum: meta.checksum,
|
|
387
|
+
}, { ETag: `"${meta.checksum}"` });
|
|
388
|
+
}
|
|
389
|
+
throw new HttpError(405, 'method not allowed');
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (section === 'versions' && !versionParam) {
|
|
393
|
+
if (method !== 'GET') throw new HttpError(405, 'method not allowed');
|
|
394
|
+
const versions = storage.listVersions(user.id, brain.id);
|
|
395
|
+
return send(res, 200, { versions, total: versions.length });
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (section === 'versions' && versionParam) {
|
|
399
|
+
if (method !== 'POST') throw new HttpError(405, 'method not allowed');
|
|
400
|
+
const version = decodeURIComponent(versionParam);
|
|
401
|
+
const archive = storage.readVersion(user.id, brain.id, version);
|
|
402
|
+
const meta = await commit(user, brain, archive, req, { force: true });
|
|
403
|
+
return send(res, 200, { restored_version: version, checksum: meta.checksum },
|
|
404
|
+
{ ETag: `"${meta.checksum}"` });
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
throw new HttpError(404, 'not found');
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function handler(req, res) {
|
|
411
|
+
const started = Date.now();
|
|
412
|
+
|
|
413
|
+
try {
|
|
414
|
+
const url = new URL(req.url, 'http://store.invalid');
|
|
415
|
+
await route(req, res, url);
|
|
416
|
+
} catch (err) {
|
|
417
|
+
if (res.headersSent) {
|
|
418
|
+
res.destroy();
|
|
419
|
+
} else if (err instanceof HttpError) {
|
|
420
|
+
const headers = {};
|
|
421
|
+
if (err.extra && err.extra.retryAfter) headers['Retry-After'] = String(err.extra.retryAfter);
|
|
422
|
+
if (err.status === 401) headers['WWW-Authenticate'] = 'Bearer';
|
|
423
|
+
if (err.status === 413) headers.Connection = 'close';
|
|
424
|
+
send(res, err.status, { error: err.message }, headers);
|
|
425
|
+
} else if (err instanceof PreconditionFailed) {
|
|
426
|
+
send(res, 412, {
|
|
427
|
+
error: 'precondition failed: the stored archive has changed. Pull, then push again.',
|
|
428
|
+
current_checksum: err.currentChecksum,
|
|
429
|
+
}, err.currentChecksum ? { ETag: `"${err.currentChecksum}"` } : {});
|
|
430
|
+
} else if (err instanceof NotFound) {
|
|
431
|
+
send(res, 404, { error: err.message });
|
|
432
|
+
} else {
|
|
433
|
+
send(res, 500, { error: 'internal error' });
|
|
434
|
+
if (log) log({ level: 'error', message: err && err.message, stack: err && err.stack });
|
|
435
|
+
}
|
|
436
|
+
} finally {
|
|
437
|
+
if (log) {
|
|
438
|
+
log({
|
|
439
|
+
level: 'info',
|
|
440
|
+
method: req.method,
|
|
441
|
+
path: (req.url || '').split('?')[0],
|
|
442
|
+
status: res.statusCode,
|
|
443
|
+
ms: Date.now() - started,
|
|
444
|
+
user: req.storeUserId || null,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return {
|
|
451
|
+
handler,
|
|
452
|
+
users,
|
|
453
|
+
storage,
|
|
454
|
+
/** Start listening. Resolves with the http.Server once bound. */
|
|
455
|
+
listen(port, host) {
|
|
456
|
+
const server = http.createServer(handler);
|
|
457
|
+
return new Promise((resolve, reject) => {
|
|
458
|
+
server.once('error', reject);
|
|
459
|
+
server.listen(port, host, () => resolve(server));
|
|
460
|
+
});
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
module.exports = { createStore, VERSION, CONTRACT, multipartField, parseEtags };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A tiny OpenID Connect issuer for tests: discovery, JWKS, an authorization
|
|
5
|
+
* endpoint that approves at once, and a token endpoint that checks PKCE. It signs
|
|
6
|
+
* real RS256 and ES256 tokens, so the verifier under test does real cryptography.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const crypto = require('crypto');
|
|
10
|
+
const http = require('http');
|
|
11
|
+
|
|
12
|
+
const b64 = (v) => Buffer.from(typeof v === 'string' ? v : JSON.stringify(v)).toString('base64url');
|
|
13
|
+
|
|
14
|
+
function makeKey(alg, kid) {
|
|
15
|
+
const pair = alg === 'ES256'
|
|
16
|
+
? crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' })
|
|
17
|
+
: crypto.generateKeyPairSync('rsa', { modulusLength: 2048 });
|
|
18
|
+
return { alg, kid, privateKey: pair.privateKey, jwk: { ...pair.publicKey.export({ format: 'jwk' }), kid, alg, use: 'sig' } };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function sign(key, header, claims) {
|
|
22
|
+
const data = `${b64({ typ: 'JWT', alg: key.alg, kid: key.kid, ...header })}.${b64(claims)}`;
|
|
23
|
+
const sig = key.alg === 'ES256'
|
|
24
|
+
? crypto.sign('sha256', Buffer.from(data), { key: key.privateKey, dsaEncoding: 'ieee-p1363' })
|
|
25
|
+
: crypto.sign('sha256', Buffer.from(data), key.privateKey);
|
|
26
|
+
return `${data}.${sig.toString('base64url')}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function startMockIssuer({ clientId = 'brain-client' } = {}) {
|
|
30
|
+
const state = {
|
|
31
|
+
keys: [makeKey('RS256', 'rsa-1'), makeKey('ES256', 'ec-1')],
|
|
32
|
+
codes: new Map(), // code -> { challenge, nonce, redirectUri, person }
|
|
33
|
+
refreshTokens: new Map(), // token -> person
|
|
34
|
+
person: { sub: 'user-123', email: 'alice@example.org', email_verified: true },
|
|
35
|
+
jwksHits: 0,
|
|
36
|
+
returnIdTokenOnRefresh: true,
|
|
37
|
+
tokenEndpointDown: false,
|
|
38
|
+
};
|
|
39
|
+
let issuer = '';
|
|
40
|
+
|
|
41
|
+
const idToken = (claims = {}, { key = state.keys[0], header = {} } = {}) => {
|
|
42
|
+
const now = Math.floor(Date.now() / 1000);
|
|
43
|
+
return sign(key, header, { iss: issuer, aud: clientId, iat: now, exp: now + 3600, ...state.person, ...claims });
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const server = http.createServer(async (req, res) => {
|
|
47
|
+
const url = new URL(req.url, issuer);
|
|
48
|
+
const json = (status, body) => { res.writeHead(status, { 'content-type': 'application/json' }); res.end(JSON.stringify(body)); };
|
|
49
|
+
|
|
50
|
+
if (url.pathname === '/.well-known/openid-configuration') {
|
|
51
|
+
return json(200, {
|
|
52
|
+
issuer, jwks_uri: `${issuer}/jwks`, authorization_endpoint: `${issuer}/authorize`, token_endpoint: `${issuer}/token`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (url.pathname === '/jwks') { state.jwksHits++; return json(200, { keys: state.keys.map((k) => k.jwk) }); }
|
|
56
|
+
|
|
57
|
+
if (url.pathname === '/authorize') {
|
|
58
|
+
const q = url.searchParams;
|
|
59
|
+
if (q.get('client_id') !== clientId || q.get('response_type') !== 'code' || q.get('code_challenge_method') !== 'S256') {
|
|
60
|
+
return json(400, { error: 'invalid_request' });
|
|
61
|
+
}
|
|
62
|
+
const code = 'mc_' + crypto.randomBytes(12).toString('hex');
|
|
63
|
+
state.codes.set(code, { challenge: q.get('code_challenge'), nonce: q.get('nonce'), redirectUri: q.get('redirect_uri'), person: { ...state.person } });
|
|
64
|
+
const back = new URL(q.get('redirect_uri'));
|
|
65
|
+
back.searchParams.set('code', code);
|
|
66
|
+
back.searchParams.set('state', q.get('state'));
|
|
67
|
+
res.writeHead(302, { location: back.toString() });
|
|
68
|
+
return res.end();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (url.pathname === '/token' && req.method === 'POST') {
|
|
72
|
+
if (state.tokenEndpointDown) return json(503, { error: 'temporarily_unavailable' });
|
|
73
|
+
const chunks = [];
|
|
74
|
+
for await (const c of req) chunks.push(c);
|
|
75
|
+
const form = new URLSearchParams(Buffer.concat(chunks).toString());
|
|
76
|
+
if (form.get('grant_type') === 'authorization_code') {
|
|
77
|
+
const entry = state.codes.get(form.get('code'));
|
|
78
|
+
state.codes.delete(form.get('code'));
|
|
79
|
+
const challenge = crypto.createHash('sha256').update(form.get('code_verifier') || '').digest('base64url');
|
|
80
|
+
if (!entry || entry.challenge !== challenge || entry.redirectUri !== form.get('redirect_uri')) return json(400, { error: 'invalid_grant' });
|
|
81
|
+
const refresh = 'mr_' + crypto.randomBytes(12).toString('hex');
|
|
82
|
+
state.refreshTokens.set(refresh, entry.person);
|
|
83
|
+
return json(200, { token_type: 'Bearer', access_token: 'unused', refresh_token: refresh, id_token: idToken({ ...entry.person, nonce: entry.nonce }) });
|
|
84
|
+
}
|
|
85
|
+
if (form.get('grant_type') === 'refresh_token') {
|
|
86
|
+
const person = state.refreshTokens.get(form.get('refresh_token'));
|
|
87
|
+
if (!person) return json(400, { error: 'invalid_grant' });
|
|
88
|
+
return json(200, { token_type: 'Bearer', access_token: 'unused', ...(state.returnIdTokenOnRefresh ? { id_token: idToken(person) } : {}) });
|
|
89
|
+
}
|
|
90
|
+
return json(400, { error: 'unsupported_grant_type' });
|
|
91
|
+
}
|
|
92
|
+
json(404, { error: 'not found' });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
await new Promise((r) => server.listen(0, '127.0.0.1', r));
|
|
96
|
+
issuer = `http://127.0.0.1:${server.address().port}`;
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
issuer, clientId, state, idToken, makeKey, sign,
|
|
100
|
+
close: () => new Promise((r) => { server.close(r); if (server.closeAllConnections) server.closeAllConnections(); }),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { startMockIssuer, makeKey, sign };
|