javascript-solid-server 0.0.225 → 0.0.226

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 ADDED
@@ -0,0 +1,8 @@
1
+ # Security
2
+
3
+ JSS is pre-alpha. Reports are handled in the open unless you ask otherwise.
4
+
5
+ - **Report a problem:** open an issue at https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues. If you would rather report privately, email the maintainer (see `MAINTAINERS.md`) or use GitHub's private vulnerability reporting on this repository when it is enabled.
6
+ - **What to include:** the version (`jss --version` or `package.json`), the request or steps that show the problem, and the fix you would make if you have one.
7
+ - **What happens next:** we confirm against the code, open an issue, fix it in a pull request that names the reporter (unless you ask not to be named), and release. Past audits are in `SECURITY-AUDIT-*.md`.
8
+ - **Scope:** anything in this repository. Deployments, plugins and client libraries have their own repositories.
@@ -97,6 +97,12 @@ jss start --idp
97
97
  - Account must have a `did:nostr:<pubkey>` WebID linked
98
98
  - User needs a NIP-07 compatible browser extension
99
99
 
100
+ **What the server checks in a NIP-98 event** (`Authorization: Nostr <base64 event>`, kind 27235):
101
+ - `u` must equal the request URL (query string optional); `method` must be present and equal the HTTP method
102
+ - a PUT, POST or PATCH with a body must carry `payload` = sha256 of the exact body bytes
103
+ - `created_at` within ±60 s of the server clock, and each event id is accepted once
104
+ - Git smart-HTTP requests are the exception, and only while the server runs with `--git`: the ref-discovery GET (`/info/refs?service=git-…-pack`) and the pack-service POSTs. There `method` may be `*` or omitted, `u` may be the repo base URL, no `payload` is required, and the same event id may be reused, so a credential helper can sign once per git operation
105
+
100
106
  **Benefits:**
101
107
  - No passwords - cryptographic authentication
102
108
  - Works with existing Nostr identity
