brain-memory 0.3.0 → 0.4.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 +54 -0
- package/SECURITY.md +44 -3
- package/bin/cloud-sync.js +49 -6
- package/commands/brain/sync.md +23 -0
- package/package.json +8 -4
- package/src/cloud-sync.js +121 -9
- 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 +145 -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 +217 -0
- package/store/test/store.test.js +445 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OpenID Connect: the ID-token verifier, and the store accepting ID tokens as
|
|
5
|
+
* bearer credentials. Real signatures against a mock issuer (mock-issuer.js).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { describe, it, before, after } = require('node:test');
|
|
9
|
+
const assert = require('node:assert/strict');
|
|
10
|
+
const crypto = require('crypto');
|
|
11
|
+
|
|
12
|
+
const { createVerifier, looksLikeJwt, OidcError } = require('../lib/oidc');
|
|
13
|
+
const { startMockIssuer, makeKey, sign } = require('./mock-issuer');
|
|
14
|
+
const { client, startReferenceStore, sampleArchive } = require('../conformance/helpers');
|
|
15
|
+
|
|
16
|
+
describe('ID-token verification', () => {
|
|
17
|
+
let idp;
|
|
18
|
+
let verifier;
|
|
19
|
+
before(async () => {
|
|
20
|
+
idp = await startMockIssuer();
|
|
21
|
+
verifier = createVerifier({ issuer: idp.issuer, audience: idp.clientId });
|
|
22
|
+
});
|
|
23
|
+
after(() => idp.close());
|
|
24
|
+
|
|
25
|
+
it('accepts a valid RS256 token and a valid ES256 token', async () => {
|
|
26
|
+
assert.equal((await verifier.verify(idp.idToken())).sub, 'user-123');
|
|
27
|
+
assert.equal((await verifier.verify(idp.idToken({}, { key: idp.state.keys[1] }))).sub, 'user-123');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('caches the issuer\'s keys', async () => {
|
|
31
|
+
const before = idp.state.jwksHits;
|
|
32
|
+
await verifier.verify(idp.idToken());
|
|
33
|
+
await verifier.verify(idp.idToken());
|
|
34
|
+
assert.equal(idp.state.jwksHits, before);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const rejects = (token, pattern, expect) => assert.rejects(verifier.verify(token, expect), (err) => {
|
|
38
|
+
assert.ok(err instanceof OidcError, `expected OidcError, got ${err}`);
|
|
39
|
+
assert.match(err.message, pattern);
|
|
40
|
+
return true;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('rejects a token for another audience', () => rejects(idp.idToken({ aud: 'someone-else' }), /audience/));
|
|
44
|
+
it('rejects a token from another issuer', () => rejects(idp.idToken({ iss: 'https://evil.example' }), /issuer/));
|
|
45
|
+
it('rejects an expired token', () => rejects(idp.idToken({ exp: Math.floor(Date.now() / 1000) - 600 }), /expired/));
|
|
46
|
+
it('rejects a token issued in the future', () => rejects(idp.idToken({ iat: Math.floor(Date.now() / 1000) + 3600 }), /future/));
|
|
47
|
+
it('rejects a token with no subject', () => rejects(idp.idToken({ sub: '' }), /subject/));
|
|
48
|
+
it('rejects a multi-audience token whose authorized party is someone else',
|
|
49
|
+
() => rejects(idp.idToken({ aud: [idp.clientId, 'other'], azp: 'other' }), /authorized party/));
|
|
50
|
+
it('checks the nonce when one is expected', async () => {
|
|
51
|
+
await rejects(idp.idToken({ nonce: 'a' }), /nonce/, { nonce: 'b' });
|
|
52
|
+
await verifier.verify(idp.idToken({ nonce: 'a' }), { nonce: 'a' });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('rejects a tampered payload', async () => {
|
|
56
|
+
const [h, p, s] = idp.idToken().split('.');
|
|
57
|
+
const claims = JSON.parse(Buffer.from(p, 'base64url').toString());
|
|
58
|
+
claims.sub = 'someone-else';
|
|
59
|
+
await rejects(`${h}.${Buffer.from(JSON.stringify(claims)).toString('base64url')}.${s}`, /signature/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('rejects alg "none"', async () => {
|
|
63
|
+
const [, p] = idp.idToken().split('.');
|
|
64
|
+
const header = Buffer.from(JSON.stringify({ alg: 'none', kid: 'rsa-1' })).toString('base64url');
|
|
65
|
+
await rejects(`${header}.${p}.`, /malformed|unsupported/);
|
|
66
|
+
await rejects(`${header}.${p}.AAAA`, /unsupported algorithm/);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('rejects an HMAC token signed with the public key (algorithm confusion)', async () => {
|
|
70
|
+
const [, p] = idp.idToken().split('.');
|
|
71
|
+
const header = Buffer.from(JSON.stringify({ alg: 'HS256', kid: 'rsa-1' })).toString('base64url');
|
|
72
|
+
const pem = crypto.createPublicKey({ key: idp.state.keys[0].jwk, format: 'jwk' }).export({ type: 'spki', format: 'pem' });
|
|
73
|
+
const mac = crypto.createHmac('sha256', pem).update(`${header}.${p}`).digest('base64url');
|
|
74
|
+
await rejects(`${header}.${p}.${mac}`, /unsupported algorithm/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('rejects a token whose algorithm does not fit the key it names', async () => {
|
|
78
|
+
// Header says ES256 but points at the RSA key.
|
|
79
|
+
const forged = sign({ ...idp.state.keys[1], kid: 'rsa-1' }, {}, {
|
|
80
|
+
iss: idp.issuer, aud: idp.clientId, sub: 'x', exp: Math.floor(Date.now() / 1000) + 60,
|
|
81
|
+
});
|
|
82
|
+
await rejects(forged, /does not match the algorithm/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('rejects a token signed by a key the issuer does not publish', async () => {
|
|
86
|
+
const stranger = makeKey('RS256', 'rsa-1'); // same kid, different key
|
|
87
|
+
const forged = sign(stranger, {}, { iss: idp.issuer, aud: idp.clientId, sub: 'x', exp: Math.floor(Date.now() / 1000) + 60 });
|
|
88
|
+
await rejects(forged, /signature/);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('rejects junk', async () => {
|
|
92
|
+
for (const junk of ['', 'a.b', 'a.b.c', 'x'.repeat(20000), null, 42]) await rejects(junk, /malformed/);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('recognises the shape of a JWT', () => {
|
|
96
|
+
assert.equal(looksLikeJwt('aaa.bbb.ccc'), true);
|
|
97
|
+
assert.equal(looksLikeJwt('bst_abc'), false);
|
|
98
|
+
assert.equal(looksLikeJwt(undefined), false);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('issuer safeguards', () => {
|
|
103
|
+
it('refuses an issuer that is not https', () => {
|
|
104
|
+
assert.throws(() => createVerifier({ issuer: 'http://idp.example.org', audience: 'x' }), /https/);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('refuses a public issuer without an allow-list', () => {
|
|
108
|
+
assert.throws(() => createVerifier({ issuer: 'https://accounts.google.com', audience: 'x' }), /allow-list/);
|
|
109
|
+
createVerifier({ issuer: 'https://accounts.google.com', audience: 'x', allowedDomains: 'example.org' });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('refuses a discovery document that names a different issuer', async () => {
|
|
113
|
+
const idp = await startMockIssuer();
|
|
114
|
+
try {
|
|
115
|
+
const fetchLying = async (url) => {
|
|
116
|
+
const res = await fetch(url);
|
|
117
|
+
const body = await res.json();
|
|
118
|
+
if (body.issuer) body.issuer = 'https://somewhere-else.example';
|
|
119
|
+
return { ok: true, status: 200, json: async () => body };
|
|
120
|
+
};
|
|
121
|
+
const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, fetch: fetchLying });
|
|
122
|
+
await assert.rejects(v.verify(idp.idToken()), /different issuer/);
|
|
123
|
+
} finally {
|
|
124
|
+
await idp.close();
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('allow-lists', () => {
|
|
130
|
+
let idp;
|
|
131
|
+
before(async () => { idp = await startMockIssuer(); });
|
|
132
|
+
after(() => idp.close());
|
|
133
|
+
|
|
134
|
+
it('admits a verified address in an allowed domain, and nobody else', async () => {
|
|
135
|
+
const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'example.org, other.test' });
|
|
136
|
+
await v.verify(idp.idToken());
|
|
137
|
+
await assert.rejects(v.verify(idp.idToken({ email: 'mallory@evil.test' })), /allow-list/);
|
|
138
|
+
await assert.rejects(v.verify(idp.idToken({ email: 'x@notexample.org' })), /allow-list/);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('ignores an address the issuer has not verified', async () => {
|
|
142
|
+
const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'example.org' });
|
|
143
|
+
await assert.rejects(v.verify(idp.idToken({ email_verified: false })), /allow-list/);
|
|
144
|
+
await assert.rejects(v.verify(idp.idToken({ email_verified: undefined })), /allow-list/);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('admits by Google hosted-domain claim and by exact address', async () => {
|
|
148
|
+
const byHd = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'corp.test' });
|
|
149
|
+
await byHd.verify(idp.idToken({ hd: 'corp.test', email: 'someone@gmail.test' }));
|
|
150
|
+
const byEmail = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedEmails: ['Alice@Example.org'] });
|
|
151
|
+
await byEmail.verify(idp.idToken());
|
|
152
|
+
await assert.rejects(byEmail.verify(idp.idToken({ email: 'bob@example.org' })), /allow-list/);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe('the store accepts ID tokens', () => {
|
|
157
|
+
let idp;
|
|
158
|
+
let ref;
|
|
159
|
+
before(async () => {
|
|
160
|
+
idp = await startMockIssuer();
|
|
161
|
+
ref = await startReferenceStore({ oidc: { issuer: idp.issuer, audience: idp.clientId } });
|
|
162
|
+
});
|
|
163
|
+
after(async () => { await ref.close(); await idp.close(); });
|
|
164
|
+
|
|
165
|
+
it('advertises the capability', async () => {
|
|
166
|
+
const { data } = await client(ref.url, null).json('GET', '/health');
|
|
167
|
+
assert.ok(data.capabilities.includes('oidc'));
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('creates the user and a first brain on first sign-in, and recognises them afterwards', async () => {
|
|
171
|
+
const api = client(ref.url, idp.idToken());
|
|
172
|
+
const me = await api.json('GET', '/auth/me');
|
|
173
|
+
assert.equal(me.status, 200);
|
|
174
|
+
assert.match(me.data.user.id, /^oidc_/);
|
|
175
|
+
assert.equal(me.data.user.email, 'alice@example.org');
|
|
176
|
+
|
|
177
|
+
const brains = await api.json('GET', '/api/brains');
|
|
178
|
+
assert.equal(brains.data.length, 1);
|
|
179
|
+
|
|
180
|
+
const archive = sampleArchive(2, 'oidc');
|
|
181
|
+
assert.equal((await api.upload(brains.data[0].id, archive)).status, 200);
|
|
182
|
+
|
|
183
|
+
// A fresh token for the same person reaches the same brain.
|
|
184
|
+
const again = client(ref.url, idp.idToken({ iat: Math.floor(Date.now() / 1000) - 5 }));
|
|
185
|
+
assert.ok((await again.download(brains.data[0].id)).body.equals(archive));
|
|
186
|
+
assert.equal(ref.store.users.list().filter((u) => u.id.startsWith('oidc_')).length, 1);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('keeps different people apart', async () => {
|
|
190
|
+
const bob = client(ref.url, idp.idToken({ sub: 'user-456', email: 'bob@example.org' }));
|
|
191
|
+
const alice = client(ref.url, idp.idToken());
|
|
192
|
+
const [aliceBrain] = (await alice.json('GET', '/api/brains')).data;
|
|
193
|
+
assert.equal((await bob.download(aliceBrain.id)).status, 404);
|
|
194
|
+
assert.equal((await bob.json('GET', '/api/brains')).data.length, 1);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('answers 401 to a forged, expired or foreign ID token', async () => {
|
|
198
|
+
const stranger = makeKey('RS256', 'rsa-1');
|
|
199
|
+
const forged = sign(stranger, {}, { iss: idp.issuer, aud: idp.clientId, sub: 'user-123', exp: Math.floor(Date.now() / 1000) + 60 });
|
|
200
|
+
for (const token of [forged, idp.idToken({ exp: 1 }), idp.idToken({ aud: 'other' })]) {
|
|
201
|
+
assert.equal((await client(ref.url, token).json('GET', '/auth/me')).status, 401);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('still accepts static tokens', async () => {
|
|
206
|
+
assert.equal((await client(ref.url, ref.token).json('GET', '/auth/me')).status, 200);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('does not treat ID tokens as credentials when OIDC is not configured', async () => {
|
|
210
|
+
const plain = await startReferenceStore();
|
|
211
|
+
try {
|
|
212
|
+
assert.equal((await client(plain.url, idp.idToken()).json('GET', '/auth/me')).status, 401);
|
|
213
|
+
} finally {
|
|
214
|
+
await plain.close();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tests for the reference server's own behaviour: the parts the contract leaves
|
|
5
|
+
* to the implementation (archive inspection, encryption at rest, tokens, limits,
|
|
6
|
+
* the command line). Contract behaviour is covered by ../conformance/.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const { describe, it, before, after } = require('node:test');
|
|
10
|
+
const assert = require('node:assert/strict');
|
|
11
|
+
const { execFileSync, spawn } = require('child_process');
|
|
12
|
+
const crypto = require('crypto');
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const os = require('os');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const zlib = require('zlib');
|
|
17
|
+
|
|
18
|
+
const { inspectArchive, ArchiveError } = require('../lib/tar');
|
|
19
|
+
const { UserStore } = require('../lib/auth');
|
|
20
|
+
const { Storage, parseKey } = require('../lib/storage');
|
|
21
|
+
const { multipartField, parseEtags } = require('../server');
|
|
22
|
+
const { makeArchive, sampleArchive, sha256, client, startReferenceStore } = require('../conformance/helpers');
|
|
23
|
+
|
|
24
|
+
const BIN = path.join(__dirname, '..', 'bin', 'brain-store.js');
|
|
25
|
+
|
|
26
|
+
function tmpDir(prefix) {
|
|
27
|
+
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('archive inspection', () => {
|
|
31
|
+
it('counts regular files and ignores directories', async () => {
|
|
32
|
+
const { fileCount } = await inspectArchive(makeArchive({ 'a.md': 'a', 'dir/b.md': 'b', 'index.json': '{}' }));
|
|
33
|
+
assert.equal(fileCount, 3);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('counts zero for an empty archive', async () => {
|
|
37
|
+
assert.equal((await inspectArchive(makeArchive({}))).fileCount, 0);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('reads archives written by the system tar, as the brain CLI produces them', async () => {
|
|
41
|
+
const dir = tmpDir('brain-store-tar-');
|
|
42
|
+
try {
|
|
43
|
+
fs.mkdirSync(path.join(dir, 'professional', 'deep', 'nested'), { recursive: true });
|
|
44
|
+
fs.writeFileSync(path.join(dir, 'index.json'), '{}');
|
|
45
|
+
fs.writeFileSync(path.join(dir, 'professional', 'one.md'), 'one');
|
|
46
|
+
// A name past the 100-byte ustar limit forces a pax or GNU long-name entry.
|
|
47
|
+
// Kept well under Windows' 260-character path limit.
|
|
48
|
+
fs.writeFileSync(path.join(dir, 'professional', 'deep', 'nested', 'x'.repeat(110) + '.md'), 'long');
|
|
49
|
+
const out = path.join(tmpDir('brain-store-out-'), 'b.tar.gz');
|
|
50
|
+
// COPYFILE_DISABLE stops macOS tar from adding an AppleDouble "._name" entry
|
|
51
|
+
// beside every file, which would otherwise be counted as regular files too.
|
|
52
|
+
execFileSync('tar', ['czf', out, '-C', dir, '.'], { env: { ...process.env, COPYFILE_DISABLE: '1' } });
|
|
53
|
+
assert.equal((await inspectArchive(fs.readFileSync(out))).fileCount, 3);
|
|
54
|
+
} finally {
|
|
55
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('rejects data that is not gzip', async () => {
|
|
60
|
+
await assert.rejects(inspectArchive(Buffer.from('plain text')), ArchiveError);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('rejects gzip that does not contain a tar archive', async () => {
|
|
64
|
+
await assert.rejects(inspectArchive(zlib.gzipSync(Buffer.alloc(2048, 7))), /not a tar archive/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('rejects a truncated archive', async () => {
|
|
68
|
+
const full = zlib.gunzipSync(makeArchive({ 'a.md': 'x'.repeat(4000) }));
|
|
69
|
+
await assert.rejects(inspectArchive(zlib.gzipSync(full.subarray(0, 1024))), /truncated/);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('stops inflating past the size bound', async () => {
|
|
73
|
+
const bomb = zlib.gzipSync(Buffer.concat([
|
|
74
|
+
zlib.gunzipSync(makeArchive({ 'big.md': 'A'.repeat(4 * 1024 * 1024) })),
|
|
75
|
+
]));
|
|
76
|
+
assert.ok(bomb.length < 64 * 1024, 'the fixture should compress well');
|
|
77
|
+
await assert.rejects(inspectArchive(bomb, { maxInflatedBytes: 1024 * 1024 }), /beyond the allowed size/);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('stops counting past the entry bound', async () => {
|
|
81
|
+
const files = {};
|
|
82
|
+
for (let i = 0; i < 50; i++) files[`m${i}.md`] = String(i);
|
|
83
|
+
await assert.rejects(inspectArchive(makeArchive(files), { maxEntries: 10 }), /too many entries/);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('users and tokens', () => {
|
|
88
|
+
let dir;
|
|
89
|
+
before(() => { dir = tmpDir('brain-store-users-'); });
|
|
90
|
+
after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
91
|
+
|
|
92
|
+
it('never writes the token to disk', () => {
|
|
93
|
+
const users = new UserStore(dir);
|
|
94
|
+
const { token } = users.add('alice', 'alice@example.test');
|
|
95
|
+
const onDisk = fs.readFileSync(path.join(dir, 'users.json'), 'utf8');
|
|
96
|
+
assert.ok(!onDisk.includes(token));
|
|
97
|
+
assert.ok(onDisk.includes(sha256(Buffer.from(token))));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('keeps users.json private to the owner', { skip: process.platform === 'win32' }, () => {
|
|
101
|
+
assert.equal(fs.statSync(path.join(dir, 'users.json')).mode & 0o077, 0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('authenticates the right user and nobody else', () => {
|
|
105
|
+
const users = new UserStore(dir);
|
|
106
|
+
const bob = users.add('bob');
|
|
107
|
+
assert.equal(users.authenticate(bob.token).name, 'bob');
|
|
108
|
+
assert.equal(users.authenticate(bob.token + 'x'), null);
|
|
109
|
+
assert.equal(users.authenticate('bst_' + 'A'.repeat(43)), null);
|
|
110
|
+
assert.equal(users.authenticate(''), null);
|
|
111
|
+
assert.equal(users.authenticate(undefined), null);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('rotating a token retires the old one', () => {
|
|
115
|
+
const users = new UserStore(dir);
|
|
116
|
+
const carol = users.add('carol');
|
|
117
|
+
const rotated = users.rotate('carol');
|
|
118
|
+
assert.equal(users.authenticate(carol.token), null);
|
|
119
|
+
assert.equal(users.authenticate(rotated.token).name, 'carol');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('refuses duplicate and unsafe names', () => {
|
|
123
|
+
const users = new UserStore(dir);
|
|
124
|
+
assert.throws(() => users.add('alice'), /already exists/);
|
|
125
|
+
for (const bad of ['', '../etc', 'a/b', 'a b', '.hidden']) {
|
|
126
|
+
assert.throws(() => users.add(bad), /user name/);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('sees users added by another process without a restart', () => {
|
|
131
|
+
const running = new UserStore(dir);
|
|
132
|
+
running.list();
|
|
133
|
+
const other = new UserStore(dir);
|
|
134
|
+
const dave = other.add('dave');
|
|
135
|
+
assert.equal(running.authenticate(dave.token).name, 'dave');
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('storage', () => {
|
|
140
|
+
it('refuses IDs that could leave the data directory', () => {
|
|
141
|
+
const dir = tmpDir('brain-store-paths-');
|
|
142
|
+
try {
|
|
143
|
+
const storage = new Storage(dir);
|
|
144
|
+
assert.equal(storage.readMeta('../../etc', crypto.randomUUID()), null);
|
|
145
|
+
assert.equal(storage.readMeta('usr_ok', '../../../etc/passwd'), null);
|
|
146
|
+
assert.throws(() => storage.readVersion('usr_ok', crypto.randomUUID(), '../meta.json'), /version not found/);
|
|
147
|
+
} finally {
|
|
148
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('keeps only the configured number of snapshots', async () => {
|
|
153
|
+
const dir = tmpDir('brain-store-retention-');
|
|
154
|
+
try {
|
|
155
|
+
const storage = new Storage(dir, { versionRetention: 3 });
|
|
156
|
+
const brain = storage.createBrain('usr_r', 'default');
|
|
157
|
+
for (let i = 0; i < 8; i++) {
|
|
158
|
+
await storage.commitArchive('usr_r', brain.id, { archive: sampleArchive(1, 'v' + i), fileCount: 2 });
|
|
159
|
+
}
|
|
160
|
+
assert.equal(storage.listVersions('usr_r', brain.id).length, 3);
|
|
161
|
+
} finally {
|
|
162
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('parses keys as hex or base64 and rejects the wrong length', () => {
|
|
167
|
+
const raw = crypto.randomBytes(32);
|
|
168
|
+
assert.ok(parseKey(raw.toString('hex')).equals(raw));
|
|
169
|
+
assert.ok(parseKey(raw.toString('base64')).equals(raw));
|
|
170
|
+
assert.equal(parseKey(''), null);
|
|
171
|
+
assert.throws(() => parseKey('abcd'), /32 bytes/);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('encryption at rest', () => {
|
|
176
|
+
let dir;
|
|
177
|
+
const key = crypto.randomBytes(32);
|
|
178
|
+
before(() => { dir = tmpDir('brain-store-enc-'); });
|
|
179
|
+
after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
180
|
+
|
|
181
|
+
it('stores ciphertext but serves and checksums the plaintext', async () => {
|
|
182
|
+
const storage = new Storage(dir, { encryptionKey: key });
|
|
183
|
+
const brain = storage.createBrain('usr_e', 'default');
|
|
184
|
+
const archive = sampleArchive(3, 'secret-marker');
|
|
185
|
+
const meta = await storage.commitArchive('usr_e', brain.id, { archive, fileCount: 4 });
|
|
186
|
+
|
|
187
|
+
const onDisk = fs.readFileSync(path.join(dir, 'brains', 'usr_e', brain.id, 'current.bin'));
|
|
188
|
+
assert.ok(!onDisk.equals(archive));
|
|
189
|
+
assert.equal(onDisk.subarray(0, 7).toString('latin1'), 'BSTORE1');
|
|
190
|
+
assert.equal(meta.checksum, sha256(archive));
|
|
191
|
+
assert.equal(meta.size_bytes, archive.length);
|
|
192
|
+
assert.ok(storage.readArchive('usr_e', brain.id).equals(archive));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('seals snapshots too', async () => {
|
|
196
|
+
const storage = new Storage(dir, { encryptionKey: key });
|
|
197
|
+
const [brain] = storage.listBrains('usr_e');
|
|
198
|
+
const before = storage.readArchive('usr_e', brain.id);
|
|
199
|
+
await storage.commitArchive('usr_e', brain.id, { archive: sampleArchive(3, 'next'), fileCount: 4 });
|
|
200
|
+
const [snapshot] = storage.listVersions('usr_e', brain.id);
|
|
201
|
+
const raw = fs.readFileSync(path.join(dir, 'brains', 'usr_e', brain.id, 'versions', snapshot.version));
|
|
202
|
+
assert.equal(raw.subarray(0, 7).toString('latin1'), 'BSTORE1');
|
|
203
|
+
assert.ok(storage.readVersion('usr_e', brain.id, snapshot.version).equals(before));
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('cannot open one user\'s archive with another user\'s key', async () => {
|
|
207
|
+
const storage = new Storage(dir, { encryptionKey: key });
|
|
208
|
+
const [brain] = storage.listBrains('usr_e');
|
|
209
|
+
const sealed = fs.readFileSync(path.join(dir, 'brains', 'usr_e', brain.id, 'current.bin'));
|
|
210
|
+
assert.throws(() => storage.open('usr_other', sealed));
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('detects tampering', async () => {
|
|
214
|
+
const storage = new Storage(dir, { encryptionKey: key });
|
|
215
|
+
const [brain] = storage.listBrains('usr_e');
|
|
216
|
+
const file = path.join(dir, 'brains', 'usr_e', brain.id, 'current.bin');
|
|
217
|
+
const sealed = fs.readFileSync(file);
|
|
218
|
+
sealed[sealed.length - 20] ^= 0xff;
|
|
219
|
+
assert.throws(() => storage.open('usr_e', sealed));
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('refuses to serve ciphertext when the key is missing', () => {
|
|
223
|
+
const keyless = new Storage(dir);
|
|
224
|
+
const [brain] = keyless.listBrains('usr_e');
|
|
225
|
+
assert.throws(() => keyless.readArchive('usr_e', brain.id), /no encryption key/);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('reads archives written before encryption was switched on', async () => {
|
|
229
|
+
const plainDir = tmpDir('brain-store-migrate-');
|
|
230
|
+
try {
|
|
231
|
+
const plain = new Storage(plainDir);
|
|
232
|
+
const brain = plain.createBrain('usr_m', 'default');
|
|
233
|
+
const archive = sampleArchive(2, 'before-key');
|
|
234
|
+
await plain.commitArchive('usr_m', brain.id, { archive, fileCount: 3 });
|
|
235
|
+
|
|
236
|
+
const keyed = new Storage(plainDir, { encryptionKey: key });
|
|
237
|
+
assert.ok(keyed.readArchive('usr_m', brain.id).equals(archive));
|
|
238
|
+
} finally {
|
|
239
|
+
fs.rmSync(plainDir, { recursive: true, force: true });
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe('request parsing', () => {
|
|
245
|
+
it('finds the named file among several multipart fields', () => {
|
|
246
|
+
const boundary = 'XBOUNDARYX';
|
|
247
|
+
const body = Buffer.concat([
|
|
248
|
+
Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="note"\r\n\r\nhello\r\n`),
|
|
249
|
+
Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="brain"; filename="brain.tar.gz"\r\nContent-Type: application/gzip\r\n\r\n`),
|
|
250
|
+
Buffer.from([0x1f, 0x8b, 0x08, 0x0d, 0x0a, 0x2d, 0x2d, 0x00]),
|
|
251
|
+
Buffer.from(`\r\n--${boundary}--\r\n`),
|
|
252
|
+
]);
|
|
253
|
+
const field = multipartField(body, `multipart/form-data; boundary=${boundary}`, 'brain');
|
|
254
|
+
assert.deepEqual([...field], [0x1f, 0x8b, 0x08, 0x0d, 0x0a, 0x2d, 0x2d, 0x00]);
|
|
255
|
+
assert.equal(multipartField(body, `multipart/form-data; boundary=${boundary}`, 'missing'), null);
|
|
256
|
+
assert.equal(multipartField(body, 'application/json', 'brain'), null);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('does not mistake a filename for the field name', () => {
|
|
260
|
+
const boundary = 'B';
|
|
261
|
+
const body = Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; filename="brain"; name="other"\r\n\r\ndata\r\n--${boundary}--\r\n`);
|
|
262
|
+
assert.equal(multipartField(body, `multipart/form-data; boundary=${boundary}`, 'brain'), null);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('parses entity tags', () => {
|
|
266
|
+
assert.deepEqual(parseEtags('"abc"'), ['abc']);
|
|
267
|
+
assert.deepEqual(parseEtags('W/"abc", "def"'), ['abc', 'def']);
|
|
268
|
+
assert.deepEqual(parseEtags('*'), ['*']);
|
|
269
|
+
assert.equal(parseEtags(undefined), null);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('limits', () => {
|
|
274
|
+
it('refuses an archive larger than the upload limit with 413', async () => {
|
|
275
|
+
const ref = await startReferenceStore({ maxUploadBytes: 2048 });
|
|
276
|
+
try {
|
|
277
|
+
const api = client(ref.url, ref.token);
|
|
278
|
+
const { data: brain } = await api.json('POST', '/api/brains', { name: 'small' });
|
|
279
|
+
const big = makeArchive({ 'big.md': crypto.randomBytes(8192).toString('hex') });
|
|
280
|
+
assert.ok(big.length > 2048);
|
|
281
|
+
// The server may close the socket as it answers, so a transport error is
|
|
282
|
+
// also an acceptable outcome for the client here.
|
|
283
|
+
const outcome = await api.upload(brain.id, big).then((r) => r.status, () => 413);
|
|
284
|
+
assert.equal(outcome, 413);
|
|
285
|
+
assert.equal((await api.download(brain.id)).status, 404);
|
|
286
|
+
} finally {
|
|
287
|
+
await ref.close();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('enforces the per-user storage quota', async () => {
|
|
292
|
+
const ref = await startReferenceStore({ maxUserBytes: 1500 });
|
|
293
|
+
try {
|
|
294
|
+
const api = client(ref.url, ref.token);
|
|
295
|
+
const { data: brain } = await api.json('POST', '/api/brains', { name: 'quota' });
|
|
296
|
+
const res = await api.upload(brain.id, makeArchive({ 'big.md': crypto.randomBytes(4096).toString('hex') }));
|
|
297
|
+
assert.equal(res.status, 413);
|
|
298
|
+
assert.match(res.data.error, /quota/);
|
|
299
|
+
} finally {
|
|
300
|
+
await ref.close();
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('caps the number of brains per user', async () => {
|
|
305
|
+
const ref = await startReferenceStore({ maxBrains: 2 });
|
|
306
|
+
try {
|
|
307
|
+
const api = client(ref.url, ref.token);
|
|
308
|
+
assert.equal((await api.json('POST', '/api/brains', { name: 'one' })).status, 201);
|
|
309
|
+
assert.equal((await api.json('POST', '/api/brains', { name: 'two' })).status, 201);
|
|
310
|
+
assert.equal((await api.json('POST', '/api/brains', { name: 'three' })).status, 403);
|
|
311
|
+
} finally {
|
|
312
|
+
await ref.close();
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('slows down token guessing with 429 and Retry-After', async () => {
|
|
317
|
+
const ref = await startReferenceStore({ failedAuthPerMinute: 3 });
|
|
318
|
+
try {
|
|
319
|
+
const stranger = client(ref.url, 'bst_wrong');
|
|
320
|
+
const statuses = [];
|
|
321
|
+
let retryAfter = null;
|
|
322
|
+
for (let i = 0; i < 5; i++) {
|
|
323
|
+
const res = await stranger.json('GET', '/api/brains');
|
|
324
|
+
statuses.push(res.status);
|
|
325
|
+
if (res.status === 429) retryAfter = res.headers.get('retry-after');
|
|
326
|
+
}
|
|
327
|
+
assert.deepEqual(statuses, [401, 401, 401, 429, 429]);
|
|
328
|
+
assert.ok(Number(retryAfter) >= 1);
|
|
329
|
+
// A valid token is unaffected by someone else's failures.
|
|
330
|
+
assert.equal((await client(ref.url, ref.token).json('GET', '/api/brains')).status, 200);
|
|
331
|
+
} finally {
|
|
332
|
+
await ref.close();
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('rate-limits a single user', async () => {
|
|
337
|
+
const ref = await startReferenceStore({ requestsPerMinute: 2 });
|
|
338
|
+
try {
|
|
339
|
+
const api = client(ref.url, ref.token);
|
|
340
|
+
const statuses = [];
|
|
341
|
+
for (let i = 0; i < 3; i++) statuses.push((await api.json('GET', '/api/brains')).status);
|
|
342
|
+
assert.deepEqual(statuses, [200, 200, 429]);
|
|
343
|
+
} finally {
|
|
344
|
+
await ref.close();
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe('server hygiene', () => {
|
|
350
|
+
it('never logs a token', async () => {
|
|
351
|
+
const lines = [];
|
|
352
|
+
const ref = await startReferenceStore({ log: (entry) => lines.push(JSON.stringify(entry)) });
|
|
353
|
+
try {
|
|
354
|
+
await client(ref.url, ref.token).json('GET', '/api/brains');
|
|
355
|
+
await client(ref.url, 'bst_wrong-token').json('GET', '/api/brains');
|
|
356
|
+
assert.ok(lines.length >= 2);
|
|
357
|
+
for (const line of lines) {
|
|
358
|
+
assert.ok(!line.includes(ref.token) && !line.includes('bst_wrong-token'), line);
|
|
359
|
+
}
|
|
360
|
+
} finally {
|
|
361
|
+
await ref.close();
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('does not offer the device-code or refresh endpoints', async () => {
|
|
366
|
+
const ref = await startReferenceStore();
|
|
367
|
+
try {
|
|
368
|
+
const anonymous = client(ref.url, null);
|
|
369
|
+
assert.equal((await anonymous.json('POST', '/auth/device/request', {})).status, 404);
|
|
370
|
+
assert.equal((await anonymous.json('POST', '/auth/refresh', { refresh_token: 'x' })).status, 404);
|
|
371
|
+
} finally {
|
|
372
|
+
await ref.close();
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it('answers unknown paths and methods cleanly', async () => {
|
|
377
|
+
const ref = await startReferenceStore();
|
|
378
|
+
try {
|
|
379
|
+
const api = client(ref.url, ref.token);
|
|
380
|
+
assert.equal((await api.json('GET', '/nope')).status, 404);
|
|
381
|
+
assert.equal((await api.json('PATCH', '/api/brains', {})).status, 405);
|
|
382
|
+
assert.equal((await api.json('POST', '/api/brains', null, { 'Content-Type': 'application/json' })).status, 201);
|
|
383
|
+
} finally {
|
|
384
|
+
await ref.close();
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
describe('command line', () => {
|
|
390
|
+
let dir;
|
|
391
|
+
before(() => { dir = tmpDir('brain-store-cli-'); });
|
|
392
|
+
after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
393
|
+
|
|
394
|
+
const run = (...args) => execFileSync(process.execPath, [BIN, ...args, '--data', dir], { encoding: 'utf8' });
|
|
395
|
+
|
|
396
|
+
it('creates a user with a first brain, and prints the token once', () => {
|
|
397
|
+
const out = run('user', 'add', 'erin', '--email', 'erin@example.test');
|
|
398
|
+
const token = /(bst_[A-Za-z0-9_-]+)/.exec(out)[1];
|
|
399
|
+
const users = new UserStore(dir);
|
|
400
|
+
const erin = users.authenticate(token);
|
|
401
|
+
assert.equal(erin.email, 'erin@example.test');
|
|
402
|
+
assert.equal(new Storage(dir).listBrains(erin.id).length, 1);
|
|
403
|
+
assert.match(run('user', 'list'), /erin/);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('removes a user, and with --purge their data', () => {
|
|
407
|
+
const users = new UserStore(dir);
|
|
408
|
+
const erin = users.find('erin');
|
|
409
|
+
run('user', 'remove', 'erin', '--purge');
|
|
410
|
+
assert.equal(new UserStore(dir).find('erin'), null);
|
|
411
|
+
assert.equal(fs.existsSync(path.join(dir, 'brains', erin.id)), false);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it('prints a usable encryption key', () => {
|
|
415
|
+
const key = execFileSync(process.execPath, [BIN, 'keygen'], { encoding: 'utf8' }).trim();
|
|
416
|
+
assert.equal(parseKey(key).length, 32);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('serves, and shuts down on SIGTERM', { skip: process.platform === 'win32' }, async () => {
|
|
420
|
+
const created = new UserStore(dir).add('frank');
|
|
421
|
+
const child = spawn(process.execPath, [BIN, 'serve', '--port', '0', '--data', dir], {
|
|
422
|
+
env: { ...process.env, STORE_QUIET: '1' },
|
|
423
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
424
|
+
});
|
|
425
|
+
try {
|
|
426
|
+
const url = await new Promise((resolve, reject) => {
|
|
427
|
+
let err = '';
|
|
428
|
+
child.stderr.on('data', (chunk) => {
|
|
429
|
+
err += chunk;
|
|
430
|
+
const m = /listening on (http:\/\/\S+)/.exec(err);
|
|
431
|
+
if (m) resolve(m[1]);
|
|
432
|
+
});
|
|
433
|
+
child.on('exit', () => reject(new Error('server exited early: ' + err)));
|
|
434
|
+
setTimeout(() => reject(new Error('server did not start: ' + err)), 10000).unref();
|
|
435
|
+
});
|
|
436
|
+
const res = await client(url, created.token).json('GET', '/auth/me');
|
|
437
|
+
assert.equal(res.status, 200);
|
|
438
|
+
assert.equal(res.data.user.name, 'frank');
|
|
439
|
+
} finally {
|
|
440
|
+
const exited = new Promise((resolve) => child.once('exit', resolve));
|
|
441
|
+
child.kill('SIGTERM');
|
|
442
|
+
await exited;
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
});
|