@hmharness/lsp 0.23.24 → 0.23.26
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/dist/tools.d.ts +12 -1
- package/dist/tools.js +8 -0
- package/package.json +23 -23
- package/src/__tests__/deveco.test.ts +60 -60
- package/src/__tests__/fixtures/fake-server.cjs +89 -89
- package/src/__tests__/lsp.test.ts +119 -119
- package/src/__tests__/trust.test.ts +68 -68
- package/src/client.ts +137 -137
- package/src/index.ts +15 -15
- package/src/process-manager.ts +118 -118
- package/src/protocol.ts +113 -113
- package/src/registry.ts +130 -130
- package/src/tools.ts +229 -209
- package/src/trust.ts +108 -108
- package/tsconfig.build.json +18 -18
- package/verify-clangd.mts +40 -40
- package/verify-live.mts +27 -27
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { test } from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtemp, rm } from 'node:fs/promises';
|
|
4
|
-
import { tmpdir } from 'node:os';
|
|
5
|
-
import { join, dirname } from 'node:path';
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { encodeMessage, MessageDecoder } from '../protocol.ts';
|
|
8
|
-
import { scrubEnv, ProcessManager } from '../process-manager.ts';
|
|
9
|
-
import { LspClient } from '../client.ts';
|
|
10
|
-
import { fileToUri } from '../registry.ts';
|
|
11
|
-
|
|
12
|
-
const FAKE_SERVER = join(dirname(fileURLToPath(import.meta.url)), 'fixtures', 'fake-server.cjs');
|
|
13
|
-
|
|
14
|
-
test('protocol: Content-Length framing round-trips through the decoder', () => {
|
|
15
|
-
const dec = new MessageDecoder();
|
|
16
|
-
const msg = { jsonrpc: '2.0' as const, id: 7, method: 'x', params: { s: '中文内容' } };
|
|
17
|
-
const wire = encodeMessage(msg);
|
|
18
|
-
// feed in two chunks to prove incremental decoding
|
|
19
|
-
const mid = Math.floor(wire.length / 2);
|
|
20
|
-
const out1 = dec.push(wire.subarray(0, mid));
|
|
21
|
-
const out2 = dec.push(wire.subarray(mid));
|
|
22
|
-
assert.deepEqual(out1, []);
|
|
23
|
-
assert.deepEqual(out2, [msg]);
|
|
24
|
-
// two messages in one chunk
|
|
25
|
-
const dec2 = new MessageDecoder();
|
|
26
|
-
const both = dec2.push(Buffer.concat([encodeMessage({ jsonrpc: '2.0', id: 1, method: 'a' }), encodeMessage({ jsonrpc: '2.0', id: 2, method: 'b' })]));
|
|
27
|
-
assert.equal(both.length, 2);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('process-manager: env scrub keeps PATH, drops secret-looking vars', () => {
|
|
31
|
-
const env = scrubEnv([]);
|
|
32
|
-
assert.ok(env.PATH !== undefined || env.Path !== undefined, 'PATH survives');
|
|
33
|
-
const probeKeys = ['NODE_AUTH_TOKEN', 'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'BIGMODEL_API_KEY'];
|
|
34
|
-
for (const k of probeKeys) assert.equal((env as Record<string, unknown>)[k], undefined, `${k} must not inherit`);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('client: full lifecycle against the fake server (initialize→sync→tier0→shutdown)', async () => {
|
|
38
|
-
const ws = await mkdtemp(join(tmpdir(), 'lsp-ws-'));
|
|
39
|
-
const manager = new ProcessManager({ id: 'fake', command: process.execPath, args: [FAKE_SERVER], source: 'explicit' }, ws);
|
|
40
|
-
const got: Array<{ uri: string; n: number }> = [];
|
|
41
|
-
const client = new LspClient(manager.start(), {
|
|
42
|
-
requestTimeoutMs: 10_000,
|
|
43
|
-
onDiagnostics: (uri, diags) => got.push({ uri, n: diags.length }),
|
|
44
|
-
});
|
|
45
|
-
const init = await client.initialize(fileToUri(ws));
|
|
46
|
-
assert.equal(init.serverInfo?.name, 'fake-lsp');
|
|
47
|
-
// document sync drives push diagnostics
|
|
48
|
-
const uri = fileToUri(join(ws, 'probe.ts'));
|
|
49
|
-
client.openDoc(uri, 'typescript', 'const x: number = DELBERATE_ERROR_MARKER;\n');
|
|
50
|
-
await new Promise((r) => setTimeout(r, 500));
|
|
51
|
-
assert.ok(got.some((g) => g.uri === uri && g.n === 1), 'one diagnostic pushed for the marker file');
|
|
52
|
-
// tier-0 requests
|
|
53
|
-
const hover = await client.hover({ uri }, { line: 0, character: 6 });
|
|
54
|
-
assert.match(JSON.stringify(hover?.contents), /fake hover/);
|
|
55
|
-
const def = await client.definition({ uri }, { line: 0, character: 6 });
|
|
56
|
-
assert.equal(Array.isArray(def) ? def[0]?.uri : def?.uri, 'file:///fake/def.ts');
|
|
57
|
-
const refs = await client.references({ uri }, { line: 0, character: 6 });
|
|
58
|
-
assert.equal(refs.length, 2);
|
|
59
|
-
// Tier-1: implementation + call hierarchy
|
|
60
|
-
const impls = await client.implementation({ uri }, { line: 0, character: 6 });
|
|
61
|
-
assert.equal(Array.isArray(impls) ? impls.length : 1, 2);
|
|
62
|
-
const item = await client.prepareCallHierarchy({ uri }, { line: 0, character: 6 });
|
|
63
|
-
const first = Array.isArray(item) ? item[0] : item;
|
|
64
|
-
const incoming = await client.callHierarchyIncoming(first);
|
|
65
|
-
assert.equal(incoming.length, 2);
|
|
66
|
-
assert.equal((incoming[0]!.from as { name?: string }).name, 'callerOne');
|
|
67
|
-
const outgoing = await client.callHierarchyOutgoing(first);
|
|
68
|
-
assert.equal(outgoing.length, 1);
|
|
69
|
-
assert.equal((outgoing[0]!.to as { name?: string }).name, 'helperFn');
|
|
70
|
-
const syms = await client.documentSymbols({ uri });
|
|
71
|
-
assert.equal(syms[0]?.name, 'mainFn');
|
|
72
|
-
assert.equal(syms[0]?.children?.[0]?.name, 'inner');
|
|
73
|
-
// clean shutdown stops the process
|
|
74
|
-
await client.shutdown();
|
|
75
|
-
await manager.stop();
|
|
76
|
-
assert.equal(manager.running, false);
|
|
77
|
-
await rm(ws, { recursive: true, force: true });
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test('registry: health probe flags a broken shim as unhealthy (fake: a command that always fails)', async () => {
|
|
81
|
-
const { discoverServers } = await import('../registry.ts');
|
|
82
|
-
// real discovery on THIS machine: whatever exists must carry a verdict
|
|
83
|
-
const servers = discoverServers(true);
|
|
84
|
-
for (const s of servers) {
|
|
85
|
-
assert.ok(typeof s.healthy === 'boolean', `${s.id} must carry a health verdict`);
|
|
86
|
-
if (!s.healthy) assert.ok((s.unhealthyReason ?? '').length > 0, `${s.id} unhealthy must say why`);
|
|
87
|
-
}
|
|
88
|
-
// a nonexistent binary is simply not discovered; a failing one is
|
|
89
|
-
// discovered-but-unhealthy only when it exists on PATH — both states stay
|
|
90
|
-
// honest. (On this dev machine rust-analyzer is a component-missing shim
|
|
91
|
-
// and MUST be unhealthy if present.)
|
|
92
|
-
const rust = servers.find((s) => s.id === 'rust-analyzer');
|
|
93
|
-
if (rust && rust.unhealthyReason) console.log('rust-analyzer verdict:', rust.unhealthyReason.slice(0, 80));
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('client: unknown method surfaces the server error honestly', async () => {
|
|
97
|
-
const ws = await mkdtemp(join(tmpdir(), 'lsp-ws-'));
|
|
98
|
-
const manager = new ProcessManager({ id: 'fake', command: process.execPath, args: [FAKE_SERVER], source: 'explicit' }, ws);
|
|
99
|
-
const client = new LspClient(manager.start(), { requestTimeoutMs: 10_000 });
|
|
100
|
-
await client.initialize(fileToUri(ws));
|
|
101
|
-
await assert.rejects(() => client.request('workspace/executeCommand', {}), /-32601|method not found/);
|
|
102
|
-
await client.shutdown();
|
|
103
|
-
await manager.stop();
|
|
104
|
-
await rm(ws, { recursive: true, force: true });
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('process-manager: restart budget refuses flapping servers', async () => {
|
|
108
|
-
const ws = await mkdtemp(join(tmpdir(), 'lsp-ws-'));
|
|
109
|
-
const manager = new ProcessManager({ id: 'crasher', command: process.execPath, args: ['-e', 'process.exit(3)'], source: 'explicit' }, ws, { maxRestarts: 2 });
|
|
110
|
-
manager.start();
|
|
111
|
-
await new Promise((r) => setTimeout(r, 300));
|
|
112
|
-
manager.start(); // restart 1
|
|
113
|
-
await new Promise((r) => setTimeout(r, 300));
|
|
114
|
-
manager.start(); // restart 2
|
|
115
|
-
await new Promise((r) => setTimeout(r, 300));
|
|
116
|
-
assert.throws(() => manager.start(), /restart budget/);
|
|
117
|
-
await manager.stop();
|
|
118
|
-
await rm(ws, { recursive: true, force: true });
|
|
119
|
-
});
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join, dirname } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { encodeMessage, MessageDecoder } from '../protocol.ts';
|
|
8
|
+
import { scrubEnv, ProcessManager } from '../process-manager.ts';
|
|
9
|
+
import { LspClient } from '../client.ts';
|
|
10
|
+
import { fileToUri } from '../registry.ts';
|
|
11
|
+
|
|
12
|
+
const FAKE_SERVER = join(dirname(fileURLToPath(import.meta.url)), 'fixtures', 'fake-server.cjs');
|
|
13
|
+
|
|
14
|
+
test('protocol: Content-Length framing round-trips through the decoder', () => {
|
|
15
|
+
const dec = new MessageDecoder();
|
|
16
|
+
const msg = { jsonrpc: '2.0' as const, id: 7, method: 'x', params: { s: '中文内容' } };
|
|
17
|
+
const wire = encodeMessage(msg);
|
|
18
|
+
// feed in two chunks to prove incremental decoding
|
|
19
|
+
const mid = Math.floor(wire.length / 2);
|
|
20
|
+
const out1 = dec.push(wire.subarray(0, mid));
|
|
21
|
+
const out2 = dec.push(wire.subarray(mid));
|
|
22
|
+
assert.deepEqual(out1, []);
|
|
23
|
+
assert.deepEqual(out2, [msg]);
|
|
24
|
+
// two messages in one chunk
|
|
25
|
+
const dec2 = new MessageDecoder();
|
|
26
|
+
const both = dec2.push(Buffer.concat([encodeMessage({ jsonrpc: '2.0', id: 1, method: 'a' }), encodeMessage({ jsonrpc: '2.0', id: 2, method: 'b' })]));
|
|
27
|
+
assert.equal(both.length, 2);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('process-manager: env scrub keeps PATH, drops secret-looking vars', () => {
|
|
31
|
+
const env = scrubEnv([]);
|
|
32
|
+
assert.ok(env.PATH !== undefined || env.Path !== undefined, 'PATH survives');
|
|
33
|
+
const probeKeys = ['NODE_AUTH_TOKEN', 'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'BIGMODEL_API_KEY'];
|
|
34
|
+
for (const k of probeKeys) assert.equal((env as Record<string, unknown>)[k], undefined, `${k} must not inherit`);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('client: full lifecycle against the fake server (initialize→sync→tier0→shutdown)', async () => {
|
|
38
|
+
const ws = await mkdtemp(join(tmpdir(), 'lsp-ws-'));
|
|
39
|
+
const manager = new ProcessManager({ id: 'fake', command: process.execPath, args: [FAKE_SERVER], source: 'explicit' }, ws);
|
|
40
|
+
const got: Array<{ uri: string; n: number }> = [];
|
|
41
|
+
const client = new LspClient(manager.start(), {
|
|
42
|
+
requestTimeoutMs: 10_000,
|
|
43
|
+
onDiagnostics: (uri, diags) => got.push({ uri, n: diags.length }),
|
|
44
|
+
});
|
|
45
|
+
const init = await client.initialize(fileToUri(ws));
|
|
46
|
+
assert.equal(init.serverInfo?.name, 'fake-lsp');
|
|
47
|
+
// document sync drives push diagnostics
|
|
48
|
+
const uri = fileToUri(join(ws, 'probe.ts'));
|
|
49
|
+
client.openDoc(uri, 'typescript', 'const x: number = DELBERATE_ERROR_MARKER;\n');
|
|
50
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
51
|
+
assert.ok(got.some((g) => g.uri === uri && g.n === 1), 'one diagnostic pushed for the marker file');
|
|
52
|
+
// tier-0 requests
|
|
53
|
+
const hover = await client.hover({ uri }, { line: 0, character: 6 });
|
|
54
|
+
assert.match(JSON.stringify(hover?.contents), /fake hover/);
|
|
55
|
+
const def = await client.definition({ uri }, { line: 0, character: 6 });
|
|
56
|
+
assert.equal(Array.isArray(def) ? def[0]?.uri : def?.uri, 'file:///fake/def.ts');
|
|
57
|
+
const refs = await client.references({ uri }, { line: 0, character: 6 });
|
|
58
|
+
assert.equal(refs.length, 2);
|
|
59
|
+
// Tier-1: implementation + call hierarchy
|
|
60
|
+
const impls = await client.implementation({ uri }, { line: 0, character: 6 });
|
|
61
|
+
assert.equal(Array.isArray(impls) ? impls.length : 1, 2);
|
|
62
|
+
const item = await client.prepareCallHierarchy({ uri }, { line: 0, character: 6 });
|
|
63
|
+
const first = Array.isArray(item) ? item[0] : item;
|
|
64
|
+
const incoming = await client.callHierarchyIncoming(first);
|
|
65
|
+
assert.equal(incoming.length, 2);
|
|
66
|
+
assert.equal((incoming[0]!.from as { name?: string }).name, 'callerOne');
|
|
67
|
+
const outgoing = await client.callHierarchyOutgoing(first);
|
|
68
|
+
assert.equal(outgoing.length, 1);
|
|
69
|
+
assert.equal((outgoing[0]!.to as { name?: string }).name, 'helperFn');
|
|
70
|
+
const syms = await client.documentSymbols({ uri });
|
|
71
|
+
assert.equal(syms[0]?.name, 'mainFn');
|
|
72
|
+
assert.equal(syms[0]?.children?.[0]?.name, 'inner');
|
|
73
|
+
// clean shutdown stops the process
|
|
74
|
+
await client.shutdown();
|
|
75
|
+
await manager.stop();
|
|
76
|
+
assert.equal(manager.running, false);
|
|
77
|
+
await rm(ws, { recursive: true, force: true });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('registry: health probe flags a broken shim as unhealthy (fake: a command that always fails)', async () => {
|
|
81
|
+
const { discoverServers } = await import('../registry.ts');
|
|
82
|
+
// real discovery on THIS machine: whatever exists must carry a verdict
|
|
83
|
+
const servers = discoverServers(true);
|
|
84
|
+
for (const s of servers) {
|
|
85
|
+
assert.ok(typeof s.healthy === 'boolean', `${s.id} must carry a health verdict`);
|
|
86
|
+
if (!s.healthy) assert.ok((s.unhealthyReason ?? '').length > 0, `${s.id} unhealthy must say why`);
|
|
87
|
+
}
|
|
88
|
+
// a nonexistent binary is simply not discovered; a failing one is
|
|
89
|
+
// discovered-but-unhealthy only when it exists on PATH — both states stay
|
|
90
|
+
// honest. (On this dev machine rust-analyzer is a component-missing shim
|
|
91
|
+
// and MUST be unhealthy if present.)
|
|
92
|
+
const rust = servers.find((s) => s.id === 'rust-analyzer');
|
|
93
|
+
if (rust && rust.unhealthyReason) console.log('rust-analyzer verdict:', rust.unhealthyReason.slice(0, 80));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('client: unknown method surfaces the server error honestly', async () => {
|
|
97
|
+
const ws = await mkdtemp(join(tmpdir(), 'lsp-ws-'));
|
|
98
|
+
const manager = new ProcessManager({ id: 'fake', command: process.execPath, args: [FAKE_SERVER], source: 'explicit' }, ws);
|
|
99
|
+
const client = new LspClient(manager.start(), { requestTimeoutMs: 10_000 });
|
|
100
|
+
await client.initialize(fileToUri(ws));
|
|
101
|
+
await assert.rejects(() => client.request('workspace/executeCommand', {}), /-32601|method not found/);
|
|
102
|
+
await client.shutdown();
|
|
103
|
+
await manager.stop();
|
|
104
|
+
await rm(ws, { recursive: true, force: true });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('process-manager: restart budget refuses flapping servers', async () => {
|
|
108
|
+
const ws = await mkdtemp(join(tmpdir(), 'lsp-ws-'));
|
|
109
|
+
const manager = new ProcessManager({ id: 'crasher', command: process.execPath, args: ['-e', 'process.exit(3)'], source: 'explicit' }, ws, { maxRestarts: 2 });
|
|
110
|
+
manager.start();
|
|
111
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
112
|
+
manager.start(); // restart 1
|
|
113
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
114
|
+
manager.start(); // restart 2
|
|
115
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
116
|
+
assert.throws(() => manager.start(), /restart budget/);
|
|
117
|
+
await manager.stop();
|
|
118
|
+
await rm(ws, { recursive: true, force: true });
|
|
119
|
+
});
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { test } from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtemp, rm, writeFile, readFile } from 'node:fs/promises';
|
|
4
|
-
import { tmpdir } from 'node:os';
|
|
5
|
-
import { join } from 'node:path';
|
|
6
|
-
import { checkTrust, trustServer, untrustServer, listTrust, serverHash, trustPath } from '../trust.ts';
|
|
7
|
-
|
|
8
|
-
async function tmpHome(): Promise<string> {
|
|
9
|
-
return mkdtemp(join(tmpdir(), 'lsp-trust-'));
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
test('trust: PATH server needs explicit trust; trust pins the hash; round-trip verifies', async () => {
|
|
13
|
-
const home = await tmpHome();
|
|
14
|
-
const bin = join(home, 'fake-server.bin');
|
|
15
|
-
await writeFile(bin, Buffer.from('fake server binary v1'));
|
|
16
|
-
const server = { id: 'fakepath', command: bin, origin: 'PATH' };
|
|
17
|
-
// untrusted initially
|
|
18
|
-
const v0 = await checkTrust(home, server);
|
|
19
|
-
assert.equal(v0.trusted, false);
|
|
20
|
-
assert.match(v0.reason, /hmh lsp trust fakepath/);
|
|
21
|
-
// trust pins the current hash
|
|
22
|
-
const t = await trustServer(home, server);
|
|
23
|
-
assert.equal(t.ok, true);
|
|
24
|
-
assert.equal(t.sha256, await serverHash(bin));
|
|
25
|
-
// now trusted, hash matches
|
|
26
|
-
const v1 = await checkTrust(home, server);
|
|
27
|
-
assert.equal(v1.trusted, true);
|
|
28
|
-
assert.match(v1.reason, /hash matches/);
|
|
29
|
-
// binary CHANGES -> tamper refusal with both hashes shown
|
|
30
|
-
await writeFile(bin, Buffer.from('tampered binary'));
|
|
31
|
-
const v2 = await checkTrust(home, server);
|
|
32
|
-
assert.equal(v2.trusted, false);
|
|
33
|
-
assert.match(v2.reason, /CHANGED/);
|
|
34
|
-
assert.equal(v2.tampered?.pinned, t.sha256);
|
|
35
|
-
assert.equal(v2.tampered?.actual, await serverHash(bin));
|
|
36
|
-
// untrust works and returns to untrusted
|
|
37
|
-
assert.equal(await untrustServer(home, 'fakepath'), true);
|
|
38
|
-
assert.equal((await checkTrust(home, server)).trusted, false);
|
|
39
|
-
assert.equal(await untrustServer(home, 'fakepath'), false);
|
|
40
|
-
await rm(home, { recursive: true, force: true });
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test('trust: DevEco-local servers auto-trust ONCE with pinning', async () => {
|
|
44
|
-
const home = await tmpHome();
|
|
45
|
-
const bin = join(home, 'clangd.exe');
|
|
46
|
-
await writeFile(bin, Buffer.from('official clangd bytes'));
|
|
47
|
-
const server = { id: 'clangd', command: bin, origin: 'DevEco-local' };
|
|
48
|
-
const v1 = await checkTrust(home, server);
|
|
49
|
-
assert.equal(v1.trusted, true);
|
|
50
|
-
assert.match(v1.reason, /auto-trusted/);
|
|
51
|
-
// the pin landed in the store (user-inspectable JSON)
|
|
52
|
-
const store = JSON.parse(await readFile(trustPath(home), 'utf8'));
|
|
53
|
-
assert.equal(store.entries.clangd.autoTrusted, true);
|
|
54
|
-
assert.equal(store.entries.clangd.sha256, await serverHash(bin));
|
|
55
|
-
// second check: now a normal trusted entry, still hash-matched
|
|
56
|
-
const v2 = await checkTrust(home, server);
|
|
57
|
-
assert.equal(v2.trusted, true);
|
|
58
|
-
// tamper detection applies to auto-trusted servers equally
|
|
59
|
-
await writeFile(bin, Buffer.from('evil'));
|
|
60
|
-
const v3 = await checkTrust(home, server);
|
|
61
|
-
assert.equal(v3.trusted, false);
|
|
62
|
-
assert.match(v3.reason, /CHANGED/);
|
|
63
|
-
// listTrust exposes the audit view
|
|
64
|
-
const entries = await listTrust(home);
|
|
65
|
-
assert.equal(entries.length, 1);
|
|
66
|
-
assert.equal(entries[0]!.id, 'clangd');
|
|
67
|
-
await rm(home, { recursive: true, force: true });
|
|
68
|
-
});
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { mkdtemp, rm, writeFile, readFile } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { checkTrust, trustServer, untrustServer, listTrust, serverHash, trustPath } from '../trust.ts';
|
|
7
|
+
|
|
8
|
+
async function tmpHome(): Promise<string> {
|
|
9
|
+
return mkdtemp(join(tmpdir(), 'lsp-trust-'));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
test('trust: PATH server needs explicit trust; trust pins the hash; round-trip verifies', async () => {
|
|
13
|
+
const home = await tmpHome();
|
|
14
|
+
const bin = join(home, 'fake-server.bin');
|
|
15
|
+
await writeFile(bin, Buffer.from('fake server binary v1'));
|
|
16
|
+
const server = { id: 'fakepath', command: bin, origin: 'PATH' };
|
|
17
|
+
// untrusted initially
|
|
18
|
+
const v0 = await checkTrust(home, server);
|
|
19
|
+
assert.equal(v0.trusted, false);
|
|
20
|
+
assert.match(v0.reason, /hmh lsp trust fakepath/);
|
|
21
|
+
// trust pins the current hash
|
|
22
|
+
const t = await trustServer(home, server);
|
|
23
|
+
assert.equal(t.ok, true);
|
|
24
|
+
assert.equal(t.sha256, await serverHash(bin));
|
|
25
|
+
// now trusted, hash matches
|
|
26
|
+
const v1 = await checkTrust(home, server);
|
|
27
|
+
assert.equal(v1.trusted, true);
|
|
28
|
+
assert.match(v1.reason, /hash matches/);
|
|
29
|
+
// binary CHANGES -> tamper refusal with both hashes shown
|
|
30
|
+
await writeFile(bin, Buffer.from('tampered binary'));
|
|
31
|
+
const v2 = await checkTrust(home, server);
|
|
32
|
+
assert.equal(v2.trusted, false);
|
|
33
|
+
assert.match(v2.reason, /CHANGED/);
|
|
34
|
+
assert.equal(v2.tampered?.pinned, t.sha256);
|
|
35
|
+
assert.equal(v2.tampered?.actual, await serverHash(bin));
|
|
36
|
+
// untrust works and returns to untrusted
|
|
37
|
+
assert.equal(await untrustServer(home, 'fakepath'), true);
|
|
38
|
+
assert.equal((await checkTrust(home, server)).trusted, false);
|
|
39
|
+
assert.equal(await untrustServer(home, 'fakepath'), false);
|
|
40
|
+
await rm(home, { recursive: true, force: true });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('trust: DevEco-local servers auto-trust ONCE with pinning', async () => {
|
|
44
|
+
const home = await tmpHome();
|
|
45
|
+
const bin = join(home, 'clangd.exe');
|
|
46
|
+
await writeFile(bin, Buffer.from('official clangd bytes'));
|
|
47
|
+
const server = { id: 'clangd', command: bin, origin: 'DevEco-local' };
|
|
48
|
+
const v1 = await checkTrust(home, server);
|
|
49
|
+
assert.equal(v1.trusted, true);
|
|
50
|
+
assert.match(v1.reason, /auto-trusted/);
|
|
51
|
+
// the pin landed in the store (user-inspectable JSON)
|
|
52
|
+
const store = JSON.parse(await readFile(trustPath(home), 'utf8'));
|
|
53
|
+
assert.equal(store.entries.clangd.autoTrusted, true);
|
|
54
|
+
assert.equal(store.entries.clangd.sha256, await serverHash(bin));
|
|
55
|
+
// second check: now a normal trusted entry, still hash-matched
|
|
56
|
+
const v2 = await checkTrust(home, server);
|
|
57
|
+
assert.equal(v2.trusted, true);
|
|
58
|
+
// tamper detection applies to auto-trusted servers equally
|
|
59
|
+
await writeFile(bin, Buffer.from('evil'));
|
|
60
|
+
const v3 = await checkTrust(home, server);
|
|
61
|
+
assert.equal(v3.trusted, false);
|
|
62
|
+
assert.match(v3.reason, /CHANGED/);
|
|
63
|
+
// listTrust exposes the audit view
|
|
64
|
+
const entries = await listTrust(home);
|
|
65
|
+
assert.equal(entries.length, 1);
|
|
66
|
+
assert.equal(entries[0]!.id, 'clangd');
|
|
67
|
+
await rm(home, { recursive: true, force: true });
|
|
68
|
+
});
|