@brud/kernel 0.0.0-stage → 0.0.1
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.
- package/LICENSE +201 -0
- package/README.md +63 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.js +13 -0
- package/dist/serviceModules/events.d.ts +1 -0
- package/dist/serviceModules/events.js +1 -0
- package/dist/serviceModules/exposedApiRegistry.d.ts +1 -0
- package/dist/serviceModules/exposedApiRegistry.js +1 -0
- package/dist/serviceModules/identityRegistry.d.ts +1 -0
- package/dist/serviceModules/identityRegistry.js +1 -0
- package/dist/serviceModules/interception.d.ts +1 -0
- package/dist/serviceModules/interception.js +1 -0
- package/dist/serviceModules/parser.d.ts +1 -0
- package/dist/serviceModules/parser.js +1 -0
- package/dist/serviceModules/router.d.ts +1 -0
- package/dist/serviceModules/router.js +1 -0
- package/dist/serviceModules/wasmCage.d.ts +1 -0
- package/dist/serviceModules/wasmCage.js +1 -0
- package/dist/services/Events/contracts/IEventsService.d.ts +64 -0
- package/dist/services/Events/contracts/IEventsService.js +1 -0
- package/dist/services/Events/contracts/errors.d.ts +31 -0
- package/dist/services/Events/contracts/errors.js +40 -0
- package/dist/services/Events/contracts/index.d.ts +3 -0
- package/dist/services/Events/contracts/index.js +3 -0
- package/dist/services/Events/contracts/tokens.d.ts +2 -0
- package/dist/services/Events/contracts/tokens.js +2 -0
- package/dist/services/Events/descriptor/EventsDescriptor.d.ts +9 -0
- package/dist/services/Events/descriptor/EventsDescriptor.js +56 -0
- package/dist/services/Events/descriptor/tokens.d.ts +1 -0
- package/dist/services/Events/descriptor/tokens.js +1 -0
- package/dist/services/Events/index.d.ts +4 -0
- package/dist/services/Events/index.js +4 -0
- package/dist/services/Events/orchestrator/EventsService.d.ts +51 -0
- package/dist/services/Events/orchestrator/EventsService.js +339 -0
- package/dist/services/Events/orchestrator/index.d.ts +2 -0
- package/dist/services/Events/orchestrator/index.js +2 -0
- package/dist/services/Events/orchestrator/limits.d.ts +22 -0
- package/dist/services/Events/orchestrator/limits.js +33 -0
- package/dist/services/Events/serviceModule.d.ts +2 -0
- package/dist/services/Events/serviceModule.js +5 -0
- package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.d.ts +44 -0
- package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.js +1 -0
- package/dist/services/ExposedApiRegistry/contracts/errors.d.ts +132 -0
- package/dist/services/ExposedApiRegistry/contracts/errors.js +195 -0
- package/dist/services/ExposedApiRegistry/contracts/index.d.ts +9 -0
- package/dist/services/ExposedApiRegistry/contracts/index.js +4 -0
- package/dist/services/ExposedApiRegistry/contracts/policy.d.ts +90 -0
- package/dist/services/ExposedApiRegistry/contracts/policy.js +169 -0
- package/dist/services/ExposedApiRegistry/contracts/registrant.d.ts +37 -0
- package/dist/services/ExposedApiRegistry/contracts/registrant.js +36 -0
- package/dist/services/ExposedApiRegistry/contracts/types.d.ts +112 -0
- package/dist/services/ExposedApiRegistry/contracts/types.js +73 -0
- package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.d.ts +44 -0
- package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.js +58 -0
- package/dist/services/ExposedApiRegistry/descriptor/tokens.d.ts +8 -0
- package/dist/services/ExposedApiRegistry/descriptor/tokens.js +8 -0
- package/dist/services/ExposedApiRegistry/index.d.ts +6 -0
- package/dist/services/ExposedApiRegistry/index.js +5 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.d.ts +61 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +407 -0
- package/dist/services/ExposedApiRegistry/orchestrator/index.d.ts +2 -0
- package/dist/services/ExposedApiRegistry/orchestrator/index.js +1 -0
- package/dist/services/ExposedApiRegistry/serviceModule.d.ts +10 -0
- package/dist/services/ExposedApiRegistry/serviceModule.js +20 -0
- package/dist/services/ExposedApiRegistry/storage/protection.d.ts +21 -0
- package/dist/services/ExposedApiRegistry/storage/protection.js +35 -0
- package/dist/services/ExposedApiRegistry/storage/scopeRecord.d.ts +45 -0
- package/dist/services/ExposedApiRegistry/storage/scopeRecord.js +178 -0
- package/dist/services/IdentityRegistry/contracts/errors.d.ts +88 -0
- package/dist/services/IdentityRegistry/contracts/errors.js +128 -0
- package/dist/services/IdentityRegistry/contracts/index.d.ts +5 -0
- package/dist/services/IdentityRegistry/contracts/index.js +3 -0
- package/dist/services/IdentityRegistry/contracts/types.d.ts +77 -0
- package/dist/services/IdentityRegistry/contracts/types.js +18 -0
- package/dist/services/IdentityRegistry/descriptor/descriptor.d.ts +36 -0
- package/dist/services/IdentityRegistry/descriptor/descriptor.js +44 -0
- package/dist/services/IdentityRegistry/descriptor/tokens.d.ts +8 -0
- package/dist/services/IdentityRegistry/descriptor/tokens.js +8 -0
- package/dist/services/IdentityRegistry/index.d.ts +11 -0
- package/dist/services/IdentityRegistry/index.js +6 -0
- package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.d.ts +47 -0
- package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.js +240 -0
- package/dist/services/IdentityRegistry/serviceModule.d.ts +10 -0
- package/dist/services/IdentityRegistry/serviceModule.js +24 -0
- package/dist/services/IdentityRegistry/storage/registryDocument.d.ts +55 -0
- package/dist/services/IdentityRegistry/storage/registryDocument.js +235 -0
- package/dist/services/Interception/contracts/IInterceptionService.d.ts +65 -0
- package/dist/services/Interception/contracts/IInterceptionService.js +1 -0
- package/dist/services/Interception/contracts/errors.d.ts +26 -0
- package/dist/services/Interception/contracts/errors.js +35 -0
- package/dist/services/Interception/contracts/index.d.ts +3 -0
- package/dist/services/Interception/contracts/index.js +3 -0
- package/dist/services/Interception/contracts/tokens.d.ts +2 -0
- package/dist/services/Interception/contracts/tokens.js +2 -0
- package/dist/services/Interception/descriptor/InterceptionDescriptor.d.ts +9 -0
- package/dist/services/Interception/descriptor/InterceptionDescriptor.js +49 -0
- package/dist/services/Interception/descriptor/tokens.d.ts +1 -0
- package/dist/services/Interception/descriptor/tokens.js +1 -0
- package/dist/services/Interception/index.d.ts +4 -0
- package/dist/services/Interception/index.js +4 -0
- package/dist/services/Interception/orchestrator/InterceptionService.d.ts +32 -0
- package/dist/services/Interception/orchestrator/InterceptionService.js +180 -0
- package/dist/services/Interception/orchestrator/index.d.ts +1 -0
- package/dist/services/Interception/orchestrator/index.js +1 -0
- package/dist/services/Interception/serviceModule.d.ts +2 -0
- package/dist/services/Interception/serviceModule.js +5 -0
- package/dist/services/Parser/contracts/IParserService.d.ts +21 -0
- package/dist/services/Parser/contracts/IParserService.js +1 -0
- package/dist/services/Parser/contracts/errors.d.ts +25 -0
- package/dist/services/Parser/contracts/errors.js +33 -0
- package/dist/services/Parser/contracts/index.d.ts +4 -0
- package/dist/services/Parser/contracts/index.js +1 -0
- package/dist/services/Parser/contracts/types.d.ts +42 -0
- package/dist/services/Parser/contracts/types.js +1 -0
- package/dist/services/Parser/descriptor/ParserDescriptor.d.ts +23 -0
- package/dist/services/Parser/descriptor/ParserDescriptor.js +50 -0
- package/dist/services/Parser/descriptor/tokens.d.ts +8 -0
- package/dist/services/Parser/descriptor/tokens.js +8 -0
- package/dist/services/Parser/index.d.ts +8 -0
- package/dist/services/Parser/index.js +8 -0
- package/dist/services/Parser/orchestrator/ParserService.d.ts +20 -0
- package/dist/services/Parser/orchestrator/ParserService.js +105 -0
- package/dist/services/Parser/orchestrator/index.d.ts +1 -0
- package/dist/services/Parser/orchestrator/index.js +1 -0
- package/dist/services/Parser/parser/indexSynthesis.d.ts +11 -0
- package/dist/services/Parser/parser/indexSynthesis.js +13 -0
- package/dist/services/Parser/parser/parameters.d.ts +14 -0
- package/dist/services/Parser/parser/parameters.js +89 -0
- package/dist/services/Parser/parser/scanner.d.ts +48 -0
- package/dist/services/Parser/parser/scanner.js +156 -0
- package/dist/services/Parser/parser/syntax.d.ts +30 -0
- package/dist/services/Parser/parser/syntax.js +34 -0
- package/dist/services/Parser/serviceModule.d.ts +2 -0
- package/dist/services/Parser/serviceModule.js +5 -0
- package/dist/services/Router/bootOptions.d.ts +2 -0
- package/dist/services/Router/bootOptions.js +2 -0
- package/dist/services/Router/contracts/IRouterService.d.ts +43 -0
- package/dist/services/Router/contracts/IRouterService.js +1 -0
- package/dist/services/Router/contracts/caller.d.ts +6 -0
- package/dist/services/Router/contracts/caller.js +6 -0
- package/dist/services/Router/contracts/errors.d.ts +56 -0
- package/dist/services/Router/contracts/errors.js +75 -0
- package/dist/services/Router/contracts/index.d.ts +8 -0
- package/dist/services/Router/contracts/index.js +4 -0
- package/dist/services/Router/contracts/kind.d.ts +7 -0
- package/dist/services/Router/contracts/kind.js +10 -0
- package/dist/services/Router/contracts/registrant.d.ts +23 -0
- package/dist/services/Router/contracts/registrant.js +13 -0
- package/dist/services/Router/contracts/types.d.ts +90 -0
- package/dist/services/Router/contracts/types.js +1 -0
- package/dist/services/Router/descriptor/RouterDescriptor.d.ts +32 -0
- package/dist/services/Router/descriptor/RouterDescriptor.js +92 -0
- package/dist/services/Router/descriptor/tokens.d.ts +9 -0
- package/dist/services/Router/descriptor/tokens.js +9 -0
- package/dist/services/Router/index.d.ts +4 -0
- package/dist/services/Router/index.js +4 -0
- package/dist/services/Router/orchestrator/RouterService.d.ts +74 -0
- package/dist/services/Router/orchestrator/RouterService.js +332 -0
- package/dist/services/Router/orchestrator/index.d.ts +2 -0
- package/dist/services/Router/orchestrator/index.js +1 -0
- package/dist/services/Router/serviceModule.d.ts +4 -0
- package/dist/services/Router/serviceModule.js +22 -0
- package/dist/services/WasmCage/index.d.ts +7 -0
- package/dist/services/WasmCage/index.js +4 -0
- package/dist/services/WasmCage/scripts/prepin.d.ts +1 -0
- package/dist/services/WasmCage/scripts/prepin.js +38 -0
- package/dist/services/WasmCage/serviceModule.d.ts +12 -0
- package/dist/services/WasmCage/serviceModule.js +18 -0
- package/dist/services/WasmCage/src/caged.d.ts +104 -0
- package/dist/services/WasmCage/src/caged.js +381 -0
- package/dist/services/WasmCage/src/descriptor.d.ts +47 -0
- package/dist/services/WasmCage/src/descriptor.js +56 -0
- package/dist/services/WasmCage/src/native.d.ts +29 -0
- package/dist/services/WasmCage/src/native.js +66 -0
- package/dist/services/WasmCage/src/nativeDigests.d.ts +9 -0
- package/dist/services/WasmCage/src/nativeDigests.js +15 -0
- package/dist/services/WasmCage/src/package.d.ts +36 -0
- package/dist/services/WasmCage/src/package.js +122 -0
- package/dist/services/shared/canonicalJson.d.ts +29 -0
- package/dist/services/shared/canonicalJson.js +93 -0
- package/dist/services/shared/documentStore.d.ts +16 -0
- package/dist/services/shared/documentStore.js +98 -0
- package/dist/src/bootstrap/bootKernelInternal.d.ts +18 -0
- package/dist/src/bootstrap/bootKernelInternal.js +420 -0
- package/dist/src/bootstrap/bootTypes.d.ts +89 -0
- package/dist/src/bootstrap/bootTypes.js +1 -0
- package/dist/src/bootstrap/config/index.d.ts +3 -0
- package/dist/src/bootstrap/config/index.js +2 -0
- package/dist/src/bootstrap/config/integrity.d.ts +34 -0
- package/dist/src/bootstrap/config/integrity.js +79 -0
- package/dist/src/bootstrap/config/parse.d.ts +15 -0
- package/dist/src/bootstrap/config/parse.js +81 -0
- package/dist/src/bootstrap/config/pathWriteGuard.d.ts +20 -0
- package/dist/src/bootstrap/config/pathWriteGuard.js +349 -0
- package/dist/src/bootstrap/config/types.d.ts +41 -0
- package/dist/src/bootstrap/config/types.js +15 -0
- package/dist/src/bootstrap/hardcore.d.ts +212 -0
- package/dist/src/bootstrap/hardcore.js +312 -0
- package/dist/src/bootstrap/hardcoreServices.d.ts +10 -0
- package/dist/src/bootstrap/hardcoreServices.js +258 -0
- package/dist/src/bootstrap/index.d.ts +8 -0
- package/dist/src/bootstrap/index.js +5 -0
- package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
- package/dist/src/bootstrap/packageRequirements.js +52 -0
- package/dist/src/contracts/accessBroker.d.ts +53 -0
- package/dist/src/contracts/accessBroker.js +1 -0
- package/dist/src/contracts/brudOperation.d.ts +20 -0
- package/dist/src/contracts/brudOperation.js +20 -0
- package/dist/src/contracts/callChain.d.ts +161 -0
- package/dist/src/contracts/callChain.js +19 -0
- package/dist/src/contracts/capability.d.ts +26 -0
- package/dist/src/contracts/capability.js +26 -0
- package/dist/src/contracts/container.d.ts +17 -0
- package/dist/src/contracts/container.js +8 -0
- package/dist/src/contracts/context.d.ts +19 -0
- package/dist/src/contracts/context.js +1 -0
- package/dist/src/contracts/contextContract.d.ts +129 -0
- package/dist/src/contracts/contextContract.js +285 -0
- package/dist/src/contracts/dispatchView.d.ts +21 -0
- package/dist/src/contracts/dispatchView.js +1 -0
- package/dist/src/contracts/handler.d.ts +28 -0
- package/dist/src/contracts/handler.js +1 -0
- package/dist/src/contracts/index.d.ts +25 -0
- package/dist/src/contracts/index.js +8 -0
- package/dist/src/contracts/kernel.d.ts +60 -0
- package/dist/src/contracts/kernel.js +1 -0
- package/dist/src/contracts/lifecycle.d.ts +59 -0
- package/dist/src/contracts/lifecycle.js +1 -0
- package/dist/src/contracts/manifest.d.ts +145 -0
- package/dist/src/contracts/manifest.js +295 -0
- package/dist/src/contracts/pipeline.d.ts +8 -0
- package/dist/src/contracts/pipeline.js +1 -0
- package/dist/src/contracts/policy.d.ts +75 -0
- package/dist/src/contracts/policy.js +77 -0
- package/dist/src/contracts/registry.d.ts +7 -0
- package/dist/src/contracts/registry.js +1 -0
- package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
- package/dist/src/contracts/serviceDescriptor.js +1 -0
- package/dist/src/contracts/serviceToken.d.ts +36 -0
- package/dist/src/contracts/serviceToken.js +16 -0
- package/dist/src/engine/BrudKernel.d.ts +118 -0
- package/dist/src/engine/BrudKernel.js +366 -0
- package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
- package/dist/src/engine/ExecutionPipeline.js +62 -0
- package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
- package/dist/src/engine/KernelAccessBroker.js +107 -0
- package/dist/src/engine/KernelRegistry.d.ts +14 -0
- package/dist/src/engine/KernelRegistry.js +33 -0
- package/dist/src/engine/OperationRegistry.d.ts +9 -0
- package/dist/src/engine/OperationRegistry.js +18 -0
- package/dist/src/engine/ServiceContainer.d.ts +13 -0
- package/dist/src/engine/ServiceContainer.js +53 -0
- package/dist/src/engine/callChain.d.ts +68 -0
- package/dist/src/engine/callChain.js +180 -0
- package/dist/src/engine/dag.d.ts +2 -0
- package/dist/src/engine/dag.js +34 -0
- package/dist/src/engine/errors.d.ts +26 -0
- package/dist/src/engine/errors.js +52 -0
- package/dist/src/engine/index.d.ts +11 -0
- package/dist/src/engine/index.js +8 -0
- package/dist/src/engine/state.d.ts +1 -0
- package/dist/src/engine/state.js +1 -0
- package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
- package/dist/src/validation/jsonSchema/compile.js +386 -0
- package/dist/src/validation/jsonSchema/index.d.ts +3 -0
- package/dist/src/validation/jsonSchema/index.js +2 -0
- package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
- package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
- package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
- package/dist/src/validation/jsonSchema/pattern.js +155 -0
- package/dist/src/validation/jsonSchema/types.d.ts +71 -0
- package/dist/src/validation/jsonSchema/types.js +15 -0
- package/package.json +59 -4
- package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createRouterDescriptor } from './descriptor/RouterDescriptor.js';
|
|
2
|
+
import { isRouterRegistrant } from './contracts/registrant.js';
|
|
3
|
+
const ROUTER_SLOT = 'brud.core.router.instance';
|
|
4
|
+
export function createService(context) {
|
|
5
|
+
return createRouterDescriptor({
|
|
6
|
+
onConstruct: (service) => {
|
|
7
|
+
context.shared[ROUTER_SLOT] = service;
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export function registerBootRegistrants(context, instances) {
|
|
12
|
+
const router = context.shared[ROUTER_SLOT];
|
|
13
|
+
if (router === undefined) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
for (const instance of instances) {
|
|
17
|
+
if (isRouterRegistrant(instance)) {
|
|
18
|
+
instance.registerWithRouter(router);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
router.closeRegistration();
|
|
22
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { WASM_CAGE_CONTRACT_METHODS, WASM_CAGE_CONTRACT_VERSION, WASM_CAGE_SERVICE_TIER, WasmCageToken, createWasmCageDescriptor, } from './src/descriptor.js';
|
|
2
|
+
export type { IWasmCage } from './src/descriptor.js';
|
|
3
|
+
export { CagedGuestError, DEFAULT_CAGED_LIMITS, createCagedParticipant, } from './src/caged.js';
|
|
4
|
+
export type { CagedParticipantLimits, CagedParticipantOptions } from './src/caged.js';
|
|
5
|
+
export { CageNativeError, nativePlatformKey } from './src/native.js';
|
|
6
|
+
export type { CageNativeErrorCode } from './src/native.js';
|
|
7
|
+
export { PARTICIPANT_PACKAGE_FORMAT, ParticipantPackageError, loadParticipantPackage } from './src/package.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { WASM_CAGE_CONTRACT_METHODS, WASM_CAGE_CONTRACT_VERSION, WASM_CAGE_SERVICE_TIER, WasmCageToken, createWasmCageDescriptor, } from './src/descriptor.js';
|
|
2
|
+
export { CagedGuestError, DEFAULT_CAGED_LIMITS, createCagedParticipant, } from './src/caged.js';
|
|
3
|
+
export { CageNativeError, nativePlatformKey } from './src/native.js';
|
|
4
|
+
export { PARTICIPANT_PACKAGE_FORMAT, ParticipantPackageError, loadParticipantPackage } from './src/package.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// The cage's pre-pin hook, run by packages/kernel/scripts/repin.ts before the
|
|
2
|
+
// hardcore pins are computed. Regenerates src/nativeDigests.ts from the
|
|
3
|
+
// binaries in native/; with --check, fails if the table is stale.
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
const cage = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
9
|
+
const nativeDir = join(cage, 'native');
|
|
10
|
+
const table = join(cage, 'src/nativeDigests.ts');
|
|
11
|
+
const entries = readdirSync(nativeDir)
|
|
12
|
+
.filter((name) => /^brud-wasm-cage\..+\.node$/u.test(name))
|
|
13
|
+
.sort()
|
|
14
|
+
.map((name) => {
|
|
15
|
+
const key = name.slice('brud-wasm-cage.'.length, -'.node'.length);
|
|
16
|
+
const digest = createHash('sha256').update(readFileSync(join(nativeDir, name))).digest('hex');
|
|
17
|
+
return ` ['${key}', '${digest}'],`;
|
|
18
|
+
});
|
|
19
|
+
const next = `/**
|
|
20
|
+
* SHA-256 digests of the cage's native binaries, as \`[platform key, digest]\`.
|
|
21
|
+
*
|
|
22
|
+
* Generated by \`services/WasmCage/scripts/prepin.ts\` (run through the kernel's
|
|
23
|
+
* \`npm run repin\`); do not edit by hand. The table is part of the cage's pinned
|
|
24
|
+
* package graph, so the hardcore content pins cover it; the loader refuses any
|
|
25
|
+
* binary whose digest is not listed here.
|
|
26
|
+
*/
|
|
27
|
+
export const WASM_CAGE_NATIVE_DIGESTS: readonly (readonly [string, string])[] = Object.freeze([
|
|
28
|
+
${entries.join('\n')}
|
|
29
|
+
]);
|
|
30
|
+
`;
|
|
31
|
+
if (readFileSync(table, 'utf8') !== next) {
|
|
32
|
+
if (process.argv.includes('--check')) {
|
|
33
|
+
console.error('native digests: stale');
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
writeFileSync(table, next, 'utf8');
|
|
37
|
+
console.log('native digests: updated');
|
|
38
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { KernelServiceModuleContext } from '../../src/bootstrap/bootTypes.js';
|
|
2
|
+
export { nativePlatformKey } from './src/native.js';
|
|
3
|
+
export { loadParticipantPackage } from './src/package.js';
|
|
4
|
+
/**
|
|
5
|
+
* Composes the WASM cage through the standard service-module contract.
|
|
6
|
+
*
|
|
7
|
+
* The module has no context fields of its own at this step: the cage's
|
|
8
|
+
* configuration (resource profile, the loader capability) lands in a later
|
|
9
|
+
* plan step. The module returns the descriptor the compiled hardcore list
|
|
10
|
+
* verifies and the kernel registers.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createService(_context: KernelServiceModuleContext): import("../../index.js").ServiceDescriptor<import("./src/descriptor.js").IWasmCage, import("./src/descriptor.js").IWasmCage>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createWasmCageDescriptor } from './src/descriptor.js';
|
|
2
|
+
// The native loader and its pinned digest table are part of the cage's package
|
|
3
|
+
// graph, so the hardcore content pins cover the digests the loader trusts.
|
|
4
|
+
export { nativePlatformKey } from './src/native.js';
|
|
5
|
+
// W-01: participants not declared official reach the kernel as packages, and
|
|
6
|
+
// this hardcore is the one that loads them (the kernel's loader asks for it).
|
|
7
|
+
export { loadParticipantPackage } from './src/package.js';
|
|
8
|
+
/**
|
|
9
|
+
* Composes the WASM cage through the standard service-module contract.
|
|
10
|
+
*
|
|
11
|
+
* The module has no context fields of its own at this step: the cage's
|
|
12
|
+
* configuration (resource profile, the loader capability) lands in a later
|
|
13
|
+
* plan step. The module returns the descriptor the compiled hardcore list
|
|
14
|
+
* verifies and the kernel registers.
|
|
15
|
+
*/
|
|
16
|
+
export function createService(_context) {
|
|
17
|
+
return createWasmCageDescriptor();
|
|
18
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { ParticipantManifest, ServiceDescriptor } from '../../../src/contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Caged participants: code running inside the WASM cage with full participant
|
|
4
|
+
* parity (Architect ruling I-07). The kernel already treats a caged participant
|
|
5
|
+
* like any other; this module is the host side of the cage's call *in*.
|
|
6
|
+
*
|
|
7
|
+
* ## Guest ABI
|
|
8
|
+
*
|
|
9
|
+
* A caged participant's module exports:
|
|
10
|
+
* - `memory` — its linear memory;
|
|
11
|
+
* - `brud_alloc(len: i32) -> i32` — returns a pointer to `len` writable bytes;
|
|
12
|
+
* - `brud_invoke(ptr: i32, len: i32) -> i64` — a call on one of its exposures;
|
|
13
|
+
* - `brud_intercept(ptr: i32, len: i32) -> i64` — an interception verdict
|
|
14
|
+
* (only when the manifest declares `interceptions`);
|
|
15
|
+
* - optionally `brud_start(ptr: i32, len: i32) -> i64` — called once when the
|
|
16
|
+
* participant starts running, with `{ kind: 'start', token }`; it answers
|
|
17
|
+
* `{ ok: true }` or `{ ok: false, error }` (a failure fails the start).
|
|
18
|
+
*
|
|
19
|
+
* It may import `brud.call(ptr: i32, len: i32) -> i64`, the routed call (its
|
|
20
|
+
* only call out): the request is `{ name, version?, method, args }`; the host
|
|
21
|
+
* resolves the exposure through the exposed-API registry as this participant,
|
|
22
|
+
* exactly as a native participant resolves, calls `method` with `args`, and
|
|
23
|
+
* answers `{ ok: true, value }` or `{ ok: false, error: { code, message } }`.
|
|
24
|
+
* A routed call is only possible while the host is calling the guest; a routed
|
|
25
|
+
* call back into the same instance is refused (re-entrancy fails closed).
|
|
26
|
+
*
|
|
27
|
+
* Routed calls may be async. The guest is suspended on a Wasmtime fiber while
|
|
28
|
+
* the host awaits, inside the original caller's call chain, then resumed with
|
|
29
|
+
* the result; the guest code stays synchronous. A caged exposure is sync when
|
|
30
|
+
* every routed call it made answered synchronously, and returns a Promise when
|
|
31
|
+
* one was awaited — exactly like native code. Interceptors stay synchronous:
|
|
32
|
+
* an awaited routed call from `brud_intercept` is answered with an error.
|
|
33
|
+
*
|
|
34
|
+
* The host writes a UTF-8 JSON request at the pointer `brud_alloc` returned and
|
|
35
|
+
* calls the export with `(ptr, len)`. The export returns `(ptr << 32) | len` of
|
|
36
|
+
* a UTF-8 JSON response in its memory.
|
|
37
|
+
*
|
|
38
|
+
* - `brud_invoke` request: `{ kind: 'invoke', exposure, method, args, caller }`
|
|
39
|
+
* where `caller` is `{ identity, callerClass, official, capabilities[], origin }`.
|
|
40
|
+
* Response: `{ ok: true, value }` or `{ ok: false, error: { code, message } }`.
|
|
41
|
+
* - `brud_intercept` request: the {@link InterceptedCall} as JSON. Response:
|
|
42
|
+
* an {@link InterceptionVerdict}.
|
|
43
|
+
*
|
|
44
|
+
* ## Security properties
|
|
45
|
+
*
|
|
46
|
+
* Plain values only (JSON in, JSON out; arguments are validated as JSON before
|
|
47
|
+
* they cross). Every call runs under a fresh fuel budget (its deadline); the
|
|
48
|
+
* memory limit is enforced at instantiation and on every growth; responses
|
|
49
|
+
* over 1 MiB are refused. A trap, an exhausted budget, a malformed response, or
|
|
50
|
+
* an invalid verdict fails closed: the call throws.
|
|
51
|
+
*
|
|
52
|
+
* The cage's outward surface is unchanged: `resolve` (through `brud.call`)
|
|
53
|
+
* stays a caged participant's only call out (W-09). Plain values only: a
|
|
54
|
+
* routed result that is not plain JSON (functions, handles, class instances,
|
|
55
|
+
* non-finite numbers) is refused. Each call into the guest has a wall-clock
|
|
56
|
+
* deadline (`deadlineMs`): once it passes, every routed call is answered with
|
|
57
|
+
* `WASM_CAGE_ROUTED_TIMEOUT`, and fuel bounds the guest's remaining work. A
|
|
58
|
+
* routed call may opt in to cancellation with `"abortable": true`: its last
|
|
59
|
+
* argument must be a plain options object, and the host adds the call's
|
|
60
|
+
* `AbortSignal` to it as `signal`, aborted when the deadline passes.
|
|
61
|
+
*
|
|
62
|
+
* Calls into a busy instance wait their turn (FIFO, at most 64 queued; the
|
|
63
|
+
* deadline counts from the moment a call is queued). A guest calling back into
|
|
64
|
+
* its own instance is refused, since waiting for itself would deadlock;
|
|
65
|
+
* interceptors cannot wait, so a busy instance fails them closed.
|
|
66
|
+
*/
|
|
67
|
+
/** Resource limits for one caged participant. */
|
|
68
|
+
export interface CagedParticipantLimits {
|
|
69
|
+
/** Fuel per call: the call's deadline. */
|
|
70
|
+
readonly fuel: number;
|
|
71
|
+
/** Maximum linear-memory size in bytes. */
|
|
72
|
+
readonly maxMemoryBytes: number;
|
|
73
|
+
/** Wall-clock deadline per call into the guest, in milliseconds (default 30 s). */
|
|
74
|
+
readonly deadlineMs?: number;
|
|
75
|
+
}
|
|
76
|
+
/** Options for {@link createCagedParticipant}. */
|
|
77
|
+
export interface CagedParticipantOptions {
|
|
78
|
+
/** The participant's service token id. */
|
|
79
|
+
readonly token: string;
|
|
80
|
+
/** The participant's manifest, declared exactly as a native participant's. */
|
|
81
|
+
readonly manifest: ParticipantManifest;
|
|
82
|
+
/** The guest module: WASM binary or WAT text bytes. */
|
|
83
|
+
readonly wasm: Uint8Array;
|
|
84
|
+
/** Resource limits. */
|
|
85
|
+
readonly limits?: CagedParticipantLimits;
|
|
86
|
+
}
|
|
87
|
+
/** Default caged-participant limits. */
|
|
88
|
+
export declare const DEFAULT_CAGED_LIMITS: CagedParticipantLimits;
|
|
89
|
+
/** A caged guest failed: a trap, an exhausted budget, or a refused response. */
|
|
90
|
+
export declare class CagedGuestError extends Error {
|
|
91
|
+
readonly code: "WASM_CAGE_GUEST_FAILED";
|
|
92
|
+
/** The guest-reported error code, when the guest reported one. */
|
|
93
|
+
readonly guestCode: string | undefined;
|
|
94
|
+
constructor(message: string, guestCode?: string, options?: ErrorOptions);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Builds the descriptor of a caged participant: a participant whose code runs
|
|
98
|
+
* inside the WASM cage and which may be a giver (exposures and interception
|
|
99
|
+
* points) and an interceptor, exactly like a native participant.
|
|
100
|
+
*
|
|
101
|
+
* @param options - Token, manifest, guest module, and limits.
|
|
102
|
+
* @returns The participant's descriptor.
|
|
103
|
+
*/
|
|
104
|
+
export declare function createCagedParticipant(options: CagedParticipantOptions): ServiceDescriptor<object>;
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { createServiceToken } from '../../../src/contracts/index.js';
|
|
3
|
+
import { compileJsonSchema } from '../../../src/validation/jsonSchema/index.js';
|
|
4
|
+
import { ExposedApiRegistryToken, withExposureBindings } from '../../ExposedApiRegistry/index.js';
|
|
5
|
+
import { InterceptionBlockedError } from '../../Interception/index.js';
|
|
6
|
+
import { loadVerifiedNative } from './native.js';
|
|
7
|
+
/** Default caged-participant limits. */
|
|
8
|
+
export const DEFAULT_CAGED_LIMITS = Object.freeze({
|
|
9
|
+
fuel: 10_000_000,
|
|
10
|
+
maxMemoryBytes: 16 * 1024 * 1024,
|
|
11
|
+
deadlineMs: 30_000,
|
|
12
|
+
});
|
|
13
|
+
/** A caged guest failed: a trap, an exhausted budget, or a refused response. */
|
|
14
|
+
export class CagedGuestError extends Error {
|
|
15
|
+
code = 'WASM_CAGE_GUEST_FAILED';
|
|
16
|
+
/** The guest-reported error code, when the guest reported one. */
|
|
17
|
+
guestCode;
|
|
18
|
+
constructor(message, guestCode, options) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
this.name = 'CagedGuestError';
|
|
21
|
+
this.guestCode = guestCode;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let engine;
|
|
25
|
+
function loadNative() {
|
|
26
|
+
const cage = loadVerifiedNative();
|
|
27
|
+
engine ??= new cage.CageEngine();
|
|
28
|
+
return { cage, engine };
|
|
29
|
+
}
|
|
30
|
+
const argsSchema = (() => {
|
|
31
|
+
const compiled = compileJsonSchema({ type: 'array' });
|
|
32
|
+
if (!compiled.ok) {
|
|
33
|
+
throw new Error('caged participant: argument schema failed to compile');
|
|
34
|
+
}
|
|
35
|
+
return compiled.schema;
|
|
36
|
+
})();
|
|
37
|
+
function deepFreeze(value) {
|
|
38
|
+
if (value !== null && typeof value === 'object') {
|
|
39
|
+
for (const child of Object.values(value)) {
|
|
40
|
+
deepFreeze(child);
|
|
41
|
+
}
|
|
42
|
+
Object.freeze(value);
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
function guestFailure(exportName, error) {
|
|
47
|
+
return error instanceof CagedGuestError
|
|
48
|
+
? error
|
|
49
|
+
: new CagedGuestError(`caged guest "${exportName}" failed: ${error.message}`, undefined, { cause: error });
|
|
50
|
+
}
|
|
51
|
+
function step(exportName, run) {
|
|
52
|
+
try {
|
|
53
|
+
return run();
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw guestFailure(exportName, error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function finish(exportName, final) {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(final.response ?? '');
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
throw new CagedGuestError(`caged guest "${exportName}" returned malformed JSON`, undefined, { cause: error });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const timeoutResponse = () => routedError('WASM_CAGE_ROUTED_TIMEOUT', 'the call into the cage passed its deadline');
|
|
68
|
+
/** Resolves with the routed answer, or with a timeout answer at the deadline. */
|
|
69
|
+
async function withDeadline(pending, deadline, controller) {
|
|
70
|
+
let timer;
|
|
71
|
+
const expired = new Promise((resolve) => {
|
|
72
|
+
timer = setTimeout(() => {
|
|
73
|
+
controller.abort(new CagedGuestError('the call into the cage passed its deadline', 'WASM_CAGE_ROUTED_TIMEOUT'));
|
|
74
|
+
resolve(timeoutResponse());
|
|
75
|
+
}, Math.max(0, deadline - Date.now()));
|
|
76
|
+
});
|
|
77
|
+
try {
|
|
78
|
+
return await Promise.race([pending, expired]);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Drives one call into the guest. Routed requests answered synchronously are
|
|
86
|
+
* resumed at once, so a guest that awaits nothing completes synchronously;
|
|
87
|
+
* the first awaited answer switches the rest of the call to async.
|
|
88
|
+
*/
|
|
89
|
+
function callGuest(instance, exportName, request, routed, deadline, allowAsync) {
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
let current = step(exportName, () => instance.callJson(exportName, JSON.stringify(request)));
|
|
92
|
+
while (!current.done) {
|
|
93
|
+
const answer = Date.now() >= deadline ? timeoutResponse() : routed(current.request ?? '', controller.signal);
|
|
94
|
+
if (typeof answer !== 'string') {
|
|
95
|
+
if (!allowAsync) {
|
|
96
|
+
answer.catch(() => undefined);
|
|
97
|
+
current = step(exportName, () => instance.resume(routedError('WASM_CAGE_ROUTED_ASYNC', 'this guest entry cannot await a routed call')));
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
return (async () => {
|
|
101
|
+
let next = answer;
|
|
102
|
+
for (;;) {
|
|
103
|
+
const resolved = typeof next === 'string' ? next : await withDeadline(next, deadline, controller);
|
|
104
|
+
current = step(exportName, () => instance.resume(resolved));
|
|
105
|
+
if (current.done) {
|
|
106
|
+
return finish(exportName, current);
|
|
107
|
+
}
|
|
108
|
+
next = Date.now() >= deadline ? timeoutResponse() : routed(current.request ?? '', controller.signal);
|
|
109
|
+
}
|
|
110
|
+
})();
|
|
111
|
+
}
|
|
112
|
+
current = step(exportName, () => instance.resume(answer));
|
|
113
|
+
}
|
|
114
|
+
return finish(exportName, current);
|
|
115
|
+
}
|
|
116
|
+
const MAX_PLAIN_DEPTH = 64;
|
|
117
|
+
/** Plain JSON only: null, booleans, finite numbers, strings, arrays and plain objects. */
|
|
118
|
+
function isPlainJson(value, depth = 0) {
|
|
119
|
+
if (depth > MAX_PLAIN_DEPTH) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
if (value === null || typeof value === 'boolean' || typeof value === 'string') {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
if (typeof value === 'number') {
|
|
126
|
+
return Number.isFinite(value);
|
|
127
|
+
}
|
|
128
|
+
if (Array.isArray(value)) {
|
|
129
|
+
return value.every((item) => isPlainJson(item, depth + 1));
|
|
130
|
+
}
|
|
131
|
+
if (typeof value === 'object') {
|
|
132
|
+
const prototype = Object.getPrototypeOf(value);
|
|
133
|
+
return (prototype === Object.prototype || prototype === null)
|
|
134
|
+
&& Object.values(value).every((item) => isPlainJson(item, depth + 1));
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
function plainCaller(caller) {
|
|
139
|
+
return {
|
|
140
|
+
identity: caller.identity,
|
|
141
|
+
callerClass: caller.callerClass,
|
|
142
|
+
official: caller.official,
|
|
143
|
+
capabilities: [...caller.capabilities].sort(),
|
|
144
|
+
origin: caller.origin,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function isVerdict(value) {
|
|
148
|
+
if (value === null || typeof value !== 'object') {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const verdict = value;
|
|
152
|
+
return verdict.verdict === 'allow'
|
|
153
|
+
|| (verdict.verdict === 'block' && typeof verdict.reason === 'string')
|
|
154
|
+
|| (verdict.verdict === 'modify' && Array.isArray(verdict.args));
|
|
155
|
+
}
|
|
156
|
+
/** A routed-call failure, as plain values. */
|
|
157
|
+
function routedError(code, message) {
|
|
158
|
+
return JSON.stringify({ ok: false, error: { code, message } });
|
|
159
|
+
}
|
|
160
|
+
/** A routed result as a plain response. */
|
|
161
|
+
function plainResponse(value) {
|
|
162
|
+
const plain = value === undefined ? null : value;
|
|
163
|
+
return isPlainJson(plain)
|
|
164
|
+
? JSON.stringify({ ok: true, value: plain })
|
|
165
|
+
: routedError('WASM_CAGE_ROUTED_NOT_PLAIN', 'the routed result is not plain JSON');
|
|
166
|
+
}
|
|
167
|
+
function failureResponse(error) {
|
|
168
|
+
const code = error?.code;
|
|
169
|
+
return routedError(typeof code === 'string' ? code : 'WASM_CAGE_ROUTED_FAILED', String(error?.message ?? error));
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Serves one routed call: resolve as the caged participant, call the method,
|
|
173
|
+
* answer with plain values. Never throws; every failure is a plain error
|
|
174
|
+
* response. Returns a Promise when the called method returned one.
|
|
175
|
+
*/
|
|
176
|
+
function serveRoutedCall(registry, raw, signal) {
|
|
177
|
+
try {
|
|
178
|
+
const request = JSON.parse(raw);
|
|
179
|
+
if (typeof request.name !== 'string' || typeof request.method !== 'string' || !Array.isArray(request.args)
|
|
180
|
+
|| (request.version !== undefined && typeof request.version !== 'number' && typeof request.version !== 'string')) {
|
|
181
|
+
return routedError('WASM_CAGE_ROUTED_INVALID', 'a routed call needs a string name, a string method and an args array');
|
|
182
|
+
}
|
|
183
|
+
const options = request.version === undefined ? undefined : { version: request.version };
|
|
184
|
+
const handle = registry().resolve(request.name, options);
|
|
185
|
+
if (handle === null) {
|
|
186
|
+
return routedError('WASM_CAGE_ROUTED_NOT_FOUND', `exposure "${request.name}" did not resolve`);
|
|
187
|
+
}
|
|
188
|
+
const method = handle[request.method];
|
|
189
|
+
if (typeof method !== 'function') {
|
|
190
|
+
return routedError('WASM_CAGE_ROUTED_NOT_FOUND', `exposure "${request.name}" has no method "${request.method}"`);
|
|
191
|
+
}
|
|
192
|
+
let args = request.args;
|
|
193
|
+
if (request.abortable === true) {
|
|
194
|
+
const last = args[args.length - 1];
|
|
195
|
+
if (last === null || typeof last !== 'object' || Array.isArray(last)) {
|
|
196
|
+
return routedError('WASM_CAGE_ROUTED_INVALID', 'an abortable routed call needs a plain options object as its last argument');
|
|
197
|
+
}
|
|
198
|
+
args = [...args.slice(0, -1), { ...last, signal }];
|
|
199
|
+
}
|
|
200
|
+
const value = method.apply(handle, args);
|
|
201
|
+
if (value !== null && typeof value === 'object' && typeof value.then === 'function') {
|
|
202
|
+
return Promise.resolve(value).then(plainResponse, failureResponse);
|
|
203
|
+
}
|
|
204
|
+
return plainResponse(value);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
return failureResponse(error);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** The instances whose guest call the current async context is inside. */
|
|
211
|
+
const activeInstances = new AsyncLocalStorage();
|
|
212
|
+
const MAX_QUEUED_CALLS = 64;
|
|
213
|
+
/**
|
|
214
|
+
* Serializes calls into one guest instance: one runs at a time, later ones
|
|
215
|
+
* wait (FIFO). A call made from inside this instance's own call is refused.
|
|
216
|
+
*/
|
|
217
|
+
function createScheduler(instance, routed, deadlineMs) {
|
|
218
|
+
const self = {};
|
|
219
|
+
const waiting = [];
|
|
220
|
+
let running = false;
|
|
221
|
+
const release = () => {
|
|
222
|
+
running = false;
|
|
223
|
+
waiting.shift()?.();
|
|
224
|
+
};
|
|
225
|
+
const start = (exportName, request, deadline, allowAsync) => {
|
|
226
|
+
if (Date.now() >= deadline) {
|
|
227
|
+
throw new CagedGuestError(`caged guest "${exportName}" waited past its deadline`, 'WASM_CAGE_QUEUE_TIMEOUT');
|
|
228
|
+
}
|
|
229
|
+
running = true;
|
|
230
|
+
const inside = new Set(activeInstances.getStore() ?? []).add(self);
|
|
231
|
+
let outcome;
|
|
232
|
+
try {
|
|
233
|
+
outcome = activeInstances.run(inside, () => callGuest(instance, exportName, request, routed, deadline, allowAsync));
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
release();
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
if (outcome instanceof Promise) {
|
|
240
|
+
return outcome.finally(release);
|
|
241
|
+
}
|
|
242
|
+
release();
|
|
243
|
+
return outcome;
|
|
244
|
+
};
|
|
245
|
+
return (exportName, request, allowAsync) => {
|
|
246
|
+
if (activeInstances.getStore()?.has(self) === true) {
|
|
247
|
+
throw new CagedGuestError(`caged guest "${exportName}" failed: re-entrant call into a busy cage instance`);
|
|
248
|
+
}
|
|
249
|
+
const deadline = Date.now() + deadlineMs;
|
|
250
|
+
if (!running && waiting.length === 0) {
|
|
251
|
+
return start(exportName, request, deadline, allowAsync);
|
|
252
|
+
}
|
|
253
|
+
if (!allowAsync || waiting.length >= MAX_QUEUED_CALLS) {
|
|
254
|
+
throw new CagedGuestError(`caged guest "${exportName}" failed: the cage instance is busy`);
|
|
255
|
+
}
|
|
256
|
+
return new Promise((resolve, reject) => {
|
|
257
|
+
waiting.push(() => {
|
|
258
|
+
try {
|
|
259
|
+
Promise.resolve(start(exportName, request, deadline, allowAsync)).then(resolve, reject);
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
reject(error);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function majorOf(version) {
|
|
269
|
+
return version === undefined ? 1 : Number(String(version).split('.')[0]);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Builds the descriptor of a caged participant: a participant whose code runs
|
|
273
|
+
* inside the WASM cage and which may be a giver (exposures and interception
|
|
274
|
+
* points) and an interceptor, exactly like a native participant.
|
|
275
|
+
*
|
|
276
|
+
* @param options - Token, manifest, guest module, and limits.
|
|
277
|
+
* @returns The participant's descriptor.
|
|
278
|
+
*/
|
|
279
|
+
export function createCagedParticipant(options) {
|
|
280
|
+
const token = createServiceToken(options.token, `Caged participant "${options.token}"`);
|
|
281
|
+
const limits = options.limits ?? DEFAULT_CAGED_LIMITS;
|
|
282
|
+
const deadlineMs = limits.deadlineMs ?? DEFAULT_CAGED_LIMITS.deadlineMs ?? 30_000;
|
|
283
|
+
const manifest = options.manifest;
|
|
284
|
+
const points = new Set((manifest.interceptionPoints ?? []).map((point) => point.exposure));
|
|
285
|
+
return {
|
|
286
|
+
token,
|
|
287
|
+
tier: 'userland',
|
|
288
|
+
dependencies: [],
|
|
289
|
+
manifest,
|
|
290
|
+
factory: (_context, broker) => {
|
|
291
|
+
const { cage, engine: cageEngine } = loadNative();
|
|
292
|
+
const module = cage.CageModule.compile(cageEngine, Buffer.from(options.wasm));
|
|
293
|
+
const guest = cage.CageInstance.instantiate(module, [{ module: 'brud', name: 'call', kind: 'RoutedCall' }], {
|
|
294
|
+
fuel: limits.fuel,
|
|
295
|
+
maxMemoryBytes: limits.maxMemoryBytes,
|
|
296
|
+
timeoutMs: 0,
|
|
297
|
+
});
|
|
298
|
+
let evaluator = null;
|
|
299
|
+
let registryAccess;
|
|
300
|
+
const registry = () => {
|
|
301
|
+
registryAccess ??= broker.requestAccess({ callerId: token.id, tier: 'userland', options: {} }, ExposedApiRegistryToken);
|
|
302
|
+
return registryAccess;
|
|
303
|
+
};
|
|
304
|
+
const routed = (request, signal) => serveRoutedCall(registry, request, signal);
|
|
305
|
+
const run = createScheduler(guest, routed, deadlineMs);
|
|
306
|
+
const invoke = (exposure, method, args, caller) => {
|
|
307
|
+
const validation = argsSchema.validate(args);
|
|
308
|
+
if (!validation.valid) {
|
|
309
|
+
throw new CagedGuestError(`caged call "${exposure}.${method}": arguments are not plain values`);
|
|
310
|
+
}
|
|
311
|
+
let callArgs = args;
|
|
312
|
+
const name = exposure.slice(0, exposure.lastIndexOf('@'));
|
|
313
|
+
if (points.has(name)) {
|
|
314
|
+
if (evaluator === null) {
|
|
315
|
+
throw new InterceptionBlockedError(exposure, 'interception unavailable');
|
|
316
|
+
}
|
|
317
|
+
const outcome = evaluator.evaluate(exposure, method, callArgs, { caller });
|
|
318
|
+
if (!outcome.allowed) {
|
|
319
|
+
throw new InterceptionBlockedError(exposure, outcome.reason, outcome.interceptor);
|
|
320
|
+
}
|
|
321
|
+
callArgs = outcome.args;
|
|
322
|
+
}
|
|
323
|
+
const settle = (raw) => {
|
|
324
|
+
const response = raw;
|
|
325
|
+
if (response?.ok === true) {
|
|
326
|
+
return deepFreeze(response.value);
|
|
327
|
+
}
|
|
328
|
+
const code = typeof response?.error?.code === 'string' ? response.error.code : undefined;
|
|
329
|
+
const message = typeof response?.error?.message === 'string' ? response.error.message : 'guest refused the call';
|
|
330
|
+
throw new CagedGuestError(`caged call "${exposure}.${method}" failed: ${message}`, code);
|
|
331
|
+
};
|
|
332
|
+
const outcome = run('brud_invoke', {
|
|
333
|
+
kind: 'invoke',
|
|
334
|
+
exposure,
|
|
335
|
+
method,
|
|
336
|
+
args: callArgs,
|
|
337
|
+
caller: plainCaller(caller),
|
|
338
|
+
}, true);
|
|
339
|
+
return outcome instanceof Promise ? outcome.then(settle) : settle(outcome);
|
|
340
|
+
};
|
|
341
|
+
const bindings = {};
|
|
342
|
+
for (const declaration of manifest.exposures ?? []) {
|
|
343
|
+
const exposure = `${declaration.name}@${String(majorOf(declaration.version))}`;
|
|
344
|
+
bindings[exposure] = {
|
|
345
|
+
projection: (_service, caller) => new Proxy(Object.freeze({}), {
|
|
346
|
+
get: (_target, method) => (typeof method !== 'string' || method === 'then' || method === 'toJSON'
|
|
347
|
+
? undefined
|
|
348
|
+
: (...args) => invoke(exposure, method, args, caller)),
|
|
349
|
+
}),
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const instance = {
|
|
353
|
+
async onRunning() {
|
|
354
|
+
if (points.size > 0) {
|
|
355
|
+
evaluator = registry().resolve('interception.evaluate');
|
|
356
|
+
}
|
|
357
|
+
if (guest.hasExport('brud_start')) {
|
|
358
|
+
const started = await run('brud_start', { kind: 'start', token: token.id }, true);
|
|
359
|
+
if (started?.ok !== true) {
|
|
360
|
+
const message = typeof started?.error?.message === 'string' ? started.error.message : 'guest refused to start';
|
|
361
|
+
throw new CagedGuestError(`caged participant "${token.id}" failed to start: ${message}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
if ((manifest.interceptions ?? []).length > 0) {
|
|
367
|
+
Object.defineProperty(instance, 'intercept', {
|
|
368
|
+
value: (call) => {
|
|
369
|
+
const verdict = run('brud_intercept', call, false);
|
|
370
|
+
if (!isVerdict(verdict)) {
|
|
371
|
+
throw new CagedGuestError('invalid verdict');
|
|
372
|
+
}
|
|
373
|
+
return deepFreeze(verdict);
|
|
374
|
+
},
|
|
375
|
+
enumerable: false,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return withExposureBindings(instance, bindings);
|
|
379
|
+
},
|
|
380
|
+
};
|
|
381
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { KernelCallAuthority, ServiceDescriptor, ServiceTier } from '../../../src/contracts/index.js';
|
|
2
|
+
/** Tier assigned to the cage's descriptor. */
|
|
3
|
+
export declare const WASM_CAGE_SERVICE_TIER: ServiceTier;
|
|
4
|
+
/** Contract version declared for the cage's interface. */
|
|
5
|
+
export declare const WASM_CAGE_CONTRACT_VERSION = "1.0.0";
|
|
6
|
+
/**
|
|
7
|
+
* Methods declared for the cage's interface.
|
|
8
|
+
*
|
|
9
|
+
* The cage's only participant-facing surface is the exposed-API registry's
|
|
10
|
+
* `resolve` (design record W-02, W-09). The cage is not a second giver; it is
|
|
11
|
+
* the mechanism that carries a caged participant's call to the registry.
|
|
12
|
+
*/
|
|
13
|
+
export declare const WASM_CAGE_CONTRACT_METHODS: readonly ["resolve"];
|
|
14
|
+
/** Public interface the cage promises to the kernel. */
|
|
15
|
+
export interface IWasmCage {
|
|
16
|
+
/**
|
|
17
|
+
* Binds the kernel's call authority.
|
|
18
|
+
*
|
|
19
|
+
* Called once from the cage's hardcore boot hook, with the authority the
|
|
20
|
+
* kernel hands that hook. Before it is called, `resolve` returns `null`
|
|
21
|
+
* rather than running without a chain.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The kernel's call authority.
|
|
24
|
+
*/
|
|
25
|
+
bindAuthority(value: KernelCallAuthority): void;
|
|
26
|
+
/**
|
|
27
|
+
* Runs one caged participant's `resolve` against the exposed-API registry.
|
|
28
|
+
*
|
|
29
|
+
* The cage starts a `runtime`-origin chain with the caged participant's
|
|
30
|
+
* recorded caller (W-06), so the frame carries the participant's
|
|
31
|
+
* kernel-issued identity and its parent relation (W-07). The cage does not
|
|
32
|
+
* decide policy: the registry evaluates the giver's Layer 1 policy. The cage
|
|
33
|
+
* is the door, and `resolve` is the only method on its surface (W-02, W-09).
|
|
34
|
+
*
|
|
35
|
+
* @param participantIdentity - The caged participant's kernel-issued token.
|
|
36
|
+
* @param name - The exposure to resolve.
|
|
37
|
+
* @param options - Version and expected owner identity.
|
|
38
|
+
* @returns The resolved handle, or `null`.
|
|
39
|
+
*/
|
|
40
|
+
resolve<T = unknown>(participantIdentity: string, name: string, options?: {
|
|
41
|
+
readonly version?: number | string;
|
|
42
|
+
readonly expectedOwner?: string;
|
|
43
|
+
}): T | null;
|
|
44
|
+
}
|
|
45
|
+
/** Token identifying the cage. */
|
|
46
|
+
export declare const WasmCageToken: import("../../../src/contracts/serviceToken.js").ServiceToken<IWasmCage>;
|
|
47
|
+
export declare function createWasmCageDescriptor(): ServiceDescriptor<IWasmCage>;
|