my-durable 0.0.56 → 0.0.58

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.
@@ -61,7 +61,13 @@ import {
61
61
  type GraphRunRecord,
62
62
  type GraphTask,
63
63
  } from "./graphs.js";
64
- import { ScheduleRegistry, type ScheduleCreateInput, type ScheduleRecord } from "./schedules.js";
64
+ import {
65
+ MAX_SCHEDULE_NAME_LENGTH,
66
+ ScheduleRegistry,
67
+ normaliseCwd,
68
+ type ScheduleCreateInput,
69
+ type ScheduleRecord,
70
+ } from "./schedules.js";
65
71
  import {
66
72
  listArchived,
67
73
  findBundle,
@@ -255,6 +261,25 @@ export function lockFile(path: string) {
255
261
  type Opened = { harness: Harness; unlock: () => void; unsubscribe: () => void; jobs: Jobs; addons: Addons };
256
262
  type Resolved = { row: SessionRow; h: Harness; c: Conversation };
257
263
 
264
+ /**
265
+ * Is `cwd` the project root `root` itself, or inside it? Pure and
266
+ * platform-injected (the gitPathFor convention): on win32 backslashes and
267
+ * forward slashes compare equal and drive/path case is folded, while POSIX
268
+ * stays byte-identical (a backslash is a legal filename character there).
269
+ * Trailing slashes are normalized on both sides, and the prefix match is
270
+ * boundary-checked, so `D:/src/repo-other` is NOT under `D:/src/repo`.
271
+ */
272
+ export function pathsUnder(root: string, cwd: string, platform: NodeJS.Platform = process.platform): boolean {
273
+ const norm = (p: string): string => {
274
+ const slashed = platform === "win32" ? p.replace(/\\/g, "/") : p;
275
+ const trimmed = slashed.length > 1 ? slashed.replace(/\/+$/, "") : slashed;
276
+ return platform === "win32" ? trimmed.toLowerCase() : trimmed;
277
+ };
278
+ const base = norm(root);
279
+ const path = norm(cwd);
280
+ return path === base || path.startsWith(`${base}/`);
281
+ }
282
+
258
283
  /**
259
284
  * Bounded, secret-free description of the error that failed a Session. It names the error class only: never the
260
285
  * cause message, so no SQL values, tokens or transcript can reach a REST error message. The library reports the
@@ -746,6 +771,30 @@ export class Store {
746
771
  return (await this.startDirRoots())[0] ?? resolve(this.startDirs[0] ?? homedir());
747
772
  }
748
773
 
774
+ /** Start-dir containment for a would-be session cwd — the rule behind
775
+ * `create` and every schedule record. `~` expands, a relative path resolves
776
+ * against the FIRST start dir (what `create` has always done), and the
777
+ * resolved path must land inside the start-dir roots: the bare `resolve()`
778
+ * in create used to let an absolute cwd override the base entirely, so
779
+ * {"cwd":"/etc"} could root a session — or a schedule's run — outside the
780
+ * sandbox the operator configured. A path that does not exist (a schedule
781
+ * may point at a mount that is offline right now) is judged lexically, like
782
+ * `containedPath`. Read paths never re-check: existing sessions whose cwd
783
+ * predates the rule are not retroactively broken. */
784
+ async assertSessionCwdAllowed(input: string): Promise<void> {
785
+ const expanded = input === "~" ? homedir() : input.startsWith("~/") ? join(homedir(), input.slice(2)) : input;
786
+ const base = this.startDirs[0] ?? homedir();
787
+ let candidate: string;
788
+ try {
789
+ candidate = await realpath(resolve(base, expanded));
790
+ } catch {
791
+ candidate = resolve(base, expanded);
792
+ }
793
+ const roots = await this.startDirRoots();
794
+ if (!this.underRoots(candidate, roots))
795
+ throw invalid(`cwd must live under a start dir (${roots.join(", ") || "none configured on this device"})`);
796
+ }
797
+
749
798
  async directories(): Promise<DirListing> {
750
799
  const dirs: { name: string; path: string }[] = [];
751
800
  for (const root of this.startDirs) {
@@ -1093,6 +1142,11 @@ export class Store {
1093
1142
  } catch {
1094
1143
  throw invalid(`cwd must be an existing folder on ${this.name}`);
1095
1144
  }
1145
+ // Same containment rule as the fs/git routes: the resolve() above lets an
1146
+ // absolute cwd override the start-dir base entirely, which is how
1147
+ // {"cwd":"/etc"} used to root a session outside the sandbox. This is the
1148
+ // single choke point — schedule runs and graph runs create here too.
1149
+ await this.assertSessionCwdAllowed(path);
1096
1150
  // Every session roots a project: the registry is the tree's group-by-cwd,
1097
1151
  // promoted to an entity you can name, label and archive (paseo model).
1098
1152
  const status = await gitStatus(cwd).catch(() => undefined);
@@ -1169,7 +1223,15 @@ export class Store {
1169
1223
 
1170
1224
  rename(row: SessionRow, name: unknown) {
1171
1225
  if (typeof name !== "string" || !name.trim()) throw invalid("A name is required");
1172
- this.catalog.prepare("UPDATE sessions SET name=? WHERE id=?").run(name.trim().slice(0, 100), row.id);
1226
+ const trimmed = name.trim();
1227
+ // Same cap and phrasing as a schedule's name — a display name, not a
1228
+ // description (the old slice(0,100) silently swallowed a 10k paste).
1229
+ if (trimmed.length > MAX_SCHEDULE_NAME_LENGTH)
1230
+ throw invalid(`name is ${trimmed.length} characters; the limit is ${MAX_SCHEDULE_NAME_LENGTH}`);
1231
+ // Control characters never belong in a display name: reject rather than
1232
+ // store verbatim (a newline) or silently truncate at a NUL.
1233
+ if (/[\u0000-\u001f\u007f]/.test(trimmed)) throw invalid("name must not contain control characters");
1234
+ this.catalog.prepare("UPDATE sessions SET name=? WHERE id=?").run(trimmed, row.id);
1173
1235
  this.notify();
1174
1236
  return this.sessionSummary(this.row(row.id));
1175
1237
  }
@@ -2179,12 +2241,18 @@ export class Store {
2179
2241
  }
2180
2242
 
2181
2243
  async createSchedule(input: ScheduleCreateInput): Promise<ScheduleView> {
2244
+ // The registry stores cwd as typed (it never stats), so an out-of-sandbox
2245
+ // cwd used to surface only when the run spawned its session. Reject the
2246
+ // POST up front with the same rule Store.create applies.
2247
+ if (typeof input?.cwd === "string" && input.cwd.trim()) await this.assertSessionCwdAllowed(normaliseCwd(input.cwd));
2182
2248
  const record = await this.schedules.create(input);
2183
2249
  this.notify();
2184
2250
  return this.scheduleView(record);
2185
2251
  }
2186
2252
 
2187
2253
  async updateSchedule(id: string, patch: SchedulePatchBody): Promise<ScheduleView> {
2254
+ // Same early refusal when a patch repoints the schedule at /etc.
2255
+ if (typeof patch?.cwd === "string" && patch.cwd.trim()) await this.assertSessionCwdAllowed(normaliseCwd(patch.cwd));
2188
2256
  const record = await this.schedules.update(id, patch);
2189
2257
  this.notify();
2190
2258
  return this.scheduleView(record);
@@ -2298,8 +2366,9 @@ export class Store {
2298
2366
  private projectForPath(cwd: string): ProjectRecord | undefined {
2299
2367
  let best: ProjectRecord | undefined;
2300
2368
  for (const project of this.projects.list({ includeArchived: false })) {
2301
- const root = project.rootPath.endsWith("/") ? project.rootPath : `${project.rootPath}/`;
2302
- const isUnder = cwd === project.rootPath || cwd.startsWith(root);
2369
+ // Slash/case-normalized compare (pathsUnder): a session cwd recorded as
2370
+ // `D:\src\repo` must match a project registered as `D:/src/repo`.
2371
+ const isUnder = pathsUnder(project.rootPath, cwd);
2303
2372
  if (isUnder && (!best || project.rootPath.length > best.rootPath.length)) best = project;
2304
2373
  }
2305
2374
  return best;