javascript-solid-server 0.0.176 → 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/.claude/scheduled_tasks.lock +1 -0
- package/README.md +1 -0
- package/docs/lws.md +84 -0
- package/package.json +1 -1
- package/src/auth/cid-doc-fetch.js +202 -0
- package/src/auth/lws-cid.js +516 -0
- package/src/auth/nostr.js +397 -8
- package/src/auth/token.js +12 -1
- package/test/lws-cid.test.js +705 -0
- package/test/nostr-cid-vm.test.js +509 -0
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LWS10-CID JWT verifier tests
|
|
3
|
+
*
|
|
4
|
+
* Covers the verifier logic in isolation by stubbing global.fetch so we
|
|
5
|
+
* can hand-craft both the JWT and the profile document. Real end-to-end
|
|
6
|
+
* tests against a running server are filed as a follow-up.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, before, beforeEach, after } from 'node:test';
|
|
10
|
+
import assert from 'node:assert';
|
|
11
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
12
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
13
|
+
import * as jose from 'jose';
|
|
14
|
+
import { hasLwsCidAuth, verifyLwsCidAuth, _clearProfileCacheForTests } from '../src/auth/lws-cid.js';
|
|
15
|
+
|
|
16
|
+
// --- helpers ---------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
function b64u(bytes) {
|
|
19
|
+
return Buffer.from(bytes).toString('base64')
|
|
20
|
+
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function jwkFromSecp256k1(privKey) {
|
|
24
|
+
const pub = secp256k1.getPublicKey(privKey, /*compressed=*/false); // 65 bytes: 0x04 || x || y
|
|
25
|
+
return {
|
|
26
|
+
kty: 'EC',
|
|
27
|
+
crv: 'secp256k1',
|
|
28
|
+
x: b64u(pub.slice(1, 33)),
|
|
29
|
+
y: b64u(pub.slice(33, 65)),
|
|
30
|
+
alg: 'ES256K',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function makeJwt({ privKey, header, payload }) {
|
|
35
|
+
const h64 = b64u(Buffer.from(JSON.stringify(header)));
|
|
36
|
+
const p64 = b64u(Buffer.from(JSON.stringify(payload)));
|
|
37
|
+
const signingInput = Buffer.from(`${h64}.${p64}`, 'utf8');
|
|
38
|
+
const msgHash = sha256(signingInput);
|
|
39
|
+
const sig = secp256k1.sign(msgHash, privKey);
|
|
40
|
+
// Compact 64-byte r||s — what JWS expects.
|
|
41
|
+
return `${h64}.${p64}.${b64u(sig.toCompactRawBytes())}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function makeRequest(token, { host = 'example.com', proto = 'https' } = {}) {
|
|
45
|
+
return {
|
|
46
|
+
headers: {
|
|
47
|
+
authorization: `Bearer ${token}`,
|
|
48
|
+
host,
|
|
49
|
+
},
|
|
50
|
+
protocol: proto,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const WEBID = 'https://example.com/profile/card.jsonld#me';
|
|
55
|
+
const DOC_URL = 'https://example.com/profile/card.jsonld';
|
|
56
|
+
const VM_ID = `${DOC_URL}#nostr-key-1`;
|
|
57
|
+
const POD_ORIGIN = 'https://example.com';
|
|
58
|
+
|
|
59
|
+
// Minimal CID-shaped profile.
|
|
60
|
+
function buildProfile(jwk, { withAuthRef = true, controller = WEBID } = {}) {
|
|
61
|
+
return {
|
|
62
|
+
'@context': {
|
|
63
|
+
cid: 'https://www.w3.org/ns/cid/v1#',
|
|
64
|
+
controller: { '@id': 'cid:controller', '@type': '@id' },
|
|
65
|
+
verificationMethod: { '@id': 'cid:verificationMethod', '@container': '@set' },
|
|
66
|
+
authentication: { '@id': 'cid:authentication', '@type': '@id', '@container': '@set' },
|
|
67
|
+
publicKeyJwk: { '@id': 'cid:publicKeyJwk', '@type': '@json' },
|
|
68
|
+
},
|
|
69
|
+
'@id': WEBID,
|
|
70
|
+
controller,
|
|
71
|
+
verificationMethod: [
|
|
72
|
+
{
|
|
73
|
+
id: VM_ID,
|
|
74
|
+
type: 'JsonWebKey',
|
|
75
|
+
controller: WEBID,
|
|
76
|
+
publicKeyJwk: jwk,
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
...(withAuthRef ? { authentication: [VM_ID] } : {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// --- fetch stub ------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
const realFetch = global.fetch;
|
|
86
|
+
let nextProfile = null;
|
|
87
|
+
let nextStatus = 200;
|
|
88
|
+
// Per-URL response overrides — set { status, headers, body } per URL to
|
|
89
|
+
// inject redirects, oversized bodies, or non-default content-types.
|
|
90
|
+
let urlResponses = new Map();
|
|
91
|
+
|
|
92
|
+
function installFetchStub() {
|
|
93
|
+
global.fetch = async (url) => {
|
|
94
|
+
const u = String(url);
|
|
95
|
+
if (urlResponses.has(u)) {
|
|
96
|
+
const { status = 200, headers = {}, body = '' } = urlResponses.get(u);
|
|
97
|
+
return new Response(body, { status, headers });
|
|
98
|
+
}
|
|
99
|
+
if (u === DOC_URL) {
|
|
100
|
+
return new Response(JSON.stringify(nextProfile), {
|
|
101
|
+
status: nextStatus,
|
|
102
|
+
headers: { 'content-type': 'application/ld+json' },
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return new Response('not found', { status: 404 });
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function restoreFetch() { global.fetch = realFetch; }
|
|
109
|
+
|
|
110
|
+
// --- tests -----------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
describe('hasLwsCidAuth', () => {
|
|
113
|
+
it('detects Bearer JWT with URL kid', () => {
|
|
114
|
+
const token = makeJwt({
|
|
115
|
+
privKey: secp256k1.utils.randomPrivateKey(),
|
|
116
|
+
header: { alg: 'ES256K', kid: VM_ID, typ: 'JWT' },
|
|
117
|
+
payload: { sub: WEBID, iss: WEBID, client_id: WEBID, iat: Math.floor(Date.now()/1000) },
|
|
118
|
+
});
|
|
119
|
+
assert.strictEqual(hasLwsCidAuth(makeRequest(token)), true);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('rejects Bearer JWT with opaque fingerprint kid (looks like IDP JWT)', () => {
|
|
123
|
+
const token = makeJwt({
|
|
124
|
+
privKey: secp256k1.utils.randomPrivateKey(),
|
|
125
|
+
header: { alg: 'ES256K', kid: 'c1f52577', typ: 'JWT' },
|
|
126
|
+
payload: { sub: WEBID, iss: WEBID, client_id: WEBID, iat: Math.floor(Date.now()/1000) },
|
|
127
|
+
});
|
|
128
|
+
assert.strictEqual(hasLwsCidAuth(makeRequest(token)), false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('rejects DPoP', () => {
|
|
132
|
+
assert.strictEqual(hasLwsCidAuth({ headers: { authorization: 'DPoP eyJ...' } }), false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('rejects Nostr', () => {
|
|
136
|
+
assert.strictEqual(hasLwsCidAuth({ headers: { authorization: 'Nostr abc' } }), false);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('rejects no authorization header', () => {
|
|
140
|
+
assert.strictEqual(hasLwsCidAuth({ headers: {} }), false);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('rejects malformed JWT', () => {
|
|
144
|
+
assert.strictEqual(hasLwsCidAuth(makeRequest('not.a.jwt')), false);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('rejects JWT with kid using non-http(s) scheme', () => {
|
|
148
|
+
const token = makeJwt({
|
|
149
|
+
privKey: secp256k1.utils.randomPrivateKey(),
|
|
150
|
+
header: { alg: 'ES256K', kid: 'urn:foo:bar#k1' },
|
|
151
|
+
payload: { sub: WEBID },
|
|
152
|
+
});
|
|
153
|
+
assert.strictEqual(hasLwsCidAuth(makeRequest(token)), false);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('rejects JWT with unaccepted alg (e.g. HS256)', () => {
|
|
157
|
+
const token = makeJwt({
|
|
158
|
+
privKey: secp256k1.utils.randomPrivateKey(),
|
|
159
|
+
header: { alg: 'HS256', kid: VM_ID },
|
|
160
|
+
payload: { sub: WEBID },
|
|
161
|
+
});
|
|
162
|
+
assert.strictEqual(hasLwsCidAuth(makeRequest(token)), false);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('verifyLwsCidAuth', () => {
|
|
167
|
+
let priv;
|
|
168
|
+
let jwk;
|
|
169
|
+
// Default valid claims — tests can override per-case.
|
|
170
|
+
function claims(now = Math.floor(Date.now() / 1000), extra = {}) {
|
|
171
|
+
return { sub: WEBID, iss: WEBID, client_id: WEBID, aud: [POD_ORIGIN], iat: now, exp: now + 60, ...extra };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
before(() => {
|
|
175
|
+
installFetchStub();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
after(() => {
|
|
179
|
+
restoreFetch();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
beforeEach(() => {
|
|
183
|
+
priv = secp256k1.utils.randomPrivateKey();
|
|
184
|
+
jwk = jwkFromSecp256k1(priv);
|
|
185
|
+
nextStatus = 200;
|
|
186
|
+
nextProfile = buildProfile(jwk);
|
|
187
|
+
urlResponses = new Map();
|
|
188
|
+
// Cache must not survive between tests; otherwise nextProfile
|
|
189
|
+
// changes are masked by a stale hit on DOC_URL.
|
|
190
|
+
_clearProfileCacheForTests();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('verifies a valid ES256K JWT against a CID-shaped profile', async () => {
|
|
194
|
+
const token = makeJwt({
|
|
195
|
+
privKey: priv,
|
|
196
|
+
header: { alg: 'ES256K', kid: VM_ID, typ: 'JWT' },
|
|
197
|
+
payload: claims(),
|
|
198
|
+
});
|
|
199
|
+
const result = await verifyLwsCidAuth(makeRequest(token));
|
|
200
|
+
assert.strictEqual(result.error, null);
|
|
201
|
+
assert.strictEqual(result.webId, WEBID);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('rejects "none" alg', async () => {
|
|
205
|
+
const token = makeJwt({
|
|
206
|
+
privKey: priv,
|
|
207
|
+
header: { alg: 'none', kid: VM_ID, typ: 'JWT' },
|
|
208
|
+
payload: claims(),
|
|
209
|
+
});
|
|
210
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
211
|
+
assert.strictEqual(r.webId, null);
|
|
212
|
+
assert.match(r.error, /none/);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('rejects missing alg with a distinct error (not the "none" message)', async () => {
|
|
216
|
+
// Built without an alg header. The detector would normally screen
|
|
217
|
+
// these out, but the verifier should still produce a clear error
|
|
218
|
+
// if called directly (defense-in-depth).
|
|
219
|
+
const h64 = b64u(Buffer.from(JSON.stringify({ kid: VM_ID, typ: 'JWT' })));
|
|
220
|
+
const p64 = b64u(Buffer.from(JSON.stringify(claims())));
|
|
221
|
+
const sig = b64u(Buffer.from('not-a-real-signature'));
|
|
222
|
+
const token = `${h64}.${p64}.${sig}`;
|
|
223
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
224
|
+
assert.match(r.error, /missing alg/);
|
|
225
|
+
assert.doesNotMatch(r.error, /"none"/);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('rejects when sub != iss', async () => {
|
|
229
|
+
const token = makeJwt({
|
|
230
|
+
privKey: priv,
|
|
231
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
232
|
+
payload: claims(undefined, { iss: 'https://other.example/profile#me' }),
|
|
233
|
+
});
|
|
234
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
235
|
+
assert.match(r.error, /sub.*iss.*client_id/);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('rejects when kid is in a different document than sub', async () => {
|
|
239
|
+
const otherKid = 'https://other.example/profile/card.jsonld#k1';
|
|
240
|
+
const token = makeJwt({
|
|
241
|
+
privKey: priv,
|
|
242
|
+
header: { alg: 'ES256K', kid: otherKid },
|
|
243
|
+
payload: claims(),
|
|
244
|
+
});
|
|
245
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
246
|
+
assert.match(r.error, /not in the subject/);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('rejects expired JWT', async () => {
|
|
250
|
+
const past = Math.floor(Date.now() / 1000) - 3600;
|
|
251
|
+
const token = makeJwt({
|
|
252
|
+
privKey: priv,
|
|
253
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
254
|
+
payload: claims(past, { iat: past - 60, exp: past }),
|
|
255
|
+
});
|
|
256
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
257
|
+
assert.match(r.error, /expired/);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('rejects nbf in the future', async () => {
|
|
261
|
+
const now = Math.floor(Date.now() / 1000);
|
|
262
|
+
const token = makeJwt({
|
|
263
|
+
privKey: priv,
|
|
264
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
265
|
+
payload: claims(now, { nbf: now + 600 }),
|
|
266
|
+
});
|
|
267
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
268
|
+
assert.match(r.error, /not yet valid/);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('rejects iat too far in the future', async () => {
|
|
272
|
+
const now = Math.floor(Date.now() / 1000);
|
|
273
|
+
const token = makeJwt({
|
|
274
|
+
privKey: priv,
|
|
275
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
276
|
+
payload: claims(now, { iat: now + 600, exp: now + 1200 }),
|
|
277
|
+
});
|
|
278
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
279
|
+
assert.match(r.error, /iat is in the future/);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('rejects iat too old', async () => {
|
|
283
|
+
const now = Math.floor(Date.now() / 1000);
|
|
284
|
+
const token = makeJwt({
|
|
285
|
+
privKey: priv,
|
|
286
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
287
|
+
payload: claims(now, { iat: now - 7200, exp: now + 60 }),
|
|
288
|
+
});
|
|
289
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
290
|
+
assert.match(r.error, /iat too old/);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('rejects non-numeric exp', async () => {
|
|
294
|
+
const now = Math.floor(Date.now() / 1000);
|
|
295
|
+
const token = makeJwt({
|
|
296
|
+
privKey: priv,
|
|
297
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
298
|
+
payload: claims(now, { exp: '9999999999' }),
|
|
299
|
+
});
|
|
300
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
301
|
+
assert.match(r.error, /exp claim/);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('rejects non-numeric iat', async () => {
|
|
305
|
+
const now = Math.floor(Date.now() / 1000);
|
|
306
|
+
const token = makeJwt({
|
|
307
|
+
privKey: priv,
|
|
308
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
309
|
+
payload: claims(now, { iat: 'right-now' }),
|
|
310
|
+
});
|
|
311
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
312
|
+
assert.match(r.error, /iat claim/);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('rejects missing exp', async () => {
|
|
316
|
+
const now = Math.floor(Date.now() / 1000);
|
|
317
|
+
const token = makeJwt({
|
|
318
|
+
privKey: priv,
|
|
319
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
320
|
+
payload: claims(now, { exp: undefined }),
|
|
321
|
+
});
|
|
322
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
323
|
+
assert.match(r.error, /exp claim is required/);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('rejects missing iat', async () => {
|
|
327
|
+
const now = Math.floor(Date.now() / 1000);
|
|
328
|
+
const token = makeJwt({
|
|
329
|
+
privKey: priv,
|
|
330
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
331
|
+
payload: claims(now, { iat: undefined }),
|
|
332
|
+
});
|
|
333
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
334
|
+
assert.match(r.error, /iat claim is required/);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('rejects lifetime > 1 hour (replay window cap)', async () => {
|
|
338
|
+
const now = Math.floor(Date.now() / 1000);
|
|
339
|
+
const token = makeJwt({
|
|
340
|
+
privKey: priv,
|
|
341
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
342
|
+
payload: claims(now, { iat: now, exp: now + 7200 }),
|
|
343
|
+
});
|
|
344
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
345
|
+
assert.match(r.error, /lifetime exceeds maximum/);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('rejects exp <= iat', async () => {
|
|
349
|
+
const now = Math.floor(Date.now() / 1000);
|
|
350
|
+
const token = makeJwt({
|
|
351
|
+
privKey: priv,
|
|
352
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
353
|
+
payload: claims(now, { iat: now, exp: now }),
|
|
354
|
+
});
|
|
355
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
356
|
+
assert.match(r.error, /exp must be after iat/);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('rejects missing aud', async () => {
|
|
360
|
+
const now = Math.floor(Date.now() / 1000);
|
|
361
|
+
const token = makeJwt({
|
|
362
|
+
privKey: priv,
|
|
363
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
364
|
+
payload: claims(now, { aud: undefined }),
|
|
365
|
+
});
|
|
366
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
367
|
+
assert.match(r.error, /aud claim is required/);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it('rejects when kid does not match any VM in the profile', async () => {
|
|
371
|
+
const ghostKid = `${DOC_URL}#nope`;
|
|
372
|
+
const token = makeJwt({
|
|
373
|
+
privKey: priv,
|
|
374
|
+
header: { alg: 'ES256K', kid: ghostKid },
|
|
375
|
+
payload: claims(),
|
|
376
|
+
});
|
|
377
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
378
|
+
assert.match(r.error, /no verificationMethod/);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('rejects when VM is not in authentication list', async () => {
|
|
382
|
+
nextProfile = buildProfile(jwk, { withAuthRef: false });
|
|
383
|
+
const token = makeJwt({
|
|
384
|
+
privKey: priv,
|
|
385
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
386
|
+
payload: claims(),
|
|
387
|
+
});
|
|
388
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
389
|
+
assert.match(r.error, /not listed in authentication/);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it('rejects when audience does not include this server', async () => {
|
|
393
|
+
const token = makeJwt({
|
|
394
|
+
privKey: priv,
|
|
395
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
396
|
+
payload: claims(undefined, { aud: ['https://elsewhere.example/'] }),
|
|
397
|
+
});
|
|
398
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
399
|
+
assert.match(r.error, /aud.*does not include/);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('rejects when server origin cannot be determined', async () => {
|
|
403
|
+
const token = makeJwt({
|
|
404
|
+
privKey: priv,
|
|
405
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
406
|
+
payload: claims(),
|
|
407
|
+
});
|
|
408
|
+
// No host header, no x-forwarded-host, no fastify hostname.
|
|
409
|
+
const req = { headers: { authorization: `Bearer ${token}` } };
|
|
410
|
+
const r = await verifyLwsCidAuth(req);
|
|
411
|
+
assert.match(r.error, /cannot determine server origin/);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it('honors x-forwarded-proto/host for aud check (behind reverse proxy)', async () => {
|
|
415
|
+
const token = makeJwt({
|
|
416
|
+
privKey: priv,
|
|
417
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
418
|
+
payload: claims(undefined, { aud: ['https://public.example'] }),
|
|
419
|
+
});
|
|
420
|
+
const req = {
|
|
421
|
+
headers: {
|
|
422
|
+
authorization: `Bearer ${token}`,
|
|
423
|
+
host: 'internal:8080',
|
|
424
|
+
'x-forwarded-proto': 'https',
|
|
425
|
+
'x-forwarded-host': 'public.example',
|
|
426
|
+
},
|
|
427
|
+
protocol: 'http',
|
|
428
|
+
};
|
|
429
|
+
const r = await verifyLwsCidAuth(req);
|
|
430
|
+
assert.strictEqual(r.error, null);
|
|
431
|
+
assert.strictEqual(r.webId, WEBID);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it('rejects tampered signature', async () => {
|
|
435
|
+
const valid = makeJwt({
|
|
436
|
+
privKey: priv,
|
|
437
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
438
|
+
payload: claims(),
|
|
439
|
+
});
|
|
440
|
+
const parts = valid.split('.');
|
|
441
|
+
const sigBuf = Buffer.from(parts[2].replace(/-/g, '+').replace(/_/g, '/'), 'base64');
|
|
442
|
+
sigBuf[0] ^= 0xff;
|
|
443
|
+
parts[2] = b64u(sigBuf);
|
|
444
|
+
const tampered = parts.join('.');
|
|
445
|
+
const r = await verifyLwsCidAuth(makeRequest(tampered));
|
|
446
|
+
assert.match(r.error, /signature/);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('rejects when VM is signed with different key than JWT', async () => {
|
|
450
|
+
const otherPriv = secp256k1.utils.randomPrivateKey();
|
|
451
|
+
nextProfile = buildProfile(jwkFromSecp256k1(otherPriv));
|
|
452
|
+
const token = makeJwt({
|
|
453
|
+
privKey: priv,
|
|
454
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
455
|
+
payload: claims(),
|
|
456
|
+
});
|
|
457
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
458
|
+
assert.match(r.error, /signature/);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
it('rejects when profile fetch fails', async () => {
|
|
462
|
+
nextStatus = 404;
|
|
463
|
+
nextProfile = null;
|
|
464
|
+
const token = makeJwt({
|
|
465
|
+
privKey: priv,
|
|
466
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
467
|
+
payload: claims(),
|
|
468
|
+
});
|
|
469
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
470
|
+
assert.match(r.error, /could not fetch/);
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it('rejects when CID document declares no subject (no @id / id)', async () => {
|
|
474
|
+
// Profile that's structurally complete enough to find a VM, but
|
|
475
|
+
// declares no top-level subject. This is rejected by the
|
|
476
|
+
// subject-identity check (which fires before the controller check
|
|
477
|
+
// — both layers exist as defense-in-depth).
|
|
478
|
+
nextProfile = {
|
|
479
|
+
'@context': { cid: 'https://www.w3.org/ns/cid/v1#' },
|
|
480
|
+
verificationMethod: [{
|
|
481
|
+
id: VM_ID,
|
|
482
|
+
type: 'JsonWebKey',
|
|
483
|
+
controller: WEBID,
|
|
484
|
+
publicKeyJwk: jwk,
|
|
485
|
+
}],
|
|
486
|
+
authentication: [VM_ID],
|
|
487
|
+
};
|
|
488
|
+
const token = makeJwt({
|
|
489
|
+
privKey: priv,
|
|
490
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
491
|
+
payload: claims(),
|
|
492
|
+
});
|
|
493
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
494
|
+
assert.match(r.error, /declares no subject/);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it("rejects when CID document's subject differs from JWT sub", async () => {
|
|
498
|
+
// Profile DOES declare a subject, but it's a different fragment
|
|
499
|
+
// than the JWT claims. Without this check, an attacker could
|
|
500
|
+
// serve a profile whose @id is "#bob" while the JWT claims "#alice"
|
|
501
|
+
// and reuse a VM controlled by bob.
|
|
502
|
+
nextProfile = {
|
|
503
|
+
...buildProfile(jwk),
|
|
504
|
+
'@id': 'https://example.com/profile/card.jsonld#bob',
|
|
505
|
+
};
|
|
506
|
+
const token = makeJwt({
|
|
507
|
+
privKey: priv,
|
|
508
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
509
|
+
payload: claims(), // sub = "...#me"
|
|
510
|
+
});
|
|
511
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
512
|
+
assert.match(r.error, /subject.*does not match JWT sub/);
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it('normalizes origin (default port and case) on both sides of aud check', async () => {
|
|
516
|
+
const token = makeJwt({
|
|
517
|
+
privKey: priv,
|
|
518
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
519
|
+
// aud uses the explicit default port and uppercase scheme.
|
|
520
|
+
payload: claims(undefined, { aud: ['HTTPS://Example.COM:443'] }),
|
|
521
|
+
});
|
|
522
|
+
const req = {
|
|
523
|
+
headers: {
|
|
524
|
+
authorization: `Bearer ${token}`,
|
|
525
|
+
host: 'example.com', // no port, lowercase
|
|
526
|
+
},
|
|
527
|
+
protocol: 'https',
|
|
528
|
+
};
|
|
529
|
+
const r = await verifyLwsCidAuth(req);
|
|
530
|
+
assert.strictEqual(r.error, null);
|
|
531
|
+
assert.strictEqual(r.webId, WEBID);
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it('rejects http: kid early with a clear message (not a generic SSRF failure)', async () => {
|
|
535
|
+
const httpKid = 'http://example.com/profile/card.jsonld#k1';
|
|
536
|
+
const httpSub = 'http://example.com/profile/card.jsonld#me';
|
|
537
|
+
const token = makeJwt({
|
|
538
|
+
privKey: priv,
|
|
539
|
+
header: { alg: 'ES256K', kid: httpKid },
|
|
540
|
+
payload: claims(undefined, { sub: httpSub, iss: httpSub, client_id: httpSub }),
|
|
541
|
+
});
|
|
542
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
543
|
+
assert.match(r.error, /kid must use https/);
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it('canonicalizes sub (case/default-port) before subject-identity check', async () => {
|
|
547
|
+
// JWT carries non-canonical sub/iss/client_id (uppercase scheme,
|
|
548
|
+
// explicit default port). Profile @id is canonical. After
|
|
549
|
+
// canonicalization, both should match and the returned webId is
|
|
550
|
+
// canonical (so downstream WAC ACL string matching works).
|
|
551
|
+
const noncanonical = 'HTTPS://Example.COM:443/profile/card.jsonld#me';
|
|
552
|
+
const token = makeJwt({
|
|
553
|
+
privKey: priv,
|
|
554
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
555
|
+
payload: claims(undefined, {
|
|
556
|
+
sub: noncanonical,
|
|
557
|
+
iss: noncanonical,
|
|
558
|
+
client_id: noncanonical,
|
|
559
|
+
}),
|
|
560
|
+
});
|
|
561
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
562
|
+
assert.strictEqual(r.error, null);
|
|
563
|
+
assert.strictEqual(r.webId, WEBID); // canonical form returned
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
it('canonicalizes kid (case/default-port) before matching VM ids', async () => {
|
|
567
|
+
// JWT carries a non-canonical kid (uppercase scheme + host,
|
|
568
|
+
// explicit default port); the profile's VM id is canonical.
|
|
569
|
+
// After URL-parse normalization both should match.
|
|
570
|
+
const nonCanonicalKid = 'HTTPS://Example.COM:443/profile/card.jsonld#nostr-key-1';
|
|
571
|
+
const token = makeJwt({
|
|
572
|
+
privKey: priv,
|
|
573
|
+
header: { alg: 'ES256K', kid: nonCanonicalKid },
|
|
574
|
+
payload: claims(),
|
|
575
|
+
});
|
|
576
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
577
|
+
assert.strictEqual(r.error, null);
|
|
578
|
+
assert.strictEqual(r.webId, WEBID);
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
it('handles comma-separated x-forwarded-host (multi-proxy chain)', async () => {
|
|
582
|
+
const token = makeJwt({
|
|
583
|
+
privKey: priv,
|
|
584
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
585
|
+
payload: claims(undefined, { aud: ['https://public.example'] }),
|
|
586
|
+
});
|
|
587
|
+
const req = {
|
|
588
|
+
headers: {
|
|
589
|
+
authorization: `Bearer ${token}`,
|
|
590
|
+
'x-forwarded-proto': 'https, http',
|
|
591
|
+
'x-forwarded-host': 'public.example, internal.lan',
|
|
592
|
+
},
|
|
593
|
+
};
|
|
594
|
+
const r = await verifyLwsCidAuth(req);
|
|
595
|
+
assert.strictEqual(r.error, null);
|
|
596
|
+
assert.strictEqual(r.webId, WEBID);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it('refuses cross-origin redirect during profile fetch', async () => {
|
|
600
|
+
urlResponses.set(DOC_URL, {
|
|
601
|
+
status: 302,
|
|
602
|
+
headers: { location: 'https://attacker.example/profile/card.jsonld' },
|
|
603
|
+
});
|
|
604
|
+
const token = makeJwt({
|
|
605
|
+
privKey: priv,
|
|
606
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
607
|
+
payload: claims(),
|
|
608
|
+
});
|
|
609
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
610
|
+
assert.match(r.error, /cross-origin redirect refused/);
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('rejects profile larger than the byte cap', async () => {
|
|
614
|
+
// 300 KB of JSON — over the 256 KB cap.
|
|
615
|
+
const huge = 'x'.repeat(300 * 1024);
|
|
616
|
+
urlResponses.set(DOC_URL, {
|
|
617
|
+
status: 200,
|
|
618
|
+
headers: { 'content-type': 'application/ld+json' },
|
|
619
|
+
body: JSON.stringify({ junk: huge }),
|
|
620
|
+
});
|
|
621
|
+
const token = makeJwt({
|
|
622
|
+
privKey: priv,
|
|
623
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
624
|
+
payload: claims(),
|
|
625
|
+
});
|
|
626
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
627
|
+
assert.match(r.error, /CID document too large/);
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it('rejects when Content-Length header announces oversize body', async () => {
|
|
631
|
+
urlResponses.set(DOC_URL, {
|
|
632
|
+
status: 200,
|
|
633
|
+
headers: {
|
|
634
|
+
'content-type': 'application/ld+json',
|
|
635
|
+
'content-length': String(10 * 1024 * 1024), // 10 MB declared
|
|
636
|
+
},
|
|
637
|
+
body: JSON.stringify({}), // body is small but header lies
|
|
638
|
+
});
|
|
639
|
+
const token = makeJwt({
|
|
640
|
+
privKey: priv,
|
|
641
|
+
header: { alg: 'ES256K', kid: VM_ID },
|
|
642
|
+
payload: claims(),
|
|
643
|
+
});
|
|
644
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
645
|
+
assert.match(r.error, /too large/);
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
it('SSRF: rejects kid pointing at localhost', async () => {
|
|
649
|
+
const localKid = 'https://localhost/profile/card.jsonld#me';
|
|
650
|
+
const token = makeJwt({
|
|
651
|
+
privKey: priv,
|
|
652
|
+
header: { alg: 'ES256K', kid: localKid },
|
|
653
|
+
payload: claims(undefined, {
|
|
654
|
+
sub: localKid, iss: localKid, client_id: localKid,
|
|
655
|
+
}),
|
|
656
|
+
});
|
|
657
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
658
|
+
// SSRF guard fires inside fetchProfile and surfaces as "could not
|
|
659
|
+
// fetch CID document: SSRF protection: ...".
|
|
660
|
+
assert.match(r.error, /SSRF protection/);
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
// --- non-ES256K alg coverage (the jose-driven branch) -------------
|
|
664
|
+
|
|
665
|
+
describe('non-ES256K algorithms via jose', () => {
|
|
666
|
+
async function runHappyPath(alg) {
|
|
667
|
+
const kp = await jose.generateKeyPair(alg, { extractable: true });
|
|
668
|
+
const publicJwk = await jose.exportJWK(kp.publicKey);
|
|
669
|
+
publicJwk.alg = alg;
|
|
670
|
+
nextProfile = buildProfile(publicJwk);
|
|
671
|
+
|
|
672
|
+
const now = Math.floor(Date.now() / 1000);
|
|
673
|
+
const token = await new jose.SignJWT(claims(now))
|
|
674
|
+
.setProtectedHeader({ alg, kid: VM_ID, typ: 'JWT' })
|
|
675
|
+
.sign(kp.privateKey);
|
|
676
|
+
const r = await verifyLwsCidAuth(makeRequest(token));
|
|
677
|
+
assert.strictEqual(r.error, null, `unexpected error: ${r.error}`);
|
|
678
|
+
assert.strictEqual(r.webId, WEBID);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
it('verifies ES256 (P-256)', async () => { await runHappyPath('ES256'); });
|
|
682
|
+
it('verifies EdDSA (Ed25519)', async () => { await runHappyPath('EdDSA'); });
|
|
683
|
+
it('verifies RS256 (RSA-2048)', async () => { await runHappyPath('RS256'); });
|
|
684
|
+
|
|
685
|
+
it('rejects RS256 with tampered payload', async () => {
|
|
686
|
+
const kp = await jose.generateKeyPair('RS256', { extractable: true, modulusLength: 2048 });
|
|
687
|
+
const publicJwk = await jose.exportJWK(kp.publicKey);
|
|
688
|
+
publicJwk.alg = 'RS256';
|
|
689
|
+
nextProfile = buildProfile(publicJwk);
|
|
690
|
+
|
|
691
|
+
const now = Math.floor(Date.now() / 1000);
|
|
692
|
+
const token = await new jose.SignJWT(claims(now))
|
|
693
|
+
.setProtectedHeader({ alg: 'RS256', kid: VM_ID })
|
|
694
|
+
.sign(kp.privateKey);
|
|
695
|
+
// Tamper with payload portion (middle section).
|
|
696
|
+
const parts = token.split('.');
|
|
697
|
+
const decoded = JSON.parse(Buffer.from(parts[1].replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString());
|
|
698
|
+
decoded.sub = 'https://attacker.example/#me';
|
|
699
|
+
parts[1] = b64u(Buffer.from(JSON.stringify(decoded)));
|
|
700
|
+
const tampered = parts.join('.');
|
|
701
|
+
const r = await verifyLwsCidAuth(makeRequest(tampered));
|
|
702
|
+
assert.notStrictEqual(r.error, null);
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
});
|