@secure-exec/browser 0.0.0-agentos-dylib-base.edaa4a4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +6 -0
  2. package/dist/child-process-bridge.d.ts +25 -0
  3. package/dist/child-process-bridge.js +50 -0
  4. package/dist/converged-base64.d.ts +2 -0
  5. package/dist/converged-base64.js +41 -0
  6. package/dist/converged-dgram-bridge.d.ts +11 -0
  7. package/dist/converged-dgram-bridge.js +147 -0
  8. package/dist/converged-driver-setup.d.ts +22 -0
  9. package/dist/converged-driver-setup.js +72 -0
  10. package/dist/converged-execution-host-bridge.d.ts +7 -0
  11. package/dist/converged-execution-host-bridge.js +85 -0
  12. package/dist/converged-executor-session.d.ts +60 -0
  13. package/dist/converged-executor-session.js +127 -0
  14. package/dist/converged-fs-bridge.d.ts +42 -0
  15. package/dist/converged-fs-bridge.js +245 -0
  16. package/dist/converged-module-servicer.d.ts +8 -0
  17. package/dist/converged-module-servicer.js +79 -0
  18. package/dist/converged-net-bridge.d.ts +28 -0
  19. package/dist/converged-net-bridge.js +155 -0
  20. package/dist/converged-permissions.d.ts +9 -0
  21. package/dist/converged-permissions.js +46 -0
  22. package/dist/converged-sync-bridge-handler.d.ts +47 -0
  23. package/dist/converged-sync-bridge-handler.js +140 -0
  24. package/dist/converged-sync-bridge-router.d.ts +33 -0
  25. package/dist/converged-sync-bridge-router.js +41 -0
  26. package/dist/driver.d.ts +91 -0
  27. package/dist/driver.js +386 -0
  28. package/dist/encoding.d.ts +4 -0
  29. package/dist/encoding.js +102 -0
  30. package/dist/generated/util-polyfill.d.ts +1 -0
  31. package/dist/generated/util-polyfill.js +2 -0
  32. package/dist/index.d.ts +9 -0
  33. package/dist/index.js +5 -0
  34. package/dist/kernel-backed-filesystem.d.ts +33 -0
  35. package/dist/kernel-backed-filesystem.js +205 -0
  36. package/dist/os-filesystem.d.ts +47 -0
  37. package/dist/os-filesystem.js +409 -0
  38. package/dist/permission-validation.d.ts +15 -0
  39. package/dist/permission-validation.js +62 -0
  40. package/dist/root-filesystem-from-vfs.d.ts +13 -0
  41. package/dist/root-filesystem-from-vfs.js +95 -0
  42. package/dist/runtime-driver.d.ts +66 -0
  43. package/dist/runtime-driver.js +611 -0
  44. package/dist/runtime.d.ts +248 -0
  45. package/dist/runtime.js +2296 -0
  46. package/dist/sidecar-wasm-module.d.ts +62 -0
  47. package/dist/sidecar-wasm-module.js +28 -0
  48. package/dist/sidecar-worker-protocol.d.ts +14 -0
  49. package/dist/sidecar-worker-protocol.js +9 -0
  50. package/dist/sidecar-worker.d.ts +19 -0
  51. package/dist/sidecar-worker.js +63 -0
  52. package/dist/signals.d.ts +13 -0
  53. package/dist/signals.js +89 -0
  54. package/dist/sync-bridge.d.ts +50 -0
  55. package/dist/sync-bridge.js +93 -0
  56. package/dist/wasi-polyfill.d.ts +1 -0
  57. package/dist/wasi-polyfill.js +2154 -0
  58. package/dist/worker-adapter.d.ts +21 -0
  59. package/dist/worker-adapter.js +41 -0
  60. package/dist/worker-protocol.d.ts +104 -0
  61. package/dist/worker-protocol.js +1 -0
  62. package/dist/worker-sidecar-client.d.ts +71 -0
  63. package/dist/worker-sidecar-client.js +152 -0
  64. package/dist/worker.d.ts +1 -0
  65. package/dist/worker.js +2125 -0
  66. package/package.json +111 -0
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Validate permission callback source strings before revival via new Function().
3
+ *
4
+ * Permission callbacks are serialized with fn.toString() on the host and revived
5
+ * in the Web Worker. Because revival uses new Function(), the source must be
6
+ * validated to prevent code injection.
7
+ */
8
+ /**
9
+ * Validate that a permission callback source string is safe to revive.
10
+ *
11
+ * Returns true if the source appears to be a safe function expression.
12
+ * Returns false if the source contains blocked patterns that could indicate
13
+ * code injection.
14
+ */
15
+ export declare function validatePermissionSource(source: string): boolean;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Validate permission callback source strings before revival via new Function().
3
+ *
4
+ * Permission callbacks are serialized with fn.toString() on the host and revived
5
+ * in the Web Worker. Because revival uses new Function(), the source must be
6
+ * validated to prevent code injection.
7
+ */
8
+ /**
9
+ * Dangerous patterns that should never appear in a permission callback.
10
+ * These could be used to escape the sandbox or access host resources.
11
+ */
12
+ const BLOCKED_PATTERNS = [
13
+ // Code execution / eval
14
+ /\beval\s*\(/,
15
+ /\bFunction\s*\(/,
16
+ /\bnew\s+Function\b/,
17
+ // Module loading
18
+ /\bimport\s*\(/,
19
+ /\bimportScripts\s*\(/,
20
+ /\brequire\s*\(/,
21
+ // Global object access
22
+ /\bglobalThis\b/,
23
+ /\bself\b/,
24
+ /\bwindow\b/,
25
+ // Process/system access
26
+ /\bprocess\s*\.\s*(?:exit|kill|binding|_linkedBinding|env)\b/,
27
+ // Network / IO escape
28
+ /\bXMLHttpRequest\b/,
29
+ /\bWebSocket\b/,
30
+ /\bfetch\s*\(/,
31
+ // Prototype pollution / constructor abuse
32
+ /\bconstructor\s*\[/,
33
+ /\b__proto__\b/,
34
+ /Object\s*\.\s*(?:defineProperty|setPrototypeOf|assign)\b/,
35
+ // Dynamic property access on dangerous objects
36
+ /\bpostMessage\b/,
37
+ ];
38
+ /**
39
+ * Validate that a permission callback source string is safe to revive.
40
+ *
41
+ * Returns true if the source appears to be a safe function expression.
42
+ * Returns false if the source contains blocked patterns that could indicate
43
+ * code injection.
44
+ */
45
+ export function validatePermissionSource(source) {
46
+ if (!source || typeof source !== "string")
47
+ return false;
48
+ const trimmed = source.trim();
49
+ // Must look like a function expression (arrow function or function keyword)
50
+ const startsLikeFunction = trimmed.startsWith("function") ||
51
+ trimmed.startsWith("(") ||
52
+ // Single-param arrow functions: x => ...
53
+ /^[a-zA-Z_$][a-zA-Z0-9_$]*\s*=>/.test(trimmed);
54
+ if (!startsLikeFunction)
55
+ return false;
56
+ // Check for blocked patterns
57
+ for (const pattern of BLOCKED_PATTERNS) {
58
+ if (pattern.test(source))
59
+ return false;
60
+ }
61
+ return true;
62
+ }
@@ -0,0 +1,13 @@
1
+ import type { RootFilesystemConfig } from "@secure-exec/core/vm-config";
2
+ import type { VirtualFileSystem } from "./runtime.js";
3
+ type RootFilesystemEntry = RootFilesystemConfig["bootstrapEntries"][number];
4
+ export interface RootFilesystemSnapshotOptions {
5
+ root?: string;
6
+ mode?: RootFilesystemConfig["mode"];
7
+ disableDefaultBaseLayer?: boolean;
8
+ }
9
+ /** Walk `vfs` and produce kernel bootstrap entries for its contents. */
10
+ export declare function collectRootFilesystemEntries(vfs: VirtualFileSystem, root?: string): Promise<RootFilesystemEntry[]>;
11
+ /** A full `RootFilesystemConfig` seeded from `vfs`. */
12
+ export declare function rootFilesystemConfigFromVfs(vfs: VirtualFileSystem, options?: RootFilesystemSnapshotOptions): Promise<RootFilesystemConfig>;
13
+ export {};
@@ -0,0 +1,95 @@
1
+ // Migration shim: snapshot a legacy caller-provided VirtualFileSystem into a
2
+ // kernel `RootFilesystemConfig` (bootstrap entries) so the converged driver can
3
+ // seed a kernel-owned VM from filesystem content that was previously handed in
4
+ // as a live TS VFS object. This bridges the legacy `options.system.filesystem`
5
+ // model to the converged kernel-owns-fs model without rewriting every caller at
6
+ // once; new callers should provide a `CreateVmConfig.rootFilesystem` directly.
7
+ import { encodeBase64 } from "./converged-base64.js";
8
+ // Kernel-owned pseudo-filesystems must not be materialized as bootstrap entries.
9
+ const SKIP_ROOTS = ["/dev", "/proc", "/sys"];
10
+ /** Walk `vfs` and produce kernel bootstrap entries for its contents. */
11
+ export async function collectRootFilesystemEntries(vfs, root = "/") {
12
+ const entries = [];
13
+ await walk(vfs, normalizeDir(root), entries);
14
+ return entries;
15
+ }
16
+ /** A full `RootFilesystemConfig` seeded from `vfs`. */
17
+ export async function rootFilesystemConfigFromVfs(vfs, options = {}) {
18
+ return {
19
+ mode: options.mode ?? "ephemeral",
20
+ disableDefaultBaseLayer: options.disableDefaultBaseLayer ?? false,
21
+ lowers: [],
22
+ bootstrapEntries: await collectRootFilesystemEntries(vfs, options.root ?? "/"),
23
+ };
24
+ }
25
+ async function walk(vfs, dir, entries) {
26
+ let children;
27
+ try {
28
+ children = await vfs.readDirWithTypes(dir);
29
+ }
30
+ catch {
31
+ return;
32
+ }
33
+ for (const child of children) {
34
+ if (child.name === "." || child.name === "..") {
35
+ continue;
36
+ }
37
+ const path = joinPath(dir, child.name);
38
+ if (SKIP_ROOTS.includes(path)) {
39
+ continue;
40
+ }
41
+ if (child.isSymbolicLink) {
42
+ const target = await vfs.readlink(path).catch(() => null);
43
+ if (target !== null) {
44
+ entries.push({ path, kind: "symlink", target, executable: false });
45
+ }
46
+ continue;
47
+ }
48
+ if (child.isDirectory) {
49
+ entries.push({ path, kind: "directory", executable: true });
50
+ await walk(vfs, path, entries);
51
+ continue;
52
+ }
53
+ entries.push(await fileEntry(vfs, path));
54
+ }
55
+ }
56
+ async function fileEntry(vfs, path) {
57
+ const bytes = await vfs.readFile(path);
58
+ const executable = await isExecutable(vfs, path);
59
+ const text = tryDecodeUtf8(bytes);
60
+ if (text !== null) {
61
+ return { path, kind: "file", content: text, encoding: "utf8", executable };
62
+ }
63
+ return {
64
+ path,
65
+ kind: "file",
66
+ content: encodeBase64(bytes),
67
+ encoding: "base64",
68
+ executable,
69
+ };
70
+ }
71
+ async function isExecutable(vfs, path) {
72
+ try {
73
+ return ((await vfs.stat(path)).mode & 0o111) !== 0;
74
+ }
75
+ catch {
76
+ return false;
77
+ }
78
+ }
79
+ function tryDecodeUtf8(bytes) {
80
+ try {
81
+ return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
82
+ }
83
+ catch {
84
+ return null;
85
+ }
86
+ }
87
+ function normalizeDir(dir) {
88
+ if (dir.length > 1 && dir.endsWith("/")) {
89
+ return dir.slice(0, -1);
90
+ }
91
+ return dir;
92
+ }
93
+ function joinPath(parent, child) {
94
+ return parent === "/" ? `/${child}` : `${parent}/${child}`;
95
+ }
@@ -0,0 +1,66 @@
1
+ import type { ProtocolFramePayloadCodec } from "@secure-exec/core/protocol-frames";
2
+ import type { CreateVmConfig } from "@secure-exec/core/vm-config";
3
+ import type { ConvergedServicer } from "./converged-driver-setup.js";
4
+ import type { ExecOptions, ExecResult, NetworkAdapter, NodeRuntimeDriver, NodeRuntimeDriverFactory, RunResult, RuntimeDriverOptions } from "./runtime.js";
5
+ export interface BrowserRuntimeDriverFactoryOptions {
6
+ workerUrl?: URL | string;
7
+ convergedSidecar?: ConvergedSidecarFactoryOptions;
8
+ }
9
+ export interface ConvergedSidecarHandle {
10
+ pushFrame(frame: Uint8Array): Uint8Array;
11
+ setNextExecutionId?(executionId: string): void;
12
+ }
13
+ export interface ConvergedSidecarFactoryOptions {
14
+ loadSidecar(): Promise<ConvergedSidecarHandle>;
15
+ config: CreateVmConfig;
16
+ codec?: ProtocolFramePayloadCodec;
17
+ onFsReadDenied?: () => void;
18
+ }
19
+ export declare class BrowserRuntimeDriver implements NodeRuntimeDriver {
20
+ private readonly worker;
21
+ private readonly pending;
22
+ private readonly controlToken;
23
+ private readonly defaultOnStdio?;
24
+ private readonly defaultTimingMitigation;
25
+ private readonly networkAdapter;
26
+ private readonly commandExecutor;
27
+ private readonly syncBridge;
28
+ private readonly childProcessSessions;
29
+ private readonly signalStates;
30
+ private readonly ready;
31
+ private readonly encoder;
32
+ private nextId;
33
+ private nextExecutionId;
34
+ private nextChildProcessSessionId;
35
+ private disposed;
36
+ private readonly networkPermission?;
37
+ private convergedServicer?;
38
+ private readonly convergedReady?;
39
+ constructor(options: RuntimeDriverOptions, factoryOptions?: BrowserRuntimeDriverFactoryOptions);
40
+ private setupConvergedSidecar;
41
+ get network(): Pick<NetworkAdapter, "fetch" | "dnsLookup" | "httpRequest">;
42
+ private handleWorkerError;
43
+ private handleWorkerMessage;
44
+ private handleSyncRequest;
45
+ private rejectAllPending;
46
+ private clearWorkerHandlers;
47
+ private allocateExecutionId;
48
+ private allocateChildProcessSessionId;
49
+ private hasPendingExecutionRequest;
50
+ private cleanupExecutionState;
51
+ private resetSyncBridgeState;
52
+ private cleanup;
53
+ private callWorker;
54
+ run<T = unknown>(code: string, filePath?: string): Promise<RunResult<T>>;
55
+ exec(code: string, options?: ExecOptions): Promise<ExecResult>;
56
+ dispatchExtensionRequest(namespace: string, payload: Uint8Array): Promise<Uint8Array>;
57
+ dispose(): void;
58
+ /**
59
+ * Snapshot the converged VM root filesystem (writable changes) so callers can
60
+ * persist them to host storage across runtimes. Returns null in legacy mode.
61
+ */
62
+ snapshotConvergedRootFilesystem(): Promise<ReturnType<ConvergedServicer["snapshotRootFilesystem"]> | null>;
63
+ terminate(): Promise<void>;
64
+ signalPendingExecution(signal?: number): boolean;
65
+ }
66
+ export declare function createBrowserRuntimeDriverFactory(factoryOptions?: BrowserRuntimeDriverFactoryOptions): NodeRuntimeDriverFactory;