@hmharness/lsp 0.23.18 → 0.23.20

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/index.d.ts CHANGED
@@ -12,3 +12,4 @@ export { ProcessManager, scrubEnv, fromPath, type ServerSpec, type ManagedServer
12
12
  export { LspClient, type LspClientOptions } from './client.js';
13
13
  export { discoverServers, serverForFile, fileToUri, type DiscoveredServer } from './registry.js';
14
14
  export { lspTools, shutdownLsp, diagnosticsSummary, type LspToolContext } from './tools.js';
15
+ export { checkTrust, trustServer, untrustServer, listTrust, serverHash, trustPath, type TrustEntry, type TrustStore, type TrustVerdict } from './trust.js';
package/dist/index.js CHANGED
@@ -12,3 +12,4 @@ export { ProcessManager, scrubEnv, fromPath } from "./process-manager.js";
12
12
  export { LspClient } from "./client.js";
13
13
  export { discoverServers, serverForFile, fileToUri } from "./registry.js";
14
14
  export { lspTools, shutdownLsp, diagnosticsSummary } from "./tools.js";
15
+ export { checkTrust, trustServer, untrustServer, listTrust, serverHash, trustPath } from "./trust.js";
package/dist/tools.d.ts CHANGED
@@ -10,6 +10,9 @@ import type { Tool } from '@hmharness/kernel';
10
10
  import type { Diagnostic } from './protocol.js';
11
11
  export interface LspToolContext {
12
12
  workspaceRoot: string;
13
+ /** HMH_HOME — required by the source-trust guard (Capability OS slice);
14
+ * when absent PATH servers are refused honestly */
15
+ home?: string;
13
16
  }
14
17
  /** Stop every client this module started (TUI/CLI shutdown path). */
15
18
  export declare function shutdownLsp(): Promise<void>;
package/dist/tools.js CHANGED
@@ -11,10 +11,24 @@ if (typeof process !== 'undefined') {
11
11
  mc.manager.killSync();
12
12
  });
13
13
  }
