javascript-solid-server 0.0.225 → 0.0.227
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/SECURITY.md +8 -0
- package/bin/jss.js +14 -0
- package/docs/authentication.md +6 -0
- package/docs/configuration.md +1 -0
- package/docs/mashlib.md +21 -0
- package/docs/security.md +4 -0
- package/package.json +1 -1
- package/src/auth/cid-doc-fetch.js +44 -51
- package/src/auth/did-nostr.js +35 -42
- package/src/auth/middleware.js +1 -1
- package/src/auth/nostr.js +70 -7
- package/src/auth/webid-tls.js +35 -21
- package/src/config.js +4 -0
- package/src/handlers/resource.js +2 -2
- package/src/mashlib/index.js +146 -4
- package/src/mashlib/pane-loader.js +95 -0
- package/src/server.js +22 -0
- package/src/utils/bounded-body.js +89 -0
- package/test/auth-fetch-body-timeout.test.js +253 -0
- package/test/nip98-replay.test.js +145 -0
- package/test/panes.test.js +282 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #641 — the auth document fetch deadline must cover the body read.
|
|
3
|
+
*
|
|
4
|
+
* A profile host that sends headers promptly and then trickles the
|
|
5
|
+
* body used to hold the authentication attempt (and the upstream
|
|
6
|
+
* socket) open indefinitely: the abort timer was cleared as soon as
|
|
7
|
+
* fetch() resolved, and the body was read with no clock on it.
|
|
8
|
+
*
|
|
9
|
+
* Three paths are covered:
|
|
10
|
+
* - did-nostr fetchWithRedirectGuard (real HTTP server, validator seam)
|
|
11
|
+
* - WebID-TLS verifyWebIdTls (real HTTP server; no SSRF guard there)
|
|
12
|
+
* - cid-doc-fetch fetchCidDocument (global.fetch stub with a trickling
|
|
13
|
+
* ReadableStream, because the SSRF guard blocks loopback)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, before, after, beforeEach } from 'node:test';
|
|
17
|
+
import assert from 'node:assert';
|
|
18
|
+
import http from 'node:http';
|
|
19
|
+
import { fetchWithRedirectGuard } from '../src/auth/did-nostr.js';
|
|
20
|
+
import { verifyWebIdTls, clearCache as clearTlsCache } from '../src/auth/webid-tls.js';
|
|
21
|
+
import { fetchCidDocument, _clearProfileCacheForTests } from '../src/auth/cid-doc-fetch.js';
|
|
22
|
+
import { readBodyWithCap } from '../src/utils/bounded-body.js';
|
|
23
|
+
|
|
24
|
+
const TIMEOUT_MS = 300;
|
|
25
|
+
const TRICKLE_MS = 50;
|
|
26
|
+
// Generous upper bound: the deadline plus scheduling slack, and far
|
|
27
|
+
// below how long the trickle would run if the timeout did not fire.
|
|
28
|
+
const MAX_ELAPSED_MS = 2500;
|
|
29
|
+
|
|
30
|
+
const allowAll = async () => ({ valid: true });
|
|
31
|
+
|
|
32
|
+
/** Start a server whose /trickle writes headers now and one byte every TRICKLE_MS forever. */
|
|
33
|
+
function startTrickleServer(contentType) {
|
|
34
|
+
const closed = [];
|
|
35
|
+
const server = http.createServer((req, res) => {
|
|
36
|
+
if (req.url.startsWith('/redirect-trickle')) {
|
|
37
|
+
// 302 whose own body trickles forever: the hop must be abandoned
|
|
38
|
+
// AND its socket released before the next hop is fetched.
|
|
39
|
+
res.writeHead(302, { Location: '/ok', 'Content-Type': 'text/plain' });
|
|
40
|
+
res.write('moved');
|
|
41
|
+
const iv = setInterval(() => res.write(' '), TRICKLE_MS);
|
|
42
|
+
const stop = () => { clearInterval(iv); closed.push(req.url); };
|
|
43
|
+
req.on('close', stop);
|
|
44
|
+
res.on('close', stop);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (req.url.startsWith('/error-trickle')) {
|
|
48
|
+
res.writeHead(500, { 'Content-Type': contentType });
|
|
49
|
+
res.write('{');
|
|
50
|
+
const iv = setInterval(() => res.write(' '), TRICKLE_MS);
|
|
51
|
+
const stop = () => { clearInterval(iv); closed.push(req.url); };
|
|
52
|
+
req.on('close', stop);
|
|
53
|
+
res.on('close', stop);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (req.url === '/ok') {
|
|
57
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
58
|
+
res.end('{"ok":true}');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (req.url.startsWith('/trickle')) {
|
|
62
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
63
|
+
res.write('{');
|
|
64
|
+
const iv = setInterval(() => res.write(' '), TRICKLE_MS);
|
|
65
|
+
const stop = () => { clearInterval(iv); closed.push(req.url); };
|
|
66
|
+
req.on('close', stop);
|
|
67
|
+
res.on('close', stop);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
res.writeHead(404).end();
|
|
71
|
+
});
|
|
72
|
+
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => {
|
|
73
|
+
resolve({ server, port: server.address().port, closed });
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function waitFor(pred, ms = 1500) {
|
|
78
|
+
const until = Date.now() + ms;
|
|
79
|
+
while (Date.now() < until) {
|
|
80
|
+
if (pred()) return true;
|
|
81
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
82
|
+
}
|
|
83
|
+
return pred();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe('#641 auth document fetch: deadline covers the body read', () => {
|
|
87
|
+
let srv;
|
|
88
|
+
before(async () => { srv = await startTrickleServer('application/json'); });
|
|
89
|
+
after(async () => {
|
|
90
|
+
// Aborted upstream requests may linger as idle keep-alive sockets;
|
|
91
|
+
// close() alone would wait for them.
|
|
92
|
+
srv.server.closeAllConnections();
|
|
93
|
+
await new Promise((resolve) => srv.server.close(resolve));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('did-nostr fetchWithRedirectGuard rejects a trickling body within the deadline and drops the socket', async () => {
|
|
97
|
+
const started = Date.now();
|
|
98
|
+
await assert.rejects(
|
|
99
|
+
() => fetchWithRedirectGuard(`http://127.0.0.1:${srv.port}/trickle/did`, {
|
|
100
|
+
_validateUrl: allowAll,
|
|
101
|
+
timeout: TIMEOUT_MS,
|
|
102
|
+
}),
|
|
103
|
+
/timed out|abort/i,
|
|
104
|
+
);
|
|
105
|
+
const elapsed = Date.now() - started;
|
|
106
|
+
assert.ok(elapsed < MAX_ELAPSED_MS, `took ${elapsed}ms`);
|
|
107
|
+
assert.ok(await waitFor(() => srv.closed.includes('/trickle/did')), 'upstream request was not closed');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('WebID-TLS verifyWebIdTls gives up on a trickling profile within the deadline and drops the socket', async () => {
|
|
111
|
+
clearTlsCache();
|
|
112
|
+
const cert = { modulus: 'abc123def456', exponent: '10001' };
|
|
113
|
+
const started = Date.now();
|
|
114
|
+
const verified = await verifyWebIdTls(cert, `http://127.0.0.1:${srv.port}/trickle/tls`, { timeout: TIMEOUT_MS });
|
|
115
|
+
const elapsed = Date.now() - started;
|
|
116
|
+
assert.strictEqual(verified, false);
|
|
117
|
+
assert.ok(elapsed < MAX_ELAPSED_MS, `took ${elapsed}ms`);
|
|
118
|
+
assert.ok(await waitFor(() => srv.closed.includes('/trickle/tls')), 'upstream request was not closed');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('did-nostr releases a trickling redirect body before following the hop', async () => {
|
|
122
|
+
const started = Date.now();
|
|
123
|
+
const r = await fetchWithRedirectGuard(`http://127.0.0.1:${srv.port}/redirect-trickle/did`, {
|
|
124
|
+
_validateUrl: allowAll,
|
|
125
|
+
timeout: TIMEOUT_MS,
|
|
126
|
+
});
|
|
127
|
+
assert.strictEqual(r.status, 200);
|
|
128
|
+
assert.strictEqual(r.body, '{"ok":true}');
|
|
129
|
+
assert.ok(Date.now() - started < MAX_ELAPSED_MS);
|
|
130
|
+
assert.ok(await waitFor(() => srv.closed.includes('/redirect-trickle/did')), 'redirect hop socket was not released');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('did-nostr returns an error status at once and releases its trickling body', async () => {
|
|
134
|
+
const started = Date.now();
|
|
135
|
+
const r = await fetchWithRedirectGuard(`http://127.0.0.1:${srv.port}/error-trickle/did`, {
|
|
136
|
+
_validateUrl: allowAll,
|
|
137
|
+
timeout: TIMEOUT_MS,
|
|
138
|
+
});
|
|
139
|
+
assert.strictEqual(r.status, 500);
|
|
140
|
+
assert.strictEqual(r.body, '');
|
|
141
|
+
assert.ok(Date.now() - started < 200, 'should return on the status, not wait for the deadline');
|
|
142
|
+
assert.ok(await waitFor(() => srv.closed.includes('/error-trickle/did')), 'error response socket was not released');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('WebID-TLS releases a trickling error-status body', async () => {
|
|
146
|
+
clearTlsCache();
|
|
147
|
+
const cert = { modulus: 'abc123def456', exponent: '10001' };
|
|
148
|
+
const started = Date.now();
|
|
149
|
+
const verified = await verifyWebIdTls(cert, `http://127.0.0.1:${srv.port}/error-trickle/tls`, { timeout: TIMEOUT_MS });
|
|
150
|
+
assert.strictEqual(verified, false);
|
|
151
|
+
assert.ok(Date.now() - started < 200, 'should fail on the status, not wait for the deadline');
|
|
152
|
+
assert.ok(await waitFor(() => srv.closed.includes('/error-trickle/tls')), 'error response socket was not released');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('cid-doc-fetch fetchCidDocument (stubbed fetch, trickling stream)', () => {
|
|
156
|
+
const realFetch = global.fetch;
|
|
157
|
+
let cancelled = 0;
|
|
158
|
+
|
|
159
|
+
before(() => {
|
|
160
|
+
global.fetch = async (url, init = {}) => {
|
|
161
|
+
const signal = init.signal;
|
|
162
|
+
const status = String(url).includes('/err') ? 404 : 200;
|
|
163
|
+
let stop;
|
|
164
|
+
const stream = new ReadableStream({
|
|
165
|
+
start(controller) {
|
|
166
|
+
controller.enqueue(new TextEncoder().encode('{'));
|
|
167
|
+
const iv = setInterval(() => {
|
|
168
|
+
try { controller.enqueue(new TextEncoder().encode(' ')); } catch { /* errored or closed */ }
|
|
169
|
+
}, TRICKLE_MS);
|
|
170
|
+
stop = () => { clearInterval(iv); cancelled++; };
|
|
171
|
+
// Mirror what a real fetch body does on abort: error the stream.
|
|
172
|
+
signal?.addEventListener('abort', () => {
|
|
173
|
+
stop();
|
|
174
|
+
try { controller.error(new DOMException('This operation was aborted', 'AbortError')); } catch { /* already closed */ }
|
|
175
|
+
}, { once: true });
|
|
176
|
+
},
|
|
177
|
+
cancel() { stop?.(); },
|
|
178
|
+
});
|
|
179
|
+
return new Response(stream, { status, headers: { 'content-type': 'application/ld+json' } });
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
after(() => { global.fetch = realFetch; });
|
|
183
|
+
beforeEach(() => { _clearProfileCacheForTests(); cancelled = 0; });
|
|
184
|
+
|
|
185
|
+
it('rejects within the deadline instead of waiting for the body', async () => {
|
|
186
|
+
const started = Date.now();
|
|
187
|
+
await assert.rejects(
|
|
188
|
+
() => fetchCidDocument('https://example.com/profile/trickle.jsonld', { timeoutMs: TIMEOUT_MS }),
|
|
189
|
+
/timed out|abort/i,
|
|
190
|
+
);
|
|
191
|
+
const elapsed = Date.now() - started;
|
|
192
|
+
assert.ok(elapsed < MAX_ELAPSED_MS, `took ${elapsed}ms`);
|
|
193
|
+
assert.ok(cancelled >= 1, 'body stream was not released');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('releases a trickling body on an error status instead of leaving it to GC', async () => {
|
|
197
|
+
const started = Date.now();
|
|
198
|
+
await assert.rejects(
|
|
199
|
+
() => fetchCidDocument('https://example.com/profile/err.jsonld', { timeoutMs: TIMEOUT_MS }),
|
|
200
|
+
/HTTP 404/,
|
|
201
|
+
);
|
|
202
|
+
assert.ok(Date.now() - started < 200, 'should fail on the status, not wait for the deadline');
|
|
203
|
+
assert.ok(await waitFor(() => cancelled >= 1), 'body stream was not released');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('caches the timeout as a failure like any other fetch error', async () => {
|
|
207
|
+
await assert.rejects(() => fetchCidDocument('https://example.com/profile/trickle2.jsonld', { timeoutMs: TIMEOUT_MS }));
|
|
208
|
+
const started = Date.now();
|
|
209
|
+
await assert.rejects(() => fetchCidDocument('https://example.com/profile/trickle2.jsonld', { timeoutMs: TIMEOUT_MS }), /timed out/);
|
|
210
|
+
assert.ok(Date.now() - started < 100, 'second call should be served from the failure cache');
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe('readBodyWithCap', () => {
|
|
215
|
+
it('never returns a partial body when the signal fires mid-read', async () => {
|
|
216
|
+
const ac = new AbortController();
|
|
217
|
+
let pull = 0;
|
|
218
|
+
const stream = new ReadableStream({
|
|
219
|
+
pull(controller) {
|
|
220
|
+
pull++;
|
|
221
|
+
controller.enqueue(new TextEncoder().encode('x'));
|
|
222
|
+
if (pull === 3) ac.abort();
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
await assert.rejects(
|
|
226
|
+
() => readBodyWithCap(new Response(stream), { maxBytes: 1024, signal: ac.signal, label: 'thing' }),
|
|
227
|
+
/thing body read timed out/,
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('still enforces the byte cap', async () => {
|
|
232
|
+
await assert.rejects(
|
|
233
|
+
() => readBodyWithCap(new Response('x'.repeat(2000)), { maxBytes: 1000, label: 'thing' }),
|
|
234
|
+
/thing too large \(>1000 bytes\)/,
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('reads a normal body to completion', async () => {
|
|
239
|
+
const ac = new AbortController();
|
|
240
|
+
const text = await readBodyWithCap(new Response('{"ok":true}'), { maxBytes: 1000, signal: ac.signal });
|
|
241
|
+
assert.strictEqual(text, '{"ok":true}');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('rejects immediately on an already-aborted signal', async () => {
|
|
245
|
+
const ac = new AbortController();
|
|
246
|
+
ac.abort();
|
|
247
|
+
await assert.rejects(
|
|
248
|
+
() => readBodyWithCap(new Response('x'), { maxBytes: 10, signal: ac.signal }),
|
|
249
|
+
/response body read timed out/,
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NIP-98 verifier: a signed header authorizes one request, one method, one
|
|
3
|
+
* URL (#643). Reported privately by Kraig Parkinson against v0.0.225.
|
|
4
|
+
*
|
|
5
|
+
* Before: the method tag was optional and `*` a wildcard, the `u` tag matched
|
|
6
|
+
* by prefix, and nothing remembered an event id, so an Authorization header
|
|
7
|
+
* captured from a GET of the pod root could be replayed within 60 s as a PUT
|
|
8
|
+
* or DELETE on any sub-path. The git smart-HTTP routes keep the lenient rules
|
|
9
|
+
* (credential helpers sign once for the repo base); everywhere else the
|
|
10
|
+
* NIP-98 MUSTs apply and a reused event id is rejected.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, it } from 'node:test';
|
|
14
|
+
import assert from 'node:assert';
|
|
15
|
+
import { createHash } from 'node:crypto';
|
|
16
|
+
import { verifyNostrAuth } from '../src/auth/nostr.js';
|
|
17
|
+
import { generateSecretKey, finalizeEvent, nip98Token } from '../src/nostr/event.js';
|
|
18
|
+
|
|
19
|
+
const HOST = 'pod.example';
|
|
20
|
+
const sk = generateSecretKey();
|
|
21
|
+
|
|
22
|
+
function tokenFromTags(tags) {
|
|
23
|
+
const event = finalizeEvent({ kind: 27235, tags, content: '' }, sk);
|
|
24
|
+
return Buffer.from(JSON.stringify(event)).toString('base64');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function req({ url, method = 'GET', token, body, git = false, type }) {
|
|
28
|
+
const r = { url, method, protocol: 'http', hostname: HOST, headers: { host: HOST, authorization: `Nostr ${token}` }, server: { gitEnabled: git } };
|
|
29
|
+
if (type) r.headers['content-type'] = type;
|
|
30
|
+
if (body !== undefined) { r.body = body; r.rawBody = body; }
|
|
31
|
+
return r;
|
|
32
|
+
}
|
|
33
|
+
const PACK = 'application/x-git-receive-pack-request';
|
|
34
|
+
|
|
35
|
+
// Every path here is unowned, so the resolver chain ends at did:nostr:<pubkey>.
|
|
36
|
+
const isAuthed = (r) => r.error === null && typeof r.webId === 'string' && r.webId.startsWith('did:nostr:');
|
|
37
|
+
|
|
38
|
+
describe('NIP-98 verifier: one header, one request (#643)', () => {
|
|
39
|
+
it('a correctly signed GET verifies', async () => {
|
|
40
|
+
const token = nip98Token(`http://${HOST}/alice/notes/`, 'GET', sk);
|
|
41
|
+
assert.ok(isAuthed(await verifyNostrAuth(req({ url: '/alice/notes/', token }))));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('the same event id a second time is rejected', async () => {
|
|
45
|
+
const token = nip98Token(`http://${HOST}/alice/again/`, 'GET', sk);
|
|
46
|
+
assert.ok(isAuthed(await verifyNostrAuth(req({ url: '/alice/again/', token }))));
|
|
47
|
+
const again = await verifyNostrAuth(req({ url: '/alice/again/', token }));
|
|
48
|
+
assert.strictEqual(again.webId, null);
|
|
49
|
+
assert.match(again.error, /Replayed event id/);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('a missing method tag is rejected outside git routes', async () => {
|
|
53
|
+
const token = tokenFromTags([['u', `http://${HOST}/alice/`]]);
|
|
54
|
+
const r = await verifyNostrAuth(req({ url: '/alice/', method: 'DELETE', token }));
|
|
55
|
+
assert.strictEqual(r.webId, null);
|
|
56
|
+
assert.match(r.error, /Missing method tag/);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('a wildcard method is rejected outside git routes', async () => {
|
|
60
|
+
const token = tokenFromTags([['u', `http://${HOST}/alice/`], ['method', '*']]);
|
|
61
|
+
const r = await verifyNostrAuth(req({ url: '/alice/', method: 'DELETE', token }));
|
|
62
|
+
assert.strictEqual(r.webId, null);
|
|
63
|
+
assert.match(r.error, /Wildcard method/);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('a signature for the base URL does not cover a sub-path outside git routes', async () => {
|
|
67
|
+
const token = nip98Token(`http://${HOST}/alice/`, 'GET', sk);
|
|
68
|
+
const r = await verifyNostrAuth(req({ url: '/alice/secret.json', token }));
|
|
69
|
+
assert.strictEqual(r.webId, null);
|
|
70
|
+
assert.match(r.error, /URL mismatch/);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('a GET token replayed as a PUT with a body is rejected twice over', async () => {
|
|
74
|
+
const token = nip98Token(`http://${HOST}/alice/notes/`, 'GET', sk);
|
|
75
|
+
const r = await verifyNostrAuth(req({ url: '/alice/notes/', method: 'PUT', token, body: '{"owned":true}' }));
|
|
76
|
+
assert.strictEqual(r.webId, null);
|
|
77
|
+
assert.match(r.error, /Method mismatch/);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('a PUT with a body needs the payload tag', async () => {
|
|
81
|
+
const token = nip98Token(`http://${HOST}/alice/doc.json`, 'PUT', sk);
|
|
82
|
+
const r = await verifyNostrAuth(req({ url: '/alice/doc.json', method: 'PUT', token, body: '{"a":1}' }));
|
|
83
|
+
assert.strictEqual(r.webId, null);
|
|
84
|
+
assert.match(r.error, /Missing payload tag/);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('a PUT with a matching payload tag verifies', async () => {
|
|
88
|
+
const body = '{"a":1}';
|
|
89
|
+
const token = nip98Token(`http://${HOST}/alice/doc2.json`, 'PUT', sk, body);
|
|
90
|
+
assert.ok(isAuthed(await verifyNostrAuth(req({ url: '/alice/doc2.json', method: 'PUT', token, body }))));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('a PUT with a mismatching payload tag is still rejected', async () => {
|
|
94
|
+
const token = nip98Token(`http://${HOST}/alice/doc3.json`, 'PUT', sk, '{"a":1}');
|
|
95
|
+
const r = await verifyNostrAuth(req({ url: '/alice/doc3.json', method: 'PUT', token, body: '{"a":2}' }));
|
|
96
|
+
assert.match(r.error, /Payload hash mismatch/);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('git routes keep the lenient rules: wildcard method and base-URL prefix', async () => {
|
|
100
|
+
const token = tokenFromTags([['u', `http://${HOST}/alice/repo`], ['method', '*']]);
|
|
101
|
+
const r = await verifyNostrAuth(req({ url: '/alice/repo/info/refs?service=git-receive-pack', token, git: true }));
|
|
102
|
+
assert.ok(isAuthed(r), r.error);
|
|
103
|
+
const push = tokenFromTags([['u', `http://${HOST}/alice/repo`]]);
|
|
104
|
+
const r2 = await verifyNostrAuth(req({ url: '/alice/repo/git-receive-pack', method: 'POST', token: push, body: Buffer.from('0000'), git: true, type: PACK }));
|
|
105
|
+
assert.ok(isAuthed(r2), r2.error);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('a git-looking path is strict when git is off, or when the request is not git-shaped', async () => {
|
|
109
|
+
const off = tokenFromTags([['u', `http://${HOST}/alice/notes/git-receive-pack`], ['method', '*']]);
|
|
110
|
+
const r = await verifyNostrAuth(req({ url: '/alice/notes/git-receive-pack', method: 'POST', token: off, body: 'x', git: false, type: PACK }));
|
|
111
|
+
assert.match(r.error, /Wildcard method/);
|
|
112
|
+
const plainGet = tokenFromTags([['u', `http://${HOST}/alice/notes/info/refs`], ['method', '*']]);
|
|
113
|
+
const r2 = await verifyNostrAuth(req({ url: '/alice/notes/info/refs', token: plainGet, git: true })); // no service query
|
|
114
|
+
assert.match(r2.error, /Wildcard method/);
|
|
115
|
+
const putPack = tokenFromTags([['u', `http://${HOST}/alice/notes/git-upload-pack`]]);
|
|
116
|
+
const r3 = await verifyNostrAuth(req({ url: '/alice/notes/git-upload-pack', method: 'PUT', token: putPack, body: 'x', git: true, type: 'text/plain' }));
|
|
117
|
+
assert.match(r3.error, /Missing method tag/);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('git routes accept the same token for every request of one operation', async () => {
|
|
121
|
+
const token = tokenFromTags([['u', `http://${HOST}/alice/twice`], ['method', '*']]);
|
|
122
|
+
const r1 = await verifyNostrAuth(req({ url: '/alice/twice/info/refs?service=git-receive-pack', token, git: true }));
|
|
123
|
+
assert.ok(isAuthed(r1), r1.error);
|
|
124
|
+
const r2 = await verifyNostrAuth(req({ url: '/alice/twice/git-receive-pack', method: 'POST', token, body: Buffer.from('0000'), git: true, type: PACK }));
|
|
125
|
+
assert.ok(isAuthed(r2), r2.error);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('the replay cache forgets an id once it can no longer verify', async () => {
|
|
129
|
+
const token = nip98Token(`http://${HOST}/alice/old/`, 'GET', sk);
|
|
130
|
+
assert.ok(isAuthed(await verifyNostrAuth(req({ url: '/alice/old/', token }))));
|
|
131
|
+
const event = JSON.parse(Buffer.from(token, 'base64').toString());
|
|
132
|
+
const stale = { ...event, created_at: event.created_at - 200 }; // outside ±60 s, so rejected on time, never on replay
|
|
133
|
+
const staleToken = Buffer.from(JSON.stringify(stale)).toString('base64');
|
|
134
|
+
const r = await verifyNostrAuth(req({ url: '/alice/old/', token: staleToken }));
|
|
135
|
+
assert.match(r.error, /timestamp/);
|
|
136
|
+
assert.ok(!/Replayed/.test(r.error));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('a payload hash is computed over the raw bytes (unchanged)', async () => {
|
|
140
|
+
const body = Buffer.from([0xff, 0x00, 0x10]);
|
|
141
|
+
const token = tokenFromTags([['u', `http://${HOST}/alice/bin`], ['method', 'PUT'], ['payload', createHash('sha256').update(body).digest('hex')]]);
|
|
142
|
+
const r = { url: '/alice/bin', method: 'PUT', protocol: 'http', hostname: HOST, headers: { host: HOST, authorization: `Nostr ${token}` }, body };
|
|
143
|
+
assert.ok(isAuthed(await verifyNostrAuth(r)));
|
|
144
|
+
});
|
|
145
|
+
});
|