@stigmer/runner 3.12.0 → 3.12.2
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/.build-fingerprint +1 -1
- package/dist/activities/execute-cursor/capture-flow.d.ts +5 -4
- package/dist/activities/execute-cursor/capture-flow.js +5 -4
- package/dist/activities/execute-cursor/capture-flow.js.map +1 -1
- package/dist/activities/execute-cursor/cas-observations.d.ts +10 -4
- package/dist/activities/execute-cursor/cas-observations.js +10 -4
- package/dist/activities/execute-cursor/cas-observations.js.map +1 -1
- package/dist/activities/execute-cursor/hook-script.js +39 -13
- package/dist/activities/execute-cursor/hook-script.js.map +1 -1
- package/dist/activities/execute-cursor/skill-resolver.d.ts +8 -0
- package/dist/activities/execute-cursor/skill-resolver.js +77 -26
- package/dist/activities/execute-cursor/skill-resolver.js.map +1 -1
- package/dist/activities/execute-deep-agent/attachment-injector.d.ts +23 -8
- package/dist/activities/execute-deep-agent/attachment-injector.js +104 -105
- package/dist/activities/execute-deep-agent/attachment-injector.js.map +1 -1
- package/dist/activities/execute-deep-agent/cas-capture-backend.d.ts +3 -1
- package/dist/activities/execute-deep-agent/cas-capture-backend.js +3 -1
- package/dist/activities/execute-deep-agent/cas-capture-backend.js.map +1 -1
- package/dist/activities/execute-deep-agent/prompt-builder.js +11 -1
- package/dist/activities/execute-deep-agent/prompt-builder.js.map +1 -1
- package/dist/activities/execute-deep-agent/setup.js +15 -3
- package/dist/activities/execute-deep-agent/setup.js.map +1 -1
- package/dist/activities/execute-deep-agent/subagent-wiring.js +16 -9
- package/dist/activities/execute-deep-agent/subagent-wiring.js.map +1 -1
- package/dist/activities/generate-session-subject.d.ts +80 -0
- package/dist/activities/generate-session-subject.js +283 -0
- package/dist/activities/generate-session-subject.js.map +1 -0
- package/dist/client/stigmer-client.d.ts +8 -0
- package/dist/client/stigmer-client.js +10 -0
- package/dist/client/stigmer-client.js.map +1 -1
- package/dist/middleware/approval-gate.d.ts +25 -10
- package/dist/middleware/approval-gate.js +33 -14
- package/dist/middleware/approval-gate.js.map +1 -1
- package/dist/middleware/otel-spans.d.ts +2 -1
- package/dist/middleware/otel-spans.js +2 -1
- package/dist/middleware/otel-spans.js.map +1 -1
- package/dist/middleware/path-normalization.d.ts +15 -2
- package/dist/middleware/path-normalization.js +39 -5
- package/dist/middleware/path-normalization.js.map +1 -1
- package/dist/runner-manager.js +3 -1
- package/dist/runner-manager.js.map +1 -1
- package/dist/runner.js +3 -1
- package/dist/runner.js.map +1 -1
- package/dist/shared/attachment-vision.js +9 -0
- package/dist/shared/attachment-vision.js.map +1 -1
- package/dist/shared/mcp-enabled-tools.d.ts +6 -2
- package/dist/shared/mcp-enabled-tools.js +6 -2
- package/dist/shared/mcp-enabled-tools.js.map +1 -1
- package/dist/shared/mcp-manager.js +8 -0
- package/dist/shared/mcp-manager.js.map +1 -1
- package/dist/shared/plan-mode-permissions.d.ts +46 -10
- package/dist/shared/plan-mode-permissions.js +56 -12
- package/dist/shared/plan-mode-permissions.js.map +1 -1
- package/dist/shared/zip-extract.d.ts +24 -6
- package/dist/shared/zip-extract.js +31 -90
- package/dist/shared/zip-extract.js.map +1 -1
- package/dist/shared/zip-structure.d.ts +61 -0
- package/dist/shared/zip-structure.js +128 -0
- package/dist/shared/zip-structure.js.map +1 -0
- package/package.json +2 -2
- package/src/__test-utils__/zip-fixtures.ts +206 -0
- package/src/activities/__tests__/generate-session-subject.test.ts +348 -0
- package/src/activities/execute-cursor/__tests__/hook-script.test.ts +60 -9
- package/src/activities/execute-cursor/__tests__/skill-resolver.test.ts +166 -49
- package/src/activities/execute-cursor/capture-flow.ts +5 -4
- package/src/activities/execute-cursor/cas-observations.ts +10 -4
- package/src/activities/execute-cursor/hook-script.ts +39 -13
- package/src/activities/execute-cursor/skill-resolver.ts +98 -31
- package/src/activities/execute-deep-agent/__tests__/attachment-injector.test.ts +165 -126
- package/src/activities/execute-deep-agent/__tests__/plan-mode-path-normalization.test.ts +246 -37
- package/src/activities/execute-deep-agent/__tests__/prompt-builder.test.ts +7 -2
- package/src/activities/execute-deep-agent/__tests__/subagent-plan-mode-permissions.test.ts +35 -4
- package/src/activities/execute-deep-agent/attachment-injector.ts +146 -142
- package/src/activities/execute-deep-agent/cas-capture-backend.ts +3 -1
- package/src/activities/execute-deep-agent/prompt-builder.ts +11 -1
- package/src/activities/execute-deep-agent/setup.ts +15 -3
- package/src/activities/execute-deep-agent/subagent-wiring.ts +16 -9
- package/src/activities/generate-session-subject.ts +370 -0
- package/src/client/stigmer-client.ts +11 -0
- package/src/middleware/__tests__/approval-gate.test.ts +130 -1
- package/src/middleware/__tests__/path-normalization.test.ts +29 -3
- package/src/middleware/approval-gate.ts +58 -24
- package/src/middleware/otel-spans.ts +2 -1
- package/src/middleware/path-normalization.ts +42 -5
- package/src/runner-manager.ts +3 -0
- package/src/runner.ts +3 -0
- package/src/shared/__tests__/attachment-vision.test.ts +4 -0
- package/src/shared/__tests__/mcp-manager.test.ts +7 -2
- package/src/shared/__tests__/plan-mode-permissions.test.ts +58 -0
- package/src/shared/__tests__/zip-extract.test.ts +106 -89
- package/src/shared/attachment-vision.ts +9 -0
- package/src/shared/filereview/__tests__/capture.test.ts +48 -0
- package/src/shared/mcp-enabled-tools.ts +6 -2
- package/src/shared/mcp-manager.ts +8 -0
- package/src/shared/plan-mode-permissions.ts +59 -12
- package/src/shared/zip-extract.ts +35 -117
- package/src/shared/zip-structure.ts +181 -0
|
@@ -946,6 +946,54 @@ describe("capture orchestration — CAS-only non-git workspace (Slice 2a)", () =
|
|
|
946
946
|
await expect(readFile(join(ws, "drop.txt"), "utf-8")).rejects.toThrow();
|
|
947
947
|
});
|
|
948
948
|
|
|
949
|
+
it("authors a DELETE from {before, after:null} and reconciles it both ways (issue #303)", async () => {
|
|
950
|
+
// The delete flow end-to-end at this seam: the harness captured the
|
|
951
|
+
// pre-delete bytes and the file is gone from disk (the tool ran). The
|
|
952
|
+
// candidate must carry a DELETE with the before blob; an approved DELETE
|
|
953
|
+
// stays deleted, a rejected DELETE is restored byte-exact from the blob.
|
|
954
|
+
const status = newStatus();
|
|
955
|
+
const storage = makeStorage();
|
|
956
|
+
const readBlob = reconcilerReadBlob(storage);
|
|
957
|
+
const enc = (s: string) => new TextEncoder().encode(s);
|
|
958
|
+
|
|
959
|
+
const baseline = await captureBaselineToLedger({
|
|
960
|
+
status, gitRoot: ws, executionId: EXEC_ID, changeSetId: CHANGE_SET_ID,
|
|
961
|
+
harnessId: HARNESS, gitWorkspace: false,
|
|
962
|
+
});
|
|
963
|
+
await captureCandidateToLedger({
|
|
964
|
+
status, gitRoot: ws, executionId: EXEC_ID, changeSetId: CHANGE_SET_ID,
|
|
965
|
+
baselineTree: baseline, harnessId: HARNESS, gitWorkspace: false, storage,
|
|
966
|
+
casCaptures: [
|
|
967
|
+
{ path: "gone-approved.txt", before: enc("A"), after: null, captureClass: FileCaptureClass.NON_GIT_CAS },
|
|
968
|
+
{ path: "gone-rejected.txt", before: enc("B"), after: null, captureClass: FileCaptureClass.NON_GIT_CAS },
|
|
969
|
+
],
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
const cand = candidateChanges(status);
|
|
973
|
+
expect(cand.map((c) => c.kind)).toEqual([FileChangeKind.DELETE, FileChangeKind.DELETE]);
|
|
974
|
+
// A DELETE names its path on the BEFORE side only, with the restorable blob.
|
|
975
|
+
expect(cand.map((c) => c.pathBefore)).toEqual(["gone-approved.txt", "gone-rejected.txt"]);
|
|
976
|
+
expect(cand.map((c) => c.pathAfter)).toEqual(["", ""]);
|
|
977
|
+
expect(cand[0].before?.body.case).toBe("ref");
|
|
978
|
+
expect(cand[0].after).toBeUndefined();
|
|
979
|
+
|
|
980
|
+
const changeSet = decidedChangeSet(status, {
|
|
981
|
+
"gone-approved.txt": FileDecisionAction.APPROVE,
|
|
982
|
+
"gone-rejected.txt": FileDecisionAction.REJECT,
|
|
983
|
+
});
|
|
984
|
+
const result = await applyCaptureDecisions({
|
|
985
|
+
status, gitRoot: ws, executionId: EXEC_ID, changeSet, harnessId: HARNESS,
|
|
986
|
+
gitWorkspace: false, storage, readBlob,
|
|
987
|
+
});
|
|
988
|
+
|
|
989
|
+
expect(result.failed).toBe(false);
|
|
990
|
+
expect(result.approvedPaths).toEqual(["gone-approved.txt"]);
|
|
991
|
+
expect(result.rejectedPaths).toEqual(["gone-rejected.txt"]);
|
|
992
|
+
// Approved DELETE: the file stays gone. Rejected DELETE: restored byte-exact.
|
|
993
|
+
await expect(readFile(join(ws, "gone-approved.txt"), "utf-8")).rejects.toThrow();
|
|
994
|
+
expect(await readFile(join(ws, "gone-rejected.txt"), "utf-8")).toBe("B");
|
|
995
|
+
});
|
|
996
|
+
|
|
949
997
|
it("is not a capture turn when neither a git ref nor a CAS manifest exists", async () => {
|
|
950
998
|
const status = newStatus();
|
|
951
999
|
const storage = makeStorage();
|
|
@@ -21,8 +21,12 @@
|
|
|
21
21
|
* identity space as the sub-agent McpAccess filter and the approval-policy
|
|
22
22
|
* maps. An enabled name the server does not expose is warned and dropped
|
|
23
23
|
* (enforce the intersection): the restriction still holds and the run
|
|
24
|
-
* proceeds with the valid subset
|
|
25
|
-
*
|
|
24
|
+
* proceeds with the valid subset. The server-side half (issue #402) rejects
|
|
25
|
+
* such names at apply time once the referenced server has discovered
|
|
26
|
+
* capabilities (stigmer-server validateEnabledToolsStep /
|
|
27
|
+
* validateDefaultEnabledToolsStep); this runtime leniency remains the
|
|
28
|
+
* safety net for manifests applied before a server's first connect and for
|
|
29
|
+
* toolsets that changed since the last discovery.
|
|
26
30
|
*/
|
|
27
31
|
|
|
28
32
|
/**
|
|
@@ -73,6 +73,14 @@ export function toMcpClientConfig(
|
|
|
73
73
|
transport: "http",
|
|
74
74
|
url: server.url,
|
|
75
75
|
headers: server.headers,
|
|
76
|
+
// Re-establish severed connections (oss#316): when a remote MCP
|
|
77
|
+
// server restarts mid-execution — the hosted bridge's sessions are
|
|
78
|
+
// in-memory, so a deploy orphans every live one — the adapter's
|
|
79
|
+
// onclose hook re-runs initialize, minting a fresh session for
|
|
80
|
+
// subsequent calls instead of stranding the agent on a dead one.
|
|
81
|
+
// Covers transport-close only; a session lost with no stream open
|
|
82
|
+
// still surfaces as a JSON-RPC session-not-found on the next call.
|
|
83
|
+
reconnect: { enabled: true, maxAttempts: 3, delayMs: 1_000 },
|
|
76
84
|
};
|
|
77
85
|
}
|
|
78
86
|
}
|
|
@@ -2,12 +2,26 @@
|
|
|
2
2
|
* The Plan-mode filesystem permission rules — the enforcement twin of
|
|
3
3
|
* `plan-mode-prompt.ts` (which carries the instruction half of the contract).
|
|
4
4
|
*
|
|
5
|
-
* Plan mode is
|
|
6
|
-
* deny every filesystem write
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
5
|
+
* Plan mode is contained BY CONSTRUCTION on the native harness: these rules
|
|
6
|
+
* deny every filesystem write everywhere AND scope reads to the session
|
|
7
|
+
* workspace (issue #528 — owner ruling: the workspace is plan mode's read
|
|
8
|
+
* boundary on cloud and desktop runners alike). Without the read boundary,
|
|
9
|
+
* model-provided absolute paths reached anywhere the process account could
|
|
10
|
+
* read — the pod filesystem (including /proc/self/environ) on cloud runners,
|
|
11
|
+
* the user's whole home directory on desktop — while plan mode still carries
|
|
12
|
+
* exfiltration-capable tools (web_fetch, MCP). Rules are first-match-wins
|
|
13
|
+
* with a permissive default (deepagents' decidePathAccess), so order is
|
|
14
|
+
* load-bearing: the workspace read-allow must precede the read-deny.
|
|
15
|
+
*
|
|
16
|
+
* The workspace-root pattern is matched as a STRING against the raw tool-call
|
|
17
|
+
* path (micromatch, dot:true), before the backend touches disk. That is
|
|
18
|
+
* exactly why the legitimate out-of-workspace reads keep working: skills,
|
|
19
|
+
* attachments, and the approved plan live in the platform dir but are
|
|
20
|
+
* addressed through the `{workspace}/.stigmer` symlink (see
|
|
21
|
+
* shared/workspace/stigmer-link.ts), so their path strings are in-root even
|
|
22
|
+
* though the bytes are not. The same holds for multi-workspace local entries
|
|
23
|
+
* (`{workspace}/{name}` symlinks). A realpath-based boundary would break
|
|
24
|
+
* both; do not "harden" this into one.
|
|
11
25
|
*
|
|
12
26
|
* Applied in execute-deep-agent/setup.ts to the parent graph AND threaded
|
|
13
27
|
* into every compiled sub-agent graph: deepagents' parent-permission
|
|
@@ -18,9 +32,9 @@
|
|
|
18
32
|
*
|
|
19
33
|
* Rules travel with a companion: every graph that carries them also installs
|
|
20
34
|
* the path-normalization middleware (middleware/path-normalization.ts,
|
|
21
|
-
*
|
|
22
|
-
* paths outright
|
|
23
|
-
*
|
|
35
|
+
* issues #429/#528), because deepagents' rule validation refuses
|
|
36
|
+
* workspace-relative paths outright and its `ls`/`glob`/`grep` schema default
|
|
37
|
+
* of "/" would deny the bare first listing. Both are derived from the same
|
|
24
38
|
* expression at each composition site so they cannot drift apart.
|
|
25
39
|
*
|
|
26
40
|
* Invariant: never combine these rules with a shell-capable (sandbox)
|
|
@@ -30,8 +44,41 @@
|
|
|
30
44
|
* for shell capability on both the parent and sub-agent backends.
|
|
31
45
|
*/
|
|
32
46
|
|
|
47
|
+
import { resolve } from "node:path";
|
|
33
48
|
import type { FilesystemPermission } from "deepagents";
|
|
34
49
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Backslash-escape every character micromatch/picomatch treats as glob
|
|
52
|
+
* syntax, so the result matches the input literally. micromatch exports no
|
|
53
|
+
* escape API of its own, and this is correctness, not caution: an unescaped
|
|
54
|
+
* `(` in a desktop project path would make the workspace read-allow rule
|
|
55
|
+
* silently never match — bricking every plan-mode read for that workspace.
|
|
56
|
+
* Semantics are pinned end-to-end through deepagents' own matcher by the
|
|
57
|
+
* special-character workspace suite in plan-mode-path-normalization.test.ts.
|
|
58
|
+
*/
|
|
59
|
+
export function escapeGlobLiteral(literal: string): string {
|
|
60
|
+
return literal.replace(/[\\*?()[\]{}!+@]/g, "\\$&");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Build the plan-mode rule set for a graph whose filesystem backend is
|
|
65
|
+
* rooted at `workspaceRootDir`. The three rules read as the policy:
|
|
66
|
+
* reads allowed in the workspace, reads denied everywhere else, writes
|
|
67
|
+
* denied everywhere.
|
|
68
|
+
*
|
|
69
|
+
* `{root}/**` matches the root itself as well as its subtree (verified
|
|
70
|
+
* against the installed micromatch), so one allow pattern suffices. The
|
|
71
|
+
* root is `path.resolve`d first because enforcement canonicalizes incoming
|
|
72
|
+
* paths (collapsed slashes, no trailing separator) before matching — a
|
|
73
|
+
* trailing slash in the pattern would silently match nothing.
|
|
74
|
+
*/
|
|
75
|
+
export function buildPlanModePermissions(
|
|
76
|
+
workspaceRootDir: string,
|
|
77
|
+
): FilesystemPermission[] {
|
|
78
|
+
const canonicalRoot = resolve(workspaceRootDir);
|
|
79
|
+
return [
|
|
80
|
+
{ operations: ["read"], paths: [`${escapeGlobLiteral(canonicalRoot)}/**`] },
|
|
81
|
+
{ operations: ["read"], paths: ["/**"], mode: "deny" },
|
|
82
|
+
{ operations: ["write"], paths: ["/**"], mode: "deny" },
|
|
83
|
+
];
|
|
84
|
+
}
|
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Skill-artifact ZIP extraction: text-decoded entries with non-fatal
|
|
3
|
+
* structural failure.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Structural parsing lives in zip-structure.ts (central-directory-based —
|
|
6
|
+
* see that module's doc for why local-header walks are never acceptable,
|
|
7
|
+
* issue #450). This module owns the skill-artifact *policy* on top of it:
|
|
8
|
+
*
|
|
9
|
+
* Input without a readable central directory is not given a second
|
|
10
|
+
* chance on purpose: every artifact reaching the runner was validated at
|
|
11
|
+
* push by a central-directory-based reader on both editions (OSS:
|
|
12
|
+
* `safearchive/zip` in stigmer-server's skill storage; cloud:
|
|
13
|
+
* commons-compress `ZipFile` in SkillArtifactExtractor), so a missing
|
|
14
|
+
* EOCD here can only mean a truncated or corrupted download — and the
|
|
15
|
+
* honest result for that is the documented empty return, not a guess.
|
|
16
|
+
* (The attachment injector consumes the same structural layer under the
|
|
17
|
+
* opposite policy — fail-hard — because its input is untrusted.)
|
|
18
|
+
*
|
|
19
|
+
* Handles stored (method 0) and deflated (method 8) entries. Returns
|
|
20
|
+
* parsed entries as path + content pairs — consumers bring their own
|
|
21
|
+
* write mechanism (WorkspaceBackend, node:fs, etc.).
|
|
8
22
|
*
|
|
9
23
|
* Extracted from skill-writer.ts so both the deep-agent and Cursor
|
|
10
24
|
* harnesses can share the same ZIP parsing logic without coupling to
|
|
@@ -12,6 +26,7 @@
|
|
|
12
26
|
*/
|
|
13
27
|
|
|
14
28
|
import { createInflateRaw } from "node:zlib";
|
|
29
|
+
import { EOCD_MIN_SIZE, parseZipStructure, type ZipStructuralEntry } from "./zip-structure.js";
|
|
15
30
|
|
|
16
31
|
// ─── Public API ──────────────────────────────────────────────────────────
|
|
17
32
|
|
|
@@ -29,18 +44,26 @@ export interface ZipFileEntry {
|
|
|
29
44
|
* entries whose filename (basename or full path) matches any excluded name.
|
|
30
45
|
*
|
|
31
46
|
* Returns an empty array for empty, truncated, or non-ZIP input rather
|
|
32
|
-
* than throwing — callers treat missing artifacts as non-fatal.
|
|
47
|
+
* than throwing — callers treat missing artifacts as non-fatal. Errors
|
|
48
|
+
* while *decompressing* a structurally valid entry (corrupt deflate
|
|
49
|
+
* stream, unsupported compression method) do propagate: at that point
|
|
50
|
+
* the archive's structure vouched for the entry, and silently dropping
|
|
51
|
+
* it would be the exact corruption this module exists to prevent.
|
|
33
52
|
*/
|
|
34
53
|
export async function extractZipFileEntries(
|
|
35
54
|
zipBytes: Uint8Array,
|
|
36
55
|
options?: { exclude?: readonly string[] },
|
|
37
56
|
): Promise<ZipFileEntry[]> {
|
|
38
|
-
if (zipBytes.length <
|
|
57
|
+
if (zipBytes.length < EOCD_MIN_SIZE) return [];
|
|
39
58
|
|
|
40
|
-
let entries:
|
|
59
|
+
let entries: ZipStructuralEntry[];
|
|
41
60
|
try {
|
|
42
|
-
entries =
|
|
43
|
-
} catch {
|
|
61
|
+
entries = parseZipStructure(zipBytes);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
console.warn(
|
|
64
|
+
"[zip-extract] archive has no readable central directory " +
|
|
65
|
+
`(truncated or corrupt download?): ${err instanceof Error ? err.message : String(err)}`,
|
|
66
|
+
);
|
|
44
67
|
return [];
|
|
45
68
|
}
|
|
46
69
|
|
|
@@ -58,16 +81,6 @@ export async function extractZipFileEntries(
|
|
|
58
81
|
return results;
|
|
59
82
|
}
|
|
60
83
|
|
|
61
|
-
// ─── Internals ───────────────────────────────────────────────────────────
|
|
62
|
-
|
|
63
|
-
interface ZipEntry {
|
|
64
|
-
name: string;
|
|
65
|
-
isDirectory: boolean;
|
|
66
|
-
compressedData: Uint8Array;
|
|
67
|
-
compressionMethod: number;
|
|
68
|
-
uncompressedSize: number;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
84
|
function isExcluded(name: string, excludeSet: ReadonlySet<string>): boolean {
|
|
72
85
|
if (excludeSet.size === 0) return false;
|
|
73
86
|
if (excludeSet.has(name)) return true;
|
|
@@ -76,104 +89,9 @@ function isExcluded(name: string, excludeSet: ReadonlySet<string>): boolean {
|
|
|
76
89
|
return excludeSet.has(basename);
|
|
77
90
|
}
|
|
78
91
|
|
|
79
|
-
|
|
80
|
-
const entries: ZipEntry[] = [];
|
|
81
|
-
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
82
|
-
let offset = 0;
|
|
83
|
-
|
|
84
|
-
while (offset < data.length - 4) {
|
|
85
|
-
const signature = view.getUint32(offset, true);
|
|
86
|
-
if (signature !== 0x04034b50) break; // Local file header signature
|
|
87
|
-
|
|
88
|
-
const generalFlags = view.getUint16(offset + 6, true);
|
|
89
|
-
const hasDataDescriptor = (generalFlags & 0x08) !== 0;
|
|
90
|
-
const compressionMethod = view.getUint16(offset + 8, true);
|
|
91
|
-
let compressedSize = view.getUint32(offset + 18, true);
|
|
92
|
-
let uncompressedSize = view.getUint32(offset + 22, true);
|
|
93
|
-
const fileNameLength = view.getUint16(offset + 26, true);
|
|
94
|
-
const extraFieldLength = view.getUint16(offset + 28, true);
|
|
95
|
-
|
|
96
|
-
const fileNameStart = offset + 30;
|
|
97
|
-
const fileName = new TextDecoder().decode(
|
|
98
|
-
data.subarray(fileNameStart, fileNameStart + fileNameLength),
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
const dataStart = fileNameStart + fileNameLength + extraFieldLength;
|
|
102
|
-
|
|
103
|
-
if (hasDataDescriptor && compressedSize === 0) {
|
|
104
|
-
const sizes = findDataDescriptor(data, view, dataStart, compressionMethod);
|
|
105
|
-
compressedSize = sizes.compressedSize;
|
|
106
|
-
uncompressedSize = sizes.uncompressedSize;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const compressedData = data.subarray(dataStart, dataStart + compressedSize);
|
|
110
|
-
|
|
111
|
-
entries.push({
|
|
112
|
-
name: fileName,
|
|
113
|
-
isDirectory: fileName.endsWith("/"),
|
|
114
|
-
compressedData,
|
|
115
|
-
compressionMethod,
|
|
116
|
-
uncompressedSize,
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
let nextOffset = dataStart + compressedSize;
|
|
120
|
-
if (hasDataDescriptor) {
|
|
121
|
-
if (nextOffset + 4 <= data.length && view.getUint32(nextOffset, true) === 0x08074b50) {
|
|
122
|
-
nextOffset += 16; // signature(4) + crc(4) + compressedSize(4) + uncompressedSize(4)
|
|
123
|
-
} else {
|
|
124
|
-
nextOffset += 12; // crc(4) + compressedSize(4) + uncompressedSize(4)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
offset = nextOffset;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return entries;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Scan forward from dataStart to find the data descriptor that contains
|
|
135
|
-
* the actual compressed and uncompressed sizes. Looks for either the
|
|
136
|
-
* optional signature 0x08074b50 or falls back to scanning the central
|
|
137
|
-
* directory for the matching entry.
|
|
138
|
-
*/
|
|
139
|
-
function findDataDescriptor(
|
|
140
|
-
data: Uint8Array,
|
|
141
|
-
view: DataView,
|
|
142
|
-
dataStart: number,
|
|
143
|
-
_compressionMethod: number,
|
|
144
|
-
): { compressedSize: number; uncompressedSize: number } {
|
|
145
|
-
for (let pos = dataStart; pos < data.length - 16; pos++) {
|
|
146
|
-
const sig = view.getUint32(pos, true);
|
|
147
|
-
if (sig === 0x08074b50) {
|
|
148
|
-
return {
|
|
149
|
-
compressedSize: view.getUint32(pos + 8, true),
|
|
150
|
-
uncompressedSize: view.getUint32(pos + 12, true),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
if (sig === 0x04034b50 || sig === 0x02014b50) {
|
|
154
|
-
const descStart = pos - 12;
|
|
155
|
-
if (descStart >= dataStart) {
|
|
156
|
-
return {
|
|
157
|
-
compressedSize: view.getUint32(descStart + 4, true),
|
|
158
|
-
uncompressedSize: view.getUint32(descStart + 8, true),
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
for (let pos = dataStart; pos < data.length - 4; pos++) {
|
|
165
|
-
const sig = view.getUint32(pos, true);
|
|
166
|
-
if (sig === 0x04034b50 || sig === 0x02014b50 || sig === 0x08074b50) {
|
|
167
|
-
const compressedSize = sig === 0x08074b50
|
|
168
|
-
? view.getUint32(pos + 8, true)
|
|
169
|
-
: pos - dataStart;
|
|
170
|
-
return { compressedSize, uncompressedSize: 0 };
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
return { compressedSize: data.length - dataStart, uncompressedSize: 0 };
|
|
174
|
-
}
|
|
92
|
+
// ─── Decompression ───────────────────────────────────────────────────────
|
|
175
93
|
|
|
176
|
-
async function decompressEntry(entry:
|
|
94
|
+
async function decompressEntry(entry: ZipStructuralEntry): Promise<string> {
|
|
177
95
|
if (entry.compressionMethod === 0) {
|
|
178
96
|
return new TextDecoder().decode(entry.compressedData);
|
|
179
97
|
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
|
|
3
|
+
* location plus central-directory walk, producing one record per entry.
|
|
4
|
+
*
|
|
5
|
+
* Parsing is *central-directory-based* — never a front-to-back walk of
|
|
6
|
+
* local file headers. This is a correctness decision, not a style choice
|
|
7
|
+
* (issues #450 and #567): local headers of streaming entries
|
|
8
|
+
* (general-purpose flag bit 3) carry zeroed sizes, and the only
|
|
9
|
+
* local-only way to recover them is scanning the payload for the
|
|
10
|
+
* data-descriptor signature — which silently truncates any stored entry
|
|
11
|
+
* whose *content* happens to contain those four bytes, and
|
|
12
|
+
* desynchronizes every entry after it. The central directory always
|
|
13
|
+
* carries the real sizes, and every consumer here holds the complete
|
|
14
|
+
* archive bytes, so nothing a local-header walk could offer is needed.
|
|
15
|
+
*
|
|
16
|
+
* This layer maps bytes to entry records and nothing else. Policy — what
|
|
17
|
+
* a structural failure means, which entries are acceptable, how payloads
|
|
18
|
+
* are decoded — belongs to the consumers, and they differ on purpose:
|
|
19
|
+
*
|
|
20
|
+
* - shared/zip-extract.ts (skill artifacts): structural failure is
|
|
21
|
+
* NON-FATAL — both editions' push gates validated every artifact
|
|
22
|
+
* with a central-directory-based reader before storage, so a defect
|
|
23
|
+
* here can only be a truncated or corrupted download.
|
|
24
|
+
* - execute-deep-agent/attachment-injector.ts (user attachments):
|
|
25
|
+
* structural failure is FAIL-HARD — the input is an untrusted
|
|
26
|
+
* upload and nothing upstream vouched for it.
|
|
27
|
+
*
|
|
28
|
+
* ZIP64 is deliberately unsupported: both consumers cap input far below
|
|
29
|
+
* every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
|
|
30
|
+
* attachment uploads: 10MB).
|
|
31
|
+
*
|
|
32
|
+
* Throws plain `Error`s on structural defects (no valid EOCD, a central
|
|
33
|
+
* directory or local header record that does not parse, a payload slice
|
|
34
|
+
* that runs past the end of the buffer); consumers translate those into
|
|
35
|
+
* their own error models.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
// ─── Entry records ───────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
export interface ZipStructuralEntry {
|
|
41
|
+
/** Entry path exactly as recorded in the central directory. */
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly isDirectory: boolean;
|
|
44
|
+
readonly compressionMethod: number;
|
|
45
|
+
/**
|
|
46
|
+
* The central directory's declared uncompressed size — authoritative for
|
|
47
|
+
* pre-extraction accounting (e.g. ZIP-bomb budgeting), but still a
|
|
48
|
+
* *declaration*: a consumer that distrusts its input must enforce it
|
|
49
|
+
* against the actual decompressed output.
|
|
50
|
+
*/
|
|
51
|
+
readonly uncompressedSize: number;
|
|
52
|
+
/** The entry's raw payload slice (a view, not a copy) of the archive buffer. */
|
|
53
|
+
readonly compressedData: Uint8Array;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ─── Format constants ────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
const LOCAL_FILE_HEADER_SIG = 0x04034b50;
|
|
59
|
+
const CENTRAL_DIRECTORY_SIG = 0x02014b50;
|
|
60
|
+
const EOCD_SIG = 0x06054b50;
|
|
61
|
+
|
|
62
|
+
/** Fixed size of the EOCD record, excluding the variable-length comment. */
|
|
63
|
+
export const EOCD_MIN_SIZE = 22;
|
|
64
|
+
/** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
|
|
65
|
+
const EOCD_MAX_COMMENT = 0xffff;
|
|
66
|
+
|
|
67
|
+
const LOCAL_HEADER_SIZE = 30;
|
|
68
|
+
const CD_RECORD_SIZE = 46;
|
|
69
|
+
|
|
70
|
+
// ─── Parsing ─────────────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Enumerate the archive's entries from its central directory.
|
|
74
|
+
*
|
|
75
|
+
* Throws on any structural defect: no valid EOCD, a central directory or
|
|
76
|
+
* local header record that doesn't parse, or a payload slice that runs
|
|
77
|
+
* past the end of the buffer.
|
|
78
|
+
*/
|
|
79
|
+
export function parseZipStructure(data: Uint8Array): ZipStructuralEntry[] {
|
|
80
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
81
|
+
const eocd = findEndOfCentralDirectory(data, view);
|
|
82
|
+
|
|
83
|
+
const entries: ZipStructuralEntry[] = [];
|
|
84
|
+
let offset = eocd.centralDirectoryOffset;
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < eocd.entryCount; i++) {
|
|
87
|
+
if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
|
|
88
|
+
throw new Error(`invalid central directory record at offset ${offset}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const compressionMethod = view.getUint16(offset + 10, true);
|
|
92
|
+
const compressedSize = view.getUint32(offset + 20, true);
|
|
93
|
+
const uncompressedSize = view.getUint32(offset + 24, true);
|
|
94
|
+
const fileNameLength = view.getUint16(offset + 28, true);
|
|
95
|
+
const extraFieldLength = view.getUint16(offset + 30, true);
|
|
96
|
+
const commentLength = view.getUint16(offset + 32, true);
|
|
97
|
+
const localHeaderOffset = view.getUint32(offset + 42, true);
|
|
98
|
+
|
|
99
|
+
const fileName = new TextDecoder().decode(
|
|
100
|
+
data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// The local header is consulted for one thing only: the payload start.
|
|
104
|
+
// Its name/extra field lengths can legitimately differ from the central
|
|
105
|
+
// directory's (streaming writers pad the extra field), so the offset
|
|
106
|
+
// cannot be derived from CD fields alone.
|
|
107
|
+
const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
|
|
108
|
+
if (dataStart + compressedSize > data.length) {
|
|
109
|
+
throw new Error(`entry "${fileName}" payload runs past end of archive`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
entries.push({
|
|
113
|
+
name: fileName,
|
|
114
|
+
isDirectory: fileName.endsWith("/"),
|
|
115
|
+
compressionMethod,
|
|
116
|
+
uncompressedSize,
|
|
117
|
+
compressedData: data.subarray(dataStart, dataStart + compressedSize),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return entries;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface EndOfCentralDirectory {
|
|
127
|
+
centralDirectoryOffset: number;
|
|
128
|
+
entryCount: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Locate and validate the end-of-central-directory record.
|
|
133
|
+
*
|
|
134
|
+
* Scans backward from the end of the archive across the maximum comment
|
|
135
|
+
* span. A signature match alone is not trusted (the four bytes can occur
|
|
136
|
+
* inside a trailing comment): the record must also point at an offset
|
|
137
|
+
* that actually holds a central directory record, or be a genuinely
|
|
138
|
+
* empty archive.
|
|
139
|
+
*/
|
|
140
|
+
function findEndOfCentralDirectory(data: Uint8Array, view: DataView): EndOfCentralDirectory {
|
|
141
|
+
const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
|
|
142
|
+
|
|
143
|
+
for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
|
|
144
|
+
if (view.getUint32(pos, true) !== EOCD_SIG) continue;
|
|
145
|
+
|
|
146
|
+
const entryCount = view.getUint16(pos + 10, true);
|
|
147
|
+
const centralDirectorySize = view.getUint32(pos + 12, true);
|
|
148
|
+
const centralDirectoryOffset = view.getUint32(pos + 16, true);
|
|
149
|
+
|
|
150
|
+
const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
|
|
151
|
+
const directoryLooksReal =
|
|
152
|
+
entryCount === 0 ||
|
|
153
|
+
(centralDirectoryOffset + 4 <= data.length &&
|
|
154
|
+
view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
|
|
155
|
+
|
|
156
|
+
if (directoryEndsAtRecord && directoryLooksReal) {
|
|
157
|
+
return { centralDirectoryOffset, entryCount };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
throw new Error("no end-of-central-directory record found");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Resolve where an entry's payload begins, from its local file header. */
|
|
165
|
+
function payloadStart(
|
|
166
|
+
data: Uint8Array,
|
|
167
|
+
view: DataView,
|
|
168
|
+
localHeaderOffset: number,
|
|
169
|
+
fileName: string,
|
|
170
|
+
): number {
|
|
171
|
+
if (
|
|
172
|
+
localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
|
|
173
|
+
view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG
|
|
174
|
+
) {
|
|
175
|
+
throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const localNameLength = view.getUint16(localHeaderOffset + 26, true);
|
|
179
|
+
const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
|
|
180
|
+
return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
|
|
181
|
+
}
|