Repository navigation
Expand file tree
/
Copy pathtest-jwt.mjs
More file actions
434 lines (396 loc) · 15.9 KB
/
Copy pathtest-jwt.mjs
File metadata and controls
434 lines (396 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* JWT verification tests for insumer-verify.
*
* Tests the JWT input path: auto-detection, signature verification,
* condition hash integrity, freshness, and expiry checks via JWT claims.
*
* Run: node test-jwt.mjs
*/
import { verifyAttestation } from "./build/index.js";
const subtle = globalThis.crypto.subtle;
let passed = 0;
let failed = 0;
function assert(condition, name) {
if (condition) {
console.log(` PASS: ${name}`);
passed++;
} else {
console.log(` FAIL: ${name}`);
failed++;
}
}
// ── Helpers ──────────────────────────────────────────────────────
function base64UrlEncode(str) {
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
function bytesToBase64Url(bytes) {
return btoa(String.fromCharCode(...bytes))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
async function createTestJwt(overrides = {}) {
const keyPair = await subtle.generateKey(
{ name: "ECDSA", namedCurve: "P-256" },
true,
["sign", "verify"]
);
const now = Math.floor(Date.now() / 1000);
// Build evaluatedCondition (matches the API's format)
const evaluatedCondition = {
type: "token_balance",
chainId: 1,
contractAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
operator: "gte",
threshold: "1000000",
decimals: 6,
};
// Compute conditionHash
const sortedKeys = Object.keys(evaluatedCondition).sort();
const canonical = JSON.stringify(evaluatedCondition, sortedKeys);
const hashBuf = await subtle.digest("SHA-256", new TextEncoder().encode(canonical));
const conditionHash =
"0x" +
Array.from(new Uint8Array(hashBuf))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
const results = [
{
condition: 0,
label: "USDC >= 1",
type: "token_balance",
chainId: 1,
met: true,
evaluatedCondition,
conditionHash,
blockNumber: "0x12a05f200",
blockTimestamp: new Date().toISOString(),
...(overrides.resultOverrides || {}),
},
];
const header = {
alg: "ES256",
typ: "JWT",
kid: "insumer-attest-v1",
...(overrides.headerOverrides || {}),
};
const payload = {
iss: "https://api.insumermodel.com",
sub: "0x1234567890abcdef1234567890abcdef12345678",
jti: "ATST-TEST1",
iat: now,
exp: now + 1800,
pass: true,
conditionHash: [conditionHash],
blockNumber: "0x12a05f200",
blockTimestamp: new Date().toISOString(),
results,
...(overrides.payloadOverrides || {}),
};
const headerB64 = base64UrlEncode(JSON.stringify(header));
const payloadB64 = base64UrlEncode(JSON.stringify(payload));
const signingInput = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
const sigBuffer = await subtle.sign(
{ name: "ECDSA", hash: "SHA-256" },
keyPair.privateKey,
signingInput
);
// Web Crypto sign returns P1363 format (raw r||s, 64 bytes)
const sigB64 = bytesToBase64Url(new Uint8Array(sigBuffer));
const jwt = `${headerB64}.${payloadB64}.${sigB64}`;
return { jwt, keyPair, header, payload };
}
// Create a mock JWKS fetch that returns the test key
function mockFetchForKey(publicKey) {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
if (typeof url === "string" && url.includes("jwks")) {
const jwk = await subtle.exportKey("jwk", publicKey);
return {
ok: true,
json: async () => ({
keys: [
{
kty: jwk.kty,
crv: jwk.crv,
x: jwk.x,
y: jwk.y,
kid: "insumer-attest-v1",
},
],
}),
};
}
return originalFetch(url);
};
return () => {
globalThis.fetch = originalFetch;
};
}
// ── Tests ────────────────────────────────────────────────────────
// Test 1: Auto-detection — string input goes to JWT path
console.log("\nTest 1: Auto-detection — string input goes to JWT path");
{
const { jwt, keyPair } = await createTestJwt();
const restore = mockFetchForKey(keyPair.publicKey);
try {
const result = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
});
assert(result.checks.signature.passed, "JWT signature should pass with matching key");
assert(result.checks.conditionHashes.passed, "Condition hashes should pass");
assert(result.checks.expiry.passed, "Expiry should pass");
assert(result.valid, "Overall should be valid");
} finally {
restore();
}
}
// Test 2: Auto-detection — object input goes to existing path
console.log("\nTest 2: Auto-detection — object input goes to existing path");
{
const result = await verifyAttestation({ data: { attestation: { id: "ATST-X", pass: true, results: [], attestedAt: new Date().toISOString(), expiresAt: new Date(Date.now() + 1800000).toISOString() }, sig: "dGVzdA==" } });
// Signature will fail (not signed by InsumerAPI key), but the point is it didn't
// try to parse as JWT
assert(!result.checks.signature.passed, "Sig fails (test sig, not InsumerAPI)");
assert(result.checks.conditionHashes.passed, "Condition hashes pass (empty results)");
assert(result.checks.expiry.passed, "Expiry passes");
}
// Test 3: JWT with valid signature, all 4 checks pass
console.log("\nTest 3: Valid JWT — all 4 checks pass");
{
const { jwt, keyPair } = await createTestJwt();
const restore = mockFetchForKey(keyPair.publicKey);
try {
const result = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
maxAge: 300,
});
assert(result.checks.signature.passed, "Signature passes");
assert(result.checks.conditionHashes.passed, "Condition hashes pass");
assert(result.checks.freshness.passed, "Freshness passes");
assert(result.checks.expiry.passed, "Expiry passes");
assert(result.valid, "Overall valid");
} finally {
restore();
}
}
// Test 4: Tampered JWT payload — signature fails
console.log("\nTest 4: Tampered JWT payload — signature fails");
{
const { jwt, keyPair } = await createTestJwt();
const restore = mockFetchForKey(keyPair.publicKey);
try {
// Tamper with the payload by changing a character
const parts = jwt.split(".");
// Decode payload, modify, re-encode
const payloadJson = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
payloadJson.pass = false; // tamper
const tamperedPayload = btoa(JSON.stringify(payloadJson))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
const tamperedJwt = `${parts[0]}.${tamperedPayload}.${parts[2]}`;
const result = await verifyAttestation(tamperedJwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
});
assert(!result.checks.signature.passed, "Signature should fail for tampered JWT");
assert(!result.valid, "Overall should be invalid");
} finally {
restore();
}
}
// Test 5: Expired JWT — exp in the past (5 min: beyond the default 60s clock skew)
console.log("\nTest 5: Expired JWT — expiry check fails");
{
const { jwt, keyPair } = await createTestJwt({
payloadOverrides: { exp: Math.floor(Date.now() / 1000) - 300 }, // 5 min ago
});
const restore = mockFetchForKey(keyPair.publicKey);
try {
const result = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
});
assert(!result.checks.expiry.passed, "Expiry should fail for expired JWT");
assert(!result.valid, "Overall should be invalid");
} finally {
restore();
}
}
// Test 5b: exp 30s in the past is inside the default clock-skew allowance; clockSkew: 0 refuses it
console.log("\nTest 5b: JWT exp within the clock-skew allowance");
{
const { jwt, keyPair } = await createTestJwt({
payloadOverrides: { exp: Math.floor(Date.now() / 1000) - 30 }, // 30s ago
});
const restore = mockFetchForKey(keyPair.publicKey);
try {
const lenient = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
});
assert(lenient.checks.expiry.passed, "exp 30s ago passes under the default 60s skew");
const strict = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
clockSkew: 0,
});
assert(!strict.checks.expiry.passed, "exp 30s ago fails with clockSkew: 0");
} finally {
restore();
}
}
// Test 6: Stale blockTimestamp in JWT — freshness check fails
console.log("\nTest 6: Stale blockTimestamp — freshness check fails");
{
const oldTimestamp = new Date(Date.now() - 120000).toISOString(); // 2 min ago
const { jwt, keyPair } = await createTestJwt({
resultOverrides: { blockTimestamp: oldTimestamp },
payloadOverrides: { blockTimestamp: oldTimestamp },
});
const restore = mockFetchForKey(keyPair.publicKey);
try {
const result = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
maxAge: 1, // 1 second — will fail
});
assert(!result.checks.freshness.passed, "Freshness should fail for stale blockTimestamp");
} finally {
restore();
}
}
// Test 7: Tampered conditionHash in JWT — condition hash check fails
console.log("\nTest 7: Tampered conditionHash in JWT — hash check fails");
{
const { jwt, keyPair } = await createTestJwt({
resultOverrides: { conditionHash: "0xdeadbeefdeadbeefdeadbeefdeadbeef" },
});
const restore = mockFetchForKey(keyPair.publicKey);
try {
const result = await verifyAttestation(jwt, {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
});
assert(!result.checks.conditionHashes.passed, "Condition hash should fail");
assert(
result.checks.conditionHashes.failures?.includes(0),
"Should report index 0 as failure"
);
} finally {
restore();
}
}
// Test 8: JWT sub matches wallet address
console.log("\nTest 8: JWT sub field contains wallet address");
{
const wallet = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
const { jwt } = await createTestJwt({
payloadOverrides: { sub: wallet },
});
// Just verify we can parse the JWT and extract sub
const parts = jwt.split(".");
const payload = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
assert(payload.sub === wallet, "JWT sub should match wallet address");
}
// Test 9: JWT jti matches attestationId
console.log("\nTest 9: JWT jti matches attestation ID");
{
const attestId = "ATST-X9Y2Z";
const { jwt } = await createTestJwt({
payloadOverrides: { jti: attestId },
});
const parts = jwt.split(".");
const payload = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
assert(payload.jti === attestId, "JWT jti should match attestation ID");
}
// Test 10: JWT exp is exactly 1800s after iat
console.log("\nTest 10: JWT exp is 1800s after iat");
{
const { jwt } = await createTestJwt();
const parts = jwt.split(".");
const payload = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
assert(payload.exp - payload.iat === 1800, "exp should be exactly 1800s after iat");
}
// Test 11: Invalid JWT string — parse error
console.log("\nTest 11: Invalid JWT string — returns parse error");
{
const result = await verifyAttestation("not.a.valid-jwt!!!");
assert(!result.valid, "Should be invalid");
assert(
result.checks.signature.reason?.includes("JWT parse error") ||
result.checks.signature.reason?.includes("JWT signature"),
"Should report parse or signature error"
);
}
// Test 12: On the JWT path too, a kid that selects no key fails the signature verdict only
console.log("\nTest 12: JWT under a kid the key set does not hold — other verdicts stay independent");
{
const { jwt, keyPair } = await createTestJwt({ headerOverrides: { kid: "insumer-attest-v9" } });
const restore = mockFetchForKey(keyPair.publicKey); // serves insumer-attest-v1 only
try {
const result = await verifyAttestation(jwt, { jwksUrl: "https://test.example.com/.well-known/jwks.json" });
assert(result.valid === false, "valid is false");
assert(!result.checks.signature.passed && /no key matching kid/.test(result.checks.signature.reason), "signature carries the key-selection reason");
assert(result.checks.conditionHashes.passed, "condition hashes are recomputed from the claims and reproduce");
assert(result.checks.expiry.passed, "expiry reports its own result");
assert(result.checks.pq.status === "absent", "no companion supplied: pq is absent");
} finally {
restore();
}
const missing = await createTestJwt({ headerOverrides: { kid: undefined } });
const restore2 = mockFetchForKey(missing.keyPair.publicKey);
try {
const result = await verifyAttestation(missing.jwt, { jwksUrl: "https://test.example.com/.well-known/jwks.json" });
assert(result.valid === false && /no kid/.test(result.checks.signature.reason), "missing kid: valid is false, the signature verdict says so");
assert(result.checks.conditionHashes.passed && result.checks.expiry.passed, "missing kid: condition hashes and expiry still report their own results");
} finally {
restore2();
}
}
// Test: malformed exp / iat claims are a failed verdict, never a rejection (1.11.1).
// exp and iat are read before the signature is checked, so anyone presenting a token
// controls them. A value that is not a finite number naming a valid date used to reach
// toISOString() and reject with RangeError.
console.log("\nTest: malformed exp / iat claims never make verifyAttestation reject");
{
const BAD = [["a string", "x"], ["an object", {}], ["1e20", 1e20], ["-1e20", -1e20]];
const forged = (claims) =>
base64UrlEncode(JSON.stringify({ alg: "ES256", typ: "JWT", kid: "insumer-attest-v1" })) + "." +
base64UrlEncode(JSON.stringify({ results: [], ...claims })) + ".";
const { keyPair } = await createTestJwt();
const restore = mockFetchForKey(keyPair.publicKey);
try {
for (const claim of ["exp", "iat"]) {
for (const [label, value] of BAD) {
let result, threw = null;
try {
result = await verifyAttestation(forged({ [claim]: value, ...(claim === "iat" ? { exp: Math.floor(Date.now() / 1000) + 600 } : {}) }), {
jwksUrl: "https://test.example.com/.well-known/jwks.json",
});
} catch (e) {
threw = e;
}
assert(threw === null && result && result.valid === false, `unsigned token, ${claim} = ${label}: resolves with valid false (${threw ? threw.name + ": " + threw.message : "no rejection"})`);
if (claim === "exp" && result) {
assert(result.checks.expiry.passed === false && /Invalid exp claim/.test(result.checks.expiry.reason), `unsigned token, exp = ${label}: expiry fails and names the exp claim`);
}
}
}
// A correctly signed token whose exp is malformed: the signature holds, expiry fails.
for (const [label, value] of BAD) {
const { jwt, keyPair: kp } = await createTestJwt({ payloadOverrides: { exp: value } });
const restore2 = mockFetchForKey(kp.publicKey);
let result, threw = null;
try {
result = await verifyAttestation(jwt, { jwksUrl: "https://test.example.com/.well-known/jwks.json" });
} catch (e) {
threw = e;
} finally {
restore2();
}
assert(threw === null && result && result.valid === false && result.checks.signature.passed === true && result.checks.expiry.passed === false,
`signed token, exp = ${label}: valid false, signature passes, expiry fails (${threw ? threw.name : "no rejection"})`);
}
} finally {
restore();
}
}
// ── Summary ──────────────────────────────────────────────────────
console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed > 0 ? 1 : 0);