|
| 1 | +import { NodejsPlatformServices } from '@metamask/kernel-node-runtime'; |
| 2 | +import { makeSQLKernelDatabase } from '@metamask/kernel-store/sqlite/nodejs'; |
| 3 | +import { waitUntilQuiescent } from '@metamask/kernel-utils'; |
| 4 | +import { |
| 5 | + Logger, |
| 6 | + makeConsoleTransport, |
| 7 | + makeArrayTransport, |
| 8 | +} from '@metamask/logger'; |
| 9 | +import type { LogEntry } from '@metamask/logger'; |
| 10 | +import { Kernel } from '@metamask/ocap-kernel'; |
| 11 | +import type { KRef, VatId } from '@metamask/ocap-kernel'; |
| 12 | +import { getWorkerFile } from '@ocap/nodejs-test-workers'; |
| 13 | +import { describe, expect, it } from 'vitest'; |
| 14 | + |
| 15 | +import { extractTestLogs, getBundleSpec } from './utils.ts'; |
| 16 | + |
| 17 | +describe('global endowments', () => { |
| 18 | + const vatId: VatId = 'v1'; |
| 19 | + const v1Root: KRef = 'ko4'; |
| 20 | + |
| 21 | + const setup = async ({ |
| 22 | + globals, |
| 23 | + allowedGlobalNames, |
| 24 | + }: { |
| 25 | + globals: string[]; |
| 26 | + allowedGlobalNames?: string[]; |
| 27 | + }) => { |
| 28 | + const entries: LogEntry[] = []; |
| 29 | + const logger = new Logger({ |
| 30 | + transports: [makeConsoleTransport(), makeArrayTransport(entries)], |
| 31 | + }); |
| 32 | + const database = await makeSQLKernelDatabase({}); |
| 33 | + const platformServices = new NodejsPlatformServices({ |
| 34 | + logger: logger.subLogger({ tags: ['vat-worker-manager'] }), |
| 35 | + workerFilePath: getWorkerFile('mock-fetch'), |
| 36 | + }); |
| 37 | + const kernel = await Kernel.make(platformServices, database, { |
| 38 | + resetStorage: true, |
| 39 | + logger, |
| 40 | + allowedGlobalNames, |
| 41 | + }); |
| 42 | + |
| 43 | + await kernel.launchSubcluster({ |
| 44 | + bootstrap: 'main', |
| 45 | + vats: { |
| 46 | + main: { |
| 47 | + bundleSpec: getBundleSpec('endowment-globals'), |
| 48 | + parameters: {}, |
| 49 | + globals, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + await waitUntilQuiescent(); |
| 54 | + |
| 55 | + return { kernel, entries }; |
| 56 | + }; |
| 57 | + |
| 58 | + it('can use TextEncoder and TextDecoder', async () => { |
| 59 | + const { kernel, entries } = await setup({ |
| 60 | + globals: ['TextEncoder', 'TextDecoder'], |
| 61 | + }); |
| 62 | + |
| 63 | + await kernel.queueMessage(v1Root, 'testTextCodec', []); |
| 64 | + await waitUntilQuiescent(); |
| 65 | + |
| 66 | + const logs = extractTestLogs(entries, vatId); |
| 67 | + expect(logs).toContain('textCodec: hello'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('can use URL and URLSearchParams', async () => { |
| 71 | + const { kernel, entries } = await setup({ |
| 72 | + globals: ['URL', 'URLSearchParams'], |
| 73 | + }); |
| 74 | + |
| 75 | + await kernel.queueMessage(v1Root, 'testUrl', []); |
| 76 | + await waitUntilQuiescent(); |
| 77 | + |
| 78 | + const logs = extractTestLogs(entries, vatId); |
| 79 | + expect(logs).toContain('url: /path params: 10'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('can use atob and btoa', async () => { |
| 83 | + const { kernel, entries } = await setup({ globals: ['atob', 'btoa'] }); |
| 84 | + |
| 85 | + await kernel.queueMessage(v1Root, 'testBase64', []); |
| 86 | + await waitUntilQuiescent(); |
| 87 | + |
| 88 | + const logs = extractTestLogs(entries, vatId); |
| 89 | + expect(logs).toContain('base64: hello world'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('can use AbortController and AbortSignal', async () => { |
| 93 | + const { kernel, entries } = await setup({ |
| 94 | + globals: ['AbortController', 'AbortSignal'], |
| 95 | + }); |
| 96 | + |
| 97 | + await kernel.queueMessage(v1Root, 'testAbort', []); |
| 98 | + await waitUntilQuiescent(); |
| 99 | + |
| 100 | + const logs = extractTestLogs(entries, vatId); |
| 101 | + expect(logs).toContain('abort: before=false after=true'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('can use setTimeout and clearTimeout', async () => { |
| 105 | + const { kernel, entries } = await setup({ |
| 106 | + globals: ['setTimeout', 'clearTimeout'], |
| 107 | + }); |
| 108 | + |
| 109 | + await kernel.queueMessage(v1Root, 'testTimers', []); |
| 110 | + await waitUntilQuiescent(); |
| 111 | + |
| 112 | + const logs = extractTestLogs(entries, vatId); |
| 113 | + expect(logs).toContain('timer: fired'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('can use real Date (not tamed)', async () => { |
| 117 | + const { kernel, entries } = await setup({ globals: ['Date'] }); |
| 118 | + |
| 119 | + await kernel.queueMessage(v1Root, 'testDate', []); |
| 120 | + await waitUntilQuiescent(); |
| 121 | + |
| 122 | + const logs = extractTestLogs(entries, vatId); |
| 123 | + expect(logs).toContain('date: isReal=true'); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('host APIs are absent when not endowed', () => { |
| 127 | + // These are Web/host APIs that are NOT JS intrinsics — they should |
| 128 | + // be genuinely absent from a SES compartment unless explicitly endowed. |
| 129 | + it.each([ |
| 130 | + 'TextEncoder', |
| 131 | + 'TextDecoder', |
| 132 | + 'URL', |
| 133 | + 'URLSearchParams', |
| 134 | + 'atob', |
| 135 | + 'btoa', |
| 136 | + 'AbortController', |
| 137 | + 'AbortSignal', |
| 138 | + 'setTimeout', |
| 139 | + 'clearTimeout', |
| 140 | + ])('does not have %s without endowing it', async (name) => { |
| 141 | + // Launch with no globals at all |
| 142 | + const { kernel, entries } = await setup({ globals: [] }); |
| 143 | + |
| 144 | + await kernel.queueMessage(v1Root, 'checkGlobal', [name]); |
| 145 | + await waitUntilQuiescent(); |
| 146 | + |
| 147 | + const logs = extractTestLogs(entries, vatId); |
| 148 | + expect(logs).toContain(`checkGlobal: ${name}=false`); |
| 149 | + }); |
| 150 | + |
| 151 | + it('throws when calling tamed Date.now without endowing Date', async () => { |
| 152 | + const { kernel } = await setup({ globals: [] }); |
| 153 | + |
| 154 | + await expect(kernel.queueMessage(v1Root, 'testDate', [])).rejects.toThrow( |
| 155 | + 'secure mode', |
| 156 | + ); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('kernel-level allowedGlobalNames restriction', () => { |
| 161 | + it('throws when a vat requests a global excluded by the kernel', async () => { |
| 162 | + // Kernel only allows TextEncoder/TextDecoder — vat also requests URL |
| 163 | + await expect( |
| 164 | + setup({ |
| 165 | + globals: ['TextEncoder', 'TextDecoder', 'URL'], |
| 166 | + allowedGlobalNames: ['TextEncoder', 'TextDecoder'], |
| 167 | + }), |
| 168 | + ).rejects.toThrow('unknown global "URL"'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('initializes when all vat globals are within allowedGlobalNames', async () => { |
| 172 | + const { kernel, entries } = await setup({ |
| 173 | + globals: ['TextEncoder', 'TextDecoder'], |
| 174 | + allowedGlobalNames: ['TextEncoder', 'TextDecoder'], |
| 175 | + }); |
| 176 | + |
| 177 | + await kernel.queueMessage(v1Root, 'testTextCodec', []); |
| 178 | + await waitUntilQuiescent(); |
| 179 | + |
| 180 | + const logs = extractTestLogs(entries, vatId); |
| 181 | + expect(logs).toContain('textCodec: hello'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('allows all globals when allowedGlobalNames is omitted', async () => { |
| 185 | + const { kernel, entries } = await setup({ |
| 186 | + globals: ['URL', 'URLSearchParams'], |
| 187 | + }); |
| 188 | + |
| 189 | + await kernel.queueMessage(v1Root, 'testUrl', []); |
| 190 | + await waitUntilQuiescent(); |
| 191 | + |
| 192 | + const logs = extractTestLogs(entries, vatId); |
| 193 | + expect(logs).toContain('url: /path params: 10'); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments