Files
safetynet/scripts/semantic-browser-smoke.cjs
T

34 lines
3.8 KiB
JavaScript
Raw Normal View History

2026-10-13 10:33:31 +02:00
// Finite native fixture: synthetic teaching data, real signed policy and CPU model.
const {app,BrowserWindow,clipboard,ClipboardItem}=require('electron');
const fs=require('node:fs'),path=require('node:path'),assert=require('node:assert/strict');
app.setPath('userData',process.env.SAFETYNET_SEMANTIC_PROFILE);
app.setPath('sessionData',process.env.SAFETYNET_SEMANTIC_PROFILE);
app.disableHardwareAcceleration();
require('../apps/browser/main.cjs');
const wait=ms=>new Promise(resolve=>setTimeout(resolve,ms));
const deadline=setTimeout(()=>app.exit(1),90000);
app.whenReady().then(async()=>{let original;try{
let win;
for(let i=0;i<150;i++){win=BrowserWindow.getAllWindows()[0];if(win&&!win.webContents.isLoading()&&win.webContents.getURL().endsWith('index.html'))break;await wait(100);}
assert.ok(win,'Trusted shell unavailable');
const enrolled=await win.webContents.executeJavaScript(`window.safetynet.enroll(${JSON.stringify({server:process.env.SAFETYNET_SEMANTIC_SERVER,code:process.env.SAFETYNET_SEMANTIC_CODE})})`);
assert.equal(enrolled.ok,true,'Enrollment failed');assert.equal(enrolled.data.ready,true);
const page=win.contentView.children.find(child=>child.webContents!==win.webContents).webContents;
for(let i=0;i<100&&page.isLoading();i++)await wait(100);
const connection=JSON.parse(process.env.SAFETYNET_SEMANTIC_FIXTURES);
original=[];for(const item of await clipboard.read()){const data={};for(const type of item.types)data[type]=await item.getType(type);if(Object.keys(data).length)original.push(new ClipboardItem(data));}
async function select(text){await page.executeJavaScript(`(()=>{let node=document.getElementById('source');node.textContent=${JSON.stringify(text)};const range=document.createRange();range.selectNodeContents(node);const selection=getSelection();selection.removeAllRanges();selection.addRange(range);})()`);}
async function copy(){win.show();win.focus();page.focus();page.sendInputEvent({type:'keyDown',keyCode:'C',modifiers:['control']});page.sendInputEvent({type:'keyUp',keyCode:'C',modifiers:['control']});await wait(500);for(let i=0;i<150;i++){const state=await win.webContents.executeJavaScript('window.safetynet.state()');if(!state.data.clipboardPending)return;await wait(100);}throw Error('Native clipboard scan timed out');}
const sentinel='Public semantic test clipboard marker';
await clipboard.writeText(sentinel);await select(connection.paraphrase);await copy();
assert.ok((await clipboard.readText())===sentinel,'Semantic confidential paraphrase reached OS clipboard');
const overview=await (await fetch(`${process.env.SAFETYNET_SEMANTIC_SERVER}/api/admin/overview`,{headers:{authorization:`Bearer ${process.env.SAFETYNET_SEMANTIC_ADMIN}`}})).json();
assert.ok(overview.events.some(event=>event.kind==='clipboard.block'&&event.metadata.ruleId==='synthetic-restructuring'),'Semantic rule did not cause centrally audited block');
await select(connection.publicText);await copy();
for(let i=0;i<30&&await clipboard.readText()!==connection.publicText;i++)await wait(100);
assert.equal(await clipboard.readText(),connection.publicText,'Public text was not copied through real local semantic inference');
assert.equal(JSON.stringify(overview.events).includes(connection.paraphrase),false,'Audit contains raw clipboard text');
console.log(JSON.stringify({nativeSemanticClipboard:true,confidentialParaphraseBlocked:true,publicTextAllowed:true,centralAuditMetadataOnly:true}));
if(original.length)await clipboard.write(original);else await clipboard.clear();original=null;clearTimeout(deadline);app.quit();
}catch(error){console.error(JSON.stringify({nativeSemanticClipboard:false,error:error.message}));if(original){try{if(original.length)await clipboard.write(original);else await clipboard.clear();}catch{}}clearTimeout(deadline);app.exit(1);}});