quelda
Version:
PDF & Illustrator security scanner (WebAssembly): files + policy in, verdict out
129 lines (128 loc) • 5.11 kB
JavaScript
// Worker side: answers the scanner's messages with the WASM engine. Kept apart
// from worker.ts (which wires it to the real engine and the worker scope) so it
// can be unit-tested with a fake engine. No scan logic: the engine does it all.
import { errorMessage } from './protocol.js';
/**
* `FileReaderSync` (dedicated workers in browsers): each `readInto` reads
* one slice of the Blob, so the file is never loaded whole. `undefined`
* where it does not exist (Node).
*/
export const fileReaderSync = (blob) => {
const FRS = globalThis
.FileReaderSync;
if (typeof FRS !== 'function')
return undefined;
const frs = new FRS();
return {
size: blob.size,
readInto(offset, into) {
const buf = frs.readAsArrayBuffer(blob.slice(offset, offset + into.length));
if (buf.byteLength !== into.length)
throw new Error('the file changed while it was read');
into.set(new Uint8Array(buf));
},
};
};
const isBlob = (x) => typeof Blob !== 'undefined' && x instanceof Blob;
/**
* Returns the worker's message handler. The engine is loaded once (`init`);
* after it throws or traps the host refuses further work (`fail`), since a
* trapped WASM instance may be left in a broken state. The scanner then
* replaces the worker.
*/
export function createHost(load, post, now = () => performance.now(), readerFor = fileReaderSync) {
let engine;
let initStarted = false;
let broken;
const call = (id, run) => {
if (engine === undefined || broken !== undefined) {
post({ type: 'fail', id, message: broken ?? 'engine not loaded' });
return;
}
let reply;
try {
reply = run(engine);
}
catch (e) {
broken = `engine failed: ${errorMessage(e)}`;
reply = { type: 'fail', id, message: broken };
}
post(reply);
};
return async (data) => {
if (typeof data !== 'object' || data === null)
return;
const msg = data;
switch (msg.type) {
case 'init': {
if (initStarted)
return;
initStarted = true;
const wasm = msg.wasm;
if (wasm !== undefined && typeof wasm !== 'string' && !(wasm instanceof WebAssembly.Module)) {
post({ type: 'init-error', message: 'invalid wasm source' });
return;
}
try {
const loaded = await load(wasm);
const version = loaded.version();
if (typeof version !== 'string')
throw new Error('engine version is not a string');
engine = loaded;
post({ type: 'ready', version });
}
catch (e) {
broken = `engine failed to load: ${errorMessage(e)}`;
post({ type: 'init-error', message: broken });
}
return;
}
case 'scan': {
const { id, bytes, blob, policy } = msg;
if (typeof id !== 'number')
return;
const ok = bytes instanceof ArrayBuffer ? blob === undefined : bytes === undefined && isBlob(blob);
if (!ok || typeof policy !== 'string') {
post({ type: 'fail', id, message: 'malformed scan request' });
return;
}
const timed = (scan) => (e) => {
const start = now();
const verdict = scan(e);
const durationMs = now() - start;
return { type: 'scan', id, verdict, durationMs, memory: e.memory() };
};
if (bytes instanceof ArrayBuffer) {
call(id, timed((e) => e.scan(new Uint8Array(bytes), policy)));
return;
}
const file = blob;
const reader = readerFor(file);
if (reader !== undefined) {
call(id, timed((e) => e.scan_source(reader, policy)));
return;
}
// No synchronous reads here: the whole file, then the slice path.
let whole;
try {
whole = await file.arrayBuffer();
}
catch (e) {
post({ type: 'input-error', id, message: `could not read the file: ${errorMessage(e)}` });
return;
}
call(id, timed((e) => e.scan(new Uint8Array(whole), policy)));
return;
}
case 'rules': {
const { id } = msg;
if (typeof id !== 'number')
return;
call(id, (e) => ({ type: 'rules', id, rules: e.list_rules() }));
return;
}
default:
return;
}
};
}