typeclaw 0.46.0 → 0.46.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/README.md +1 -1
- package/package.json +2 -2
- package/src/agent/multimodal/looker.ts +11 -1
- package/src/agent/network/safe-http.ts +17 -8
- package/src/agent/plugin-tools.ts +1 -3
- package/src/agent/tool-file-safety.ts +414 -145
- package/src/agent/tools/webfetch/fetch.ts +22 -2
- package/src/agent/tools/webfetch/strategies/jq.ts +1 -2
- package/src/agent/tools/webfetch/strategies/readability.ts +39 -16
- package/src/agent/tools/webfetch/strategies/selector.ts +4 -2
- package/src/agent/tools/webfetch/strategies/snapshot.ts +3 -2
- package/src/agent/tools/webfetch/tool.ts +3 -3
- package/src/agent/tools-without-local-file-operands.ts +9 -6
- package/src/bundled-plugins/backup/README.md +23 -10
- package/src/bundled-plugins/backup/git-auth.ts +6 -4
- package/src/bundled-plugins/backup/index.ts +104 -47
- package/src/bundled-plugins/backup/runner.ts +102 -5
- package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
- package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
- package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
- package/src/bundled-plugins/github-cli-auth/index.ts +110 -31
- package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
- package/src/bundled-plugins/memory/README.md +9 -0
- package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
- package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
- package/src/bundled-plugins/memory/vector/hybrid.ts +10 -2
- package/src/bundled-plugins/memory/vector/store.ts +21 -9
- package/src/bundled-plugins/researcher/write-report.ts +3 -3
- package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +8 -3
- package/src/bundled-plugins/security/policies/private-surface-read.ts +616 -50
- package/src/bundled-plugins/security/policies/ssrf.ts +119 -34
- package/src/channels/adapters/github/inbound.ts +11 -0
- package/src/channels/adapters/github/index.ts +48 -54
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
- package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
- package/src/channels/adapters/slack-bot-edit.ts +85 -20
- package/src/channels/adapters/slack-bot.ts +7 -2
- package/src/channels/adapters/slack-format.ts +245 -0
- package/src/channels/github-token-bridge.ts +2 -3
- package/src/cli/inspect.ts +16 -0
- package/src/container/docker-app.ts +42 -13
- package/src/container/shared.ts +19 -6
- package/src/container/start.ts +13 -0
- package/src/cron/bridge.ts +9 -2
- package/src/doctor/checks.ts +5 -2
- package/src/git/bound-gc-config.ts +82 -0
- package/src/hostd/spawn.ts +133 -47
- package/src/init/checkpoint.ts +4 -4
- package/src/init/dockerfile.ts +100 -88
- package/src/init/gitignore.ts +5 -6
- package/src/init/run-bun-install.ts +16 -2
- package/src/init/validate-api-key.ts +1 -0
- package/src/inspect/item-list.ts +12 -6
- package/src/inspect/replay.ts +21 -9
- package/src/inspect/session-list.ts +71 -33
- package/src/mcp/tools.ts +1 -0
- package/src/network/internal-destinations.ts +267 -0
- package/src/permissions/permissions.ts +93 -2
- package/src/plugin/types.ts +3 -2
- package/src/role-claim/code.ts +9 -5
- package/src/run/index.ts +45 -36
- package/src/sandbox/canonical-secrets.ts +6 -0
- package/src/sandbox/env-exposure.ts +18 -15
- package/src/sandbox/index.ts +1 -0
- package/src/sandbox/policy.ts +5 -3
- package/src/sandbox/privileged-runtime.ts +1 -0
- package/src/sandbox/symlinks.ts +1 -1
- package/src/secrets/export-claude-credentials-file.ts +13 -22
- package/src/secrets/export-codex-auth-file.ts +12 -28
- package/src/skills/typeclaw-config/SKILL.md +22 -6
- package/src/skills/typeclaw-permissions/SKILL.md +22 -9
- package/src/skills/typeclaw-plugins/SKILL.md +37 -0
- package/src/usage/scan.ts +11 -2
- package/typeclaw.schema.json +5 -0
- package/src/git/secret-history.ts +0 -371
|
@@ -1,371 +0,0 @@
|
|
|
1
|
-
import { isAbsolute, join } from 'node:path'
|
|
2
|
-
|
|
3
|
-
import { CANONICAL_AGENT_SECRET_DIRS, CANONICAL_AGENT_SECRET_FILES } from '@/sandbox/canonical-secrets'
|
|
4
|
-
|
|
5
|
-
import { hooklessGitArgs } from './hookless'
|
|
6
|
-
import { resolveAgentGit } from './resolve-agent-git'
|
|
7
|
-
|
|
8
|
-
type ScanResult = { ok: true } | { ok: false; paths: string[] }
|
|
9
|
-
|
|
10
|
-
const contaminatedCache = new Map<string, string[]>()
|
|
11
|
-
|
|
12
|
-
export class GitSecretHistoryError extends Error {
|
|
13
|
-
constructor(paths: readonly string[]) {
|
|
14
|
-
super(
|
|
15
|
-
[
|
|
16
|
-
`Model-driven Git/bash is disabled because Git metadata is contaminated or can conceal objects: ${paths.join(', ')}.`,
|
|
17
|
-
'Assume credentials in the repository were exposed and rotate them first. Purge canonical secret paths and every refs/replace entry, expire all reflogs, and run Git garbage collection to remove unreachable objects before retrying.',
|
|
18
|
-
'Suggested operator sequence: rewrite/purge the affected history and replacement refs, run `git reflog expire --expire=now --all`, then `git gc --prune=now`, and restart TypeClaw before retrying.',
|
|
19
|
-
'TypeClaw inspects path names and object reachability without reading blob contents. Reachable, reflog-referenced, and unreachable commits are path-attributable via their root tree and block only when a canonical secret path is present; standalone dangling trees and blobs are not path-attributable and are ignored; a live ref, worktree HEAD, or pseudoref (including FETCH_HEAD) pointing at a non-commit still fails closed.',
|
|
20
|
-
].join(' '),
|
|
21
|
-
)
|
|
22
|
-
this.name = 'GitSecretHistoryError'
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function assertNoCanonicalSecretsInGit(agentDir: string): Promise<void> {
|
|
27
|
-
const contaminated = contaminatedCache.get(agentDir)
|
|
28
|
-
if (contaminated !== undefined) throw new GitSecretHistoryError(contaminated)
|
|
29
|
-
|
|
30
|
-
const scan = await scanCanonicalSecretsInGit(agentDir)
|
|
31
|
-
if (!scan.ok) {
|
|
32
|
-
contaminatedCache.set(agentDir, scan.paths)
|
|
33
|
-
throw new GitSecretHistoryError(scan.paths)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export async function scanCanonicalSecretsInGit(agentDir: string): Promise<ScanResult> {
|
|
38
|
-
const layout = resolveAgentGit(agentDir)
|
|
39
|
-
if (layout === null) return { ok: true }
|
|
40
|
-
const gitArgs = layout.gitArgs
|
|
41
|
-
const inside = await runGit(agentDir, gitArgs, ['rev-parse', '--is-inside-work-tree'])
|
|
42
|
-
if (inside.trim() !== 'true') throw new GitSecretHistoryError(['Git metadata did not resolve to a work tree'])
|
|
43
|
-
|
|
44
|
-
const replacements = await runGit(agentDir, gitArgs, ['for-each-ref', '--format=%(refname)', 'refs/replace'])
|
|
45
|
-
if (replacements.trim() !== '') return { ok: false, paths: ['replacement refs exist under refs/replace'] }
|
|
46
|
-
|
|
47
|
-
const nonCommitRoots = await scanNonCommitRefRoots(agentDir, gitArgs)
|
|
48
|
-
if (!nonCommitRoots.ok) return nonCommitRoots
|
|
49
|
-
|
|
50
|
-
const index = await runGit(agentDir, gitArgs, ['ls-files', '--cached', '-z'])
|
|
51
|
-
const reachablePaths = matchingCanonicalPaths(splitNul(index))
|
|
52
|
-
|
|
53
|
-
const objects = await runGit(agentDir, gitArgs, ['rev-list', '--objects', '--all', '--reflog'])
|
|
54
|
-
for (const line of objects.split('\n')) {
|
|
55
|
-
const separator = line.indexOf(' ')
|
|
56
|
-
if (separator >= 0) reachablePaths.push(...matchingCanonicalPaths([decodeGitPath(line.slice(separator + 1))]))
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const unreachableMatches = await scanUnreachableObjects(agentDir, gitArgs)
|
|
60
|
-
if (!unreachableMatches.ok) return unreachableMatches
|
|
61
|
-
|
|
62
|
-
const matches = [...new Set([...reachablePaths, ...unreachableMatches.paths])].sort()
|
|
63
|
-
if (matches.length > 0) return { ok: false, paths: matches }
|
|
64
|
-
|
|
65
|
-
return { ok: true }
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type UnreachableScan = { ok: false; paths: string[] } | { ok: true; paths: string[] }
|
|
69
|
-
|
|
70
|
-
// FETCH_HEAD and MERGE_HEAD are the two root refs `for-each-ref --include-root-refs` deliberately
|
|
71
|
-
// omits (Git classifies them as pseudorefs). They are filesystem-only and multi-line (one oid per
|
|
72
|
-
// line), so they are always read from their files directly rather than enumerated.
|
|
73
|
-
const GIT_MULTILINE_PSEUDOREFS = ['FETCH_HEAD', 'MERGE_HEAD'] as const
|
|
74
|
-
|
|
75
|
-
// Single-oid root-ref fallback for Git < 2.45, which lacks `for-each-ref --include-root-refs`. Not
|
|
76
|
-
// exhaustive by design — any all-caps `*_HEAD`-style root ref (e.g. a custom one) is caught by the
|
|
77
|
-
// flag on modern Git; this list only backstops the ones Git itself commonly writes on old versions.
|
|
78
|
-
const GIT_FALLBACK_ROOT_REFS = [
|
|
79
|
-
'HEAD',
|
|
80
|
-
'ORIG_HEAD',
|
|
81
|
-
'CHERRY_PICK_HEAD',
|
|
82
|
-
'REVERT_HEAD',
|
|
83
|
-
'REBASE_HEAD',
|
|
84
|
-
'BISECT_HEAD',
|
|
85
|
-
'AUTO_MERGE',
|
|
86
|
-
] as const
|
|
87
|
-
|
|
88
|
-
// A ref, worktree HEAD, or root ref/pseudoref pointing at (or a tag peeling to) a non-commit makes
|
|
89
|
-
// that tree/blob live, so neither fsck mode reports it and rev-list cannot root-anchor its path.
|
|
90
|
-
// Such a live root has no repository path prefix, so it fails closed exactly like an orphan tree.
|
|
91
|
-
// Ordinary commit-ish roots are handled by the reachable/reflog path scans.
|
|
92
|
-
async function scanNonCommitRefRoots(agentDir: string, gitArgs: readonly string[]): Promise<UnreachableScan> {
|
|
93
|
-
const listing = await runGit(agentDir, gitArgs, ['worktree', 'list', '--porcelain'])
|
|
94
|
-
const headRefs: string[] = []
|
|
95
|
-
const linkedWorktrees: string[] = []
|
|
96
|
-
for (const line of listing.split('\n')) {
|
|
97
|
-
if (line.startsWith('HEAD ')) headRefs.push(line.slice('HEAD '.length).trim())
|
|
98
|
-
else if (line.startsWith('worktree ')) {
|
|
99
|
-
const path = line.slice('worktree '.length).trim()
|
|
100
|
-
if (path !== '' && path !== agentDir) linkedWorktrees.push(path)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
const refs = await runGit(agentDir, gitArgs, ['for-each-ref', '--format=%(objectname)'])
|
|
104
|
-
const rootRefOids = await collectRootRefOids(agentDir, gitArgs, linkedWorktrees)
|
|
105
|
-
const roots = [...new Set([...headRefs, ...refs.split('\n'), ...rootRefOids].map((oid) => oid.trim()))].filter(
|
|
106
|
-
(oid) => oid !== '' && !/^0+$/.test(oid),
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
for (const root of roots) {
|
|
110
|
-
// A pruning `git gc` deletes the object a reset/rebase left in a pseudoref (ORIG_HEAD, etc.) but
|
|
111
|
-
// never rewrites the ref, so it dangles at an oid that is now absent from the object DB. A gone
|
|
112
|
-
// object cannot conceal a secret, so skip it rather than failing the whole scan closed on the
|
|
113
|
-
// peel error. Only a root whose object still EXISTS and is a non-commit remains blocking.
|
|
114
|
-
if (!(await objectExists(agentDir, gitArgs, root))) continue
|
|
115
|
-
const target = await peelTag(agentDir, gitArgs, root)
|
|
116
|
-
if (target.type !== 'commit') return { ok: false, paths: ['unattributable dangling Git objects'] }
|
|
117
|
-
}
|
|
118
|
-
return { ok: true, paths: [] }
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Collects every oid pinned by a top-level root ref across the main worktree AND every linked one
|
|
122
|
-
// (each linked worktree owns its own root-ref files under `$GIT_COMMON_DIR/worktrees/<id>/`, so a
|
|
123
|
-
// non-commit pinned only there must be probed too). The main worktree keeps the caller's gitArgs so
|
|
124
|
-
// the .gitstore --git-dir/--work-tree layout still resolves (that layout reports no linked
|
|
125
|
-
// worktrees); linked worktrees are discovered from their own path.
|
|
126
|
-
//
|
|
127
|
-
// Enumeration is exhaustive rather than a fixed allowlist: `for-each-ref --include-root-refs` lists
|
|
128
|
-
// ALL root refs Git recognizes — including arbitrary `*_HEAD`-style ones like CUSTOM_HEAD — so no
|
|
129
|
-
// unlisted root ref can slip a non-commit past the guard. On Git < 2.45 (no flag) we fall back to
|
|
130
|
-
// probing the common single-oid root refs by name. FETCH_HEAD/MERGE_HEAD are always read from their
|
|
131
|
-
// files (multi-line; the flag omits them) via `rev-parse --git-path`. Absent refs are skipped.
|
|
132
|
-
async function collectRootRefOids(
|
|
133
|
-
agentDir: string,
|
|
134
|
-
gitArgs: readonly string[],
|
|
135
|
-
linkedWorktrees: readonly string[],
|
|
136
|
-
): Promise<string[]> {
|
|
137
|
-
const probes: { cwd: string; args: readonly string[] }[] = [
|
|
138
|
-
{ cwd: agentDir, args: gitArgs },
|
|
139
|
-
...linkedWorktrees.map((path) => ({ cwd: path, args: [] as readonly string[] })),
|
|
140
|
-
]
|
|
141
|
-
const oids: string[] = []
|
|
142
|
-
for (const probe of probes) {
|
|
143
|
-
const rootRefs = await tryGit(probe.cwd, probe.args, [
|
|
144
|
-
'for-each-ref',
|
|
145
|
-
'--include-root-refs',
|
|
146
|
-
'--format=%(objectname)',
|
|
147
|
-
])
|
|
148
|
-
if (rootRefs.ok) {
|
|
149
|
-
for (const line of rootRefs.stdout.split('\n')) oids.push(line.trim())
|
|
150
|
-
} else {
|
|
151
|
-
for (const name of GIT_FALLBACK_ROOT_REFS) {
|
|
152
|
-
const resolved = await tryGit(probe.cwd, probe.args, ['rev-parse', '--verify', '--quiet', name])
|
|
153
|
-
if (resolved.ok) oids.push(resolved.stdout.trim())
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
for (const name of GIT_MULTILINE_PSEUDOREFS) {
|
|
157
|
-
const path = (await runGit(probe.cwd, probe.args, ['rev-parse', '--git-path', name])).trim()
|
|
158
|
-
if (path === '') continue
|
|
159
|
-
const file = Bun.file(isAbsolute(path) ? path : join(probe.cwd, path))
|
|
160
|
-
if (!(await file.exists())) continue
|
|
161
|
-
for (const line of (await file.text()).split('\n')) {
|
|
162
|
-
const oid = line.split(/[\s\t]/, 1)[0]?.trim()
|
|
163
|
-
if (oid !== undefined && /^[0-9a-f]{40,64}$/.test(oid)) oids.push(oid)
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return oids
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Only an unreachable commit is safely attributable: it records a repository root tree, so we walk
|
|
171
|
-
// it (and tags peeling to a commit) and report the actual canonical secret paths it carries.
|
|
172
|
-
//
|
|
173
|
-
// Standalone unreachable trees/blobs (and tags peeling only to them) are deliberately NOT blocked.
|
|
174
|
-
// A bare blob never stored its filename and an orphan tree lost the parent that carried its prefix,
|
|
175
|
-
// so their names cannot prove they sat under a canonical secret dir. Failing closed on them bricked
|
|
176
|
-
// all model-driven bash on benign debris that TypeClaw's own backup rebase and history rewrites
|
|
177
|
-
// leave behind and never garbage-collect. Accepted residual: a secret surviving ONLY as pathless
|
|
178
|
-
// objects (e.g. `git add -f secrets.json` then `git reset`, which leaves no reflog entry, or a
|
|
179
|
-
// committed secret whose every path-bearing commit has since vanished). While any path-bearing
|
|
180
|
-
// commit survives — reachable, reflog, or unreachable — the caller's `rev-list --all --reflog` scan
|
|
181
|
-
// and this unreachable-commit walk still report the path; `--no-reflogs` keeps this a superset that
|
|
182
|
-
// routes benign reflog-reachable commits through commit attribution instead of double-reporting.
|
|
183
|
-
async function scanUnreachableObjects(agentDir: string, gitArgs: readonly string[]): Promise<UnreachableScan> {
|
|
184
|
-
const fsck = await runGit(agentDir, gitArgs, ['fsck', '--unreachable', '--no-reflogs', '--no-progress'])
|
|
185
|
-
const unreachable = parseUnreachableObjects(fsck)
|
|
186
|
-
if (unreachable.length === 0) return { ok: true, paths: [] }
|
|
187
|
-
|
|
188
|
-
const commits = new Set<string>()
|
|
189
|
-
for (const object of unreachable) {
|
|
190
|
-
if (object.type === 'commit') commits.add(object.oid)
|
|
191
|
-
else if (object.type === 'tag') {
|
|
192
|
-
const target = await peelTag(agentDir, gitArgs, object.oid)
|
|
193
|
-
if (target.type === 'commit') commits.add(target.oid)
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const attributed = new Set<string>()
|
|
198
|
-
const matches: string[] = []
|
|
199
|
-
for (const commit of commits) {
|
|
200
|
-
const rootTree = (await runGit(agentDir, gitArgs, ['rev-parse', `${commit}^{tree}`])).trim()
|
|
201
|
-
const entries = await runGit(agentDir, gitArgs, ['ls-tree', '-r', '-t', '-z', '--full-tree', rootTree])
|
|
202
|
-
attributed.add(rootTree)
|
|
203
|
-
matches.push(...collectTreeEntries(entries, attributed))
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return matches.length > 0 ? { ok: false, paths: [...new Set(matches)].sort() } : { ok: true, paths: [] }
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// `cat-file -e` exits 0 when the object is present and exactly 1 when it is absent. Any other exit
|
|
210
|
-
// code is an operational Git error (corruption, bad invocation), which must fail closed like every
|
|
211
|
-
// other scan step rather than being misread as a benign "object gone".
|
|
212
|
-
async function objectExists(agentDir: string, gitArgs: readonly string[], oid: string): Promise<boolean> {
|
|
213
|
-
const { exitCode, stderr } = await spawnGit(agentDir, gitArgs, ['cat-file', '-e', oid])
|
|
214
|
-
if (exitCode === 0) return true
|
|
215
|
-
if (exitCode === 1) return false
|
|
216
|
-
throw new GitSecretHistoryError([`Git metadata scan failed (cat-file): ${redactGitError(stderr)}`])
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
type PeeledTag = { type: 'commit' | 'tree' | 'blob' | 'unknown'; oid: string }
|
|
220
|
-
|
|
221
|
-
// `<tag>^{}` recursively peels tag-of-tag chains to the final non-tag object. A malformed or cyclic
|
|
222
|
-
// tag makes rev-parse/cat-file exit non-zero, which throws in runGit and fails the whole scan closed.
|
|
223
|
-
async function peelTag(agentDir: string, gitArgs: readonly string[], tag: string): Promise<PeeledTag> {
|
|
224
|
-
const oid = (await runGit(agentDir, gitArgs, ['rev-parse', `${tag}^{}`])).trim()
|
|
225
|
-
const type = (await runGit(agentDir, gitArgs, ['cat-file', '-t', oid])).trim()
|
|
226
|
-
if (type === 'commit' || type === 'tree' || type === 'blob') return { type, oid }
|
|
227
|
-
return { type: 'unknown', oid }
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
type UnreachableObject = { type: 'commit' | 'tree' | 'blob' | 'tag'; oid: string }
|
|
231
|
-
|
|
232
|
-
function parseUnreachableObjects(fsck: string): UnreachableObject[] {
|
|
233
|
-
const objects: UnreachableObject[] = []
|
|
234
|
-
for (const line of fsck.split('\n')) {
|
|
235
|
-
const match = /^unreachable (commit|tree|blob|tag) ([0-9a-f]{40,64})$/.exec(line.trim())
|
|
236
|
-
if (match) objects.push({ type: match[1] as UnreachableObject['type'], oid: match[2] as string })
|
|
237
|
-
}
|
|
238
|
-
return objects
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function collectTreeEntries(nulSeparated: string, attributed: Set<string>): string[] {
|
|
242
|
-
const matches: string[] = []
|
|
243
|
-
for (const entry of splitNul(nulSeparated)) {
|
|
244
|
-
const tab = entry.indexOf('\t')
|
|
245
|
-
if (tab < 0) continue
|
|
246
|
-
const meta = entry.slice(0, tab).split(/\s+/)
|
|
247
|
-
const oid = meta[2]
|
|
248
|
-
const path = decodeGitPath(entry.slice(tab + 1))
|
|
249
|
-
if (oid !== undefined) attributed.add(oid)
|
|
250
|
-
if (matchesKnownRootPath(path)) matches.push(path)
|
|
251
|
-
}
|
|
252
|
-
return matches
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function decodeGitPath(raw: string): string {
|
|
256
|
-
if (!raw.startsWith('"') || !raw.endsWith('"')) return raw
|
|
257
|
-
const bytes: number[] = []
|
|
258
|
-
const encoder = new TextEncoder()
|
|
259
|
-
const escaped = raw.slice(1, -1)
|
|
260
|
-
for (let index = 0; index < escaped.length; index += 1) {
|
|
261
|
-
const character = escaped[index] as string
|
|
262
|
-
if (character !== '\\') {
|
|
263
|
-
bytes.push(...encoder.encode(character))
|
|
264
|
-
continue
|
|
265
|
-
}
|
|
266
|
-
const next = escaped[++index]
|
|
267
|
-
if (next === undefined) return raw
|
|
268
|
-
const simple: Record<string, number> = {
|
|
269
|
-
a: 7,
|
|
270
|
-
b: 8,
|
|
271
|
-
t: 9,
|
|
272
|
-
n: 10,
|
|
273
|
-
v: 11,
|
|
274
|
-
f: 12,
|
|
275
|
-
r: 13,
|
|
276
|
-
'"': 34,
|
|
277
|
-
'\\': 92,
|
|
278
|
-
}
|
|
279
|
-
const simpleByte = simple[next]
|
|
280
|
-
if (simpleByte !== undefined) {
|
|
281
|
-
bytes.push(simpleByte)
|
|
282
|
-
continue
|
|
283
|
-
}
|
|
284
|
-
if (/[0-7]/.test(next)) {
|
|
285
|
-
let octal = next
|
|
286
|
-
while (octal.length < 3 && index + 1 < escaped.length && /[0-7]/.test(escaped[index + 1] as string)) {
|
|
287
|
-
octal += escaped[++index]
|
|
288
|
-
}
|
|
289
|
-
bytes.push(Number.parseInt(octal, 8))
|
|
290
|
-
continue
|
|
291
|
-
}
|
|
292
|
-
bytes.push(...encoder.encode(next))
|
|
293
|
-
}
|
|
294
|
-
return new TextDecoder().decode(Uint8Array.from(bytes))
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export function resetGitSecretHistoryCacheForTests(): void {
|
|
298
|
-
contaminatedCache.clear()
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function matchingCanonicalPaths(paths: readonly string[]): string[] {
|
|
302
|
-
return paths.filter((raw) => matchesKnownRootPath(normalizeGitPath(raw)))
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
function normalizeGitPath(raw: string): string {
|
|
306
|
-
return raw.replaceAll('\\', '/').replace(/^\.\//, '')
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function matchesKnownRootPath(raw: string): boolean {
|
|
310
|
-
const path = normalizeGitPath(raw)
|
|
311
|
-
if (CANONICAL_AGENT_SECRET_FILES.some((secret) => path === secret)) return true
|
|
312
|
-
return CANONICAL_AGENT_SECRET_DIRS.some((dir) => path.startsWith(`${dir}/`))
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function splitNul(value: string): string[] {
|
|
316
|
-
return value.split('\0').filter((entry) => entry !== '')
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
async function spawnGit(
|
|
320
|
-
agentDir: string,
|
|
321
|
-
gitArgs: readonly string[],
|
|
322
|
-
args: readonly string[],
|
|
323
|
-
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
|
|
324
|
-
const proc = Bun.spawn(['git', ...hooklessGitArgs(['-C', agentDir, ...gitArgs, ...args])], {
|
|
325
|
-
stdout: 'pipe',
|
|
326
|
-
stderr: 'pipe',
|
|
327
|
-
env: {
|
|
328
|
-
PATH: process.env.PATH ?? '/usr/local/bin:/usr/bin:/bin',
|
|
329
|
-
HOME: process.env.HOME ?? '/tmp',
|
|
330
|
-
GIT_TERMINAL_PROMPT: '0',
|
|
331
|
-
GIT_PAGER: 'cat',
|
|
332
|
-
GIT_CONFIG_GLOBAL: '/dev/null',
|
|
333
|
-
GIT_CONFIG_SYSTEM: '/dev/null',
|
|
334
|
-
GIT_NO_REPLACE_OBJECTS: '1',
|
|
335
|
-
// Keep the scan strictly local. GIT_NO_LAZY_FETCH stops a promisor/partial-clone fetch on
|
|
336
|
-
// Git >=2.45 (and backported maints), but is silently ignored on older Git; the empty
|
|
337
|
-
// GIT_ALLOW_PROTOCOL whitelist (a since-2.20 control) denies every transport, so even the
|
|
338
|
-
// internal lazy `git fetch` those versions still attempt fails closed before touching a remote.
|
|
339
|
-
GIT_NO_LAZY_FETCH: '1',
|
|
340
|
-
GIT_ALLOW_PROTOCOL: '',
|
|
341
|
-
},
|
|
342
|
-
})
|
|
343
|
-
const [stdout, stderr, exitCode] = await Promise.all([
|
|
344
|
-
new Response(proc.stdout).text(),
|
|
345
|
-
new Response(proc.stderr).text(),
|
|
346
|
-
proc.exited,
|
|
347
|
-
])
|
|
348
|
-
return { stdout, stderr, exitCode }
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async function runGit(agentDir: string, gitArgs: readonly string[], args: readonly string[]): Promise<string> {
|
|
352
|
-
const { stdout, stderr, exitCode } = await spawnGit(agentDir, gitArgs, args)
|
|
353
|
-
if (exitCode === 0) return stdout
|
|
354
|
-
throw new GitSecretHistoryError([`Git metadata scan failed (${args[0] ?? 'unknown'}): ${redactGitError(stderr)}`])
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// Non-throwing variant for probes whose failure is a valid signal (an absent root ref, or an
|
|
358
|
-
// `--include-root-refs` flag unsupported on old Git) rather than a contamination error.
|
|
359
|
-
async function tryGit(
|
|
360
|
-
agentDir: string,
|
|
361
|
-
gitArgs: readonly string[],
|
|
362
|
-
args: readonly string[],
|
|
363
|
-
): Promise<{ ok: true; stdout: string } | { ok: false }> {
|
|
364
|
-
const { stdout, exitCode } = await spawnGit(agentDir, gitArgs, args)
|
|
365
|
-
return exitCode === 0 ? { ok: true, stdout } : { ok: false }
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function redactGitError(stderr: string): string {
|
|
369
|
-
const firstLine = stderr.split(/\r?\n/, 1)[0]?.trim()
|
|
370
|
-
return firstLine === undefined || firstLine === '' ? 'unknown Git error' : firstLine.slice(0, 200)
|
|
371
|
-
}
|