harnery 0.41.0 → 0.43.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/dist/commander.d.ts +16 -0
- package/dist/commander.d.ts.map +1 -1
- package/dist/commander.js +21 -0
- package/dist/commands/agents.d.ts.map +1 -1
- package/dist/commands/agents.js +63 -5
- package/dist/commands/artifacts.d.ts.map +1 -1
- package/dist/commands/artifacts.js +113 -12
- package/dist/commands/browse.d.ts +164 -0
- package/dist/commands/browse.d.ts.map +1 -1
- package/dist/commands/browse.js +263 -51
- package/dist/commands/claude-desktop.d.ts.map +1 -1
- package/dist/commands/claude-desktop.js +52 -0
- package/dist/commands/disk.d.ts +6 -0
- package/dist/commands/disk.d.ts.map +1 -0
- package/dist/commands/disk.js +80 -0
- package/dist/commands/doctor.d.ts +8 -0
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +48 -1
- package/dist/commands/files.d.ts +16 -0
- package/dist/commands/files.d.ts.map +1 -1
- package/dist/commands/files.js +54 -2
- package/dist/commands/init.d.ts +2 -2
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +43 -9
- package/dist/commands/rm.d.ts +4 -0
- package/dist/commands/rm.d.ts.map +1 -0
- package/dist/commands/rm.js +87 -0
- package/dist/commands/servers.d.ts +7 -0
- package/dist/commands/servers.d.ts.map +1 -0
- package/dist/commands/servers.js +394 -0
- package/dist/commands/tunnel.d.ts +65 -0
- package/dist/commands/tunnel.d.ts.map +1 -1
- package/dist/commands/tunnel.js +372 -103
- package/dist/commands/web-fetch.d.ts +3 -2
- package/dist/commands/web-fetch.d.ts.map +1 -1
- package/dist/commands/web-fetch.js +25 -19
- package/dist/commands/web.d.ts +7 -1
- package/dist/commands/web.d.ts.map +1 -1
- package/dist/commands/web.js +9 -3
- package/dist/core/adapters/profiles.d.ts +9 -0
- package/dist/core/adapters/profiles.d.ts.map +1 -1
- package/dist/core/adapters/profiles.js +22 -1
- package/dist/core/adapters/types.d.ts +9 -0
- package/dist/core/adapters/types.d.ts.map +1 -1
- package/dist/core/agents/cli.js +13 -6
- package/dist/core/agents/render/prompt-context.d.ts.map +1 -1
- package/dist/core/agents/render/prompt-context.js +11 -5
- package/dist/core/agents/render/session-context.d.ts.map +1 -1
- package/dist/core/agents/render/session-context.js +59 -13
- package/dist/core/agents/state/live-coordination-view.d.ts +20 -0
- package/dist/core/agents/state/live-coordination-view.d.ts.map +1 -1
- package/dist/core/agents/state/live-coordination-view.js +60 -0
- package/dist/core/agents/state/names.d.ts +43 -3
- package/dist/core/agents/state/names.d.ts.map +1 -1
- package/dist/core/agents/state/names.js +87 -3
- package/dist/core/artifacts/delivery-card.js +9 -2
- package/dist/core/artifacts/index.d.ts +140 -4
- package/dist/core/artifacts/index.d.ts.map +1 -1
- package/dist/core/artifacts/index.js +522 -49
- package/dist/core/config.d.ts +68 -11
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +128 -3
- package/dist/core/diagnostics/bundle.d.ts.map +1 -1
- package/dist/core/diagnostics/bundle.js +2 -2
- package/dist/core/events/v3/producers/recorder.d.ts.map +1 -1
- package/dist/core/events/v3/producers/recorder.js +16 -0
- package/dist/core/events/v3/projection.js +9 -1
- package/dist/core/events/v3/support-storage/pack-reader.d.ts.map +1 -1
- package/dist/core/events/v3/support-storage/pack-reader.js +42 -16
- package/dist/core/hooks/adapter/events.d.ts +17 -0
- package/dist/core/hooks/adapter/events.d.ts.map +1 -1
- package/dist/core/hooks/adapter/events.js +5 -0
- package/dist/core/hooks/adapter/runtime-telemetry.js +3 -1
- package/dist/core/hooks/adapter/wiring.d.ts +37 -7
- package/dist/core/hooks/adapter/wiring.d.ts.map +1 -1
- package/dist/core/hooks/adapter/wiring.js +62 -4
- package/dist/core/hooks/cli.js +131 -23
- package/dist/core/hooks/fork-lineage.d.ts +40 -0
- package/dist/core/hooks/fork-lineage.d.ts.map +1 -0
- package/dist/core/hooks/fork-lineage.js +62 -0
- package/dist/core/hooks/resolve/transcript.d.ts +16 -0
- package/dist/core/hooks/resolve/transcript.d.ts.map +1 -1
- package/dist/core/hooks/resolve/transcript.js +84 -0
- package/dist/core/hooks/run-markers.d.ts +37 -0
- package/dist/core/hooks/run-markers.d.ts.map +1 -0
- package/dist/core/hooks/run-markers.js +109 -0
- package/dist/core/hooks/shell-rm.d.ts +2 -0
- package/dist/core/hooks/shell-rm.d.ts.map +1 -0
- package/dist/core/hooks/shell-rm.js +105 -0
- package/dist/core/hooks/shell-tokens.d.ts +12 -0
- package/dist/core/hooks/shell-tokens.d.ts.map +1 -0
- package/dist/core/hooks/shell-tokens.js +97 -0
- package/dist/core/hooks/shell-waiter.d.ts.map +1 -1
- package/dist/core/hooks/shell-waiter.js +3 -93
- package/dist/core/semantic/contract.d.ts +5 -5
- package/dist/core/servers/index.d.ts +211 -0
- package/dist/core/servers/index.d.ts.map +1 -0
- package/dist/core/servers/index.js +560 -0
- package/dist/core/servers/net.d.ts +58 -0
- package/dist/core/servers/net.d.ts.map +1 -0
- package/dist/core/servers/net.js +270 -0
- package/dist/core/servers/tunnels.d.ts +14 -0
- package/dist/core/servers/tunnels.d.ts.map +1 -0
- package/dist/core/servers/tunnels.js +33 -0
- package/dist/core/storage/builtins.js +1 -0
- package/dist/core/storage/process-log.d.ts +12 -0
- package/dist/core/storage/process-log.d.ts.map +1 -1
- package/dist/core/storage/process-log.js +10 -2
- package/dist/core/workflow/engine.d.ts.map +1 -1
- package/dist/core/workflow/engine.js +30 -1
- package/dist/core/workflow/sandbox-projection.d.ts +13 -2
- package/dist/core/workflow/sandbox-projection.d.ts.map +1 -1
- package/dist/core/workflow/sandbox-projection.js +23 -0
- package/dist/core/workflow/spawn-claude.d.ts.map +1 -1
- package/dist/core/workflow/spawn-claude.js +6 -1
- package/dist/core/workflow/spawn-codex.d.ts +2 -0
- package/dist/core/workflow/spawn-codex.d.ts.map +1 -1
- package/dist/core/workflow/spawn-codex.js +20 -1
- package/dist/core/workflow/spawn-cursor.d.ts.map +1 -1
- package/dist/core/workflow/spawn-cursor.js +6 -1
- package/dist/core/workflow/spawn-opencode.d.ts.map +1 -1
- package/dist/core/workflow/spawn-opencode.js +4 -1
- package/dist/core/workflow/types.d.ts +47 -2
- package/dist/core/workflow/types.d.ts.map +1 -1
- package/dist/core/workflow/worker-access.d.ts +92 -0
- package/dist/core/workflow/worker-access.d.ts.map +1 -0
- package/dist/core/workflow/worker-access.js +450 -0
- package/dist/lib/browser/batch-steps.d.ts +18 -0
- package/dist/lib/browser/batch-steps.d.ts.map +1 -0
- package/dist/lib/browser/batch-steps.js +82 -0
- package/dist/lib/browser/collect/collector.d.ts +99 -0
- package/dist/lib/browser/collect/collector.d.ts.map +1 -0
- package/dist/lib/browser/collect/collector.js +202 -0
- package/dist/lib/browser/collect/fields.d.ts +31 -0
- package/dist/lib/browser/collect/fields.d.ts.map +1 -0
- package/dist/lib/browser/collect/fields.js +75 -0
- package/dist/lib/browser/collect/index.d.ts +6 -0
- package/dist/lib/browser/collect/index.d.ts.map +1 -0
- package/dist/lib/browser/collect/index.js +5 -0
- package/dist/lib/browser/collect/page-driver.d.ts +14 -0
- package/dist/lib/browser/collect/page-driver.d.ts.map +1 -0
- package/dist/lib/browser/collect/page-driver.js +306 -0
- package/dist/lib/browser/collect/presets.d.ts +52 -0
- package/dist/lib/browser/collect/presets.d.ts.map +1 -0
- package/dist/lib/browser/collect/presets.js +176 -0
- package/dist/lib/browser/collect/scroll-plan.d.ts +85 -0
- package/dist/lib/browser/collect/scroll-plan.d.ts.map +1 -0
- package/dist/lib/browser/collect/scroll-plan.js +118 -0
- package/dist/lib/browser/index.d.ts +1 -1
- package/dist/lib/browser/index.d.ts.map +1 -1
- package/dist/lib/browser/page-review-judge.d.ts +2 -0
- package/dist/lib/browser/page-review-judge.d.ts.map +1 -1
- package/dist/lib/browser/page-review-judge.js +13 -0
- package/dist/lib/browser/qa-run-contracts.d.ts +10 -0
- package/dist/lib/browser/qa-run-contracts.d.ts.map +1 -1
- package/dist/lib/browser/qa-run.d.ts.map +1 -1
- package/dist/lib/browser/qa-run.js +1 -0
- package/dist/lib/browser/windows-chrome.d.ts +59 -0
- package/dist/lib/browser/windows-chrome.d.ts.map +1 -0
- package/dist/lib/browser/windows-chrome.js +179 -0
- package/dist/lib/claude-desktop-share.d.ts +136 -0
- package/dist/lib/claude-desktop-share.d.ts.map +1 -0
- package/dist/lib/claude-desktop-share.js +394 -0
- package/dist/lib/disk-usage.d.ts +50 -0
- package/dist/lib/disk-usage.d.ts.map +1 -0
- package/dist/lib/disk-usage.js +303 -0
- package/dist/lib/docs-lint.d.ts +6 -0
- package/dist/lib/docs-lint.d.ts.map +1 -1
- package/dist/lib/docs-lint.js +8 -3
- package/dist/lib/guarded-remove.d.ts +34 -0
- package/dist/lib/guarded-remove.d.ts.map +1 -0
- package/dist/lib/guarded-remove.js +310 -0
- package/dist/lib/instructions/templates.js +8 -8
- package/dist/lib/tunnel/allowlist.d.ts +44 -0
- package/dist/lib/tunnel/allowlist.d.ts.map +1 -0
- package/dist/lib/tunnel/allowlist.js +204 -0
- package/dist/lib/tunnel/current-address.d.ts +41 -0
- package/dist/lib/tunnel/current-address.d.ts.map +1 -0
- package/dist/lib/tunnel/current-address.js +123 -0
- package/dist/lib/tunnel/error-page.d.ts +1 -1
- package/dist/lib/tunnel/error-page.d.ts.map +1 -1
- package/dist/lib/tunnel/error-page.js +19 -8
- package/dist/lib/tunnel/gate.js +33 -7
- package/dist/lib/tunnel/path-scope.d.ts +22 -0
- package/dist/lib/tunnel/path-scope.d.ts.map +1 -0
- package/dist/lib/tunnel/path-scope.js +62 -0
- package/dist/lib/tunnel/processes.d.ts +39 -0
- package/dist/lib/tunnel/processes.d.ts.map +1 -0
- package/dist/lib/tunnel/processes.js +121 -0
- package/dist/lib/tunnel/state.d.ts +33 -3
- package/dist/lib/tunnel/state.d.ts.map +1 -1
- package/dist/lib/tunnel/state.js +115 -30
- package/package.json +7 -1
- package/schemas/config.schema.json +91 -0
- package/src/commander.ts +51 -0
- package/src/commands/agents.ts +111 -11
- package/src/commands/artifacts.ts +171 -19
- package/src/commands/browse.ts +400 -57
- package/src/commands/claude-desktop.ts +61 -0
- package/src/commands/disk.ts +114 -0
- package/src/commands/doctor.ts +52 -1
- package/src/commands/files.ts +93 -2
- package/src/commands/init.ts +55 -5
- package/src/commands/rm.ts +115 -0
- package/src/commands/servers.ts +506 -0
- package/src/commands/tunnel.ts +494 -116
- package/src/commands/web-fetch.ts +30 -19
- package/src/commands/web.ts +9 -3
- package/src/core/adapters/profiles.ts +22 -1
- package/src/core/adapters/types.ts +10 -0
- package/src/core/agents/cli.ts +14 -6
- package/src/core/agents/render/prompt-context.ts +25 -8
- package/src/core/agents/render/session-context.ts +84 -16
- package/src/core/agents/state/live-coordination-view.ts +73 -0
- package/src/core/agents/state/names.ts +117 -6
- package/src/core/artifacts/delivery-card.ts +10 -2
- package/src/core/artifacts/index.ts +697 -44
- package/src/core/config.ts +183 -4
- package/src/core/diagnostics/bundle.ts +2 -1
- package/src/core/events/v3/producers/recorder.ts +16 -0
- package/src/core/events/v3/projection.ts +11 -1
- package/src/core/events/v3/support-storage/pack-reader.ts +45 -19
- package/src/core/hooks/adapter/events.ts +19 -0
- package/src/core/hooks/adapter/runtime-telemetry.ts +3 -1
- package/src/core/hooks/adapter/wiring.ts +116 -8
- package/src/core/hooks/cli.ts +152 -22
- package/src/core/hooks/fork-lineage.ts +82 -0
- package/src/core/hooks/resolve/transcript.ts +88 -0
- package/src/core/hooks/run-markers.ts +132 -0
- package/src/core/hooks/shell-rm.ts +103 -0
- package/src/core/hooks/shell-tokens.ts +96 -0
- package/src/core/hooks/shell-waiter.ts +4 -91
- package/src/core/servers/index.ts +712 -0
- package/src/core/servers/net.ts +283 -0
- package/src/core/servers/tunnels.ts +39 -0
- package/src/core/storage/builtins.ts +1 -0
- package/src/core/storage/process-log.ts +17 -2
- package/src/core/workflow/engine.ts +38 -1
- package/src/core/workflow/sandbox-projection.ts +38 -2
- package/src/core/workflow/spawn-claude.ts +5 -1
- package/src/core/workflow/spawn-codex.ts +18 -1
- package/src/core/workflow/spawn-cursor.ts +5 -1
- package/src/core/workflow/spawn-opencode.ts +4 -1
- package/src/core/workflow/types.ts +62 -2
- package/src/core/workflow/worker-access.ts +525 -0
- package/src/lib/browser/batch-steps.ts +79 -0
- package/src/lib/browser/collect/collector.ts +306 -0
- package/src/lib/browser/collect/fields.ts +87 -0
- package/src/lib/browser/collect/index.ts +47 -0
- package/src/lib/browser/collect/page-driver.ts +327 -0
- package/src/lib/browser/collect/presets.ts +237 -0
- package/src/lib/browser/collect/scroll-plan.ts +179 -0
- package/src/lib/browser/index.ts +1 -0
- package/src/lib/browser/page-review-judge.ts +16 -0
- package/src/lib/browser/qa-run-contracts.ts +11 -0
- package/src/lib/browser/qa-run.ts +1 -0
- package/src/lib/browser/windows-chrome.ts +227 -0
- package/src/lib/claude-desktop-share.ts +511 -0
- package/src/lib/disk-usage.ts +347 -0
- package/src/lib/docs-lint.ts +8 -3
- package/src/lib/guarded-remove.ts +376 -0
- package/src/lib/instructions/templates.ts +8 -8
- package/src/lib/tunnel/allowlist.ts +208 -0
- package/src/lib/tunnel/current-address.ts +158 -0
- package/src/lib/tunnel/error-page.ts +25 -9
- package/src/lib/tunnel/gate.ts +38 -9
- package/src/lib/tunnel/path-scope.ts +73 -0
- package/src/lib/tunnel/processes.ts +149 -0
- package/src/lib/tunnel/state.ts +143 -30
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { lstatSync, readdirSync, readlinkSync, realpathSync, rmSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, dirname, isAbsolute, parse, relative, resolve, sep } from "node:path";
|
|
5
|
+
const protectedNames = new Set([".git", ".harnery", ".credentials", ".ssh", ".gnupg"]);
|
|
6
|
+
const MAX_ENTRIES = 10_000;
|
|
7
|
+
/** Permanent removal is deliberately limited to inspected, untracked local outputs. */
|
|
8
|
+
export function guardedRemove(paths, options, checkClaims = () => { }) {
|
|
9
|
+
const cwd = realpathSync(process.cwd());
|
|
10
|
+
const root = checkedPath(options.root, new Set(), true);
|
|
11
|
+
const allowed = allowedProtected(options.allowProtected ?? [], root, options.liveRoots ?? []);
|
|
12
|
+
const rootStat = lstatSync(root);
|
|
13
|
+
const rootFingerprint = fingerprint(rootStat);
|
|
14
|
+
if (!rootStat.isDirectory())
|
|
15
|
+
throw new Error("--root must be a directory");
|
|
16
|
+
if (root === parse(root).root || root === realpathSync(homedir()))
|
|
17
|
+
throw new Error("--root cannot be a filesystem root or home directory");
|
|
18
|
+
if (paths.length === 0 || paths.length > 100)
|
|
19
|
+
throw new Error("Supply 1-100 explicit targets");
|
|
20
|
+
const targets = paths.map((path) => checkedPath(path, allowed));
|
|
21
|
+
const snapshots = new Map();
|
|
22
|
+
const removedProtected = new Set();
|
|
23
|
+
const links = [];
|
|
24
|
+
const gitDirs = new Set();
|
|
25
|
+
let bytes = 0;
|
|
26
|
+
const devices = new Set();
|
|
27
|
+
for (const target of targets) {
|
|
28
|
+
const home = realpathSync(homedir());
|
|
29
|
+
if (target === home || strictlyInside(target, home))
|
|
30
|
+
throw new Error(`Cannot remove the home directory or its ancestor: ${target}`);
|
|
31
|
+
const systemRoots = process.platform === "win32"
|
|
32
|
+
? [
|
|
33
|
+
process.env.SystemRoot,
|
|
34
|
+
process.env.ProgramFiles,
|
|
35
|
+
process.env["ProgramFiles(x86)"],
|
|
36
|
+
].filter((value) => Boolean(value))
|
|
37
|
+
: [
|
|
38
|
+
"/etc",
|
|
39
|
+
"/usr",
|
|
40
|
+
"/boot",
|
|
41
|
+
"/dev",
|
|
42
|
+
"/proc",
|
|
43
|
+
"/sys",
|
|
44
|
+
"/bin",
|
|
45
|
+
"/sbin",
|
|
46
|
+
"/lib",
|
|
47
|
+
"/lib64",
|
|
48
|
+
"/run",
|
|
49
|
+
];
|
|
50
|
+
if (systemRoots.some((system) => target === resolve(system) ||
|
|
51
|
+
strictlyInside(resolve(system), target) ||
|
|
52
|
+
strictlyInside(target, resolve(system))))
|
|
53
|
+
throw new Error(`System files are protected: ${target}`);
|
|
54
|
+
if (!strictlyInside(root, target))
|
|
55
|
+
throw new Error(`Target must be strictly inside --root: ${target}`);
|
|
56
|
+
if (target === cwd || strictlyInside(target, cwd))
|
|
57
|
+
throw new Error(`Cannot remove the working directory or its ancestor: ${target}`);
|
|
58
|
+
if (targets.some((other) => other !== target && strictlyInside(other, target)) ||
|
|
59
|
+
targets.filter((other) => other === target).length > 1)
|
|
60
|
+
throw new Error("Targets must not duplicate or contain one another");
|
|
61
|
+
const stat = lstatSync(target);
|
|
62
|
+
if (stat.dev !== rootStat.dev)
|
|
63
|
+
throw new Error(`Target crosses a mount boundary: ${target}`);
|
|
64
|
+
if (stat.isDirectory() && !options.recursive)
|
|
65
|
+
throw new Error(`Directory removal requires --recursive: ${target}`);
|
|
66
|
+
const device = stat.dev;
|
|
67
|
+
devices.add(device);
|
|
68
|
+
const pending = [target];
|
|
69
|
+
while (pending.length) {
|
|
70
|
+
const path = pending.pop();
|
|
71
|
+
const info = lstatSync(path);
|
|
72
|
+
if (basename(path) === ARTIFACT_RECORD && insideArtifactWorkspace(path, null))
|
|
73
|
+
throw new Error(`An artifact's record is managed by the artifact commands: ${path}`);
|
|
74
|
+
if (protectedNames.has(basename(path).toLowerCase())) {
|
|
75
|
+
if (!allowed.has(path))
|
|
76
|
+
throw new Error(protectedMessage(path));
|
|
77
|
+
removedProtected.add(path);
|
|
78
|
+
}
|
|
79
|
+
if (info.isSymbolicLink()) {
|
|
80
|
+
// A link inside the tree is removed as a link; its target is never
|
|
81
|
+
// read, followed, or counted. (The target itself and its ancestors
|
|
82
|
+
// were already refused if they were links, in checkedPath.)
|
|
83
|
+
if (path === target)
|
|
84
|
+
throw new Error(`Path contains a symlink: ${path}`);
|
|
85
|
+
if (snapshots.size >= MAX_ENTRIES)
|
|
86
|
+
throw new Error(`Removal exceeds the ${MAX_ENTRIES}-entry limit; use the owning cleanup command`);
|
|
87
|
+
snapshots.set(path, fingerprint(info));
|
|
88
|
+
links.push({ path, target: readlinkSync(path) });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (info.dev !== device || (!info.isDirectory() && !info.isFile()))
|
|
92
|
+
throw new Error(`Mount boundary or special file cannot be removed: ${path}`);
|
|
93
|
+
if (snapshots.size >= MAX_ENTRIES)
|
|
94
|
+
throw new Error(`Removal exceeds the ${MAX_ENTRIES}-entry limit; use the owning cleanup command`);
|
|
95
|
+
snapshots.set(path, fingerprint(info));
|
|
96
|
+
if (info.isFile())
|
|
97
|
+
bytes += info.size;
|
|
98
|
+
else {
|
|
99
|
+
const names = readdirSync(path);
|
|
100
|
+
if (names.includes("HEAD") &&
|
|
101
|
+
names.includes("objects") &&
|
|
102
|
+
names.includes("config") &&
|
|
103
|
+
!insideAllowed(path, allowed))
|
|
104
|
+
throw new Error(`Repository metadata cannot be removed: ${path}. To remove it on purpose, name it with --allow-protected ${path}`);
|
|
105
|
+
if (allowed.has(path))
|
|
106
|
+
removedProtected.add(path);
|
|
107
|
+
if (isRepositoryMetadata(path))
|
|
108
|
+
gitDirs.add(path);
|
|
109
|
+
for (const name of names)
|
|
110
|
+
pending.push(resolve(path, name));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
assertUntracked(target);
|
|
114
|
+
}
|
|
115
|
+
if (devices.size > 1)
|
|
116
|
+
throw new Error("Targets must be on one filesystem");
|
|
117
|
+
for (const path of allowed)
|
|
118
|
+
if (!removedProtected.has(path))
|
|
119
|
+
throw new Error(`--allow-protected names a path no target removes: ${path}`);
|
|
120
|
+
for (const dir of gitDirs)
|
|
121
|
+
assertNoUnpushedHistory(dir);
|
|
122
|
+
checkClaims(targets);
|
|
123
|
+
const report = {
|
|
124
|
+
schema: "harnery.removal/v1",
|
|
125
|
+
applied: false,
|
|
126
|
+
root,
|
|
127
|
+
targets,
|
|
128
|
+
entries: snapshots.size,
|
|
129
|
+
bytes,
|
|
130
|
+
protected: [...removedProtected].sort(),
|
|
131
|
+
links,
|
|
132
|
+
};
|
|
133
|
+
if (!options.yes || options.dryRun)
|
|
134
|
+
return report;
|
|
135
|
+
// Recheck the entire batch before its first mutation. Filesystem changes after
|
|
136
|
+
// these checks remain possible; this is a guard, not a transactional filesystem.
|
|
137
|
+
if (checkedPath(options.root, new Set(), true) !== root ||
|
|
138
|
+
fingerprint(lstatSync(root)) !== rootFingerprint)
|
|
139
|
+
throw new Error("Allowed root changed during inspection");
|
|
140
|
+
for (const target of targets) {
|
|
141
|
+
checkedPath(target, allowed);
|
|
142
|
+
assertUntracked(target);
|
|
143
|
+
}
|
|
144
|
+
for (const dir of gitDirs)
|
|
145
|
+
assertNoUnpushedHistory(dir);
|
|
146
|
+
checkClaims(targets);
|
|
147
|
+
for (const [path, previous] of snapshots) {
|
|
148
|
+
if (fingerprint(lstatSync(path)) !== previous)
|
|
149
|
+
throw new Error(`Target changed during inspection: ${path}`);
|
|
150
|
+
}
|
|
151
|
+
for (const target of targets)
|
|
152
|
+
rmSync(target, { recursive: Boolean(options.recursive), force: false });
|
|
153
|
+
return { ...report, applied: true };
|
|
154
|
+
}
|
|
155
|
+
/** The metadata file the artifact commands keep in each artifact workspace. */
|
|
156
|
+
const ARTIFACT_RECORD = ".harnery-artifact.json";
|
|
157
|
+
/**
|
|
158
|
+
* True when `path` lies strictly inside one artifact workspace,
|
|
159
|
+
* <dir>/.harnery/artifacts/<id>/..., under the `.harnery` component `state`
|
|
160
|
+
* (any `.harnery` when null). Artifact workspaces hold local outputs, so their
|
|
161
|
+
* contents may be removed; the workspace itself, its record file, and the rest
|
|
162
|
+
* of Harnery state stay protected and follow the artifact commands. `orSelf`
|
|
163
|
+
* accepts the workspace directory itself (used for --root, which is never removed).
|
|
164
|
+
*/
|
|
165
|
+
function insideArtifactWorkspace(path, state, orSelf = false) {
|
|
166
|
+
const parts = path.split(sep);
|
|
167
|
+
for (let i = 0; i < parts.length; i++) {
|
|
168
|
+
if (parts[i].toLowerCase() !== ".harnery")
|
|
169
|
+
continue;
|
|
170
|
+
if (state !== null && parts.slice(0, i + 1).join(sep) !== state)
|
|
171
|
+
continue;
|
|
172
|
+
// .harnery / artifacts / <id> / <something>
|
|
173
|
+
return (parts[i + 1] === "artifacts" && Boolean(parts[i + 2]) && parts.length >= i + (orSelf ? 3 : 4));
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
function protectedMessage(path) {
|
|
178
|
+
return `Protected state cannot be removed: ${path}. To remove it on purpose, name it with --allow-protected ${path}`;
|
|
179
|
+
}
|
|
180
|
+
function insideAllowed(path, allowed) {
|
|
181
|
+
for (const entry of allowed)
|
|
182
|
+
if (path === entry || strictlyInside(entry, path))
|
|
183
|
+
return true;
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Resolves the caller's --allow-protected paths. Each must be one exact
|
|
188
|
+
* protected path strictly inside the root, reached without symlinks, and
|
|
189
|
+
* never the Git metadata or Harnery state of a live workspace root.
|
|
190
|
+
*/
|
|
191
|
+
function allowedProtected(paths, root, liveRoots) {
|
|
192
|
+
const allowed = new Set();
|
|
193
|
+
const live = liveRoots.map((dir) => resolve(dir));
|
|
194
|
+
for (const input of paths) {
|
|
195
|
+
if (!input?.trim() || /[\0\r\n*?[\]$]/.test(input))
|
|
196
|
+
throw new Error("Use a nonempty explicit --allow-protected path without wildcards");
|
|
197
|
+
const path = resolve(input);
|
|
198
|
+
let cursor = dirname(path);
|
|
199
|
+
while (true) {
|
|
200
|
+
if (lstatSync(cursor).isSymbolicLink())
|
|
201
|
+
throw new Error(`--allow-protected path contains a symlink: ${cursor}`);
|
|
202
|
+
const parent = dirname(cursor);
|
|
203
|
+
if (parent === cursor)
|
|
204
|
+
break;
|
|
205
|
+
cursor = parent;
|
|
206
|
+
}
|
|
207
|
+
const info = lstatSync(path);
|
|
208
|
+
if (info.isSymbolicLink())
|
|
209
|
+
throw new Error(`--allow-protected path is a symlink: ${path}`);
|
|
210
|
+
if (!strictlyInside(root, path))
|
|
211
|
+
throw new Error(`--allow-protected path must be strictly inside --root: ${path}`);
|
|
212
|
+
const named = protectedNames.has(basename(path).toLowerCase());
|
|
213
|
+
if (!named && !(info.isDirectory() && isRepositoryMetadata(path)))
|
|
214
|
+
throw new Error(`--allow-protected must name a protected path itself: ${path}`);
|
|
215
|
+
for (const dir of live) {
|
|
216
|
+
const own = [".git", ".harnery"].map((name) => resolve(dir, name));
|
|
217
|
+
if (path === dir ||
|
|
218
|
+
strictlyInside(path, dir) ||
|
|
219
|
+
own.some((state) => path === state || strictlyInside(state, path)))
|
|
220
|
+
throw new Error(`The live workspace's own state cannot be allowed: ${path}`);
|
|
221
|
+
}
|
|
222
|
+
allowed.add(realpathSync(path));
|
|
223
|
+
}
|
|
224
|
+
return allowed;
|
|
225
|
+
}
|
|
226
|
+
function isRepositoryMetadata(dir) {
|
|
227
|
+
const names = readdirSync(dir);
|
|
228
|
+
return names.includes("HEAD") && names.includes("objects") && names.includes("config");
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Removing a repository's metadata loses any commit that exists nowhere
|
|
232
|
+
* else. An allowed Git directory must have no branch or tag commit missing
|
|
233
|
+
* from its remotes; anything that cannot be checked is refused.
|
|
234
|
+
*/
|
|
235
|
+
function assertNoUnpushedHistory(gitDir) {
|
|
236
|
+
const result = spawnSync("git", [
|
|
237
|
+
"--git-dir",
|
|
238
|
+
gitDir,
|
|
239
|
+
"rev-list",
|
|
240
|
+
"--max-count=1",
|
|
241
|
+
"--branches",
|
|
242
|
+
"--tags",
|
|
243
|
+
"--not",
|
|
244
|
+
"--remotes",
|
|
245
|
+
], {
|
|
246
|
+
encoding: "utf8",
|
|
247
|
+
timeout: 10_000,
|
|
248
|
+
env: { ...process.env, LC_ALL: "C", GIT_DIR: undefined, GIT_WORK_TREE: undefined },
|
|
249
|
+
});
|
|
250
|
+
if (result.error || result.status !== 0)
|
|
251
|
+
throw new Error(`Cannot confirm ${gitDir} holds no unpushed history`);
|
|
252
|
+
if (result.stdout.trim())
|
|
253
|
+
throw new Error(`Repository has commits not on any remote: ${gitDir}`);
|
|
254
|
+
}
|
|
255
|
+
function strictlyInside(parent, child) {
|
|
256
|
+
const rel = relative(parent, child);
|
|
257
|
+
return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
258
|
+
}
|
|
259
|
+
function checkedPath(input, allowed = new Set(), isRoot = false) {
|
|
260
|
+
if (!input?.trim() || /[\0\r\n*?[\]$]/.test(input))
|
|
261
|
+
throw new Error("Use a nonempty explicit path without wildcards or unresolved variables");
|
|
262
|
+
const path = resolve(input);
|
|
263
|
+
let cursor = path;
|
|
264
|
+
while (true) {
|
|
265
|
+
if (protectedNames.has(basename(cursor).toLowerCase()) &&
|
|
266
|
+
!allowed.has(cursor) &&
|
|
267
|
+
!insideArtifactWorkspace(path, cursor, isRoot))
|
|
268
|
+
throw new Error(cursor === path ? protectedMessage(path) : `Protected state contains this path: ${cursor}`);
|
|
269
|
+
const info = lstatSync(cursor);
|
|
270
|
+
if (info.isSymbolicLink())
|
|
271
|
+
throw new Error(`Path contains a symlink: ${cursor}`);
|
|
272
|
+
const parent = dirname(cursor);
|
|
273
|
+
if (parent === cursor)
|
|
274
|
+
break;
|
|
275
|
+
cursor = parent;
|
|
276
|
+
}
|
|
277
|
+
return realpathSync(path);
|
|
278
|
+
}
|
|
279
|
+
function fingerprint(stat) {
|
|
280
|
+
return [stat.dev, stat.ino, stat.mode, stat.size, stat.mtimeMs, stat.ctimeMs].join(":");
|
|
281
|
+
}
|
|
282
|
+
function assertUntracked(target) {
|
|
283
|
+
const git = (args) => spawnSync("git", ["-C", dirname(target), ...args], {
|
|
284
|
+
encoding: "utf8",
|
|
285
|
+
timeout: 10_000,
|
|
286
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
287
|
+
env: {
|
|
288
|
+
...process.env,
|
|
289
|
+
LC_ALL: "C",
|
|
290
|
+
GIT_LITERAL_PATHSPECS: "1",
|
|
291
|
+
GIT_DIR: undefined,
|
|
292
|
+
GIT_WORK_TREE: undefined,
|
|
293
|
+
GIT_INDEX_FILE: undefined,
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
const detected = git(["rev-parse", "--show-toplevel"]);
|
|
297
|
+
if (detected.error)
|
|
298
|
+
throw new Error(`Cannot check Git ownership: ${detected.error.message}`);
|
|
299
|
+
if (detected.status !== 0) {
|
|
300
|
+
if (detected.status === 128 && detected.stderr.startsWith("fatal: not a git repository"))
|
|
301
|
+
return;
|
|
302
|
+
throw new Error(`Cannot check Git ownership: ${detected.stderr.trim()}`);
|
|
303
|
+
}
|
|
304
|
+
const repo = detected.stdout.trim();
|
|
305
|
+
const tracked = git(["ls-files", "--cached", "-z", "--full-name", "--", target]);
|
|
306
|
+
if (tracked.error || tracked.status !== 0)
|
|
307
|
+
throw new Error(`Cannot inspect Git index in ${repo}`);
|
|
308
|
+
if (tracked.stdout.length)
|
|
309
|
+
throw new Error(`Tracked files are protected; use git rm for intentional source removal: ${target}`);
|
|
310
|
+
}
|
|
@@ -67,8 +67,7 @@ export function renderInstructionsBlock(binName, skills = { decide: true, counci
|
|
|
67
67
|
return `## harnery coordination
|
|
68
68
|
|
|
69
69
|
This project runs [harnery](https://harnery.com) for multi-agent coordination.
|
|
70
|
-
You share this checkout with other agents; the surfaces below keep you oriented
|
|
71
|
-
and out of each other's way, and let you dispatch a team of your own when a job
|
|
70
|
+
You share this checkout with other agents; the surfaces below keep you oriented and out of each other's way, and let you dispatch a team of your own when a job
|
|
72
71
|
is bigger than one session. Run \`${b} <command> --help\` for any command's full
|
|
73
72
|
surface. ${deeper}
|
|
74
73
|
|
|
@@ -81,8 +80,7 @@ widely-shared files.
|
|
|
81
80
|
**Task lifecycle.** Beside the activity peers already see, declare whether your
|
|
82
81
|
objective is still open: \`${b} agents lifecycle blocked --reason "<why>"\` when
|
|
83
82
|
it cannot proceed, \`${b} agents lifecycle done\` when it is complete, and
|
|
84
|
-
\`${b} agents lifecycle active\` to reopen. \`done\` requires a current task and
|
|
85
|
-
a passing Git finalization check (dirty or unpushed work refuses, and nothing is
|
|
83
|
+
\`${b} agents lifecycle active\` to reopen. \`done\` requires a current task and a passing Git finalization check (dirty or unpushed work refuses, and nothing is
|
|
86
84
|
written). Ordinary \`set-task\` calls never change lifecycle, and a transition
|
|
87
85
|
that re-mints the session title tells you the new name to copy. ${endPointer}
|
|
88
86
|
|
|
@@ -107,8 +105,7 @@ It reclaims an abandoned namesake (no live process) and refuses only when anothe
|
|
|
107
105
|
live process still holds the name; never hand-edit Harnery's history, heartbeat,
|
|
108
106
|
or derived identity cache.
|
|
109
107
|
|
|
110
|
-
**Declare intent on shell commands.** Every command you run is captured to the
|
|
111
|
-
coordination ledger (\`.harnery/ledgers/v3/\`). Lead a shell command with a
|
|
108
|
+
**Declare intent on shell commands.** Every command you run is captured to the coordination ledger (\`.harnery/ledgers/v3/\`). Lead a shell command with a
|
|
112
109
|
\`# intent: <why>\` comment (or set the tool's description) so the recorded event
|
|
113
110
|
carries a reason instead of \`(no intent)\`; the [tool-intent
|
|
114
111
|
guide](https://harnery.com/guides/tool-intent/) owns the details.
|
|
@@ -132,6 +129,10 @@ After review, use \`${b} artifacts discard <id> --reason "<why no longer needed>
|
|
|
132
129
|
Do not create a repo-root temp directory; \`${b} artifacts clean\` previews
|
|
133
130
|
expired cleanup and requires \`--yes\` to delete anything.
|
|
134
131
|
|
|
132
|
+
**Removing local outputs.** Use \`${b} rm --root <directory> <paths...>\` instead of shell \`rm\`, which skips resolved-path, Git ownership, and peer-claim checks. Preview
|
|
133
|
+
is the default; \`--yes\` deletes permanently and \`--recursive\` permits directories.
|
|
134
|
+
Use \`git rm\` for tracked source and the artifact commands for managed evidence.
|
|
135
|
+
|
|
135
136
|
**Local file links.** When the operator should open a local repo file, mint the
|
|
136
137
|
URL with \`${b} files url <repo-relative-path>\` instead of guessing it. HTML opens
|
|
137
138
|
as a real page with working scripts and relative assets; other files open in the
|
|
@@ -142,8 +143,7 @@ can't resolve from the repo, file it instead. \`${b} decision file "<question>"\
|
|
|
142
143
|
records it and lets you proceed on a stated default; \`${b} decision search "<terms>"\`
|
|
143
144
|
surfaces prior decisions, so check for precedent before re-deciding. ${decidePointer}
|
|
144
145
|
|
|
145
|
-
**Councils.** For a hard or contested decision, convene a council of agents.
|
|
146
|
-
\`${b} council create "<objective>"\` runs structured rounds toward a decision. ${councilPointer}`;
|
|
146
|
+
**Councils.** For a hard or contested decision, convene a council of agents. \`${b} council create "<objective>"\` runs structured rounds toward a decision. ${councilPointer}`;
|
|
147
147
|
}
|
|
148
148
|
function decideBody(b) {
|
|
149
149
|
return `The decision docket is a persistent queue for decisions you would otherwise
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type IpFamily = 4 | 6;
|
|
2
|
+
export interface ParsedIp {
|
|
3
|
+
family: IpFamily;
|
|
4
|
+
value: bigint;
|
|
5
|
+
}
|
|
6
|
+
export interface AllowEntry extends ParsedIp {
|
|
7
|
+
/** Prefix length in bits (32 or 128 for a single address). */
|
|
8
|
+
prefix: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Parse one address. Returns null for anything that is not an IP literal. A
|
|
12
|
+
* zone id (`fe80::1%en0`) is dropped, and an IPv4-mapped IPv6 address is
|
|
13
|
+
* reported as the IPv4 address it carries.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseIp(raw: string): ParsedIp | null;
|
|
16
|
+
/**
|
|
17
|
+
* Parse an allowlist entry: `203.0.113.8`, `203.0.113.0/24`, `2601:db8::1`, or
|
|
18
|
+
* `2601:db8:1:2::/64`. Host bits below the prefix are cleared, so
|
|
19
|
+
* `203.0.113.77/24` means the network `203.0.113.0/24`. Returns null when the
|
|
20
|
+
* entry is not a valid address or its prefix length is out of range.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseAllowEntry(raw: string): AllowEntry | null;
|
|
23
|
+
/** Canonical text of an address (dotted quad, or compressed lowercase IPv6). */
|
|
24
|
+
export declare function formatIp(ip: ParsedIp): string;
|
|
25
|
+
/** Canonical text of an entry; a single address carries no `/len` suffix. */
|
|
26
|
+
export declare function formatAllowEntry(entry: AllowEntry): string;
|
|
27
|
+
/**
|
|
28
|
+
* Canonical spelling of an allowlist entry, or null when invalid. Two spellings
|
|
29
|
+
* of one address or range (`2601:DB8:0:0::1`, `2601:db8::1`) normalize equal.
|
|
30
|
+
*/
|
|
31
|
+
export declare function normalizeAllowEntry(raw: string): string | null;
|
|
32
|
+
export interface Allowlist {
|
|
33
|
+
entries: AllowEntry[];
|
|
34
|
+
/** Entries that could not be parsed; they never match anything. */
|
|
35
|
+
invalid: string[];
|
|
36
|
+
}
|
|
37
|
+
export declare function compileAllowlist(rawEntries: readonly string[]): Allowlist;
|
|
38
|
+
/** True when `ip` lies inside any entry. A missing or malformed address never matches. */
|
|
39
|
+
export declare function allowlistMatches(list: Allowlist, ip: string): boolean;
|
|
40
|
+
/** The IPv6 /64 network prefix of an address, as a canonical `…/64` entry. */
|
|
41
|
+
export declare function ipv6Slash64(address: string): string | null;
|
|
42
|
+
/** A single IPv4 address as a `/32` entry. */
|
|
43
|
+
export declare function ipv4Slash32(address: string): string | null;
|
|
44
|
+
//# sourceMappingURL=allowlist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allowlist.d.ts","sourceRoot":"","sources":["../../../src/lib/tunnel/allowlist.ts"],"names":[],"mappings":"AAaA,MAAM,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,CAAC;AAE7B,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,QAAQ,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,UAAW,SAAQ,QAAQ;IAC1C,8DAA8D;IAC9D,MAAM,EAAE,MAAM,CAAC;CAChB;AAyCD;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAYpD;AAQD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAmB9D;AAyBD,gFAAgF;AAChF,wBAAgB,QAAQ,CAAC,EAAE,EAAE,QAAQ,GAAG,MAAM,CAO7C;AAED,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAG1D;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG9D;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,UAAU,EAAE,CAAC;IACtB,mEAAmE;IACnE,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAWzE;AAED,0FAA0F;AAC1F,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CASrE;AAED,8EAA8E;AAC9E,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI1D;AAED,8CAA8C;AAC9C,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI1D"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Address allowlist for the tunnel gate: exact addresses and CIDR ranges, IPv4
|
|
2
|
+
// and IPv6. Pure functions with no I/O, shared by the gate worker (matching)
|
|
3
|
+
// and the `tunnel allow` commands (validation + canonical spelling).
|
|
4
|
+
//
|
|
5
|
+
// An IPv6 phone and laptop on one Wi-Fi network have different addresses but
|
|
6
|
+
// share a /64 prefix, so a range entry such as `2601:db8:1:2::/64` admits both.
|
|
7
|
+
// An entry without a prefix length is a single address (/32 or /128).
|
|
8
|
+
//
|
|
9
|
+
// IPv4-mapped IPv6 addresses (`::ffff:203.0.113.8`) are IPv4 addresses reached
|
|
10
|
+
// through a dual-stack socket, so they parse and match as IPv4.
|
|
11
|
+
import { isIPv4, isIPv6 } from "node:net";
|
|
12
|
+
const FULL_BITS = { 4: 32, 6: 128 };
|
|
13
|
+
function parseIpv4(text) {
|
|
14
|
+
if (!isIPv4(text))
|
|
15
|
+
return null;
|
|
16
|
+
let value = 0n;
|
|
17
|
+
for (const part of text.split("."))
|
|
18
|
+
value = (value << 8n) | BigInt(Number(part));
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function parseIpv6(text) {
|
|
22
|
+
if (!isIPv6(text))
|
|
23
|
+
return null;
|
|
24
|
+
let rest = text;
|
|
25
|
+
// A trailing dotted quad (`::ffff:1.2.3.4`, `64:ff9b::1.2.3.4`) is two groups.
|
|
26
|
+
const dotted = rest.match(/^(.*:)(\d+\.\d+\.\d+\.\d+)$/);
|
|
27
|
+
if (dotted) {
|
|
28
|
+
const v4 = parseIpv4(dotted[2]);
|
|
29
|
+
if (v4 === null)
|
|
30
|
+
return null;
|
|
31
|
+
rest = `${dotted[1]}${(v4 >> 16n).toString(16)}:${(v4 & 0xffffn).toString(16)}`;
|
|
32
|
+
}
|
|
33
|
+
const halves = rest.split("::");
|
|
34
|
+
if (halves.length > 2)
|
|
35
|
+
return null;
|
|
36
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
37
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
38
|
+
const missing = 8 - head.length - tail.length;
|
|
39
|
+
if (halves.length === 1 ? missing !== 0 : missing < 1)
|
|
40
|
+
return null;
|
|
41
|
+
const groups = [
|
|
42
|
+
...head,
|
|
43
|
+
...Array.from({ length: halves.length === 2 ? missing : 0 }, () => "0"),
|
|
44
|
+
...tail,
|
|
45
|
+
];
|
|
46
|
+
if (groups.length !== 8)
|
|
47
|
+
return null;
|
|
48
|
+
let value = 0n;
|
|
49
|
+
for (const group of groups) {
|
|
50
|
+
if (!/^[0-9a-f]{1,4}$/i.test(group))
|
|
51
|
+
return null;
|
|
52
|
+
value = (value << 16n) | BigInt(Number.parseInt(group, 16));
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Parse one address. Returns null for anything that is not an IP literal. A
|
|
58
|
+
* zone id (`fe80::1%en0`) is dropped, and an IPv4-mapped IPv6 address is
|
|
59
|
+
* reported as the IPv4 address it carries.
|
|
60
|
+
*/
|
|
61
|
+
export function parseIp(raw) {
|
|
62
|
+
let text = raw.trim();
|
|
63
|
+
if (text.startsWith("[") && text.endsWith("]"))
|
|
64
|
+
text = text.slice(1, -1);
|
|
65
|
+
const zone = text.indexOf("%");
|
|
66
|
+
if (zone !== -1)
|
|
67
|
+
text = text.slice(0, zone);
|
|
68
|
+
if (!text)
|
|
69
|
+
return null;
|
|
70
|
+
const v4 = parseIpv4(text);
|
|
71
|
+
if (v4 !== null)
|
|
72
|
+
return { family: 4, value: v4 };
|
|
73
|
+
const v6 = parseIpv6(text);
|
|
74
|
+
if (v6 === null)
|
|
75
|
+
return null;
|
|
76
|
+
if (v6 >> 32n === 0xffffn)
|
|
77
|
+
return { family: 4, value: v6 & 0xffffffffn };
|
|
78
|
+
return { family: 6, value: v6 };
|
|
79
|
+
}
|
|
80
|
+
function maskFor(family, prefix) {
|
|
81
|
+
const bits = BigInt(FULL_BITS[family]);
|
|
82
|
+
const host = bits - BigInt(prefix);
|
|
83
|
+
return ((1n << bits) - 1n) ^ ((1n << host) - 1n);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Parse an allowlist entry: `203.0.113.8`, `203.0.113.0/24`, `2601:db8::1`, or
|
|
87
|
+
* `2601:db8:1:2::/64`. Host bits below the prefix are cleared, so
|
|
88
|
+
* `203.0.113.77/24` means the network `203.0.113.0/24`. Returns null when the
|
|
89
|
+
* entry is not a valid address or its prefix length is out of range.
|
|
90
|
+
*/
|
|
91
|
+
export function parseAllowEntry(raw) {
|
|
92
|
+
const text = raw.trim();
|
|
93
|
+
const slash = text.indexOf("/");
|
|
94
|
+
const address = parseIp(slash === -1 ? text : text.slice(0, slash));
|
|
95
|
+
if (!address)
|
|
96
|
+
return null;
|
|
97
|
+
let prefix = FULL_BITS[address.family];
|
|
98
|
+
if (slash !== -1) {
|
|
99
|
+
const lengthText = text.slice(slash + 1);
|
|
100
|
+
if (!/^\d{1,3}$/.test(lengthText))
|
|
101
|
+
return null;
|
|
102
|
+
prefix = Number(lengthText);
|
|
103
|
+
// A prefix on an IPv4-mapped IPv6 entry counts from the IPv6 start, so
|
|
104
|
+
// `::ffff:1.2.3.0/120` is the IPv4 /24. Anything shorter is not an IPv4 range.
|
|
105
|
+
if (address.family === 4 && /:/.test(text.slice(0, slash))) {
|
|
106
|
+
if (prefix < 96 || prefix > 128)
|
|
107
|
+
return null;
|
|
108
|
+
prefix -= 96;
|
|
109
|
+
}
|
|
110
|
+
if (prefix > FULL_BITS[address.family])
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return { ...address, prefix, value: address.value & maskFor(address.family, prefix) };
|
|
114
|
+
}
|
|
115
|
+
function compress(groups) {
|
|
116
|
+
// RFC 5952: collapse the longest run of two or more zero groups (first wins).
|
|
117
|
+
let bestStart = -1;
|
|
118
|
+
let bestLen = 0;
|
|
119
|
+
for (let i = 0; i < 8;) {
|
|
120
|
+
if (groups[i] !== 0) {
|
|
121
|
+
i++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
let j = i;
|
|
125
|
+
while (j < 8 && groups[j] === 0)
|
|
126
|
+
j++;
|
|
127
|
+
if (j - i > bestLen) {
|
|
128
|
+
bestStart = i;
|
|
129
|
+
bestLen = j - i;
|
|
130
|
+
}
|
|
131
|
+
i = j;
|
|
132
|
+
}
|
|
133
|
+
if (bestLen < 2)
|
|
134
|
+
return groups.map((g) => g.toString(16)).join(":");
|
|
135
|
+
const left = groups.slice(0, bestStart).map((g) => g.toString(16));
|
|
136
|
+
const right = groups.slice(bestStart + bestLen).map((g) => g.toString(16));
|
|
137
|
+
return `${left.join(":")}::${right.join(":")}`;
|
|
138
|
+
}
|
|
139
|
+
/** Canonical text of an address (dotted quad, or compressed lowercase IPv6). */
|
|
140
|
+
export function formatIp(ip) {
|
|
141
|
+
if (ip.family === 4) {
|
|
142
|
+
return [24n, 16n, 8n, 0n].map((shift) => Number((ip.value >> shift) & 0xffn)).join(".");
|
|
143
|
+
}
|
|
144
|
+
const groups = [];
|
|
145
|
+
for (let i = 7; i >= 0; i--)
|
|
146
|
+
groups.push(Number((ip.value >> BigInt(i * 16)) & 0xffffn));
|
|
147
|
+
return compress(groups);
|
|
148
|
+
}
|
|
149
|
+
/** Canonical text of an entry; a single address carries no `/len` suffix. */
|
|
150
|
+
export function formatAllowEntry(entry) {
|
|
151
|
+
const address = formatIp(entry);
|
|
152
|
+
return entry.prefix === FULL_BITS[entry.family] ? address : `${address}/${entry.prefix}`;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Canonical spelling of an allowlist entry, or null when invalid. Two spellings
|
|
156
|
+
* of one address or range (`2601:DB8:0:0::1`, `2601:db8::1`) normalize equal.
|
|
157
|
+
*/
|
|
158
|
+
export function normalizeAllowEntry(raw) {
|
|
159
|
+
const entry = parseAllowEntry(raw);
|
|
160
|
+
return entry ? formatAllowEntry(entry) : null;
|
|
161
|
+
}
|
|
162
|
+
export function compileAllowlist(rawEntries) {
|
|
163
|
+
const entries = [];
|
|
164
|
+
const invalid = [];
|
|
165
|
+
for (const raw of rawEntries) {
|
|
166
|
+
const text = raw.trim();
|
|
167
|
+
if (!text)
|
|
168
|
+
continue;
|
|
169
|
+
const entry = parseAllowEntry(text);
|
|
170
|
+
if (entry)
|
|
171
|
+
entries.push(entry);
|
|
172
|
+
else
|
|
173
|
+
invalid.push(text);
|
|
174
|
+
}
|
|
175
|
+
return { entries, invalid };
|
|
176
|
+
}
|
|
177
|
+
/** True when `ip` lies inside any entry. A missing or malformed address never matches. */
|
|
178
|
+
export function allowlistMatches(list, ip) {
|
|
179
|
+
const address = parseIp(ip);
|
|
180
|
+
if (!address)
|
|
181
|
+
return false;
|
|
182
|
+
for (const entry of list.entries) {
|
|
183
|
+
if (entry.family !== address.family)
|
|
184
|
+
continue;
|
|
185
|
+
const mask = maskFor(entry.family, entry.prefix);
|
|
186
|
+
if ((address.value & mask) === entry.value)
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
/** The IPv6 /64 network prefix of an address, as a canonical `…/64` entry. */
|
|
192
|
+
export function ipv6Slash64(address) {
|
|
193
|
+
const ip = parseIp(address);
|
|
194
|
+
if (!ip || ip.family !== 6)
|
|
195
|
+
return null;
|
|
196
|
+
return formatAllowEntry({ ...ip, prefix: 64, value: ip.value & maskFor(6, 64) });
|
|
197
|
+
}
|
|
198
|
+
/** A single IPv4 address as a `/32` entry. */
|
|
199
|
+
export function ipv4Slash32(address) {
|
|
200
|
+
const ip = parseIp(address);
|
|
201
|
+
if (!ip || ip.family !== 4)
|
|
202
|
+
return null;
|
|
203
|
+
return `${formatIp(ip)}/32`;
|
|
204
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { TunnelConfig } from "./state.js";
|
|
2
|
+
export declare const TRACE_URL_V4 = "https://1.1.1.1/cdn-cgi/trace";
|
|
3
|
+
export declare const TRACE_URL_V6 = "https://[2606:4700:4700::1111]/cdn-cgi/trace";
|
|
4
|
+
export interface DetectedAddresses {
|
|
5
|
+
v4?: string;
|
|
6
|
+
v6?: string;
|
|
7
|
+
/** One note per family that could not be read. */
|
|
8
|
+
failures: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface DetectOptions {
|
|
11
|
+
fetch?: typeof fetch;
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
/** Tries per family; a transient failure on a flaky network should not drop an address. */
|
|
14
|
+
attempts?: number;
|
|
15
|
+
}
|
|
16
|
+
/** The `ip=` value of a Cloudflare trace body, or null. */
|
|
17
|
+
export declare function parseTraceIp(body: string): string | null;
|
|
18
|
+
/** Both families in parallel. A family that fails is reported, not thrown. */
|
|
19
|
+
export declare function detectPublicAddresses(options?: DetectOptions): Promise<DetectedAddresses>;
|
|
20
|
+
export interface CurrentAddressPlan {
|
|
21
|
+
config: TunnelConfig;
|
|
22
|
+
/** Entries newly present in the allowlist. */
|
|
23
|
+
added: string[];
|
|
24
|
+
/** Automatic entries from an earlier refresh that are now gone. */
|
|
25
|
+
removed: string[];
|
|
26
|
+
/** The automatic entries after this refresh (the detected, not already listed by hand). */
|
|
27
|
+
automatic: string[];
|
|
28
|
+
changed: boolean;
|
|
29
|
+
warnings: string[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Fold freshly detected addresses into the allowlist.
|
|
33
|
+
*
|
|
34
|
+
* When nothing was detected the config comes back untouched with a warning:
|
|
35
|
+
* a failed lookup must never empty the list. When at least one family was
|
|
36
|
+
* detected, a family that was not is treated as absent from this network, so
|
|
37
|
+
* its old automatic entry goes. The entries for the detected families replace
|
|
38
|
+
* every previous automatic entry; manual entries are kept in place.
|
|
39
|
+
*/
|
|
40
|
+
export declare function planCurrentAddresses(config: TunnelConfig, detected: DetectedAddresses): CurrentAddressPlan;
|
|
41
|
+
//# sourceMappingURL=current-address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-address.d.ts","sourceRoot":"","sources":["../../../src/lib/tunnel/current-address.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,eAAO,MAAM,YAAY,kCAAkC,CAAC;AAC5D,eAAO,MAAM,YAAY,iDAAiD,CAAC;AAE3E,MAAM,WAAW,iBAAiB;IAChC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,kDAAkD;IAClD,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2FAA2F;IAC3F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,2DAA2D;AAC3D,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGxD;AAoBD,8EAA8E;AAC9E,wBAAsB,qBAAqB,CACzC,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,iBAAiB,CAAC,CAgB5B;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,YAAY,CAAC;IACrB,8CAA8C;IAC9C,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,mEAAmE;IACnE,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,2FAA2F;IAC3F,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAOD;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,YAAY,EACpB,QAAQ,EAAE,iBAAiB,GAC1B,kBAAkB,CAkDpB"}
|