14
- async function clientFor(server, workspaceRoot) {
14
+ async function clientFor(server, workspaceRoot, home) {
15
15
  const existing = clients.get(server.id);
16
16
  if (existing)
17
17
  return existing;
18
+ // Capability OS slice (03 方案 supply-chain guard): no binary executes
19
+ // without source trust + a matching sha256 pin. PATH servers need one-time
20
+ // `hmh lsp trust <id>`; DevEco-local auto-trusts WITH pinning; a changed
21
+ // binary is refused as tampering.
22
+ if (home) {
23
+ const { checkTrust } = await import("./trust.js");
24
+ const verdict = await checkTrust(home, server);
25
+ if (!verdict.trusted) {
26
+ throw new Error(`lsp ${server.id} blocked by source trust: ${verdict.reason}`);
27
+ }
28
+ }
29
+ else if (!server.origin.includes('DevEco-local')) {
30
+ throw new Error(`lsp ${server.id} blocked: PATH servers need one-time trust (hmh lsp trust ${server.id})`);
31
+ }
18
32
  const manager = new ProcessManager(server, workspaceRoot);
19
33
  const diagnostics = new Map();
20
34
  const client = new LspClient(manager.start(), {
@@ -57,7 +71,7 @@ export function lspTools(ctx) {
57
71
  if (!server)
58
72
  return { output: `no language server for ${file} (available: ${servers().map((s) => s.id).join(', ') || 'none on PATH'})`, isError: true };
59
73
  const abs = resolveFile(ctx, file);
60
- const mc = await clientFor(server, ctx.workspaceRoot);
74
+ const mc = await clientFor(server, ctx.workspaceRoot, ctx.home);
61
75
  return { output: await fn(mc, fileToUri(abs)) };
62
76
  };
63
77
  return [
@@ -0,0 +1,45 @@
1
+ export interface TrustEntry {
2
+ id: string;
3
+ command: string;
4
+ sha256: string;
5
+ origin: string;
6
+ trustedAt: string;
7
+ autoTrusted: boolean;
8
+ }
9
+ export interface TrustStore {
10
+ kind: 'hmharness-lsp-trust';
11
+ version: 1;
12
+ entries: Record<string, TrustEntry>;
13
+ }
14
+ export declare function trustPath(home: string): string;
15
+ /** sha256 of the server binary (the executable file itself). */
16
+ export declare function serverHash(command: string): Promise<string>;
17
+ export interface TrustVerdict {
18
+ trusted: boolean;
19
+ reason: string;
20
+ /** set when the id is trusted but the BINARY changed (tamper case) */
21
+ tampered?: {
22
+ pinned: string;
23
+ actual: string;
24
+ };
25
+ }
26
+ /** Check whether a discovered server may be executed. */
27
+ export declare function checkTrust(home: string, server: {
28
+ id: string;
29
+ command: string;
30
+ origin: string;
31
+ }): Promise<TrustVerdict>;
32
+ /** Explicit user trust: pins the CURRENT hash. */
33
+ export declare function trustServer(home: string, server: {
34
+ id: string;
35
+ command: string;
36
+ origin: string;
37
+ }): Promise<{
38
+ ok: boolean;
39
+ error?: string;
40
+ sha256?: string;
41
+ }>;
42
+ /** Revoke trust for a server id. */
43
+ export declare function untrustServer(home: string, id: string): Promise<boolean>;
44
+ /** Read-only view for `hmh lsp list`. */
45
+ export declare function listTrust(home: string): Promise<TrustEntry[]>;
package/dist/trust.js ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @hmharness/lsp - source trust (Capability OS first slice, 03 方案 supply-chain guard)
3
+ *
4
+ * Running a language server means executing a binary we discovered on the
5
+ * machine. The 03 plan's supply-chain rule: 来源白名单 + hash. This module
6
+ * is that guard:
7
+ *
8
+ * - every server binary gets a sha256 PIN at trust time
9
+ * - DevEco-local servers (official IDE install layout) are auto-trusted
10
+ * ONCE — the pin still applies, so a modified binary is caught
11
+ * - PATH servers require explicit one-time user trust (hmh lsp trust <id>)
12
+ * - a trusted binary whose hash CHANGED is refused (tamper detection),
13
+ * with the old and new hash both shown for audit
14
+ *
15
+ * The trust store lives in HMH_HOME/cognitive/lsp-trust.json — plain JSON,
16
+ * user-inspectable, no telemetry.
17
+ */
18
+ import { createHash } from 'node:crypto';
19
+ import { readFile, writeFile, mkdir } from 'node:fs/promises';
20
+ import { join } from 'node:path';
21
+ export function trustPath(home) {
22
+ return join(home, 'cognitive', 'lsp-trust.json');
23
+ }
24
+ async function loadTrust(home) {
25
+ try {
26
+ const j = JSON.parse(await readFile(trustPath(home), 'utf8'));
27
+ if (j && j.kind === 'hmharness-lsp-trust' && j.entries && typeof j.entries === 'object')
28
+ return j;
29
+ }
30
+ catch { /* absent/corrupt = empty store */ }
31
+ return { kind: 'hmharness-lsp-trust', version: 1, entries: {} };
32
+ }
33
+ /** sha256 of the server binary (the executable file itself). */
34
+ export async function serverHash(command) {
35
+ const buf = await readFile(command);
36
+ return createHash('sha256').update(buf).digest('hex');
37
+ }
38
+ /** Check whether a discovered server may be executed. */
39
+ export async function checkTrust(home, server) {
40
+ const store = await loadTrust(home);
41
+ const entry = store.entries[server.id];
42
+ const actual = await serverHash(server.command).catch(() => '');
43
+ if (!entry) {
44
+ if (server.origin.includes('DevEco-local')) {
45
+ // official IDE install layout: auto-trust once, pin the hash now
46
+ const pinned = { id: server.id, command: server.command, sha256: actual, origin: server.origin, trustedAt: new Date().toISOString(), autoTrusted: true };
47
+ const next = { ...store, entries: { ...store.entries, [server.id]: pinned } };
48
+ await mkdir(join(home, 'cognitive'), { recursive: true });
49
+ await writeFile(trustPath(home), JSON.stringify(next, null, 1), 'utf8');
50
+ return { trusted: true, reason: `auto-trusted (official DevEco-local layout), sha256 pinned ${actual.slice(0, 12)}…` };
51
+ }
52
+ return { trusted: false, reason: `PATH server not trusted yet — run: hmh lsp trust ${server.id}` };
53
+ }
54
+ if (entry.sha256 && actual && entry.sha256 !== actual) {
55
+ return { trusted: false, reason: `binary CHANGED since trust (tamper?) — pinned ${entry.sha256.slice(0, 12)}…, actual ${actual.slice(0, 12)}…; re-trust only if you know why it changed`, tampered: { pinned: entry.sha256, actual } };
56
+ }
57
+ return { trusted: true, reason: `trusted since ${entry.trustedAt.slice(0, 10)} (${entry.autoTrusted ? 'auto, DevEco-local' : 'user'}), hash matches` };
58
+ }
59
+ /** Explicit user trust: pins the CURRENT hash. */
60
+ export async function trustServer(home, server) {
61
+ const sha = await serverHash(server.command).catch(() => '');
62
+ if (!sha)
63
+ return { ok: false, error: 'cannot hash the server binary (unreadable?)' };
64
+ const store = await loadTrust(home);
65
+ store.entries[server.id] = { id: server.id, command: server.command, sha256: sha, origin: server.origin, trustedAt: new Date().toISOString(), autoTrusted: false };
66
+ await mkdir(join(home, 'cognitive'), { recursive: true });
67
+ await writeFile(trustPath(home), JSON.stringify(store, null, 1), 'utf8');
68
+ return { ok: true, sha256: sha };
69
+ }
70
+ /** Revoke trust for a server id. */
71
+ export async function untrustServer(home, id) {
72
+ const store = await loadTrust(home);
73
+ if (!store.entries[id])
74
+ return false;
75
+ delete store.entries[id];
76
+ await writeFile(trustPath(home), JSON.stringify(store, null, 1), 'utf8');
77
+ return true;
78
+ }
79
+ /** Read-only view for `hmh lsp list`. */
80
+ export async function listTrust(home) {
81
+ const store = await loadTrust(home);
82
+ return Object.values(store.entries);
83
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/lsp",
3
- "version": "0.23.18",
3
+ "version": "0.23.20",
4
4
  "description": "hmharness LSP Client/Manager — drives external language servers (LSP 3.18 over stdio) for agent code intelligence. Client only: we never implement language servers.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -15,7 +15,7 @@
15
15
  "build": "tsc -p tsconfig.build.json"
16
16
  },
17
17
  "dependencies": {
18
- "@hmharness/kernel": "0.23.18"
18
+ "@hmharness/kernel": "0.23.20"
19
19
  },
20
20
  "engines": {
21
21
  "node": ">=22"
@@ -0,0 +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
+ });
package/src/index.ts CHANGED
@@ -12,3 +12,4 @@ export { ProcessManager, scrubEnv, fromPath, type ServerSpec, type ManagedServer
12
12
  export { LspClient, type LspClientOptions } from './client.ts';
13
13
  export { discoverServers, serverForFile, fileToUri, type DiscoveredServer } from './registry.ts';
14
14
  export { lspTools, shutdownLsp, diagnosticsSummary, type LspToolContext } from './tools.ts';
15
+ export { checkTrust, trustServer, untrustServer, listTrust, serverHash, trustPath, type TrustEntry, type TrustStore, type TrustVerdict } from './trust.ts';
package/src/tools.ts CHANGED
@@ -14,6 +14,9 @@ import type { Diagnostic } from './protocol.ts';
14
14
 
15
15
  export interface LspToolContext {
16
16
  workspaceRoot: string;
17
+ /** HMH_HOME — required by the source-trust guard (Capability OS slice);
18
+ * when absent PATH servers are refused honestly */
19
+ home?: string;
17
20
  }
18
21
 
19
22
  interface ManagedClient {
@@ -34,9 +37,22 @@ if (typeof process !== 'undefined') {
34
37
  });
35
38
  }
36
39
 
37
- async function clientFor(server: DiscoveredServer, workspaceRoot: string): Promise<ManagedClient> {
40
+ async function clientFor(server: DiscoveredServer, workspaceRoot: string, home?: string): Promise<ManagedClient> {
38
41
  const existing = clients.get(server.id);
39
42
  if (existing) return existing;
43
+ // Capability OS slice (03 方案 supply-chain guard): no binary executes
44
+ // without source trust + a matching sha256 pin. PATH servers need one-time
45
+ // `hmh lsp trust <id>`; DevEco-local auto-trusts WITH pinning; a changed
46
+ // binary is refused as tampering.
47
+ if (home) {
48
+ const { checkTrust } = await import('./trust.ts');
49
+ const verdict = await checkTrust(home, server);
50
+ if (!verdict.trusted) {
51
+ throw new Error(`lsp ${server.id} blocked by source trust: ${verdict.reason}`);
52
+ }
53
+ } else if (!server.origin.includes('DevEco-local')) {
54
+ throw new Error(`lsp ${server.id} blocked: PATH servers need one-time trust (hmh lsp trust ${server.id})`);
55
+ }
40
56
  const manager = new ProcessManager(server, workspaceRoot);
41
57
  const diagnostics = new Map<string, Diagnostic[]>();
42
58
  const client = new LspClient(manager.start(), {
@@ -83,7 +99,7 @@ export function lspTools(ctx: LspToolContext): Tool[] {
83
99
  const server = pick(file);
84
100
  if (!server) return { output: `no language server for ${file} (available: ${servers().map((s) => s.id).join(', ') || 'none on PATH'})`, isError: true };
85
101
  const abs = resolveFile(ctx, file);
86
- const mc = await clientFor(server, ctx.workspaceRoot);
102
+ const mc = await clientFor(server, ctx.workspaceRoot, ctx.home);
87
103
  return { output: await fn(mc, fileToUri(abs)) };
88
104
  };
89
105
 
package/src/trust.ts ADDED
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @hmharness/lsp - source trust (Capability OS first slice, 03 方案 supply-chain guard)
3
+ *
4
+ * Running a language server means executing a binary we discovered on the
5
+ * machine. The 03 plan's supply-chain rule: 来源白名单 + hash. This module
6
+ * is that guard:
7
+ *
8
+ * - every server binary gets a sha256 PIN at trust time
9
+ * - DevEco-local servers (official IDE install layout) are auto-trusted
10
+ * ONCE — the pin still applies, so a modified binary is caught
11
+ * - PATH servers require explicit one-time user trust (hmh lsp trust <id>)
12
+ * - a trusted binary whose hash CHANGED is refused (tamper detection),
13
+ * with the old and new hash both shown for audit
14
+ *
15
+ * The trust store lives in HMH_HOME/cognitive/lsp-trust.json — plain JSON,
16
+ * user-inspectable, no telemetry.
17
+ */
18
+ import { createHash } from 'node:crypto';
19
+ import { readFile, writeFile, mkdir } from 'node:fs/promises';
20
+ import { join } from 'node:path';
21
+
22
+ export interface TrustEntry {
23
+ id: string;
24
+ command: string;
25
+ sha256: string;
26
+ origin: string;
27
+ trustedAt: string;
28
+ autoTrusted: boolean;
29
+ }
30
+
31
+ export interface TrustStore {
32
+ kind: 'hmharness-lsp-trust';
33
+ version: 1;
34
+ entries: Record<string, TrustEntry>;
35
+ }
36
+
37
+ export function trustPath(home: string): string {
38
+ return join(home, 'cognitive', 'lsp-trust.json');
39
+ }
40
+
41
+ async function loadTrust(home: string): Promise<TrustStore> {
42
+ try {
43
+ const j = JSON.parse(await readFile(trustPath(home), 'utf8')) as TrustStore;
44
+ if (j && j.kind === 'hmharness-lsp-trust' && j.entries && typeof j.entries === 'object') return j;
45
+ } catch { /* absent/corrupt = empty store */ }
46
+ return { kind: 'hmharness-lsp-trust', version: 1, entries: {} };
47
+ }
48
+
49
+ /** sha256 of the server binary (the executable file itself). */
50
+ export async function serverHash(command: string): Promise<string> {
51
+ const buf = await readFile(command);
52
+ return createHash('sha256').update(buf).digest('hex');
53
+ }
54
+
55
+ export interface TrustVerdict {
56
+ trusted: boolean;
57
+ reason: string;
58
+ /** set when the id is trusted but the BINARY changed (tamper case) */
59
+ tampered?: { pinned: string; actual: string };
60
+ }
61
+
62
+ /** Check whether a discovered server may be executed. */
63
+ export async function checkTrust(home: string, server: { id: string; command: string; origin: string }): Promise<TrustVerdict> {
64
+ const store = await loadTrust(home);
65
+ const entry = store.entries[server.id];
66
+ const actual = await serverHash(server.command).catch(() => '');
67
+ if (!entry) {
68
+ if (server.origin.includes('DevEco-local')) {
69
+ // official IDE install layout: auto-trust once, pin the hash now
70
+ const pinned: TrustEntry = { id: server.id, command: server.command, sha256: actual, origin: server.origin, trustedAt: new Date().toISOString(), autoTrusted: true };
71
+ const next = { ...store, entries: { ...store.entries, [server.id]: pinned } };
72
+ await mkdir(join(home, 'cognitive'), { recursive: true });
73
+ await writeFile(trustPath(home), JSON.stringify(next, null, 1), 'utf8');
74
+ return { trusted: true, reason: `auto-trusted (official DevEco-local layout), sha256 pinned ${actual.slice(0, 12)}…` };
75
+ }
76
+ return { trusted: false, reason: `PATH server not trusted yet — run: hmh lsp trust ${server.id}` };
77
+ }
78
+ if (entry.sha256 && actual && entry.sha256 !== actual) {
79
+ return { trusted: false, reason: `binary CHANGED since trust (tamper?) — pinned ${entry.sha256.slice(0, 12)}…, actual ${actual.slice(0, 12)}…; re-trust only if you know why it changed`, tampered: { pinned: entry.sha256, actual } };
80
+ }
81
+ return { trusted: true, reason: `trusted since ${entry.trustedAt.slice(0, 10)} (${entry.autoTrusted ? 'auto, DevEco-local' : 'user'}), hash matches` };
82
+ }
83
+
84
+ /** Explicit user trust: pins the CURRENT hash. */
85
+ export async function trustServer(home: string, server: { id: string; command: string; origin: string }): Promise<{ ok: boolean; error?: string; sha256?: string }> {
86
+ const sha = await serverHash(server.command).catch(() => '');
87
+ if (!sha) return { ok: false, error: 'cannot hash the server binary (unreadable?)' };
88
+ const store = await loadTrust(home);
89
+ store.entries[server.id] = { id: server.id, command: server.command, sha256: sha, origin: server.origin, trustedAt: new Date().toISOString(), autoTrusted: false };
90
+ await mkdir(join(home, 'cognitive'), { recursive: true });
91
+ await writeFile(trustPath(home), JSON.stringify(store, null, 1), 'utf8');
92
+ return { ok: true, sha256: sha };
93
+ }
94
+
95
+ /** Revoke trust for a server id. */
96
+ export async function untrustServer(home: string, id: string): Promise<boolean> {
97
+ const store = await loadTrust(home);
98
+ if (!store.entries[id]) return false;
99
+ delete store.entries[id];
100
+ await writeFile(trustPath(home), JSON.stringify(store, null, 1), 'utf8');
101
+ return true;
102
+ }
103
+
104
+ /** Read-only view for `hmh lsp list`. */
105
+ export async function listTrust(home: string): Promise<TrustEntry[]> {
106
+ const store = await loadTrust(home);
107
+ return Object.values(store.entries);
108
+ }