Skip to content

Reference source · 0.1.0-dev.11

negative-conformance.test.ts

tests/negative-conformance.test.ts · Download original file

import test from 'node:test';
import assert from 'node:assert/strict';
import { fixture,PATIENT } from '../src/reference/fixtures.ts';
import { SCOPES } from '../src/authorization.ts';
import { DurableMirror } from '../src/mirror.ts';
import { open,seal } from '../src/envelope.ts';
import { signedMalformed,malformedHeaders } from './helpers/adversarial.ts';
import { b64,unb64 } from '../src/core.ts';
async function auth(f:ReturnType<typeof fixture>){const r=await f.client.requestAccess({patient:PATIENT,scopes:Object.values(SCOPES),requested_duration_seconds:1800,refresh_requested:true});if(r.status==='pending'){f.host.authorization.approve(f.app,r.request_id,PATIENT,f.clock());await f.client.approvalStatus(r.request_id);}}
test('signed malformed headers, duplicate JSON, signatures and encodings are rejected before application processing',()=>{
 const f=fixture();try{const valid=seal({status:'ok'},f.hostIdentity,f.hostCredential,f.appIdentity.public,f.clock(),null);assert.equal(open(valid,f.appIdentity,f.trust,f.clock()).body.status,'ok');for(const change of malformedHeaders(f.clock()))assert.throws(()=>open(signedMalformed(f.hostIdentity,f.hostCredential,f.appIdentity.public,f.clock(),change),f.appIdentity,f.trust,f.clock()));
 assert.throws(()=>open(signedMalformed(f.hostIdentity,f.hostCredential,f.appIdentity.public,f.clock(),{},true),f.appIdentity,f.trust,f.clock()));
 const signature=unb64(valid.signature);signature[0]^=1;assert.throws(()=>open({...valid,signature:b64(signature)},f.appIdentity,f.trust,f.clock()));assert.throws(()=>open({...valid,nonce:valid.nonce+'='},f.appIdentity,f.trust,f.clock()));
 }finally{f.host.close();f.trust.close();}
});
test('malformed authenticated change pages clear the mirror instead of advancing or exposing stale records',async()=>{
 const f=fixture();const mirror=new DurableMirror(':memory:',f.client,PATIENT);try{await auth(f);await mirror.sync();const changes=f.client.changes.bind(f.client);
 const bad=[{events:[null]},{has_more:'false'}, {events:[{event_id:'bad',kind:'created',version:1,resource:{resourceType:'Patient',id:'other-patient'}}]},
 {events:[{event_id:'bad',kind:'deleted',version:2,resource:{resourceType:'Condition',id:'x',code:{text:'Forbidden tombstone content'}}}]},
 {events:[{event_id:'bad',kind:'unknown',version:1,resource:{resourceType:'Patient',id:PATIENT}}]},
 {events:[{event_id:'bad',kind:'updated',version:1.5,resource:{resourceType:'Patient',id:PATIENT}}]},
 {events:[{event_id:'bad',kind:'updated',version:2,resource:{resourceType:'Patient',id:PATIENT,meta:{security:[{system:'http://terminology.hl7.org/CodeSystem/v3-ActCode',code:'DELAU'}]}}}]},
 ];for(const mutation of bad){f.client.changes=async()=>({status:'ok',cursor:mirror.state().cursor,visibility_epoch:mirror.state().visibility_epoch,has_more:false,events:[],...mutation}) as any;await assert.rejects(mirror.sync());assert.throws(()=>mirror.resources(true),/mirror_unavailable/);f.client.changes=changes;await mirror.sync();assert.equal(mirror.resources().length,1);}
 }finally{mirror.close();f.host.close();f.trust.close();}
});
test('malformed staged baseline metadata is never presented as a complete mirrored record',async()=>{
 const f=fixture();try{await auth(f);const snapshot=f.client.syncSnapshot.bind(f.client);for(const edit of [{total:-1},{page_cursor:'unexpected'},{visibility_epoch:''},{resources:[{version:1,resource:{resourceType:'Patient',id:'wrong'}}]}]){const mirror=new DurableMirror(':memory:',f.client,PATIENT);f.client.syncSnapshot=async(...args)=>({...await snapshot(...args),...edit}) as any;await assert.rejects(mirror.sync());assert.throws(()=>mirror.resources(true),/mirror_unavailable/);mirror.close();}f.client.syncSnapshot=snapshot;
 }finally{f.host.close();f.trust.close();}
});