package/docs/security.md CHANGED
@@ -69,6 +69,10 @@ data directory. Two clients racing with the same, still valid, ETag get one `2xx
69
69
  `412`; the loser's write is never silently overwritten. A request that cannot take the lock
70
70
  within 30 seconds gets `503` with `Retry-After: 1`.
71
71
 
72
+ ### Identity document fetches are bounded
73
+
74
+ Authentication can make the server fetch a document it does not control: a CID document for LWS-CID tokens, a DID document and WebID back-link for did:nostr, or a WebID profile for WebID-TLS. Every such fetch runs under one deadline that covers the connection, the response headers **and** the body read, plus a byte cap. The CID and did:nostr fetchers follow redirects themselves and apply the 5-second deadline per hop; the WebID-TLS profile fetch lets `fetch` follow redirects and applies one 5-second deadline to the whole chain. In addition, the CID and did:nostr fetches also go through the SSRF guard (`validateExternalUrl`) on the original URL and on every redirect hop, with cross-origin redirects refused. A host that answers promptly and then trickles the body cannot hold an authentication attempt, or the outbound socket, open beyond the deadline: the fetch fails, the failure is cached (1 minute for CID and did:nostr lookups, 5 minutes for a WebID-TLS verification), and the request is answered as unauthenticated. Responses that are not read (redirect hops, error statuses, oversized bodies) are cancelled so the socket is released at once.
75
+
72
76
  ## Subdomain Mode (XSS Protection)
73
77
 
74
78
  By default, JSS uses **path-based pods** (`/alice/`, `/bob/`). This is simple but has a security limitation: all pods share the same origin, making cross-site scripting (XSS) attacks possible between pods.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-solid-server",
3
- "version": "0.0.225",
3
+ "version": "0.0.226",
4
4
  "description": "A minimal, fast Solid server",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -18,7 +18,9 @@
18
18
  * - Body size cap enforced via Content-Length up front AND a
19
19
  * streaming reader cap (cancel on overage), so untrusted hosts
20
20
  * can't OOM us with a large payload.
21
- * - 5-second timeout per request via AbortController.
21
+ * - 5-second timeout per request via AbortController, covering the
22
+ * connection, the headers AND the body read, so a host that
23
+ * trickles the body cannot hold the auth attempt open (#641).
22
24
  *
23
25
  * Throws on any failure; callers convert to whatever they want
24
26
  * (LWS-CID surfaces the error string, the NIP-98 path treats
@@ -26,6 +28,7 @@
26
28
  */
27
29
 
28
30
  import { validateExternalUrl } from '../utils/ssrf.js';
31
+ import { readBodyWithCap, discardBody } from '../utils/bounded-body.js';
29
32
 
30
33
  // Default body cap (256 KB) — CID documents are tiny in practice.
31
34
  const DEFAULT_MAX_BYTES = 256 * 1024;
@@ -73,6 +76,8 @@ export function _clearProfileCacheForTests() {
73
76
  * claims or is derived from a request).
74
77
  * @param {object} [opts]
75
78
  * @param {number} [opts.maxBytes=DEFAULT_MAX_BYTES]
79
+ * @param {number} [opts.timeoutMs=FETCH_TIMEOUT_MS] - deadline per hop,
80
+ * covering headers and body
76
81
  * @returns {Promise<object>} parsed JSON
77
82
  * @throws on any validation, network, redirect, size, or parse failure
78
83
  */
@@ -118,6 +123,7 @@ function setCached(url, entry) {
118
123
 
119
124
  async function fetchCidDocumentNoCache(docUrl, opts = {}) {
120
125
  const maxBytes = opts.maxBytes ?? DEFAULT_MAX_BYTES;
126
+ const timeoutMs = opts.timeoutMs ?? FETCH_TIMEOUT_MS;
121
127
  const originalOrigin = new URL(docUrl).origin;
122
128
  let currentUrl = docUrl;
123
129
 
@@ -132,8 +138,11 @@ async function fetchCidDocumentNoCache(docUrl, opts = {}) {
132
138
  throw new Error(`SSRF protection: ${validation.error}`);
133
139
  }
134
140
 
141
+ // One deadline covers the connection, the headers AND the body
142
+ // read (#641). The timer is only cleared in the finally, after
143
+ // the body has been consumed or the hop has been abandoned.
135
144
  const controller = new AbortController();
136
- const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
145
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
137
146
  let res;
138
147
  try {
139
148
  res = await fetch(currentUrl, {
@@ -141,62 +150,46 @@ async function fetchCidDocumentNoCache(docUrl, opts = {}) {
141
150
  redirect: 'manual',
142
151
  signal: controller.signal,
143
152
  });
144
- } finally {
145
- clearTimeout(timer);
146
- }
147
153
 
148
- if (res.status >= 300 && res.status < 400) {
149
- if (isLastAllowedHop) {
150
- throw new Error(`too many redirects (>${MAX_REDIRECTS})`);
154
+ if (res.status >= 300 && res.status < 400) {
155
+ if (isLastAllowedHop) {
156
+ throw new Error(`too many redirects (>${MAX_REDIRECTS})`);
157
+ }
158
+ const loc = res.headers.get('location');
159
+ if (!loc) throw new Error(`redirect ${res.status} without Location`);
160
+ const nextUrl = new URL(loc, currentUrl).toString();
161
+ const nextOrigin = new URL(nextUrl).origin;
162
+ if (nextOrigin !== originalOrigin) {
163
+ throw new Error(`cross-origin redirect refused: ${originalOrigin} → ${nextOrigin}`);
164
+ }
165
+ currentUrl = nextUrl;
166
+ continue;
151
167
  }
152
- const loc = res.headers.get('location');
153
- if (!loc) throw new Error(`redirect ${res.status} without Location`);
154
- const nextUrl = new URL(loc, currentUrl).toString();
155
- const nextOrigin = new URL(nextUrl).origin;
156
- if (nextOrigin !== originalOrigin) {
157
- throw new Error(`cross-origin redirect refused: ${originalOrigin} → ${nextOrigin}`);
158
- }
159
- currentUrl = nextUrl;
160
- continue;
161
- }
162
168
 
163
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
169
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
164
170
 
165
- const ct = (res.headers.get('content-type') || '').toLowerCase();
166
- if (!ct.includes('json')) {
167
- throw new Error(`unexpected content-type: ${ct || '(none)'}`);
168
- }
171
+ const ct = (res.headers.get('content-type') || '').toLowerCase();
172
+ if (!ct.includes('json')) {
173
+ throw new Error(`unexpected content-type: ${ct || '(none)'}`);
174
+ }
169
175
 
170
- const declared = Number(res.headers.get('content-length'));
171
- if (Number.isFinite(declared) && declared > maxBytes) {
172
- throw new Error(`CID document too large (Content-Length=${declared} > ${maxBytes})`);
176
+ const declared = Number(res.headers.get('content-length'));
177
+ if (Number.isFinite(declared) && declared > maxBytes) {
178
+ throw new Error(`CID document too large (Content-Length=${declared} > ${maxBytes})`);
179
+ }
180
+ const text = await readBodyWithCap(res, {
181
+ maxBytes,
182
+ signal: controller.signal,
183
+ label: 'CID document',
184
+ });
185
+ return JSON.parse(text);
186
+ } finally {
187
+ clearTimeout(timer);
188
+ // Every exit that did not consume the body (redirect hop, error
189
+ // status, bad content-type, oversize, abort) releases the socket.
190
+ discardBody(res);
173
191
  }
174
- const text = await readBodyWithCap(res, maxBytes);
175
- return JSON.parse(text);
176
192
  }
177
193
  throw new Error('profile fetch loop exited unexpectedly');
178
194
  }
179
195
 
180
- async function readBodyWithCap(res, maxBytes) {
181
- const reader = res.body?.getReader?.();
182
- if (!reader) {
183
- const text = await res.text();
184
- if (Buffer.byteLength(text, 'utf8') > maxBytes) {
185
- throw new Error(`CID document too large (>${maxBytes} bytes)`);
186
- }
187
- return text;
188
- }
189
- const chunks = [];
190
- let total = 0;
191
- for (;;) {
192
- const { value, done } = await reader.read();
193
- if (done) break;
194
- total += value.byteLength;
195
- if (total > maxBytes) {
196
- try { await reader.cancel(); } catch { /* noop */ }
197
- throw new Error(`CID document too large (>${maxBytes} bytes)`);
198
- }
199
- chunks.push(value);
200
- }
201
- return Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))).toString('utf8');
202
- }
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  import { validateExternalUrl } from '../utils/ssrf.js';
11
+ import { readBodyWithCap, discardBody } from '../utils/bounded-body.js';
11
12
  import { extractNostrPubkeysFromProfile } from './nostr-keys.js';
12
13
 
13
14
  // Default DID resolver endpoint
@@ -68,6 +69,7 @@ function rateLimitedError(key, message) {
68
69
  // bypass the initial validateExternalUrl check)
69
70
  // - cross-origin redirects refused (open-redirect → arbitrary host)
70
71
  // - response size cap before reading the body
72
+ // - the per-hop timeout covers the body read too (#641)
71
73
  const MAX_REDIRECTS = 5;
72
74
  const DEFAULT_FETCH_TIMEOUT_MS = 5000;
73
75
  const DEFAULT_MAX_BYTES = 1 * 1024 * 1024; // 1 MB — DID docs / WebID profiles are tiny
@@ -75,7 +77,8 @@ const DEFAULT_MAX_BYTES = 1 * 1024 * 1024; // 1 MB — DID docs / WebID profiles
75
77
  /**
76
78
  * Fetch with a timeout, manual redirect following, SSRF re-validation
77
79
  * per hop, and a body-size cap. Returns `{ url, status, headers, body }`
78
- * — `body` is a string (caller decides whether to JSON-parse).
80
+ * — `body` is a string (caller decides whether to JSON-parse), and is
81
+ * empty on a non-2xx status: error bodies are not read.
79
82
  *
80
83
  * Throws on validation, network, redirect, or size failures so the
81
84
  * resolver can swallow them uniformly into a null/false return.
@@ -109,6 +112,8 @@ export async function fetchWithRedirectGuard(initialUrl, {
109
112
  if (!validation.valid) {
110
113
  throw new Error(`SSRF protection: ${validation.error}`);
111
114
  }
115
+ // One deadline covers the connection, the headers AND the body
116
+ // read (#641); the timer is cleared only after the body is in.
112
117
  const controller = new AbortController();
113
118
  const timer = setTimeout(() => controller.abort(), timeout);
114
119
  let res;
@@ -118,51 +123,39 @@ export async function fetchWithRedirectGuard(initialUrl, {
118
123
  redirect: 'manual',
119
124
  signal: controller.signal,
120
125
  });
121
- } finally {
122
- clearTimeout(timer);
123
- }
124
- if (res.status >= 300 && res.status < 400) {
125
- if (isLastAllowedHop) throw new Error(`too many redirects (>${MAX_REDIRECTS})`);
126
- const loc = res.headers.get('location');
127
- if (!loc) throw new Error(`redirect ${res.status} without Location`);
128
- const nextUrl = new URL(loc, currentUrl).toString();
129
- const nextOrigin = new URL(nextUrl).origin;
130
- if (nextOrigin !== originalOrigin) {
131
- throw new Error(`cross-origin redirect refused: ${originalOrigin} → ${nextOrigin}`);
126
+ if (res.status >= 300 && res.status < 400) {
127
+ if (isLastAllowedHop) throw new Error(`too many redirects (>${MAX_REDIRECTS})`);
128
+ const loc = res.headers.get('location');
129
+ if (!loc) throw new Error(`redirect ${res.status} without Location`);
130
+ const nextUrl = new URL(loc, currentUrl).toString();
131
+ const nextOrigin = new URL(nextUrl).origin;
132
+ if (nextOrigin !== originalOrigin) {
133
+ throw new Error(`cross-origin redirect refused: ${originalOrigin} → ${nextOrigin}`);
134
+ }
135
+ currentUrl = nextUrl;
136
+ continue;
132
137
  }
133
- currentUrl = nextUrl;
134
- continue;
135
- }
136
- // Cap the body before reading. Content-Length pre-check rejects
137
- // a server that advertises an oversized response; the streaming
138
- // cap rejects servers that lie about Content-Length.
139
- const declared = Number(res.headers.get('content-length'));
140
- if (Number.isFinite(declared) && declared > maxBytes) {
141
- throw new Error(`response too large (Content-Length=${declared} > ${maxBytes})`);
142
- }
143
- const reader = res.body?.getReader?.();
144
- let body = '';
145
- if (!reader) {
146
- body = await res.text();
147
- if (Buffer.byteLength(body, 'utf8') > maxBytes) {
148
- throw new Error(`response too large (>${maxBytes} bytes)`);
138
+ // Error statuses fail fast with an empty body: callers only use
139
+ // `body` on 2xx, and returning here lets discardBody() in the
140
+ // finally cancel a trickling error page at once instead of
141
+ // reading it until the deadline.
142
+ if (!res.ok) {
143
+ return { url: currentUrl, status: res.status, headers: res.headers, body: '' };
149
144
  }
150
- } else {
151
- const chunks = [];
152
- let total = 0;
153
- for (;;) {
154
- const { value, done } = await reader.read();
155
- if (done) break;
156
- total += value.byteLength;
157
- if (total > maxBytes) {
158
- try { await reader.cancel(); } catch { /* noop */ }
159
- throw new Error(`response too large (>${maxBytes} bytes)`);
160
- }
161
- chunks.push(value);
145
+ // Cap the body before reading. Content-Length pre-check rejects
146
+ // a server that advertises an oversized response; the streaming
147
+ // cap rejects servers that lie about Content-Length.
148
+ const declared = Number(res.headers.get('content-length'));
149
+ if (Number.isFinite(declared) && declared > maxBytes) {
150
+ throw new Error(`response too large (Content-Length=${declared} > ${maxBytes})`);
162
151
  }
163
- body = Buffer.concat(chunks).toString('utf8');
152
+ const body = await readBodyWithCap(res, { maxBytes, signal: controller.signal });
153
+ return { url: currentUrl, status: res.status, headers: res.headers, body };
154
+ } finally {
155
+ clearTimeout(timer);
156
+ // Redirect hops and error exits never read the body; release the socket.
157
+ discardBody(res);
164
158
  }
165
- return { url: currentUrl, status: res.status, headers: res.headers, body };
166
159
  }
167
160
  throw new Error('fetch loop exited unexpectedly');
168
161
  }
package/src/auth/nostr.js CHANGED
@@ -43,6 +43,53 @@ const HTTP_AUTH_KIND = 27235;
43
43
  // Timestamp tolerance in seconds
44
44
  const TIMESTAMP_TOLERANCE = 60;
45
45
 
46
+ // Git smart-HTTP routes keep the lenient rules (missing or `*` method, URL
47
+ // prefix match, no payload tag): git credential helpers sign once for the
48
+ // repo base URL and git sends the pack bodies. Everywhere else the NIP-98
49
+ // MUSTs apply, or a captured header could be replayed as a different method
50
+ // against any sub-path (#643).
51
+ const WRITE_METHODS = new Set(['PUT', 'POST', 'PATCH']);
52
+
53
+ // A git smart-HTTP request, recognised by its shape (the ref discovery GET
54
+ // with a service query, or a pack-service POST with the git content type),
55
+ // and only while the server has git enabled. A plain resource whose path
56
+ // happens to end in /info/refs or /git-receive-pack gets the strict rules.
57
+ function isGitSmartHttp(request) {
58
+ if (request.server?.gitEnabled !== true) return false;
59
+ const [path, query = ''] = request.url.split('?');
60
+ const method = request.method.toUpperCase();
61
+ if (path.endsWith('/info/refs')) {
62
+ return method === 'GET' && /(^|&)service=git-(upload|receive)-pack(&|$)/.test(query);
63
+ }
64
+ if (/\/git-(upload|receive)-pack$/.test(path)) {
65
+ const type = request.headers['content-type'] || '';
66
+ return method === 'POST' && /^application\/x-git-(upload|receive)-pack-request/.test(type);
67
+ }
68
+ return false;
69
+ }
70
+
71
+ // Replay cache: event ids accepted inside the timestamp window (#643). An
72
+ // event is valid for ±TIMESTAMP_TOLERANCE, so an id older than twice that
73
+ // can never verify again and is dropped by a periodic sweep, as the DPoP
74
+ // jti cache in solid-oidc.js does; noteEventId itself is O(1). Not applied
75
+ // on git routes: git reuses the credential helper's one token for every
76
+ // request of an operation (info/refs, then the pack service).
77
+ const seenEventIds = new Map();
78
+ const REPLAY_WINDOW = TIMESTAMP_TOLERANCE * 2;
79
+ const REPLAY_SWEEP_INTERVAL = 60 * 1000;
80
+ function sweepEventIds() {
81
+ const now = Math.floor(Date.now() / 1000);
82
+ for (const [id, at] of seenEventIds) {
83
+ if (now - at > REPLAY_WINDOW) seenEventIds.delete(id);
84
+ }
85
+ }
86
+ setInterval(sweepEventIds, REPLAY_SWEEP_INTERVAL).unref();
87
+ function noteEventId(id, now) {
88
+ if (seenEventIds.has(id)) return false;
89
+ seenEventIds.set(id, now);
90
+ return true;
91
+ }
92
+
46
93
  // Profile-fetch body-size cap. Matches the LWS-CID verifier; both
47
94
  // callers go through the shared fetchCidDocument helper.
48
95
  const MAX_PROFILE_BYTES = 256 * 1024;
@@ -222,9 +269,10 @@ export async function verifyNostrAuth(request) {
222
269
  let urlMatches = normalizedEventUrl === normalizedRequestUrl ||
223
270
  normalizedEventUrl === normalizedRequestUrlNoQuery;
224
271
 
225
- // For git clients: allow prefix matching (event URL is base of request URL)
226
- // This enables git credential helpers that sign for the repo base URL
227
- if (!urlMatches && normalizedRequestUrlNoQuery.startsWith(normalizedEventUrl + '/')) {
272
+ // Git routes only: allow prefix matching (event URL is base of request
273
+ // URL), for git credential helpers that sign for the repo base URL.
274
+ const gitRoute = isGitSmartHttp(request);
275
+ if (!urlMatches && gitRoute && normalizedRequestUrlNoQuery.startsWith(normalizedEventUrl + '/')) {
228
276
  urlMatches = true;
229
277
  }
230
278
 
@@ -232,11 +280,14 @@ export async function verifyNostrAuth(request) {
232
280
  return { webId: null, error: `URL mismatch: event URL "${eventUrl}" does not match request URL "${fullUrl}"` };
233
281
  }
234
282
 
235
- // Validate method tag matches request method
236
- // For git clients: allow '*' as wildcard method
237
- // If method tag is missing, infer from HTTP request (lenient mode)
283
+ // Validate method tag matches request method (NIP-98: MUST). Git routes
284
+ // only: a missing tag is inferred from the request and '*' is a wildcard.
238
285
  const eventMethod = getTagValue(event, 'method');
239
- if (eventMethod && eventMethod !== '*' && eventMethod.toUpperCase() !== request.method.toUpperCase()) {
286
+ if (!eventMethod) {
287
+ if (!gitRoute) return { webId: null, error: 'Missing method tag in event' };
288
+ } else if (eventMethod === '*') {
289
+ if (!gitRoute) return { webId: null, error: 'Wildcard method is only accepted on git routes' };
290
+ } else if (eventMethod.toUpperCase() !== request.method.toUpperCase()) {
240
291
  return { webId: null, error: `Method mismatch: expected ${request.method}, got ${eventMethod}` };
241
292
  }
242
293
 
@@ -247,6 +298,12 @@ export async function verifyNostrAuth(request) {
247
298
  // to a falsy value (`null`, `false`, `0`, `""`) is still hash-checked
248
299
  // rather than silently skipping the integrity guard (Copilot review on
249
300
  // #573). Fastify leaves request.body `undefined` when no body was sent.
301
+ // A write with a body must carry the payload tag (git routes excepted:
302
+ // the pack bodies are not signed), or a replayed header would authorize
303
+ // whatever body the replayer attaches.
304
+ if (!payloadTag && !gitRoute && request.body !== undefined && WRITE_METHODS.has(request.method.toUpperCase())) {
305
+ return { webId: null, error: 'Missing payload tag for request with body' };
306
+ }
250
307
  if (payloadTag && request.body !== undefined) {
251
308
  // Hash the EXACT bytes the client signed. NIP-98's `payload` tag is
252
309
  // sha256(request body) over the wire bytes. crypto.update() accepts a
@@ -300,6 +357,12 @@ export async function verifyNostrAuth(request) {
300
357
  return { webId: null, error: 'Invalid Schnorr signature' };
301
358
  }
302
359
 
360
+ // Each signed event authorizes one request (#643), except on git routes,
361
+ // where one token covers the requests of a git operation.
362
+ if (!gitRoute && !noteEventId(event.id, now)) {
363
+ return { webId: null, error: 'Replayed event id' };
364
+ }
365
+
303
366
  // First lookup: the resource's owner WebID profile. If the pod owner
304
367
  // declared this pubkey as a verificationMethod (CID v1 / LWS-CID
305
368
  // shape — produced by the doctor's B.2 path), authenticate as the
@@ -12,6 +12,7 @@
12
12
  */
13
13
 
14
14
  import { turtleToJsonLd } from '../rdf/turtle.js';
15
+ import { readBodyWithCap, discardBody } from '../utils/bounded-body.js';
15
16
 
16
17
  // cert: ontology namespace
17
18
  const CERT_NS = 'http://www.w3.org/ns/auth/cert#';
@@ -20,19 +21,35 @@ const CERT_NS = 'http://www.w3.org/ns/auth/cert#';
20
21
  const cache = new Map();
21
22
  const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
22
23
 
24
+ const PROFILE_FETCH_TIMEOUT_MS = 5000;
25
+ const PROFILE_MAX_BYTES = 1024 * 1024; // 1 MB — profiles are tiny
26
+
23
27
  /**
24
- * Fetch with timeout
28
+ * Fetch a profile document as text under one deadline that covers
29
+ * the connection, the headers AND the body read (#641), plus a byte
30
+ * cap. Returns `{ contentType, text }`; throws on HTTP error, timeout
31
+ * or overage.
25
32
  */
26
- async function fetchWithTimeout(url, options = {}, timeout = 5000) {
33
+ async function fetchProfileText(url, options = {}, timeout = PROFILE_FETCH_TIMEOUT_MS) {
27
34
  const controller = new AbortController();
28
- const id = setTimeout(() => controller.abort(), timeout);
35
+ const timer = setTimeout(() => controller.abort(), timeout);
36
+ let response;
29
37
  try {
30
- const response = await fetch(url, { ...options, signal: controller.signal });
31
- clearTimeout(id);
32
- return response;
33
- } catch (err) {
34
- clearTimeout(id);
35
- throw err;
38
+ response = await fetch(url, { ...options, signal: controller.signal });
39
+ if (!response.ok) {
40
+ throw new Error(`Failed to fetch WebID profile: ${response.status}`);
41
+ }
42
+ const contentType = response.headers.get('content-type') || '';
43
+ const text = await readBodyWithCap(response, {
44
+ maxBytes: PROFILE_MAX_BYTES,
45
+ signal: controller.signal,
46
+ label: 'WebID profile',
47
+ });
48
+ return { contentType, text };
49
+ } finally {
50
+ clearTimeout(timer);
51
+ // An error status or abort leaves the body unread; release the socket.
52
+ discardBody(response);
36
53
  }
37
54
  }
38
55
 
@@ -134,21 +151,16 @@ function parseKeyObject(keyObj) {
134
151
  /**
135
152
  * Fetch and parse WebID profile
136
153
  * @param {string} webId - WebID URI to fetch
154
+ * @param {object} [opts]
155
+ * @param {number} [opts.timeout] - deadline in ms covering headers and body
137
156
  * @returns {Promise<Array<{modulus: string, exponent: string}>>}
138
157
  */
139
- async function fetchProfileKeys(webId) {
140
- const response = await fetchWithTimeout(webId, {
158
+ async function fetchProfileKeys(webId, { timeout } = {}) {
159
+ const { contentType, text } = await fetchProfileText(webId, {
141
160
  headers: {
142
161
  'Accept': 'application/ld+json, text/turtle, application/json'
143
162
  }
144
- });
145
-
146
- if (!response.ok) {
147
- throw new Error(`Failed to fetch WebID profile: ${response.status}`);
148
- }
149
-
150
- const contentType = response.headers.get('content-type') || '';
151
- const text = await response.text();
163
+ }, timeout);
152
164
 
153
165
  let jsonLd;
154
166
 
@@ -175,9 +187,11 @@ async function fetchProfileKeys(webId) {
175
187
  * Verify certificate against WebID profile
176
188
  * @param {object} certificate - Node.js TLS certificate object
177
189
  * @param {string} webId - WebID URI
190
+ * @param {object} [opts]
191
+ * @param {number} [opts.timeout] - profile fetch deadline in ms (tests)
178
192
  * @returns {Promise<boolean>} True if certificate matches profile
179
193
  */
180
- export async function verifyWebIdTls(certificate, webId) {
194
+ export async function verifyWebIdTls(certificate, webId, { timeout } = {}) {
181
195
  if (!certificate.modulus || !certificate.exponent) {
182
196
  throw new Error('Certificate missing modulus or exponent');
183
197
  }
@@ -195,7 +209,7 @@ export async function verifyWebIdTls(certificate, webId) {
195
209
  }
196
210
 
197
211
  try {
198
- const profileKeys = await fetchProfileKeys(webId);
212
+ const profileKeys = await fetchProfileKeys(webId, { timeout });
199
213
 
200
214
  // Check if any key matches
201
215
  const verified = profileKeys.some(key =>
package/src/server.js CHANGED
@@ -334,6 +334,8 @@ export function createServer(options = {}) {
334
334
  fastify.decorateRequest('connegEnabled', null);
335
335
  fastify.decorateRequest('notificationsEnabled', null);
336
336
  fastify.decorateRequest('idpEnabled', null);
337
+ // read by the NIP-98 verifier: the lenient git rules apply only while git is on (#643)
338
+ fastify.decorate('gitEnabled', gitEnabled);
337
339
  fastify.decorateRequest('subdomainsEnabled', null);
338
340
  fastify.decorateRequest('baseDomain', null);
339
341
  fastify.decorateRequest('podName', null);
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Read a fetch Response body under a byte cap AND an abort signal.
3
+ *
4
+ * Pass the same AbortSignal that was handed to `fetch()` and keep its
5
+ * timer alive until this resolves. That is what bounds a server that
6
+ * sends headers promptly and then trickles the body (#641): a timeout
7
+ * that is cleared as soon as `fetch()` resolves only covers the
8
+ * connection and headers, and the body read would otherwise wait for
9
+ * as long as the remote chooses to keep sending.
10
+ *
11
+ * On abort the reader is cancelled (which releases the upstream
12
+ * socket) and the read rejects with a timeout error, never with a
13
+ * partial body.
14
+ *
15
+ * @param {Response} res
16
+ * @param {object} opts
17
+ * @param {number} opts.maxBytes - reject once more than this many bytes arrive
18
+ * @param {AbortSignal} [opts.signal] - the fetch's signal; abort ends the read
19
+ * @param {string} [opts.label='response'] - noun used in error messages
20
+ * @returns {Promise<string>} body as UTF-8 text
21
+ */
22
+ export async function readBodyWithCap(res, { maxBytes, signal, label = 'response' }) {
23
+ const tooLarge = () => new Error(`${label} too large (>${maxBytes} bytes)`);
24
+ const timedOut = () => new Error(`${label} body read timed out`);
25
+
26
+ if (signal?.aborted) throw timedOut();
27
+
28
+ const reader = res.body?.getReader?.();
29
+ if (!reader) {
30
+ // Non-streaming Response (polyfills, hand-built stubs): race the
31
+ // text read against the signal so an abort still ends the wait.
32
+ const text = await raceAbort(res.text(), signal, timedOut);
33
+ if (Buffer.byteLength(text, 'utf8') > maxBytes) throw tooLarge();
34
+ return text;
35
+ }
36
+
37
+ // A real fetch body errors on abort by itself; cancelling here also
38
+ // covers stream implementations that do not watch the signal.
39
+ const onAbort = () => { reader.cancel().catch(() => { /* noop */ }); };
40
+ signal?.addEventListener('abort', onAbort, { once: true });
41
+ try {
42
+ const chunks = [];
43
+ let total = 0;
44
+ for (;;) {
45
+ let result;
46
+ try {
47
+ result = await reader.read();
48
+ } catch (err) {
49
+ if (signal?.aborted) throw timedOut();
50
+ throw err;
51
+ }
52
+ // Check abort before honouring `done`: a cancelled reader resolves
53
+ // `{ done: true }`, which must not pass off a partial body as complete.
54
+ if (signal?.aborted) throw timedOut();
55
+ const { value, done } = result;
56
+ if (done) break;
57
+ total += value.byteLength;
58
+ if (total > maxBytes) {
59
+ try { await reader.cancel(); } catch { /* noop */ }
60
+ throw tooLarge();
61
+ }
62
+ chunks.push(value);
63
+ }
64
+ return Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))).toString('utf8');
65
+ } finally {
66
+ signal?.removeEventListener('abort', onAbort);
67
+ }
68
+ }
69
+
70
+ function raceAbort(promise, signal, makeError) {
71
+ if (!signal) return promise;
72
+ return new Promise((resolve, reject) => {
73
+ const onAbort = () => reject(makeError());
74
+ signal.addEventListener('abort', onAbort, { once: true });
75
+ promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort));
76
+ });
77
+ }
78
+
79
+ /**
80
+ * Release a Response whose body will not be read (redirect hop, error
81
+ * status, unexpected content-type, oversize, abort). Without this the
82
+ * upstream socket stays busy until the Response is garbage-collected,
83
+ * which on a trickling host means well past the deadline. Fire and
84
+ * forget: cancel() on undici tears the connection down at once.
85
+ */
86
+ export function discardBody(res) {
87
+ if (!res?.body || res.bodyUsed) return;
88
+ try { res.body.cancel().catch(() => { /* locked or already closed */ }); } catch { /* noop */ }
89
+ }
@@ -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
+ });