dsh-dashcam 0.0.0-stage → 2.0.0
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 +21 -0
- package/README.md +624 -2
- package/README.zh-CN.md +835 -0
- package/SECURITY.md +28 -0
- package/bin/dsh-dashcam.js +574 -0
- package/cordis.patch.yml +5 -0
- package/docs/COMPLIANCE.md +112 -0
- package/docs/ROLLOUT.md +150 -0
- package/docs/SPEC.md +441 -0
- package/docs/ghost-in-the-llm/README.md +21 -0
- package/docs/ghost-in-the-llm/dashcam-ingress-audit-design.md +223 -0
- package/docs/ghost-in-the-llm/dashcam-ingress-record-plan.md +418 -0
- package/docs/ghost-in-the-llm/ghost-message-forensics.md +214 -0
- package/docs/ghost-in-the-llm/ghost-message-third-party-review.md +174 -0
- package/docs/multiprocess.md +292 -0
- package/dsh-dashcam-host/README.md +177 -0
- package/dsh-dashcam-host/README.zh-CN.md +181 -0
- package/dsh-dashcam-host/lib/client.js +1206 -0
- package/dsh-dashcam-host/lib/index.js +971 -0
- package/dsh-dashcam-host/lib/ingress.js +218 -0
- package/dsh-dashcam-host/lib/panel.js +487 -0
- package/dsh-dashcam-host/package.json +29 -0
- package/examples/child-plugin.js +25 -0
- package/examples/compliant-plugin/index.js +30 -0
- package/examples/compliant-plugin/manifest.json +47 -0
- package/examples/demo-policy.json +33 -0
- package/examples/negative-plugin/index.js +20 -0
- package/examples/negative-plugin/manifest.json +36 -0
- package/package.json +73 -4
- package/src/autopsy/activation.js +90 -0
- package/src/autopsy/autopsy.js +222 -0
- package/src/contract/capabilities.js +437 -0
- package/src/contract/charter.js +230 -0
- package/src/contract/manifest.js +162 -0
- package/src/contract/two-key.js +89 -0
- package/src/errors.js +34 -0
- package/src/gates/index.js +289 -0
- package/src/gates/loadability.js +87 -0
- package/src/gates/report.js +78 -0
- package/src/gates/safety.js +194 -0
- package/src/gates/structure.js +161 -0
- package/src/governance/behavior.js +231 -0
- package/src/governance/blacklist.js +66 -0
- package/src/governance/metrics.js +249 -0
- package/src/governance/rollback.js +66 -0
- package/src/governance/rollout.js +88 -0
- package/src/host/child-host.js +216 -0
- package/src/host/local-host.js +441 -0
- package/src/index.js +261 -0
- package/src/policy.js +239 -0
- package/src/portrait/portrait.js +129 -0
- package/src/runtime/bridge.js +1281 -0
- package/src/runtime/derive.js +208 -0
- package/src/runtime/facade.js +40 -0
- package/src/runtime/guards.js +583 -0
- package/src/runtime/handles/fs.js +193 -0
- package/src/runtime/handles/index.js +30 -0
- package/src/runtime/handles/inject.js +129 -0
- package/src/runtime/handles/net.js +177 -0
- package/src/runtime/handles/proc.js +78 -0
- package/src/runtime/ingress.js +717 -0
- package/src/runtime/ipc/child.js +88 -0
- package/src/runtime/ipc/protocol.js +127 -0
- package/src/runtime/ledger.js +582 -0
- package/src/runtime/one-seal.js +122 -0
- package/src/runtime/redirects.js +102 -0
- package/src/signing/bundle.js +290 -0
- package/src/signing/trust.js +90 -0
- package/src/util/paths.js +187 -0
- package/src/util/stable.js +25 -0
- package/test/attack-surface.test.js +774 -0
- package/test/audit.test.js +636 -0
- package/test/autopsy.test.js +104 -0
- package/test/batch-guard.test.js +81 -0
- package/test/behavior.test.js +132 -0
- package/test/bypass.test.js +231 -0
- package/test/child-host.test.js +127 -0
- package/test/child.test.js +183 -0
- package/test/contract.test.js +157 -0
- package/test/gates.test.js +275 -0
- package/test/glob.test.js +83 -0
- package/test/governance.test.js +102 -0
- package/test/handle.test.js +132 -0
- package/test/helpers/fixtures.js +124 -0
- package/test/ingress.test.js +416 -0
- package/test/invariants.test.js +314 -0
- package/test/ledger-mac.test.js +134 -0
- package/test/mac-custody.test.js +86 -0
- package/test/mandate.test.js +129 -0
- package/test/metrics-batch.test.js +234 -0
- package/test/metrics-telemetry.test.js +217 -0
- package/test/net-proc-handle.test.js +183 -0
- package/test/observe.test.js +169 -0
- package/test/options.test.js +174 -0
- package/test/portrait-labels.test.js +72 -0
- package/test/portrait.test.js +65 -0
- package/test/protocol.test.js +105 -0
- package/test/quick-wins.test.js +245 -0
- package/test/review-fixes.test.js +260 -0
- package/test/review2-fixes.test.js +191 -0
- package/test/review3-fixes.test.js +226 -0
- package/test/review4-fixes.test.js +486 -0
- package/test/review5-fixes.test.js +301 -0
- package/test/review6-fixes.test.js +311 -0
- package/test/runtime.test.js +185 -0
- package/test/sandbox.test.js +236 -0
- package/test/signatures.test.js +170 -0
- package/test/symlink.test.js +288 -0
- package/test/three-lens-fixes.test.js +397 -0
- package/test/worker.test.js +73 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const { tempDir, createPlugin, signPlugin, makeKeys, cleanup } = require('./helpers/fixtures');
|
|
8
|
+
const { runAutopsy } = require('../src/autopsy/autopsy');
|
|
9
|
+
|
|
10
|
+
test('Autopsy:完整包离线六段全部通过', () => {
|
|
11
|
+
const keys = makeKeys();
|
|
12
|
+
const dir = tempDir('autopsy-ok-');
|
|
13
|
+
const plugin = createPlugin({ dir, requests: [] });
|
|
14
|
+
signPlugin(plugin, keys);
|
|
15
|
+
const report = runAutopsy({
|
|
16
|
+
pluginDir: dir,
|
|
17
|
+
profile: { plugins: [plugin.manifest.id], registry: [] },
|
|
18
|
+
bundlePatch: [],
|
|
19
|
+
policy: {},
|
|
20
|
+
});
|
|
21
|
+
assert.equal(report.ok, true, JSON.stringify(report.missing));
|
|
22
|
+
assert.deepEqual(report.segments.map((segment) => segment.name), [
|
|
23
|
+
'existence', 'declaration', 'activation', 'registration', 'capability', 'uninstall',
|
|
24
|
+
]);
|
|
25
|
+
assert.ok(report.segments.every((segment) => segment.status === 'pass'));
|
|
26
|
+
cleanup(dir);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('Autopsy:缺少入口文件时 existence 段指明缺失项', () => {
|
|
30
|
+
const keys = makeKeys();
|
|
31
|
+
const dir = tempDir('autopsy-entry-');
|
|
32
|
+
const plugin = createPlugin({ dir, requests: [] });
|
|
33
|
+
signPlugin(plugin, keys);
|
|
34
|
+
fs.rmSync(path.join(dir, 'index.js'));
|
|
35
|
+
const report = runAutopsy({ pluginDir: dir, profile: { plugins: [plugin.manifest.id] } });
|
|
36
|
+
assert.equal(report.ok, false);
|
|
37
|
+
const existence = report.segments.find((segment) => segment.name === 'existence');
|
|
38
|
+
assert.equal(existence.status, 'fail');
|
|
39
|
+
assert.ok(existence.missing.some((item) => item.item === 'index.js'));
|
|
40
|
+
cleanup(dir);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('Autopsy:profile 未启用插件时 activation 段失败', () => {
|
|
44
|
+
const keys = makeKeys();
|
|
45
|
+
const dir = tempDir('autopsy-activation-');
|
|
46
|
+
const plugin = createPlugin({ dir, requests: [] });
|
|
47
|
+
signPlugin(plugin, keys);
|
|
48
|
+
const report = runAutopsy({ pluginDir: dir, profile: { plugins: [] } });
|
|
49
|
+
assert.equal(report.ok, false);
|
|
50
|
+
const activation = report.segments.find((segment) => segment.name === 'activation');
|
|
51
|
+
assert.equal(activation.status, 'fail');
|
|
52
|
+
assert.ok(activation.missing.some((item) => item.item === 'activation'));
|
|
53
|
+
cleanup(dir);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('Autopsy:bundle patch 可离线激活,不启动宿主', () => {
|
|
57
|
+
const keys = makeKeys();
|
|
58
|
+
const dir = tempDir('autopsy-patch-');
|
|
59
|
+
const plugin = createPlugin({
|
|
60
|
+
dir,
|
|
61
|
+
requests: [],
|
|
62
|
+
patches: [{ path: 'patches/fix.js', reason: '修复入口' }],
|
|
63
|
+
uninstall: { reversePatches: [{ path: 'patches/revert.js', reason: '回滚修复' }] },
|
|
64
|
+
});
|
|
65
|
+
signPlugin(plugin, keys);
|
|
66
|
+
const report = runAutopsy({
|
|
67
|
+
pluginDir: dir,
|
|
68
|
+
profile: { plugins: [] },
|
|
69
|
+
bundlePatch: [{ op: 'add', path: 'plugins.-', value: plugin.manifest.id }],
|
|
70
|
+
});
|
|
71
|
+
assert.equal(report.ok, true, JSON.stringify(report.missing));
|
|
72
|
+
assert.equal(report.segments.find((segment) => segment.name === 'activation').status, 'pass');
|
|
73
|
+
assert.equal(report.offline, true);
|
|
74
|
+
|
|
75
|
+
const missingReverse = createPlugin({
|
|
76
|
+
dir: tempDir('autopsy-reverse-'),
|
|
77
|
+
requests: [],
|
|
78
|
+
patches: [{ path: 'patches/fix.js', reason: '修复入口' }],
|
|
79
|
+
});
|
|
80
|
+
signPlugin(missingReverse, keys);
|
|
81
|
+
const failed = runAutopsy({
|
|
82
|
+
pluginDir: missingReverse.dir,
|
|
83
|
+
profile: { plugins: [missingReverse.manifest.id] },
|
|
84
|
+
});
|
|
85
|
+
const uninstall = failed.segments.find((segment) => segment.name === 'uninstall');
|
|
86
|
+
assert.equal(uninstall.status, 'fail');
|
|
87
|
+
assert.ok(uninstall.missing.some((item) => item.item === 'uninstall.reversePatches'));
|
|
88
|
+
cleanup(dir);
|
|
89
|
+
cleanup(missingReverse.dir);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('Autopsy:缺少 profile 与注册项时给出 skip / fail', () => {
|
|
93
|
+
const keys = makeKeys();
|
|
94
|
+
const dir = tempDir('autopsy-profile-');
|
|
95
|
+
const plugin = createPlugin({ dir, requests: [], contributes: {} });
|
|
96
|
+
signPlugin(plugin, keys);
|
|
97
|
+
const report = runAutopsy({ pluginDir: dir });
|
|
98
|
+
const activation = report.segments.find((segment) => segment.name === 'activation');
|
|
99
|
+
assert.equal(activation.status, 'skip');
|
|
100
|
+
const registration = report.segments.find((segment) => segment.name === 'registration');
|
|
101
|
+
assert.equal(registration.status, 'fail');
|
|
102
|
+
assert.ok(registration.missing.some((item) => item.item === 'contributes.registrations'));
|
|
103
|
+
cleanup(dir);
|
|
104
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { tempDir, cleanup } = require('./helpers/fixtures');
|
|
7
|
+
const { Ledger } = require('../src/runtime/ledger');
|
|
8
|
+
const { createGuardChain } = require('../src/runtime/guards');
|
|
9
|
+
|
|
10
|
+
function guardFor(dir, requests, grants) {
|
|
11
|
+
const manifest = {
|
|
12
|
+
id: 'com.example.batch',
|
|
13
|
+
name: 'Batch',
|
|
14
|
+
version: '1.0.0',
|
|
15
|
+
entry: 'index.js',
|
|
16
|
+
kind: 'dashcam',
|
|
17
|
+
dashcam: { spec: 'dsh-dashcam/1', content: null, requests },
|
|
18
|
+
};
|
|
19
|
+
const ledger = new Ledger(path.join(dir, 'ledger.jsonl'));
|
|
20
|
+
return {
|
|
21
|
+
ledger,
|
|
22
|
+
guard: createGuardChain({ manifest, grants, ledger, pluginRoot: dir }),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('批量判定:observe 窗口内越界必须留下 deny,不能记成 allow', () => {
|
|
27
|
+
const dir = tempDir('batch-observe-');
|
|
28
|
+
const inside = path.join(dir, 'inside.txt');
|
|
29
|
+
const outside = path.join(dir, '..', 'outside.txt');
|
|
30
|
+
const scope = { paths: [dir.replace(/\\/g, '/') + '/**'] };
|
|
31
|
+
const { guard, ledger } = guardFor(
|
|
32
|
+
dir,
|
|
33
|
+
[{ capability: 'fs.read', scope, reason: '读取自身文件' }],
|
|
34
|
+
[{ id: 'g', capability: 'fs.read', scope, enabled: true, expiresAt: null, oneSeal: false }],
|
|
35
|
+
);
|
|
36
|
+
try {
|
|
37
|
+
guard.beginObserve();
|
|
38
|
+
let result;
|
|
39
|
+
try {
|
|
40
|
+
result = guard.enforceBatch('fs.read', [{ path: inside }, { path: outside }]);
|
|
41
|
+
} finally {
|
|
42
|
+
guard.endObserve();
|
|
43
|
+
}
|
|
44
|
+
assert.equal(result.observed, true, '窗口内不该抛错');
|
|
45
|
+
assert.equal(result.count, 2);
|
|
46
|
+
|
|
47
|
+
const records = ledger.records();
|
|
48
|
+
assert.equal(records.length, 1, '批量只写一条');
|
|
49
|
+
assert.equal(records[0].decision, 'deny', '窗口内的越界不能被记成 allow');
|
|
50
|
+
assert.equal(records[0].scope.count, 2);
|
|
51
|
+
assert.equal(records[0].intercepted, true);
|
|
52
|
+
} finally {
|
|
53
|
+
cleanup(dir);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('批量判定:混合能力不能记成单一能力(cred 不能被藏进 fs)', () => {
|
|
58
|
+
const dir = tempDir('batch-mixed-');
|
|
59
|
+
const plain = path.join(dir, 'notes.txt');
|
|
60
|
+
const secret = path.join(dir, 'id_rsa');
|
|
61
|
+
const scope = { paths: [dir.replace(/\\/g, '/') + '/**'] };
|
|
62
|
+
const { guard, ledger } = guardFor(
|
|
63
|
+
dir,
|
|
64
|
+
[
|
|
65
|
+
{ capability: 'fs.read', scope, reason: '读取普通文件' },
|
|
66
|
+
{ capability: 'cred.read', scope: { targets: [scope.paths[0]] }, reason: '读取凭证' },
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
{ id: 'g1', capability: 'fs.read', scope, enabled: true, expiresAt: null, oneSeal: false },
|
|
70
|
+
{ id: 'g2', capability: 'cred.read', scope: { targets: [scope.paths[0]] }, enabled: true, expiresAt: null, oneSeal: false },
|
|
71
|
+
],
|
|
72
|
+
);
|
|
73
|
+
try {
|
|
74
|
+
guard.enforceBatch('fs.read', [{ path: plain }, { path: secret }]);
|
|
75
|
+
const record = ledger.records().at(-1);
|
|
76
|
+
assert.equal(record.capability, 'mixed', '混合批次必须标 mixed,而不是最后一个能力');
|
|
77
|
+
assert.deepEqual(record.scope.capabilities.sort(), ['cred.read', 'fs.read']);
|
|
78
|
+
} finally {
|
|
79
|
+
cleanup(dir);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const assert = require('node:assert');
|
|
5
|
+
const { buildBehaviorReport, buildDeclarationDraft } = require('../src/governance/behavior');
|
|
6
|
+
const { createTrust, spkiFingerprint } = require('../src/signing/trust');
|
|
7
|
+
const crypto = require('node:crypto');
|
|
8
|
+
|
|
9
|
+
test('行为画像:按插件聚合实际读/写/连/跑,零契约如实标注', () => {
|
|
10
|
+
const records = [
|
|
11
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'fs', action: 'fs.read', scope: { path: 'C:/repo/a.ts' }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:00:00Z', intercepted: false },
|
|
12
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'fs', action: 'fs.write', scope: { paths: ['C:/repo/out.txt'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:01:00Z', intercepted: false },
|
|
13
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'proc', action: 'proc.spawn', scope: { command: 'git' }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:02:00Z', intercepted: false },
|
|
14
|
+
{ plugin: 'beta', pluginVersion: '2.0.0', capability: 'net', action: 'net.outbound', scope: { host: 'api.example.com' }, decision: 'deny', ruleId: 'not_declared', profiled: true, manifestHash: 'deadbeef', ts: '2026-11-09T10:03:00Z', intercepted: true },
|
|
15
|
+
// 遥测记录不计入能力集合。
|
|
16
|
+
{ plugin: 'dsh-dashcam-host', pluginVersion: '1.0.0', capability: 'telemetry', action: 'mount', scope: {}, decision: 'allow', ruleId: 'mount', profiled: false, manifestHash: null, ts: '2026-11-09T09:59:00Z', intercepted: false },
|
|
17
|
+
];
|
|
18
|
+
const report = buildBehaviorReport(records);
|
|
19
|
+
assert.equal(report.plugins.length, 3);
|
|
20
|
+
const alpha = report.plugins.find((section) => section.plugin === 'alpha');
|
|
21
|
+
assert.equal(alpha.activity.total, 3);
|
|
22
|
+
assert.equal(alpha.activity.allow, 3);
|
|
23
|
+
assert.equal(alpha.contract.zeroContract, true);
|
|
24
|
+
assert.deepEqual(alpha.observed.fsRead, ['C:/repo/a.ts']);
|
|
25
|
+
assert.deepEqual(alpha.observed.fsWrite, ['C:/repo/out.txt']);
|
|
26
|
+
assert.deepEqual(alpha.observed.commands, ['git']);
|
|
27
|
+
assert.deepEqual(alpha.capabilities, ['fs', 'proc']);
|
|
28
|
+
const beta = report.plugins.find((section) => section.plugin === 'beta');
|
|
29
|
+
assert.equal(beta.activity.intercepted, 1);
|
|
30
|
+
assert.equal(beta.contract.profiled, true);
|
|
31
|
+
assert.equal(beta.contract.manifestHash, 'deadbeef');
|
|
32
|
+
const host = report.plugins.find((section) => section.plugin === 'dsh-dashcam-host');
|
|
33
|
+
assert.deepEqual(host.capabilities, []);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('行为画像:传入 manifest 时输出能力级声明 diff', () => {
|
|
37
|
+
const manifest = {
|
|
38
|
+
id: 'alpha',
|
|
39
|
+
dashcam: { requests: [{ capability: 'fs.read' }, { capability: 'fs.write' }] },
|
|
40
|
+
};
|
|
41
|
+
const records = [
|
|
42
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'fs', action: 'fs.read', scope: { path: 'x' }, decision: 'allow', ruleId: 'two-key', profiled: true, manifestHash: 'h', ts: '2026-11-09T10:00:00Z', intercepted: false },
|
|
43
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'proc', action: 'proc.spawn', scope: { command: 'rm' }, decision: 'deny', ruleId: 'not_declared', profiled: true, manifestHash: 'h', ts: '2026-11-09T10:01:00Z', intercepted: true },
|
|
44
|
+
];
|
|
45
|
+
const report = buildBehaviorReport(records, { manifest });
|
|
46
|
+
const diff = report.plugins[0].declarationDiff;
|
|
47
|
+
// 家族级 diff:声明 fs.read/fs.write 归并为 fs。
|
|
48
|
+
assert.deepEqual(diff.declared, ['fs']);
|
|
49
|
+
assert.deepEqual(diff.undeclared, ['proc']);
|
|
50
|
+
assert.deepEqual(diff.declaredNotObserved, []);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('N2:SPKI 指纹前后 8 位,随信任表归一化产出', () => {
|
|
54
|
+
// 生成一把真实 Ed25519 密钥取 SPKI PEM,指纹可复算。
|
|
55
|
+
const { publicKey } = crypto.generateKeyPairSync('ed25519');
|
|
56
|
+
const pem = publicKey.export({ type: 'spki', format: 'pem' });
|
|
57
|
+
const expected = (() => {
|
|
58
|
+
const der = publicKey.export({ type: 'spki', format: 'der' });
|
|
59
|
+
const hex = crypto.createHash('sha256').update(der).digest('hex');
|
|
60
|
+
return `${hex.slice(0, 8)}…${hex.slice(-8)}`;
|
|
61
|
+
})();
|
|
62
|
+
assert.equal(spkiFingerprint(pem), expected);
|
|
63
|
+
const trust = createTrust([{ keyId: 'k1', roles: ['publisher'], publicKey: pem }]);
|
|
64
|
+
assert.equal(trust.keys[0].fingerprint, expected);
|
|
65
|
+
assert.equal(spkiFingerprint(''), null);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('draft:从账本观测反推声明草案(fs 折目录、proc 取首词、凭证单独分组)', () => {
|
|
69
|
+
const records = [
|
|
70
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'fs', action: 'fs.read', scope: { paths: ['C:/repo/src/a.ts', 'C:/repo/src/b.ts', 'C:/Users/x/.ssh/id_rsa'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:00:00Z', intercepted: false },
|
|
71
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'fs', action: 'fs.write', scope: { paths: ['C:/repo/dist/out.js'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:01:00Z', intercepted: false },
|
|
72
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'proc', action: 'proc.spawn', scope: { commands: ['git status', 'git push'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:02:00Z', intercepted: false },
|
|
73
|
+
{ plugin: 'alpha', pluginVersion: '1.0.0', capability: 'net', action: 'net.outbound', scope: { hosts: ['api.example.com:443'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:03:00Z', intercepted: false },
|
|
74
|
+
];
|
|
75
|
+
const draft = buildDeclarationDraft(records);
|
|
76
|
+
assert.equal(draft.generatedFrom, 'ledger');
|
|
77
|
+
assert.ok(draft.note.includes('人工收敛'));
|
|
78
|
+
const alpha = draft.plugins.find((section) => section.plugin === 'alpha');
|
|
79
|
+
assert.equal(alpha.zeroContract, true);
|
|
80
|
+
const byCapability = new Map(alpha.requests.map((request) => [request.capability, request]));
|
|
81
|
+
// 普通读折叠到目录;同目录两个文件只建议一个目录。
|
|
82
|
+
const fsRead = byCapability.get('fs.read');
|
|
83
|
+
assert.deepEqual(fsRead.scope.paths, ['C:/repo/src']);
|
|
84
|
+
// 凭证路径(id_rsa)升级为 cred.read 且保留精确目标,不并入 fs.read。
|
|
85
|
+
const credRead = byCapability.get('cred.read');
|
|
86
|
+
assert.deepEqual(credRead.scope.targets, ['C:/Users/x/.ssh/id_rsa']);
|
|
87
|
+
// 写折叠到目录。
|
|
88
|
+
assert.deepEqual(byCapability.get('fs.write').scope.paths, ['C:/repo/dist']);
|
|
89
|
+
// proc 取首词去重。
|
|
90
|
+
assert.deepEqual(byCapability.get('proc.spawn').scope.commands, ['git']);
|
|
91
|
+
// net 原样。
|
|
92
|
+
assert.deepEqual(byCapability.get('net.outbound').scope.hosts, ['api.example.com:443']);
|
|
93
|
+
// 每条 reason 都写明来源与「需人工收敛」。
|
|
94
|
+
for (const request of alpha.requests) {
|
|
95
|
+
assert.ok(request.reason.includes('由账本观测反推'));
|
|
96
|
+
assert.ok(request.reason.includes('需人工收敛'));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('draft:fs.delete 保留精确路径不放大;已有 manifest 的家族不再重复建议', () => {
|
|
101
|
+
const records = [
|
|
102
|
+
{ plugin: 'alpha', capability: 'fs', action: 'fs.read', scope: { paths: ['C:/repo/src/a.ts'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:00:00Z', intercepted: false },
|
|
103
|
+
{ plugin: 'alpha', capability: 'fs', action: 'fs.delete', scope: { paths: ['C:/repo/cache.tmp', 'C:/repo/old.log'] }, decision: 'allow', ruleId: 'unprofiled', profiled: false, manifestHash: null, ts: '2026-11-09T10:01:00Z', intercepted: false },
|
|
104
|
+
];
|
|
105
|
+
// 不传 manifest:fs.read 与 fs.delete 都建议,delete 保留精确路径。
|
|
106
|
+
const bare = buildDeclarationDraft(records);
|
|
107
|
+
const bareAlpha = bare.plugins.find((section) => section.plugin === 'alpha');
|
|
108
|
+
const bareByCapability = new Map(bareAlpha.requests.map((request) => [request.capability, request]));
|
|
109
|
+
assert.deepEqual(bareByCapability.get('fs.delete').scope.paths, ['C:/repo/cache.tmp', 'C:/repo/old.log']);
|
|
110
|
+
assert.ok(bareByCapability.get('fs.read'));
|
|
111
|
+
|
|
112
|
+
// 传入 manifest:fs.read 已声明,不再建议;fs.delete 是另一个精确能力,
|
|
113
|
+
// 精确路径仍在(删除能力按目录授权等于把整个目录交给人家删)。
|
|
114
|
+
const manifest = {
|
|
115
|
+
id: 'alpha',
|
|
116
|
+
dashcam: { requests: [{ capability: 'fs.read', scope: { paths: ['C:/repo'] }, reason: 'already declared' }] },
|
|
117
|
+
};
|
|
118
|
+
const withManifest = buildDeclarationDraft(records, { manifest });
|
|
119
|
+
const alpha = withManifest.plugins.find((section) => section.plugin === 'alpha');
|
|
120
|
+
assert.deepEqual(alpha.coveredByManifest, ['fs.read']);
|
|
121
|
+
const capabilities = alpha.requests.map((request) => request.capability);
|
|
122
|
+
assert.ok(!capabilities.includes('fs.read'));
|
|
123
|
+
assert.deepEqual(capabilities, ['fs.delete']);
|
|
124
|
+
// 别的插件不吃这份 manifest 的覆盖。
|
|
125
|
+
const other = withManifest.plugins.find((section) => section.plugin !== 'alpha');
|
|
126
|
+
if (other) assert.deepEqual(other.coveredByManifest, []);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('draft:零记录给出空草案,不编造', () => {
|
|
130
|
+
const draft = buildDeclarationDraft([]);
|
|
131
|
+
assert.deepEqual(draft.plugins, []);
|
|
132
|
+
});
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const http = require('node:http');
|
|
8
|
+
const net = require('node:net');
|
|
9
|
+
const dns = require('node:dns');
|
|
10
|
+
const { tempDir, createPlugin, cleanup } = require('./helpers/fixtures');
|
|
11
|
+
const { loadManifest } = require('../src/contract/manifest');
|
|
12
|
+
const { normalizeGrant } = require('../src/contract/capabilities');
|
|
13
|
+
const { Ledger } = require('../src/runtime/ledger');
|
|
14
|
+
const { OneSealStore } = require('../src/runtime/one-seal');
|
|
15
|
+
const { createGuardChain } = require('../src/runtime/guards');
|
|
16
|
+
const { createBridge } = require('../src/runtime/bridge');
|
|
17
|
+
|
|
18
|
+
function setup({ requests = [], files = {} } = {}) {
|
|
19
|
+
const dir = tempDir('bypass-');
|
|
20
|
+
createPlugin({
|
|
21
|
+
dir,
|
|
22
|
+
requests,
|
|
23
|
+
files,
|
|
24
|
+
});
|
|
25
|
+
const { manifest } = loadManifest(dir);
|
|
26
|
+
const ledger = new Ledger(path.join(dir, 'ledger.jsonl'));
|
|
27
|
+
const seal = new OneSealStore();
|
|
28
|
+
const grants = requests.map((request, index) => normalizeGrant({
|
|
29
|
+
capability: request.cap,
|
|
30
|
+
scope: request.scope,
|
|
31
|
+
}, { pluginRoot: dir }, index));
|
|
32
|
+
const guard = createGuardChain({ manifest, grants, ledger, seal, pluginRoot: dir });
|
|
33
|
+
return { dir, manifest, ledger, seal, guard };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test('绕过测试:直调 Node API 仍被守卫链拦截', () => {
|
|
37
|
+
const runtime = setup({
|
|
38
|
+
requests: [{ cap: 'fs.read', scope: { paths: ['$PLUGIN/**'] }, reason: '读取自身文件' }],
|
|
39
|
+
});
|
|
40
|
+
// 外部文件必须在 Bridge 安装前创建:安装后 write 同样受 Two-Key 约束。
|
|
41
|
+
const outsideDir = tempDir('bypass-out-');
|
|
42
|
+
const outside = path.join(outsideDir, 'secret.txt');
|
|
43
|
+
fs.writeFileSync(outside, 'secret');
|
|
44
|
+
const own = path.join(runtime.dir, 'own.txt');
|
|
45
|
+
fs.writeFileSync(own, 'own');
|
|
46
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
47
|
+
try {
|
|
48
|
+
assert.throws(() => fs.readFileSync(outside, 'utf8'), /未声明|未授予/);
|
|
49
|
+
assert.equal(fs.readFileSync(own, 'utf8'), 'own');
|
|
50
|
+
} finally {
|
|
51
|
+
bridge.uninstall();
|
|
52
|
+
cleanup(runtime.dir);
|
|
53
|
+
cleanup(outsideDir);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('绕过测试:第三方库通过 require 访问 fs 同样被拦截', () => {
|
|
58
|
+
const runtime = setup({
|
|
59
|
+
requests: [{ cap: 'fs.read', scope: { paths: ['$PLUGIN/**'] }, reason: '读取自身文件' }],
|
|
60
|
+
files: {
|
|
61
|
+
'lib.js': [
|
|
62
|
+
"'use strict';",
|
|
63
|
+
"const fs = require('node:fs');",
|
|
64
|
+
'module.exports = (target) => fs.readFileSync(target, "utf8");',
|
|
65
|
+
].join('\n'),
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
const outsideDir = tempDir('bypass-lib-');
|
|
69
|
+
const outside = path.join(outsideDir, 'secret.txt');
|
|
70
|
+
fs.writeFileSync(outside, 'secret');
|
|
71
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
72
|
+
try {
|
|
73
|
+
const lib = require(path.join(runtime.dir, 'lib.js'));
|
|
74
|
+
assert.throws(() => lib(outside), /未声明|未授予/);
|
|
75
|
+
} finally {
|
|
76
|
+
bridge.uninstall();
|
|
77
|
+
delete require.cache[require.resolve(path.join(runtime.dir, 'lib.js'))];
|
|
78
|
+
cleanup(runtime.dir);
|
|
79
|
+
cleanup(outsideDir);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('绕过测试:patch 注入被完整性校验发现并触发失败关闭', () => {
|
|
84
|
+
const realReadFileSync = fs.readFileSync;
|
|
85
|
+
const runtime = setup({
|
|
86
|
+
requests: [{ cap: 'fs.read', scope: { paths: ['$PLUGIN/**'] }, reason: '读取自身文件' }],
|
|
87
|
+
});
|
|
88
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
89
|
+
try {
|
|
90
|
+
// 模拟恶意插件在安装前保存原始函数,再把它写回覆盖 Bridge。
|
|
91
|
+
fs.readFileSync = realReadFileSync;
|
|
92
|
+
const injected = bridge.verifyIntegrity();
|
|
93
|
+
assert.equal(injected.ok, false);
|
|
94
|
+
assert.ok(injected.violations.some((violation) => violation.target === 'readFileSync'));
|
|
95
|
+
assert.equal(runtime.guard.isCompromised(), true);
|
|
96
|
+
const tamper = runtime.ledger.records().find((record) => record.ruleId === 'tamper_detected');
|
|
97
|
+
assert.ok(tamper);
|
|
98
|
+
assert.throws(
|
|
99
|
+
() => runtime.guard.enforce('fs.read', { path: path.join(runtime.dir, 'any.txt') }),
|
|
100
|
+
/完整性/,
|
|
101
|
+
);
|
|
102
|
+
} finally {
|
|
103
|
+
bridge.uninstall();
|
|
104
|
+
cleanup(runtime.dir);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('绕过测试:直接网络与环境变量访问也被拦截', () => {
|
|
109
|
+
const key = 'DSH_BYPASS_API_KEY';
|
|
110
|
+
process.env[key] = 'secret';
|
|
111
|
+
const runtime = setup({
|
|
112
|
+
requests: [{ cap: 'cred.read', scope: { targets: [`env:${key}`] }, reason: '读取密钥' }],
|
|
113
|
+
});
|
|
114
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
115
|
+
try {
|
|
116
|
+
assert.equal(process.env[key], 'secret');
|
|
117
|
+
assert.throws(() => process.env.DSH_UNKNOWN_API_KEY, /未声明|未授予/);
|
|
118
|
+
assert.throws(() => http.get('http://attacker.example:80/'), /未声明|未授予/);
|
|
119
|
+
} finally {
|
|
120
|
+
bridge.uninstall();
|
|
121
|
+
delete process.env[key];
|
|
122
|
+
cleanup(runtime.dir);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('绕过测试:fs.openSync 不能绕开句柄关口', async () => {
|
|
127
|
+
const runtime = setup({
|
|
128
|
+
requests: [{ cap: 'fs.read', scope: { paths: ['$PLUGIN/**'] }, reason: '读取自身文件' }],
|
|
129
|
+
});
|
|
130
|
+
const outsideDir = tempDir('bypass-open-');
|
|
131
|
+
const outside = path.join(outsideDir, 'secret.txt');
|
|
132
|
+
fs.writeFileSync(outside, 'secret');
|
|
133
|
+
const own = path.join(runtime.dir, 'own.txt');
|
|
134
|
+
fs.writeFileSync(own, 'own');
|
|
135
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
136
|
+
try {
|
|
137
|
+
assert.throws(() => fs.openSync(outside, 'r'), /未声明|未授予/);
|
|
138
|
+
// fsp.open 是异步 API:判定失败表现为 rejected promise,不是同步抛错。
|
|
139
|
+
await assert.rejects(() => fs.promises.open(outside, 'r'), /未声明|未授予/);
|
|
140
|
+
const fd = fs.openSync(own, 'r');
|
|
141
|
+
fs.closeSync(fd);
|
|
142
|
+
} finally {
|
|
143
|
+
bridge.uninstall();
|
|
144
|
+
cleanup(runtime.dir);
|
|
145
|
+
cleanup(outsideDir);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('绕过测试:Socket 实例直连不能绕开出站管控', () => {
|
|
150
|
+
const runtime = setup({ requests: [] });
|
|
151
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
152
|
+
try {
|
|
153
|
+
const socket = new net.Socket();
|
|
154
|
+
assert.throws(() => socket.connect(9, '127.0.0.1'), /未声明|未授予|manifest 中声明/);
|
|
155
|
+
} finally {
|
|
156
|
+
bridge.uninstall();
|
|
157
|
+
cleanup(runtime.dir);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('绕过测试:dns.promises 同样受出站管控', () => {
|
|
162
|
+
const runtime = setup({ requests: [] });
|
|
163
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
164
|
+
try {
|
|
165
|
+
assert.throws(() => dns.promises.lookup('example.com'), /未声明|未授予|manifest 中声明/);
|
|
166
|
+
} finally {
|
|
167
|
+
bridge.uninstall();
|
|
168
|
+
cleanup(runtime.dir);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('绕过测试:覆写敏感环境变量需要 cred.write,而不是 env.write', () => {
|
|
173
|
+
const sensitive = 'DSH_BYPASS_WRITE_KEY';
|
|
174
|
+
const plain = 'DSH_BYPASS_WRITE_FLAG';
|
|
175
|
+
const runtime = setup({
|
|
176
|
+
requests: [{ cap: 'env.write', scope: { keys: ['DSH_BYPASS_WRITE_*'] }, reason: '写入运行期标志' }],
|
|
177
|
+
});
|
|
178
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
179
|
+
try {
|
|
180
|
+
assert.throws(() => { process.env[sensitive] = 'x'; }, /未声明|未授予|manifest 中声明/);
|
|
181
|
+
// 非敏感键有 env.write 授予即可写;读回验证放到卸载之后(读是另一项能力)。
|
|
182
|
+
process.env[plain] = '1';
|
|
183
|
+
} finally {
|
|
184
|
+
bridge.uninstall();
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
assert.equal(process.env[plain], '1');
|
|
188
|
+
} finally {
|
|
189
|
+
delete process.env[plain];
|
|
190
|
+
delete process.env[sensitive];
|
|
191
|
+
cleanup(runtime.dir);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('绕过测试:fs.promises.link 需要读权限(硬链接二次命名)', () => {
|
|
196
|
+
const runtime = setup({
|
|
197
|
+
requests: [{ cap: 'fs.write', scope: { paths: ['$PLUGIN/**'] }, reason: '写入自身目录' }],
|
|
198
|
+
});
|
|
199
|
+
const outsideDir = tempDir('bypass-hardlink-');
|
|
200
|
+
const outside = path.join(outsideDir, 'secret.txt');
|
|
201
|
+
fs.writeFileSync(outside, 'secret');
|
|
202
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
203
|
+
try {
|
|
204
|
+
// 只给写权限:把范围外文件 link 进插件目录,等于给它起了第二个名字。
|
|
205
|
+
// realpath 帮不上忙(两个名字都是真的),所以 link 必须同时要读权限。
|
|
206
|
+
assert.throws(
|
|
207
|
+
() => fs.promises.link(outside, path.join(runtime.dir, 'stolen.txt')),
|
|
208
|
+
/未声明|未授予|manifest/,
|
|
209
|
+
);
|
|
210
|
+
} finally {
|
|
211
|
+
bridge.uninstall();
|
|
212
|
+
cleanup(runtime.dir);
|
|
213
|
+
cleanup(outsideDir);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('覆盖自检:句柄与实例入口登记在 guarded 里,knownGaps 不为空', () => {
|
|
218
|
+
const runtime = setup({ requests: [] });
|
|
219
|
+
const bridge = createBridge({ guard: runtime.guard, ledger: runtime.ledger, plugin: runtime.manifest });
|
|
220
|
+
try {
|
|
221
|
+
const report = bridge.coverage();
|
|
222
|
+
for (const entry of ['fs.open', 'fs.openSync', 'fsp.open', 'net.Socket#connect', 'dns.promises.lookup']) {
|
|
223
|
+
assert.ok(report.guarded.includes(entry), `coverage 缺少 ${entry}`);
|
|
224
|
+
}
|
|
225
|
+
assert.ok(report.knownGaps.length >= 4);
|
|
226
|
+
assert.ok(report.entries > 80);
|
|
227
|
+
} finally {
|
|
228
|
+
bridge.uninstall();
|
|
229
|
+
cleanup(runtime.dir);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 多进程运行时垂直切片的集成测试。
|
|
5
|
+
* 验证:授权内读取成功、越界读取被宿主拒绝且账本留痕、子进程崩溃时 invoke 能感知。
|
|
6
|
+
*
|
|
7
|
+
* 注意:createGuardChain 期望 manifest.dashcam.requests 里的 scope 是**已展开的绝对路径**
|
|
8
|
+
* (真实流程由 parseManifest 完成)。这里直接内联 manifest,所以 scope 要自己展开成
|
|
9
|
+
* `<dir>/...`,不能用 `$PLUGIN` 占位符——否则会被判成「未声明」。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const test = require('node:test');
|
|
13
|
+
const assert = require('node:assert/strict');
|
|
14
|
+
const fs = require('node:fs');
|
|
15
|
+
const path = require('node:path');
|
|
16
|
+
const { tempDir, cleanup } = require('./helpers/fixtures');
|
|
17
|
+
const { Ledger } = require('../src/runtime/ledger');
|
|
18
|
+
const { createGuardChain } = require('../src/runtime/guards');
|
|
19
|
+
const { ChildHost } = require('../src/host/child-host');
|
|
20
|
+
|
|
21
|
+
const CHILD_ENTRY = path.join(__dirname, '..', 'examples', 'child-plugin.js');
|
|
22
|
+
|
|
23
|
+
function setup(subpath = '/**') {
|
|
24
|
+
const dir = tempDir('childhost-');
|
|
25
|
+
const scope = { paths: [dir.replace(/\\/g, '/') + subpath] };
|
|
26
|
+
const ledger = new Ledger(path.join(dir, 'ledger.jsonl'));
|
|
27
|
+
const guard = createGuardChain({
|
|
28
|
+
manifest: {
|
|
29
|
+
id: 'com.example.child',
|
|
30
|
+
name: 'Child',
|
|
31
|
+
version: '1.0.0',
|
|
32
|
+
entry: 'x.js',
|
|
33
|
+
kind: 'dashcam',
|
|
34
|
+
dashcam: {
|
|
35
|
+
spec: 'dsh-dashcam/1',
|
|
36
|
+
content: null,
|
|
37
|
+
requests: [{ capability: 'fs.read', scope, reason: '读自身文件' }],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
grants: [{ id: 'g', capability: 'fs.read', scope, enabled: true, expiresAt: null, oneSeal: false }],
|
|
41
|
+
ledger,
|
|
42
|
+
pluginRoot: dir,
|
|
43
|
+
});
|
|
44
|
+
return { dir, ledger, guard };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
test('多进程切片:授权内读取成功(IO 发生在宿主)', async () => {
|
|
48
|
+
const runtime = setup('/**');
|
|
49
|
+
fs.writeFileSync(path.join(runtime.dir, 'a.txt'), 'HELLO');
|
|
50
|
+
|
|
51
|
+
const host = new ChildHost({ guard: runtime.guard, childEntry: CHILD_ENTRY });
|
|
52
|
+
host.start();
|
|
53
|
+
try {
|
|
54
|
+
const result = await host.invoke({ path: path.join(runtime.dir, 'a.txt') });
|
|
55
|
+
assert.deepEqual(result, { ok: true, text: 'HELLO' });
|
|
56
|
+
} finally {
|
|
57
|
+
host.stop();
|
|
58
|
+
cleanup(runtime.dir);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('多进程切片:越界读取被宿主拒绝,且账本留痕', async () => {
|
|
63
|
+
const runtime = setup('/inside/**');
|
|
64
|
+
fs.mkdirSync(path.join(runtime.dir, 'inside'), { recursive: true });
|
|
65
|
+
const outside = path.join(runtime.dir, 'outside.txt');
|
|
66
|
+
fs.writeFileSync(outside, 'SECRET');
|
|
67
|
+
|
|
68
|
+
const host = new ChildHost({ guard: runtime.guard, childEntry: CHILD_ENTRY });
|
|
69
|
+
host.start();
|
|
70
|
+
try {
|
|
71
|
+
await assert.rejects(
|
|
72
|
+
() => host.invoke({ path: outside }),
|
|
73
|
+
/未声明|未授予|manifest/,
|
|
74
|
+
);
|
|
75
|
+
const denies = runtime.ledger.records().filter((record) => record.decision === 'deny');
|
|
76
|
+
assert.ok(denies.length >= 1, '宿主侧必须记账');
|
|
77
|
+
assert.ok(denies.some((record) => record.ruleId === 'not_declared' || record.ruleId === 'not_granted'));
|
|
78
|
+
} finally {
|
|
79
|
+
host.stop();
|
|
80
|
+
cleanup(runtime.dir);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('多进程切片:子进程崩溃时 invoke 以错误收场而不是悬挂', async () => {
|
|
85
|
+
const runtime = setup('/**');
|
|
86
|
+
fs.writeFileSync(path.join(runtime.dir, 'a.txt'), 'X');
|
|
87
|
+
|
|
88
|
+
const host = new ChildHost({ guard: runtime.guard, childEntry: CHILD_ENTRY, invokeTimeoutMs: 2000 });
|
|
89
|
+
host.start();
|
|
90
|
+
try {
|
|
91
|
+
const result = await host.invoke({ path: path.join(runtime.dir, 'a.txt') });
|
|
92
|
+
assert.equal(result.text, 'X');
|
|
93
|
+
|
|
94
|
+
host.stop();
|
|
95
|
+
await assert.rejects(() => host.invoke({ path: 'x' }));
|
|
96
|
+
} finally {
|
|
97
|
+
host.stop();
|
|
98
|
+
cleanup(runtime.dir);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('多进程切片:批量读一次 RPC 读多个文件,且账本只写一条', async () => {
|
|
103
|
+
const runtime = setup('/**');
|
|
104
|
+
fs.writeFileSync(path.join(runtime.dir, 'a.txt'), 'A');
|
|
105
|
+
fs.writeFileSync(path.join(runtime.dir, 'b.txt'), 'B');
|
|
106
|
+
|
|
107
|
+
const host = new ChildHost({ guard: runtime.guard, childEntry: CHILD_ENTRY });
|
|
108
|
+
host.start();
|
|
109
|
+
try {
|
|
110
|
+
const result = await host.invoke({
|
|
111
|
+
paths: [path.join(runtime.dir, 'a.txt'), path.join(runtime.dir, 'b.txt')],
|
|
112
|
+
});
|
|
113
|
+
assert.deepEqual(result, { ok: true, texts: ['A', 'B'] });
|
|
114
|
+
|
|
115
|
+
const batchRecords = runtime.ledger.records().filter((record) => record.ruleId === 'two-key-batch');
|
|
116
|
+
assert.equal(batchRecords.length, 1, '批量读只该写一条 batch 账本');
|
|
117
|
+
assert.equal(batchRecords[0].scope.count, 2);
|
|
118
|
+
} finally {
|
|
119
|
+
host.stop();
|
|
120
|
+
cleanup(runtime.dir);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('ChildHost 已接进公开导出', () => {
|
|
125
|
+
const api = require('../src/index');
|
|
126
|
+
assert.equal(typeof api.ChildHost, 'function');
|
|
127
|
+
});
|