opencode-ahp 0.1.8 → 0.1.10

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.
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Directory allowlist (design §3.3): the optional
3
+ * `allowedWorkingDirectories` option — an allowlist of absolute path
4
+ * prefixes. When configured, client-named paths must resolve under one of
5
+ * the prefixes (after symlink resolution): the
6
+ * `createSession.workingDirectories` target and the unconfined `resource*`
7
+ * commands (`resourceResolve` / `resourceList` / `resourceMkdir`).
8
+ *
9
+ * Pure decision logic — filesystem access is injected
10
+ * (`PathPolicyEnv`), so unit tests never touch the disk.
11
+ */
12
+
13
+ import { isAbsolute, normalize } from "node:path";
14
+ import { isPathInside } from "./host-util.js";
15
+
16
+ /**
17
+ * The filesystem half of the decision, injected to keep this module pure:
18
+ * canonicalize the symlinks of the EXISTING prefix portion of `path` —
19
+ * walk up to the deepest existing ancestor, canonicalize it, and re-append
20
+ * the missing tail. The result may not yet exist (the tail is never
21
+ * created). May throw (e.g. EACCES on an existing ancestor).
22
+ */
23
+ export interface PathPolicyEnv {
24
+ resolveExisting: (path: string) => Promise<string>;
25
+ }
26
+
27
+ /**
28
+ * The directory allowlist. `prefixes` are already normalized (see
29
+ * `normalizeDirPrefix`; the plugin entry additionally canonicalizes the
30
+ * ones that exist), `env` supplies the target resolution. An empty prefix
31
+ * list is the unrestricted policy.
32
+ */
33
+ export class PathPolicy {
34
+ private readonly prefixes: readonly string[];
35
+ private readonly env?: PathPolicyEnv;
36
+
37
+ private constructor(prefixes: readonly string[], env?: PathPolicyEnv) {
38
+ this.prefixes = prefixes;
39
+ this.env = env;
40
+ }
41
+
42
+ /** No prefixes — every path is permitted. */
43
+ static unrestricted(): PathPolicy {
44
+ return new PathPolicy([]);
45
+ }
46
+
47
+ static restricted(prefixes: readonly string[], env: PathPolicyEnv): PathPolicy {
48
+ return new PathPolicy(prefixes, env);
49
+ }
50
+
51
+ /**
52
+ * Whether `path` is permitted: its symlink-resolved form (existing
53
+ * prefix portion canonicalized, missing tail re-appended) must be a
54
+ * prefix itself or strictly inside one. When the resolved form cannot
55
+ * be determined (the resolver throws — e.g. EACCES on an existing
56
+ * ancestor) the decision defers: the underlying operation runs and
57
+ * produces its own errno error.
58
+ */
59
+ async allows(path: string): Promise<boolean> {
60
+ if (this.prefixes.length === 0) return true;
61
+ let resolved: string;
62
+ try {
63
+ resolved = await this.env!.resolveExisting(path);
64
+ } catch {
65
+ return true;
66
+ }
67
+ return this.prefixes.some((prefix) => isPathInside(resolved, prefix));
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Lexically normalize an allowlist prefix: resolve `.`/`..` segments,
73
+ * drop the trailing slash (root stays `/`). Throws when `p` is not
74
+ * absolute — a relative prefix would silently scope the host to the wrong
75
+ * place, so it is a configuration error (the plugin entry fails closed:
76
+ * AHP off, opencode keeps serving).
77
+ */
78
+ export function normalizeDirPrefix(p: string): string {
79
+ if (!isAbsolute(p)) throw new Error(`not an absolute path: ${p}`);
80
+ let n = normalize(p);
81
+ // POSIX preserves a leading double slash; on Linux it is just `/`.
82
+ if (n.startsWith("//")) n = n.slice(1);
83
+ if (n.length > 1 && n.endsWith("/")) n = n.slice(0, -1);
84
+ return n;
85
+ }
package/src/transport.ts CHANGED
@@ -157,7 +157,9 @@ interface BunUpgradeResult {
157
157
  }
158
158
  interface BunServer {
159
159
  port: number;
160
- stop(): void;
160
+ /** `force`: synchronously drain every connection instead of letting
161
+ * open WebSockets outlive the stop (Bun server.stop(force?)). */
162
+ stop(force?: boolean): void;
161
163
  upgrade(req: BunRequest, options?: { data?: unknown }): BunUpgradeResult;
162
164
  }
163
165
  interface BunServeOptions {
@@ -270,7 +272,14 @@ export function createListener(opts: ListenerOptions, callbacks: TransportCallba
270
272
  if (ws && ws.readyState === 1) ws.close(code, reason);
271
273
  },
272
274
  stop() {
273
- server.stop();
275
+ // Force stop: a graceful stop lets open WebSockets drain on their
276
+ // own, and the port stays bound while they do — a location reload
277
+ // racing that window fails to rebind ("Is port in use?"), leaving
278
+ // the fresh host listener-less while the stale socket keeps serving
279
+ // (observed on the LAN test server). Teardown means
280
+ // "the location is going" anyway, so dropping the connections is
281
+ // the right semantics regardless.
282
+ server.stop(true);
274
283
  openSockets.clear();
275
284
  },
276
285
  };