From 13d2dc2a4912edcea872311112b60a9bf6545d6a Mon Sep 17 00:00:00 2001 From: PDFly-source Date: Tue, 1 Dec 2026 13:44:31 +0530 Subject: [PATCH] Add LocalProof stale-signature, revoked-device and cross-merchant regression tests --- worker/test/localproof.test.js | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/worker/test/localproof.test.js b/worker/test/localproof.test.js index feb576c6..e4a09427 100644 --- a/worker/test/localproof.test.js +++ b/worker/test/localproof.test.js @@ -108,3 +108,44 @@ test("LocalProof event reads remain scoped to the authenticated merchant", async assert.equal(boundMerchant,"merchant-A"); assert.deepEqual((await result.json()).observations,[]); }); + + +test("LocalProof rejects stale signed requests before device lookup", async () => { + const oldTimestamp=String(Date.now()-6*60*1000); + let dbCalls=0; + const request=new Request("https://paynex.example/api/v1/localproof/events",{method:"POST", + headers:{"x-localproof-device":"123e4567-e89b-42d3-a456-426614174000","x-localproof-timestamp":oldTimestamp, + "x-localproof-nonce":"0123456789abcdef0123456789abcdef","x-localproof-signature":"AA"}, + body:JSON.stringify({event_uuid:"123e4567-e89b-42d3-a456-426614174001"})}); + const result=await handleLocalProofRequest(request,{DB:{prepare(){dbCalls++;throw new Error("DB must not be reached");}}},"*",async()=>null,json); + assert.equal(result.status,401); + assert.equal(dbCalls,0); +}); + +test("LocalProof rejects a revoked device before signature verification or writes", async () => { + let writes=0; + const request=new Request("https://paynex.example/api/v1/localproof/events",{method:"POST", + headers:{"x-localproof-device":"123e4567-e89b-42d3-a456-426614174000","x-localproof-timestamp":String(Date.now()), + "x-localproof-nonce":"0123456789abcdef0123456789abcdef","x-localproof-signature":"AA"}, + body:JSON.stringify({event_uuid:"123e4567-e89b-42d3-a456-426614174001"})}); + const db={prepare(sql){return {bind(){return this;},async first(){return {id:"123e4567-e89b-42d3-a456-426614174000",merchant_id:"merchant-A",public_key_jwk:"{}",status:"revoked"};},async run(){writes++;}};}}; + const result=await handleLocalProofRequest(request,{DB:db},"*",async()=>null,json); + assert.equal(result.status,401); + assert.equal(writes,0); +}); + +test("LocalProof refuses to bind a device key already owned by another merchant", async () => { + const pair=await crypto.subtle.generateKey({name:"ECDSA",namedCurve:"P-256"},true,["sign","verify"]); + const jwk=await crypto.subtle.exportKey("jwk",pair.publicKey); + let inserted=false; + const db={prepare(sql){return {bind(){return this;},async first(){ + if(sql.includes("FROM localproof_devices WHERE key_thumbprint")) return {id:"123e4567-e89b-42d3-a456-426614174000",merchant_id:"merchant-B",status:"active"}; + return null; + },async batch(){inserted=true;return [];}};}}; + const request=new Request("https://paynex.example/api/v1/localproof/devices",{method:"POST", + headers:{"content-type":"application/json"},body:JSON.stringify({label:"Test device",public_key_jwk:jwk})}); + const result=await handleLocalProofRequest(request,{DB:db},"*",async()=> "merchant-A",json); + assert.equal(result.status,409); + assert.equal(inserted,false); + assert.match((await result.json()).error,/another merchant/); +});