crysk 0.0.0 → 0.3.1
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/LICENSE +176 -0
- package/README.md +611 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/SKILL-y8rv8msh.md +134 -0
- package/dist/SKILL-ygka1bs0.md +515 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/ci-admission-qk9r9taq.md +124 -0
- package/dist/cli.js +42501 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +124 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +515 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +945 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +910 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +984 -0
- package/src/pr/answered-findings.ts +169 -0
- package/src/pr/ci-admission-gate.ts +743 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +845 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const MAX_BYTES = 4 * 1024 * 1024;
|
|
2
|
+
const SECRET =
|
|
3
|
+
/authorization|cookie|password|secret|api.?key|access.?token|refresh.?token|^token$|^key$/i;
|
|
4
|
+
export function redactEvidenceText(value: string): string {
|
|
5
|
+
return value
|
|
6
|
+
.replace(
|
|
7
|
+
/\b(?:authorization|proxy-authorization|cookie|set-cookie)\s*:[^\r\n]*/gi,
|
|
8
|
+
"[REDACTED HEADER]",
|
|
9
|
+
)
|
|
10
|
+
.replace(/https?:\/\/[^\s<>"']+/gi, (text) => {
|
|
11
|
+
try {
|
|
12
|
+
const url = new URL(text);
|
|
13
|
+
url.username = "";
|
|
14
|
+
url.password = "";
|
|
15
|
+
for (const key of [...url.searchParams.keys()])
|
|
16
|
+
if (SECRET.test(key)) url.searchParams.set(key, "[REDACTED]");
|
|
17
|
+
return url.toString();
|
|
18
|
+
} catch {
|
|
19
|
+
return "[REDACTED URL]";
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
.replace(
|
|
23
|
+
/\b(?:sk-[\w-]+|gh[pousr]_[\w]+|github_pat_[\w]+|Bearer\s+[^\s,;]+)/gi,
|
|
24
|
+
"[REDACTED]",
|
|
25
|
+
)
|
|
26
|
+
.replace(
|
|
27
|
+
/((?:api[_-]?key|access[_-]?token|refresh[_-]?token|token|password|secret)\s*[=:]\s*)[^\s,;&]+/gi,
|
|
28
|
+
"$1[REDACTED]",
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
/** JSON-only bounded traversal: never call getters, custom iterators or toJSON. */
|
|
32
|
+
export function redactEvidence(
|
|
33
|
+
value: unknown,
|
|
34
|
+
depth = 0,
|
|
35
|
+
budget = { nodes: 0 },
|
|
36
|
+
): unknown {
|
|
37
|
+
if (++budget.nodes > 20000 || depth > 12)
|
|
38
|
+
throw new Error("capture shape limit");
|
|
39
|
+
if (typeof value === "string") {
|
|
40
|
+
if (value.length > MAX_BYTES) throw new Error("capture string limit");
|
|
41
|
+
return redactEvidenceText(value);
|
|
42
|
+
}
|
|
43
|
+
if (value === null || typeof value === "boolean" || typeof value === "number")
|
|
44
|
+
return value;
|
|
45
|
+
if (value === undefined) return null;
|
|
46
|
+
if (typeof value !== "object") throw new Error("non-JSON capture value");
|
|
47
|
+
const proto = Object.getPrototypeOf(value);
|
|
48
|
+
if (proto !== Object.prototype && proto !== Array.prototype && proto !== null)
|
|
49
|
+
throw new Error("non-JSON capture object");
|
|
50
|
+
const result: Record<string, unknown> = {};
|
|
51
|
+
for (const [key, descriptor] of Object.entries(
|
|
52
|
+
Object.getOwnPropertyDescriptors(value),
|
|
53
|
+
)) {
|
|
54
|
+
if (key === "length" && Array.isArray(value)) continue;
|
|
55
|
+
if (!("value" in descriptor)) throw new Error("capture accessor rejected");
|
|
56
|
+
if (!descriptor.enumerable) continue;
|
|
57
|
+
result[key] = SECRET.test(key)
|
|
58
|
+
? "[REDACTED]"
|
|
59
|
+
: redactEvidence(descriptor.value, depth + 1, budget);
|
|
60
|
+
}
|
|
61
|
+
return Array.isArray(value) ? Object.values(result) : result;
|
|
62
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { WorkspaceReadBroker } from "./workspace-read-broker";
|
|
2
|
+
|
|
3
|
+
export interface WorkspaceCwdAuthorization {
|
|
4
|
+
readonly approved: true;
|
|
5
|
+
readonly canonicalCwd: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface WorkspaceCwdDenial {
|
|
9
|
+
readonly approved: false;
|
|
10
|
+
readonly reason: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type WorkspaceCwdAuthorizationResult =
|
|
14
|
+
| WorkspaceCwdAuthorization
|
|
15
|
+
| WorkspaceCwdDenial;
|
|
16
|
+
|
|
17
|
+
export function authorizeWorkspaceCwd(
|
|
18
|
+
workspaceRoot: string,
|
|
19
|
+
cwd: string,
|
|
20
|
+
): WorkspaceCwdAuthorizationResult {
|
|
21
|
+
const broker = new WorkspaceReadBroker({ workspaceRoot });
|
|
22
|
+
const result = broker.authorizePath(cwd);
|
|
23
|
+
if (!result.approved) {
|
|
24
|
+
return { approved: false, reason: result.reason };
|
|
25
|
+
}
|
|
26
|
+
return { approved: true, canonicalCwd: result.canonicalPath };
|
|
27
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// §6.3: redaction occurs BEFORE events, callbacks, logs, persistence, and
|
|
2
|
+
// thrown user-visible errors. These patterns are the persisted-witness
|
|
3
|
+
// allowlist: anything matching them is replaced wholesale rather than
|
|
4
|
+
// partially, because a partially-redacted credential is still a credential.
|
|
5
|
+
const REDACTED = "[REDACTED]";
|
|
6
|
+
|
|
7
|
+
const REDACTION_PATTERNS: readonly RegExp[] = [
|
|
8
|
+
/sk-[A-Za-z0-9_-]{8,}/g,
|
|
9
|
+
/gh[pousr]_[A-Za-z0-9_]{20,}/g,
|
|
10
|
+
/Bearer\s+\S+/gi,
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
// A keyword followed by a value: `token: abc`, `api_key="abc"`, `password hunter2`. When
|
|
14
|
+
// only whitespace separates the two, the next word may be ordinary prose ("access token is
|
|
15
|
+
// invalid"), which is the provider's sentence and not a secret. Those few words are kept; any
|
|
16
|
+
// other word after whitespace, and anything after a delimiter, is still treated as a value.
|
|
17
|
+
const KEYWORD_VALUE = /(api[_-]?key|token|password)((?:["':=]|\s)+)(\S+)/gi;
|
|
18
|
+
const PROSE_AFTER_KEYWORD = new Set([
|
|
19
|
+
"is",
|
|
20
|
+
"was",
|
|
21
|
+
"are",
|
|
22
|
+
"were",
|
|
23
|
+
"has",
|
|
24
|
+
"have",
|
|
25
|
+
"had",
|
|
26
|
+
"will",
|
|
27
|
+
"would",
|
|
28
|
+
"can",
|
|
29
|
+
"could",
|
|
30
|
+
"must",
|
|
31
|
+
"should",
|
|
32
|
+
"not",
|
|
33
|
+
"no",
|
|
34
|
+
"in",
|
|
35
|
+
"on",
|
|
36
|
+
"at",
|
|
37
|
+
"of",
|
|
38
|
+
"or",
|
|
39
|
+
"and",
|
|
40
|
+
"to",
|
|
41
|
+
"for",
|
|
42
|
+
"from",
|
|
43
|
+
"with",
|
|
44
|
+
"expired",
|
|
45
|
+
"invalid",
|
|
46
|
+
"rejected",
|
|
47
|
+
"revoked",
|
|
48
|
+
"missing",
|
|
49
|
+
"required",
|
|
50
|
+
"provided",
|
|
51
|
+
"found",
|
|
52
|
+
"refused",
|
|
53
|
+
"denied",
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
export function redactDiagnostic(text: string): string {
|
|
57
|
+
let redacted = text;
|
|
58
|
+
for (const pattern of REDACTION_PATTERNS) {
|
|
59
|
+
redacted = redacted.replace(pattern, REDACTED);
|
|
60
|
+
}
|
|
61
|
+
return redacted.replace(
|
|
62
|
+
KEYWORD_VALUE,
|
|
63
|
+
(match: string, _keyword: string, separator: string, word: string) =>
|
|
64
|
+
/^\s+$/.test(separator) &&
|
|
65
|
+
// Only trailing sentence punctuation is ignored: `is1234` or `no123!` is a value
|
|
66
|
+
// that happens to start with a prose word, and stays redacted.
|
|
67
|
+
PROSE_AFTER_KEYWORD.has(word.toLowerCase().replace(/[.,;:!?)\]"']+$/, ""))
|
|
68
|
+
? match
|
|
69
|
+
: REDACTED,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
lstatSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
realpathSync,
|
|
6
|
+
type Stats,
|
|
7
|
+
} from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { wrapBlock } from "#review/boundary";
|
|
10
|
+
|
|
11
|
+
export type WorkspaceDenialCode = "path_not_approved";
|
|
12
|
+
|
|
13
|
+
// §6.2 defaults: per-file 512 KiB, aggregate 8 MiB per broker instance.
|
|
14
|
+
export const DEFAULT_MAX_FILE_BYTES = 512 * 1024;
|
|
15
|
+
export const DEFAULT_MAX_AGGREGATE_BYTES = 8 * 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
// §6.2 item 3 default deny list, applied to the path relative to the root.
|
|
18
|
+
// Basename patterns guard the final component; the git entries are exact
|
|
19
|
+
// relative paths because their danger is location, not name.
|
|
20
|
+
const DEFAULT_SENSITIVE_BASENAME_PATTERNS = [
|
|
21
|
+
/^\.env/,
|
|
22
|
+
/credential/i,
|
|
23
|
+
/secret/i,
|
|
24
|
+
/private[-_]?key/i,
|
|
25
|
+
/\.pem$/i,
|
|
26
|
+
/^id_rsa/,
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
const DEFAULT_SENSITIVE_RELATIVE_PATHS = [
|
|
30
|
+
".git/config",
|
|
31
|
+
".git/credentials",
|
|
32
|
+
] as const;
|
|
33
|
+
|
|
34
|
+
// Git's global/one-shot config and repo-redirection flags can repoint git at
|
|
35
|
+
// an arbitrary tree or inject config from env without touching args order —
|
|
36
|
+
// none of them are ever legitimate inside a brokered read.
|
|
37
|
+
const DENIED_GIT_FLAGS = [
|
|
38
|
+
"--git-dir",
|
|
39
|
+
"--work-tree",
|
|
40
|
+
"--super-prefix",
|
|
41
|
+
"--namespace",
|
|
42
|
+
"--exec-path",
|
|
43
|
+
"-c",
|
|
44
|
+
] as const;
|
|
45
|
+
|
|
46
|
+
export const GIT_NOT_FOUND_STDERR = "git not found at pinned paths";
|
|
47
|
+
|
|
48
|
+
const PINNED_GIT_PATHS = [
|
|
49
|
+
"/usr/bin/git",
|
|
50
|
+
"/opt/homebrew/bin/git",
|
|
51
|
+
"/usr/local/bin/git",
|
|
52
|
+
] as const;
|
|
53
|
+
|
|
54
|
+
export function resolvePinnedGitPath(
|
|
55
|
+
override?: string,
|
|
56
|
+
existsFn: (p: string) => boolean = existsSync,
|
|
57
|
+
): string | null {
|
|
58
|
+
if (override !== undefined) {
|
|
59
|
+
return existsFn(override) ? override : null;
|
|
60
|
+
}
|
|
61
|
+
for (const candidate of PINNED_GIT_PATHS) {
|
|
62
|
+
if (existsFn(candidate)) return candidate;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function buildStrippedGitEnv(): Record<string, string> {
|
|
68
|
+
const childEnv: Record<string, string> = {};
|
|
69
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
70
|
+
if (/^GIT_/i.test(key)) continue;
|
|
71
|
+
if (value !== undefined) childEnv[key] = value;
|
|
72
|
+
}
|
|
73
|
+
return childEnv;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface WorkspaceAuthorizationApproved {
|
|
77
|
+
readonly approved: true;
|
|
78
|
+
readonly canonicalPath: string;
|
|
79
|
+
readonly code?: undefined;
|
|
80
|
+
readonly reason?: undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface WorkspaceAuthorizationDenied {
|
|
84
|
+
readonly approved: false;
|
|
85
|
+
readonly code: WorkspaceDenialCode;
|
|
86
|
+
readonly reason: string;
|
|
87
|
+
readonly canonicalPath?: undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type WorkspaceAuthorizationResult =
|
|
91
|
+
| WorkspaceAuthorizationApproved
|
|
92
|
+
| WorkspaceAuthorizationDenied;
|
|
93
|
+
|
|
94
|
+
export interface WorkspaceReadBrokerOptions {
|
|
95
|
+
readonly workspaceRoot: string;
|
|
96
|
+
readonly sensitivePatterns?: readonly (string | RegExp)[];
|
|
97
|
+
readonly maxFileBytes?: number;
|
|
98
|
+
readonly maxAggregateBytes?: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// §6.3: broker-returned text is untrusted data and must be nonce-delimited and
|
|
102
|
+
// labelled before prompt inclusion. "comment_body" is the closest existing
|
|
103
|
+
// closed-vocabulary tag; the union is deliberately not widened here.
|
|
104
|
+
export interface WorkspaceReadThroughOptions {
|
|
105
|
+
readonly nonce: string;
|
|
106
|
+
readonly lstatFn?: (p: string) => Stats;
|
|
107
|
+
readonly readFileFn?: (p: string) => Buffer;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type WorkspaceReadThroughResult =
|
|
111
|
+
| {
|
|
112
|
+
readonly approved: true;
|
|
113
|
+
readonly bytes: Buffer;
|
|
114
|
+
readonly wrapped: string;
|
|
115
|
+
}
|
|
116
|
+
| WorkspaceAuthorizationDenied;
|
|
117
|
+
|
|
118
|
+
function isInsideRoot(candidate: string, root: string): boolean {
|
|
119
|
+
const rel = path.relative(root, candidate);
|
|
120
|
+
return !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function resolveCanonicalPath(targetPath: string): string | null {
|
|
124
|
+
try {
|
|
125
|
+
return realpathSync(targetPath);
|
|
126
|
+
} catch {
|
|
127
|
+
let curr = path.dirname(targetPath);
|
|
128
|
+
const trail: string[] = [path.basename(targetPath)];
|
|
129
|
+
while (curr !== path.dirname(curr)) {
|
|
130
|
+
if (existsSync(curr)) {
|
|
131
|
+
try {
|
|
132
|
+
const canonicalAncestor = realpathSync(curr);
|
|
133
|
+
return path.join(canonicalAncestor, ...trail);
|
|
134
|
+
} catch {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
trail.unshift(path.basename(curr));
|
|
139
|
+
curr = path.dirname(curr);
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export class WorkspaceReadBroker {
|
|
146
|
+
readonly workspaceRoot: string;
|
|
147
|
+
readonly canonicalRoot: string;
|
|
148
|
+
readonly sensitivePatterns: readonly (string | RegExp)[];
|
|
149
|
+
readonly maxFileBytes: number;
|
|
150
|
+
readonly maxAggregateBytes: number;
|
|
151
|
+
private aggregateBytes = 0;
|
|
152
|
+
|
|
153
|
+
constructor(options: WorkspaceReadBrokerOptions | string) {
|
|
154
|
+
const root = typeof options === "string" ? options : options.workspaceRoot;
|
|
155
|
+
this.workspaceRoot = path.resolve(root);
|
|
156
|
+
try {
|
|
157
|
+
this.canonicalRoot = realpathSync(this.workspaceRoot);
|
|
158
|
+
} catch {
|
|
159
|
+
this.canonicalRoot = this.workspaceRoot;
|
|
160
|
+
}
|
|
161
|
+
this.sensitivePatterns =
|
|
162
|
+
typeof options === "object" && options.sensitivePatterns
|
|
163
|
+
? options.sensitivePatterns
|
|
164
|
+
: [];
|
|
165
|
+
this.maxFileBytes =
|
|
166
|
+
typeof options === "object" && options.maxFileBytes !== undefined
|
|
167
|
+
? options.maxFileBytes
|
|
168
|
+
: DEFAULT_MAX_FILE_BYTES;
|
|
169
|
+
this.maxAggregateBytes =
|
|
170
|
+
typeof options === "object" && options.maxAggregateBytes !== undefined
|
|
171
|
+
? options.maxAggregateBytes
|
|
172
|
+
: DEFAULT_MAX_AGGREGATE_BYTES;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private checkSensitiveRelative(rel: string): boolean {
|
|
176
|
+
const normalizedRel = rel.split(path.sep).join("/");
|
|
177
|
+
if (
|
|
178
|
+
(DEFAULT_SENSITIVE_RELATIVE_PATHS as readonly string[]).includes(
|
|
179
|
+
normalizedRel,
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
const basename = path.basename(normalizedRel);
|
|
185
|
+
for (const pattern of DEFAULT_SENSITIVE_BASENAME_PATTERNS) {
|
|
186
|
+
if (pattern.test(basename)) return true;
|
|
187
|
+
}
|
|
188
|
+
for (const pattern of this.sensitivePatterns) {
|
|
189
|
+
if (
|
|
190
|
+
typeof pattern === "string"
|
|
191
|
+
? basename === pattern || normalizedRel === pattern
|
|
192
|
+
: pattern.test(basename) || pattern.test(normalizedRel)
|
|
193
|
+
) {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// §6.2 item 1: realpath alone is not enough for READS — a symlink that
|
|
201
|
+
// resolves to another in-root location passes the containment proof but
|
|
202
|
+
// still lets a reader follow re-pointed components. Returns "" when the
|
|
203
|
+
// candidate cannot be walked lexically below either root spelling (an alias
|
|
204
|
+
// reached through a symlink), else the offending component name, else null.
|
|
205
|
+
private firstSymlinkComponent(resolvedPath: string): string | null {
|
|
206
|
+
const candidates = [this.canonicalRoot, this.workspaceRoot];
|
|
207
|
+
let rel: string | null = null;
|
|
208
|
+
for (const base of candidates) {
|
|
209
|
+
const candidateRel = path.relative(base, resolvedPath);
|
|
210
|
+
if (!candidateRel.startsWith("..") && !path.isAbsolute(candidateRel)) {
|
|
211
|
+
// The root itself has no components below it to inspect.
|
|
212
|
+
if (candidateRel.length === 0) return null;
|
|
213
|
+
rel = candidateRel;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// Realpath proved containment, so a lexical path outside both spellings
|
|
218
|
+
// of the root can only be an alias reached through a symlink; fail
|
|
219
|
+
// closed ("" sentinel) rather than walk an attacker-chosen chain.
|
|
220
|
+
if (rel === null) return "";
|
|
221
|
+
|
|
222
|
+
let walked = this.canonicalRoot;
|
|
223
|
+
for (const segment of rel.split(path.sep)) {
|
|
224
|
+
walked = path.join(walked, segment);
|
|
225
|
+
let stats: Stats;
|
|
226
|
+
try {
|
|
227
|
+
stats = lstatSync(walked);
|
|
228
|
+
} catch {
|
|
229
|
+
// Not-yet-existing components cannot be symlinks; the subsequent
|
|
230
|
+
// explicit lstat of the final path still gates the read itself.
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
if (stats.isSymbolicLink()) return segment;
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Shared read-path gate: containment (via authorizePath), then per-component
|
|
239
|
+
// symlink rejection (§6.2 item 1), then the sensitive deny list (§6.2 item 3,
|
|
240
|
+
// distinct reason; denied reads never return bytes).
|
|
241
|
+
private authorizeRead(
|
|
242
|
+
lexicalResolvedPath: string,
|
|
243
|
+
canonicalPath: string,
|
|
244
|
+
): WorkspaceAuthorizationDenied | null {
|
|
245
|
+
const symlinkComponent = this.firstSymlinkComponent(lexicalResolvedPath);
|
|
246
|
+
if (symlinkComponent !== null) {
|
|
247
|
+
return {
|
|
248
|
+
approved: false,
|
|
249
|
+
code: "path_not_approved",
|
|
250
|
+
reason:
|
|
251
|
+
symlinkComponent.length > 0
|
|
252
|
+
? `Symlink traversal rejected at component '${symlinkComponent}' of ${lexicalResolvedPath}`
|
|
253
|
+
: `Read path does not resolve lexically below the workspace root: ${lexicalResolvedPath}`,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// The sensitive deny list keys on the root-relative shape of the
|
|
258
|
+
// canonical location so an alias spelling cannot dodge it.
|
|
259
|
+
let relToWorkspace = path.relative(this.workspaceRoot, canonicalPath);
|
|
260
|
+
if (relToWorkspace.startsWith("..") || path.isAbsolute(relToWorkspace)) {
|
|
261
|
+
relToWorkspace = path.basename(canonicalPath);
|
|
262
|
+
}
|
|
263
|
+
if (this.checkSensitiveRelative(relToWorkspace)) {
|
|
264
|
+
return {
|
|
265
|
+
approved: false,
|
|
266
|
+
code: "path_not_approved",
|
|
267
|
+
reason: `Sensitive path denied: ${relToWorkspace.split(path.sep).join("/")}`,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
readThrough(
|
|
275
|
+
targetPath: string,
|
|
276
|
+
options: WorkspaceReadThroughOptions,
|
|
277
|
+
): WorkspaceReadThroughResult {
|
|
278
|
+
const auth = this.authorizePath(targetPath);
|
|
279
|
+
if (!auth.approved) return auth;
|
|
280
|
+
|
|
281
|
+
// The component walk must follow the caller's lexical spelling: realpath
|
|
282
|
+
// has already dissolved any symlinks out of auth.canonicalPath.
|
|
283
|
+
const lexicalResolved = path.isAbsolute(targetPath)
|
|
284
|
+
? path.resolve(targetPath)
|
|
285
|
+
: path.resolve(this.workspaceRoot, targetPath);
|
|
286
|
+
const readDenial = this.authorizeRead(lexicalResolved, auth.canonicalPath);
|
|
287
|
+
if (readDenial !== null) return readDenial;
|
|
288
|
+
|
|
289
|
+
const lstatFn = options.lstatFn ?? ((p: string) => lstatSync(p));
|
|
290
|
+
const readFileFn = options.readFileFn ?? ((p: string) => readFileSync(p));
|
|
291
|
+
|
|
292
|
+
try {
|
|
293
|
+
const finalStats = lstatFn(auth.canonicalPath);
|
|
294
|
+
// TOCTOU re-check of the final component after authorization.
|
|
295
|
+
if (finalStats.isSymbolicLink()) {
|
|
296
|
+
return {
|
|
297
|
+
approved: false,
|
|
298
|
+
code: "path_not_approved",
|
|
299
|
+
reason: `Symlink traversal rejected at final component of ${targetPath}`,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
} catch {
|
|
303
|
+
return {
|
|
304
|
+
approved: false,
|
|
305
|
+
code: "path_not_approved",
|
|
306
|
+
reason: `Could not stat approved path: ${targetPath}`,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let bytes: Buffer;
|
|
311
|
+
try {
|
|
312
|
+
bytes = readFileFn(auth.canonicalPath);
|
|
313
|
+
} catch {
|
|
314
|
+
return {
|
|
315
|
+
approved: false,
|
|
316
|
+
code: "path_not_approved",
|
|
317
|
+
reason: `Could not read approved path: ${targetPath}`,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (bytes.byteLength > this.maxFileBytes) {
|
|
322
|
+
return {
|
|
323
|
+
approved: false,
|
|
324
|
+
code: "path_not_approved",
|
|
325
|
+
reason: `File exceeds per-file byte bound (${this.maxFileBytes}): ${targetPath} is ${bytes.byteLength} bytes`,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (this.aggregateBytes + bytes.byteLength > this.maxAggregateBytes) {
|
|
330
|
+
return {
|
|
331
|
+
approved: false,
|
|
332
|
+
code: "path_not_approved",
|
|
333
|
+
reason: `Aggregate read bound exceeded (${this.maxAggregateBytes}) before ${targetPath}`,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
this.aggregateBytes += bytes.byteLength;
|
|
337
|
+
|
|
338
|
+
// §6.3: broker output is untrusted data — nonce-delimited and labelled
|
|
339
|
+
// under an existing closed-vocabulary tag before prompt inclusion.
|
|
340
|
+
return {
|
|
341
|
+
approved: true,
|
|
342
|
+
bytes,
|
|
343
|
+
wrapped: wrapBlock("comment_body", options.nonce, bytes.toString("utf8")),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
resetAggregate(): void {
|
|
348
|
+
this.aggregateBytes = 0;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
authorizePath(candidatePath: string): WorkspaceAuthorizationResult {
|
|
352
|
+
const resolvedPath = path.isAbsolute(candidatePath)
|
|
353
|
+
? path.resolve(candidatePath)
|
|
354
|
+
: path.resolve(this.workspaceRoot, candidatePath);
|
|
355
|
+
|
|
356
|
+
const canonical = resolveCanonicalPath(resolvedPath);
|
|
357
|
+
if (!canonical) {
|
|
358
|
+
return {
|
|
359
|
+
approved: false,
|
|
360
|
+
code: "path_not_approved",
|
|
361
|
+
reason: `Could not resolve canonical path for: ${candidatePath}`,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (!isInsideRoot(canonical, this.canonicalRoot)) {
|
|
366
|
+
return {
|
|
367
|
+
approved: false,
|
|
368
|
+
code: "path_not_approved",
|
|
369
|
+
reason: `Path escapes workspace root: ${candidatePath} -> ${canonical}`,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return {
|
|
374
|
+
approved: true,
|
|
375
|
+
canonicalPath: canonical,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
authorizeGitArgs(args: readonly string[]): WorkspaceAuthorizationResult {
|
|
380
|
+
for (const arg of args) {
|
|
381
|
+
const deniedFlag = DENIED_GIT_FLAGS.find((flag) => arg === flag);
|
|
382
|
+
if (deniedFlag !== undefined || arg.startsWith("--config-env")) {
|
|
383
|
+
return {
|
|
384
|
+
approved: false,
|
|
385
|
+
code: "path_not_approved",
|
|
386
|
+
reason: `denied git flag: ${arg}`,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
let currentDir = this.canonicalRoot;
|
|
392
|
+
|
|
393
|
+
for (let i = 0; i < args.length; i++) {
|
|
394
|
+
if (args[i] === "-C") {
|
|
395
|
+
const target = args[i + 1];
|
|
396
|
+
if (!target) {
|
|
397
|
+
return {
|
|
398
|
+
approved: false,
|
|
399
|
+
code: "path_not_approved",
|
|
400
|
+
reason: "Missing directory argument following -C",
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
i++;
|
|
404
|
+
const nextResolved = path.isAbsolute(target)
|
|
405
|
+
? path.resolve(target)
|
|
406
|
+
: path.resolve(currentDir, target);
|
|
407
|
+
|
|
408
|
+
const canonical = resolveCanonicalPath(nextResolved);
|
|
409
|
+
if (!canonical || !isInsideRoot(canonical, this.canonicalRoot)) {
|
|
410
|
+
return {
|
|
411
|
+
approved: false,
|
|
412
|
+
code: "path_not_approved",
|
|
413
|
+
reason: `git -C directory escapes workspace root: ${target}`,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
currentDir = canonical;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return {
|
|
421
|
+
approved: true,
|
|
422
|
+
canonicalPath: currentDir,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
async execGit(
|
|
427
|
+
args: readonly string[],
|
|
428
|
+
options: {
|
|
429
|
+
cwd?: string;
|
|
430
|
+
spawnFn?: typeof Bun.spawn;
|
|
431
|
+
gitPath?: string;
|
|
432
|
+
} = {},
|
|
433
|
+
): Promise<{ exitCode: number; stdout: string; stderr: string }> {
|
|
434
|
+
const auth = this.authorizeGitArgs(args);
|
|
435
|
+
if (!auth.approved) {
|
|
436
|
+
return {
|
|
437
|
+
exitCode: 1,
|
|
438
|
+
stdout: "",
|
|
439
|
+
stderr: `path_not_approved: ${auth.reason}`,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Ambient-PATH git is a hijack surface: spawn only from pinned absolute
|
|
444
|
+
// locations, and never let inherited GIT_* env repoint the child.
|
|
445
|
+
const gitPath = resolvePinnedGitPath(options.gitPath);
|
|
446
|
+
if (gitPath === null) {
|
|
447
|
+
return { exitCode: 127, stdout: "", stderr: GIT_NOT_FOUND_STDERR };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const spawnFn = options.spawnFn ?? Bun.spawn;
|
|
451
|
+
const proc = spawnFn([gitPath, ...args], {
|
|
452
|
+
cwd: auth.canonicalPath,
|
|
453
|
+
env: buildStrippedGitEnv(),
|
|
454
|
+
stdout: "pipe",
|
|
455
|
+
stderr: "pipe",
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
459
|
+
new Response(proc.stdout).text(),
|
|
460
|
+
new Response(proc.stderr).text(),
|
|
461
|
+
proc.exited,
|
|
462
|
+
]);
|
|
463
|
+
|
|
464
|
+
return { exitCode, stdout, stderr };
|
|
465
|
+
}
|
|
466
|
+
}
|