Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions worker/test/localproof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
Loading