javascript-solid-server 0.0.177 → 0.0.178

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-solid-server",
3
- "version": "0.0.177",
3
+ "version": "0.0.178",
4
4
  "description": "A minimal, fast Solid server",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Shared CID-document / WebID-profile fetcher.
3
+ *
4
+ * Used by both the LWS10-CID JWT verifier (`src/auth/lws-cid.js`) and
5
+ * the NIP-98 → WebID VM-lookup path (`src/auth/nostr.js`). Centralized
6
+ * so future SSRF / redirect / DoS hardening only needs to land in one
7
+ * place.
8
+ *
9
+ * Defenses (mirrored from the original lws-cid implementation):
10
+ *
11
+ * - URL validated through `validateExternalUrl` (loopback, private
12
+ * IPs, http-in-prod blocked) on the original URL AND every
13
+ * redirect Location.
14
+ * - Manual redirect handling — no automatic following — capped at
15
+ * MAX_REDIRECTS hops.
16
+ * - Cross-origin redirects refused (an open redirect on the WebID's
17
+ * host can't substitute an attacker-controlled CID document).
18
+ * - Body size cap enforced via Content-Length up front AND a
19
+ * streaming reader cap (cancel on overage), so untrusted hosts
20
+ * can't OOM us with a large payload.
21
+ * - 5-second timeout per request via AbortController.
22
+ *
23
+ * Throws on any failure; callers convert to whatever they want
24
+ * (LWS-CID surfaces the error string, the NIP-98 path treats
25
+ * throw-as-null and falls back to the existing did:nostr resolver).
26
+ */
27
+
28
+ import { validateExternalUrl } from '../utils/ssrf.js';
29
+
30
+ // Default body cap (256 KB) — CID documents are tiny in practice.
31
+ const DEFAULT_MAX_BYTES = 256 * 1024;
32
+ const MAX_REDIRECTS = 5;
33
+ const FETCH_TIMEOUT_MS = 5000;
34
+
35
+ // Auth is on the hot path; refetching the CID document on every
36
+ // request is unacceptable for both latency and reliability (and would
37
+ // amplify self-traffic when the profile is hosted on this same
38
+ // server). Bounded LRU — an attacker could otherwise grow the cache
39
+ // without limit by sending tokens / requests with many distinct
40
+ // document URLs. Mirrors the pattern in did-nostr.js.
41
+ const profileCache = new Map(); // url -> { profile, timestamp, failureTtl?, error? }
42
+ const PROFILE_CACHE_TTL = 5 * 60 * 1000; // 5 minutes for hits
43
+ const PROFILE_FAILURE_TTL = 60 * 1000; // 1 minute for misses
44
+ const PROFILE_CACHE_MAX = 1000;
45
+
46
+ /** @internal — exposed for tests */
47
+ export function _clearProfileCacheForTests() {
48
+ profileCache.clear();
49
+ }
50
+
51
+ /**
52
+ * Fetch and parse a JSON CID document with SSRF, redirect, and
53
+ * body-size protections — and a bounded TTL cache so auth-path callers
54
+ * don't refetch on every request.
55
+ *
56
+ * Content-Type expectation: the response must declare a JSON-bearing
57
+ * Content-Type (matching `/json/`, e.g. `application/ld+json`,
58
+ * `application/json`, `application/json+ld`). A missing or non-JSON
59
+ * Content-Type rejects with a clear error rather than silently
60
+ * attempting JSON.parse — this caught real misconfigurations during
61
+ * #398 review where Turtle / HTML error pages were being served at
62
+ * profile URLs. Deployments serving WebID profiles should make sure
63
+ * their host returns the correct Content-Type for the CID document.
64
+ *
65
+ * Cache contract: keyed by `docUrl` only. All callers MUST pass
66
+ * consistent `opts` (in practice today everyone passes
67
+ * maxBytes = 256 KB). If a future caller needs a stricter limit, the
68
+ * cache key needs to incorporate it (or that caller needs its own
69
+ * cache) — otherwise a permissive entry would be returned to a strict
70
+ * caller and the cap wouldn't be re-enforced.
71
+ *
72
+ * @param {string} docUrl - URL to fetch (untrusted — comes from JWT
73
+ * claims or is derived from a request).
74
+ * @param {object} [opts]
75
+ * @param {number} [opts.maxBytes=DEFAULT_MAX_BYTES]
76
+ * @returns {Promise<object>} parsed JSON
77
+ * @throws on any validation, network, redirect, size, or parse failure
78
+ */
79
+ export async function fetchCidDocument(docUrl, opts = {}) {
80
+ // Cache hit (or recent failure) — return immediately. On hit we
81
+ // delete-then-set so this entry moves to the tail of the Map's
82
+ // insertion order, giving LRU eviction without a separate structure.
83
+ const cached = profileCache.get(docUrl);
84
+ if (cached) {
85
+ const ttl = cached.failureTtl ? PROFILE_FAILURE_TTL : PROFILE_CACHE_TTL;
86
+ if (Date.now() - cached.timestamp < ttl) {
87
+ profileCache.delete(docUrl);
88
+ profileCache.set(docUrl, cached);
89
+ if (cached.failureTtl) throw new Error(cached.error);
90
+ return cached.profile;
91
+ }
92
+ profileCache.delete(docUrl);
93
+ }
94
+
95
+ try {
96
+ const profile = await fetchCidDocumentNoCache(docUrl, opts);
97
+ setCached(docUrl, { profile, timestamp: Date.now() });
98
+ return profile;
99
+ } catch (err) {
100
+ setCached(docUrl, {
101
+ timestamp: Date.now(),
102
+ failureTtl: true,
103
+ error: err.message,
104
+ });
105
+ throw err;
106
+ }
107
+ }
108
+
109
+ /** Insert into the bounded LRU; evict the oldest entry past the cap. */
110
+ function setCached(url, entry) {
111
+ profileCache.set(url, entry);
112
+ while (profileCache.size > PROFILE_CACHE_MAX) {
113
+ const oldest = profileCache.keys().next().value;
114
+ if (oldest === undefined) break;
115
+ profileCache.delete(oldest);
116
+ }
117
+ }
118
+
119
+ async function fetchCidDocumentNoCache(docUrl, opts = {}) {
120
+ const maxBytes = opts.maxBytes ?? DEFAULT_MAX_BYTES;
121
+ const originalOrigin = new URL(docUrl).origin;
122
+ let currentUrl = docUrl;
123
+
124
+ for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
125
+ const isLastAllowedHop = hop === MAX_REDIRECTS;
126
+ const validation = await validateExternalUrl(currentUrl, {
127
+ requireHttps: process.env.NODE_ENV === 'production',
128
+ blockPrivateIPs: true,
129
+ resolveDNS: true,
130
+ });
131
+ if (!validation.valid) {
132
+ throw new Error(`SSRF protection: ${validation.error}`);
133
+ }
134
+
135
+ const controller = new AbortController();
136
+ const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
137
+ let res;
138
+ try {
139
+ res = await fetch(currentUrl, {
140
+ headers: { Accept: 'application/ld+json, application/json;q=0.9' },
141
+ redirect: 'manual',
142
+ signal: controller.signal,
143
+ });
144
+ } finally {
145
+ clearTimeout(timer);
146
+ }
147
+
148
+ if (res.status >= 300 && res.status < 400) {
149
+ if (isLastAllowedHop) {
150
+ throw new Error(`too many redirects (>${MAX_REDIRECTS})`);
151
+ }
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
+
163
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
164
+
165
+ const ct = (res.headers.get('content-type') || '').toLowerCase();
166
+ if (!ct.includes('json')) {
167
+ throw new Error(`unexpected content-type: ${ct || '(none)'}`);
168
+ }
169
+
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})`);
173
+ }
174
+ const text = await readBodyWithCap(res, maxBytes);
175
+ return JSON.parse(text);
176
+ }
177
+ throw new Error('profile fetch loop exited unexpectedly');
178
+ }
179
+
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
+ }
@@ -35,7 +35,10 @@
35
35
  import * as jose from 'jose';
36
36
  import { secp256k1 } from '@noble/curves/secp256k1';
37
37
  import { sha256 } from '@noble/hashes/sha256';
38
- import { validateExternalUrl } from '../utils/ssrf.js';
38
+ import { fetchCidDocument, _clearProfileCacheForTests } from './cid-doc-fetch.js';
39
+
40
+ // Re-export for the existing test suite, which already calls this.
41
+ export { _clearProfileCacheForTests };
39
42
 
40
43
  // JWS algorithms we accept. ES256K (RFC8812) is the primary target —
41
44
  // secp256k1, the same curve as Nostr — but we support the common JWS
@@ -55,27 +58,11 @@ const MAX_LIFETIME = 3600; // 1 hour
55
58
  // Clock skew tolerance for exp/nbf checks (seconds).
56
59
  const CLOCK_SKEW = 60;
57
60
 
58
- // Profile fetch cache. Auth is on the hot path; refetching the CID
59
- // document on every request is unacceptable for both latency and
60
- // reliability. Mirrors the pattern in did-nostr.js, but bounded an
61
- // attacker can otherwise grow the cache without limit by sending tokens
62
- // with many distinct `sub` URLs.
63
- const profileCache = new Map(); // url -> { profile, timestamp, failureTtl?, error? }
64
- const PROFILE_CACHE_TTL = 5 * 60 * 1000; // 5 minutes for hits
65
- const PROFILE_FAILURE_TTL = 60 * 1000; // 1 minute for misses
66
- const PROFILE_CACHE_MAX = 1000; // simple LRU bound
67
-
68
- // Manual-redirect cap so a chain can't loop or grind.
69
- const MAX_REDIRECTS = 5;
70
-
71
- // Max profile body size — guards against DoS via giant JSON bodies on
72
- // untrusted URLs. CID documents are tiny in practice (~1-5 KB).
73
- const MAX_PROFILE_BYTES = 256 * 1024; // 256 KB
74
-
75
- /** @internal — exposed for tests */
76
- export function _clearProfileCacheForTests() {
77
- profileCache.clear();
78
- }
61
+ // Max profile body size passed to the shared fetcher. CID documents
62
+ // are tiny in practice (~1-5 KB); 256 KB leaves plenty of headroom
63
+ // while bounding any DoS attempt. The cache itself lives in
64
+ // cid-doc-fetch.js so both this module and the NIP-98 path benefit.
65
+ const MAX_PROFILE_BYTES = 256 * 1024;
79
66
 
80
67
  /**
81
68
  * Cheap detector — does this request carry an LWS-CID JWT?
@@ -427,165 +414,13 @@ function normalizeOrigin(s) {
427
414
  }
428
415
  }
429
416
 
430
- async function fetchProfile(docUrl) {
431
- // Cache hit (or recent failure) — return immediately. On hit we
432
- // delete-then-reset so this entry moves to the tail of the Map's
433
- // insertion order, giving us LRU eviction without an extra structure.
434
- const cached = profileCache.get(docUrl);
435
- if (cached) {
436
- const ttl = cached.failureTtl ? PROFILE_FAILURE_TTL : PROFILE_CACHE_TTL;
437
- if (Date.now() - cached.timestamp < ttl) {
438
- profileCache.delete(docUrl);
439
- profileCache.set(docUrl, cached);
440
- if (cached.failureTtl) throw new Error(cached.error);
441
- return cached.profile;
442
- }
443
- profileCache.delete(docUrl);
444
- }
445
-
446
- try {
447
- const profile = await fetchProfileNoCache(docUrl);
448
- setCached(docUrl, { profile, timestamp: Date.now() });
449
- return profile;
450
- } catch (err) {
451
- setCached(docUrl, {
452
- timestamp: Date.now(),
453
- failureTtl: true,
454
- error: err.message,
455
- });
456
- throw err;
457
- }
458
- }
459
-
460
- /** Insert into the bounded LRU; evict the oldest entry past the cap. */
461
- function setCached(url, entry) {
462
- profileCache.set(url, entry);
463
- while (profileCache.size > PROFILE_CACHE_MAX) {
464
- // Map iterates in insertion order; first key is the oldest.
465
- const oldest = profileCache.keys().next().value;
466
- if (oldest === undefined) break;
467
- profileCache.delete(oldest);
468
- }
469
- }
470
-
471
- /**
472
- * Fetch the CID document with SSRF protection.
473
- *
474
- * docUrl comes from JWT claims (sub, kid) BEFORE the signature is
475
- * verified, so it's untrusted. We:
476
- * 1. Validate it through the existing SSRF guard (blocks loopback,
477
- * private IPs, http (in production), DNS that resolves to private
478
- * addresses).
479
- * 2. Disable automatic redirects and re-validate every Location to
480
- * defeat redirect-based bypasses (mirrors the cors-proxy pattern).
481
- * Cross-origin redirects are refused — otherwise a target
482
- * attacker-controlled host could serve a substitute CID document
483
- * for the WebID's origin.
484
- * 3. Cap redirects so a chain can't loop.
485
- * 4. Cap response body size so a giant payload can't OOM us.
486
- * 5. Always send a fresh Accept and a small read-side timeout.
487
- */
488
- async function fetchProfileNoCache(docUrl) {
489
- const originalOrigin = new URL(docUrl).origin;
490
- let currentUrl = docUrl;
491
-
492
- // Hop 0 is the original request; up to MAX_REDIRECTS subsequent
493
- // redirects are followed, after which we throw.
494
- for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
495
- const isLastAllowedHop = hop === MAX_REDIRECTS;
496
- const validation = await validateExternalUrl(currentUrl, {
497
- // Allow http on dev only — production deploys should always be https.
498
- requireHttps: process.env.NODE_ENV === 'production',
499
- blockPrivateIPs: true,
500
- resolveDNS: true,
501
- });
502
- if (!validation.valid) {
503
- throw new Error(`SSRF protection: ${validation.error}`);
504
- }
505
-
506
- const controller = new AbortController();
507
- const timer = setTimeout(() => controller.abort(), 5000);
508
- let res;
509
- try {
510
- res = await fetch(currentUrl, {
511
- // Prefer JSON-LD but accept plain JSON too — some WebID hosts
512
- // serve `application/json` for `card.jsonld`. The body is JSON
513
- // either way; we don't perform JSON-LD-specific processing here.
514
- headers: { Accept: 'application/ld+json, application/json;q=0.9' },
515
- redirect: 'manual',
516
- signal: controller.signal,
517
- });
518
- } finally {
519
- clearTimeout(timer);
520
- }
521
-
522
- // Manual redirect handling — re-validate every Location and require
523
- // same-origin so a redirect can't substitute an attacker-controlled
524
- // CID document for the WebID's origin.
525
- if (res.status >= 300 && res.status < 400) {
526
- if (isLastAllowedHop) {
527
- throw new Error(`too many redirects (>${MAX_REDIRECTS})`);
528
- }
529
- const loc = res.headers.get('location');
530
- if (!loc) throw new Error(`redirect ${res.status} without Location`);
531
- const nextUrl = new URL(loc, currentUrl).toString();
532
- const nextOrigin = new URL(nextUrl).origin;
533
- if (nextOrigin !== originalOrigin) {
534
- throw new Error(
535
- `cross-origin redirect refused: ${originalOrigin} → ${nextOrigin}`,
536
- );
537
- }
538
- currentUrl = nextUrl;
539
- continue;
540
- }
541
-
542
- if (!res.ok) {
543
- throw new Error(`HTTP ${res.status}`);
544
- }
545
-
546
- // Size guard. Two layers: trust Content-Length when present, then
547
- // also enforce as we read so a streaming response can't lie.
548
- const declared = Number(res.headers.get('content-length'));
549
- if (Number.isFinite(declared) && declared > MAX_PROFILE_BYTES) {
550
- throw new Error(
551
- `CID document too large (Content-Length=${declared} > ${MAX_PROFILE_BYTES})`,
552
- );
553
- }
554
- const text = await readBodyWithCap(res, MAX_PROFILE_BYTES);
555
- return JSON.parse(text);
556
- }
557
- // Loop exited without returning or redirecting — defensive fallback.
558
- throw new Error('profile fetch loop exited unexpectedly');
559
- }
560
-
561
417
  /**
562
- * Read response body with a hard byte cap. Aborts the stream as soon as
563
- * the cap is exceeded so we don't buffer the entire untrusted payload.
418
+ * Fetch the CID document. Delegates to the shared `fetchCidDocument`
419
+ * helper which handles SSRF / redirects / body cap AND a bounded TTL
420
+ * cache (see src/auth/cid-doc-fetch.js).
564
421
  */
565
- async function readBodyWithCap(res, maxBytes) {
566
- const reader = res.body?.getReader?.();
567
- if (!reader) {
568
- // No streaming reader (older runtimes / mocked responses) — fall
569
- // back to .text() but enforce the cap after the fact.
570
- const text = await res.text();
571
- if (Buffer.byteLength(text, 'utf8') > maxBytes) {
572
- throw new Error(`CID document too large (>${maxBytes} bytes)`);
573
- }
574
- return text;
575
- }
576
- const chunks = [];
577
- let total = 0;
578
- for (;;) {
579
- const { value, done } = await reader.read();
580
- if (done) break;
581
- total += value.byteLength;
582
- if (total > maxBytes) {
583
- try { await reader.cancel(); } catch { /* noop */ }
584
- throw new Error(`CID document too large (>${maxBytes} bytes)`);
585
- }
586
- chunks.push(value);
587
- }
588
- return Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))).toString('utf8');
422
+ async function fetchProfile(docUrl) {
423
+ return fetchCidDocument(docUrl, { maxBytes: MAX_PROFILE_BYTES });
589
424
  }
590
425
 
591
426
  function findVerificationMethod(profile, kid, baseUrl) {
@@ -613,7 +448,9 @@ function isInProofPurpose(profile, predicate, kid, baseUrl) {
613
448
  return false;
614
449
  }
615
450
 
616
- function normalizeControllers(value, baseUrl) {
451
+ // Exported so the NIP-98 → WebID path (src/auth/nostr.js) can perform
452
+ // the same controller consistency check the LWS-CID verifier uses.
453
+ export function normalizeControllers(value, baseUrl) {
617
454
  if (value === undefined || value === null) return [];
618
455
  const list = Array.isArray(value) ? value : [value];
619
456
  const out = [];