char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Stores split refactor (#27240)521a64cd0

char/aliceai-t5-moe
1.9 KiB44 linesraw
1/**
2 * Sandbox harness - builds the srcdoc document for the sandboxed iframe
3 *
4 * Produces the HTML/CSP/worker shim that runs untrusted model code in an
5 * opaque origin. Consumed by sandbox.service.
6 */
7
8import WORKER_SHIM from './sandbox-worker.js?raw';
9import { NEWLINE } from '$lib/constants';
10
11/**
12 * CSP for the harness document, inherited by the blob worker. connect-src
13 * falls back to default-src, removing network egress for model and vendored
14 * code. 'unsafe-eval' is required by the worker's AsyncFunction constructor,
15 * 'unsafe-inline' by the inline script below, worker-src by the blob worker.
16 */
17const HARNESS_CSP = `default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval'; worker-src blob:`;
18
19/**
20 * Harness loaded as srcdoc into a sandboxed iframe (allow-scripts only).
21 * The opaque origin is the security boundary: no access to the app origin,
22 * its storage or its API. The harness spawns a worker so model code never
23 * runs on a main thread, which makes the parent timeout enforceable by
24 * removing the iframe. The prelude runs in the worker before the shim,
25 * exposing globals such as `nerdamer` to model code.
26 */
27export function buildSandboxHarness(preludeJs: string): string {
28	return `<!doctype html><meta http-equiv="Content-Security-Policy" content="${HARNESS_CSP}"><script>
29const SHIM = ${JSON.stringify(preludeJs + NEWLINE + WORKER_SHIM)};
30addEventListener('message', (event) => {
31	const respond = (payload) => parent.postMessage(payload, '*');
32	let worker;
33	try {
34		worker = new Worker(URL.createObjectURL(new Blob([SHIM], { type: 'text/javascript' })));
35	} catch (err) {
36		respond({ logs: [], result: null, error: 'Worker creation failed: ' + err });
37		return;
38	}
39	worker.onmessage = (msg) => respond(msg.data);
40	worker.onerror = (err) => respond({ logs: [], result: null, error: String(err.message || err) });
41	worker.postMessage({ code: event.data.code });
42});
43</script>`;
44}