[share] Share links and the browser build: custos-share (blob v1, key in the fragment, optional password), custos-wasm (JSON ABI, no wasm-bindgen), docs/SHARE.md, threat model (v0.3.0)

This commit is contained in:
2026-10-31 18:20:51 +01:00
parent 4aac99fb47
commit 5ee1c0da6b
16 changed files with 1153 additions and 5 deletions
+46
View File
@@ -0,0 +1,46 @@
// The whole JavaScript side of custos-wasm (see src/lib.rs, "ABI"): load the module, give it the
// browser's CSPRNG, and pass JSON requests in and out. The vault key never leaves WebAssembly memory.
/** @param {BufferSource | Response | Promise<Response>} source the custos_wasm.wasm bytes or a fetch of them */
export async function loadCustos(source) {
let memory;
const env = {
random_fill(ptr, len) {
// getRandomValues fills at most 65,536 bytes per call.
for (let off = 0; off < len; off += 65536) {
crypto.getRandomValues(new Uint8Array(memory.buffer, ptr + off, Math.min(65536, len - off)));
}
},
};
const { instance } =
source instanceof Response || source instanceof Promise
? await WebAssembly.instantiateStreaming(source, { env })
: await WebAssembly.instantiate(source, { env });
const x = instance.exports;
memory = x.memory;
const enc = new TextEncoder();
const dec = new TextDecoder();
/** Sends one request; resolves to the parsed response ({ok: true, …} or {ok: false, error}). */
function call(request) {
const bytes = enc.encode(JSON.stringify(request));
const ptr = x.alloc(bytes.length);
new Uint8Array(memory.buffer, ptr, bytes.length).set(bytes);
bytes.fill(0);
const packed = x.call(ptr, bytes.length); // frees and zeroes the request
const outPtr = Number(packed >> 32n);
const outLen = Number(packed & 0xffffffffn);
const out = new Uint8Array(memory.buffer, outPtr, outLen);
const text = dec.decode(out);
x.dealloc(outPtr, outLen); // zeroes the response in WebAssembly memory
return JSON.parse(text);
}
return { call };
}
/** Base64 of bytes, for the `open` and `share_open` requests. */
export function toBase64(bytes) {
let s = '';
for (let i = 0; i < bytes.length; i += 0x8000) s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
return btoa(s);
}
+32
View File
@@ -0,0 +1,32 @@
// node test.mjs <custos_wasm.wasm> [<vault file> <password>]: the JS loader against the real build.
import fs from 'node:fs';
import { loadCustos, toBase64 } from './custos.mjs';
const [wasmPath, vaultPath, password] = process.argv.slice(2);
const c = await loadCustos(fs.readFileSync(wasmPath));
const assert = (ok, msg) => { if (!ok) { console.error('FAIL', msg); process.exit(1); } };
const bad = c.call({ op: 'share_open', blob: 'Q1NIMQA=', fragment: 'k=x' });
assert(!bad.ok && bad.code === 'Malformed', JSON.stringify(bad));
assert(c.call({ op: 'items' }).error === 'The vault is locked.', 'locked');
if (vaultPath) {
const t0 = performance.now();
const r = c.call({ op: 'open', vault: toBase64(fs.readFileSync(vaultPath)), password });
const ms = Math.round(performance.now() - t0);
assert(r.ok, r.error);
console.log(`unlocked ${r.items.length} items in ${ms} ms (WebAssembly, default KDF)`);
const item = r.items.find((i) => i.hasMainSecret);
for (const pw of [null, 'by-phone']) {
const t1 = performance.now();
const s = c.call({ op: 'share_seal', id: item.id, password: pw });
assert(s.ok, s.error);
const o = c.call({ op: 'share_open', blob: s.blob, fragment: s.fragment, password: pw });
assert(o.ok && o.payload.title === item.title, JSON.stringify(o.error));
console.log(`share ${pw ? 'with' : 'without'} password: sealed and opened in ${Math.round(performance.now() - t1)} ms`);
}
const fresh = new Set(Array.from({ length: 100 }, () => c.call({ op: 'share_seal', id: item.id }).fragment));
assert(fresh.size === 100, 'link keys repeat: random_fill is broken');
c.call({ op: 'lock' });
}
console.log('ok');