typeclaw 0.46.0 → 0.46.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/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 +396 -136
- package/src/agent/tools/webfetch/fetch.ts +22 -2
- 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 +83 -43
- package/src/bundled-plugins/backup/runner.ts +83 -0
- 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/researcher/write-report.ts +3 -3
- package/src/bundled-plugins/security/policies/private-surface-read.ts +610 -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/container/docker-app.ts +42 -13
- 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/dockerfile.ts +13 -0
- 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/plugin/types.ts +3 -2
- package/src/sandbox/env-exposure.ts +18 -15
- package/src/sandbox/policy.ts +5 -3
- package/src/sandbox/privileged-runtime.ts +1 -0
- 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,4 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
closeSync,
|
|
3
|
+
constants,
|
|
4
|
+
fstatSync,
|
|
5
|
+
lstatSync,
|
|
6
|
+
openSync,
|
|
7
|
+
opendirSync,
|
|
8
|
+
realpathSync,
|
|
9
|
+
statSync,
|
|
10
|
+
type Dir,
|
|
11
|
+
type Dirent,
|
|
12
|
+
type Stats,
|
|
13
|
+
} from 'node:fs'
|
|
2
14
|
import { homedir } from 'node:os'
|
|
3
15
|
import path from 'node:path'
|
|
4
16
|
import { fileURLToPath } from 'node:url'
|
|
@@ -31,6 +43,7 @@ export const GUARD_PRIVATE_SURFACE_READ = 'privateSurfaceRead'
|
|
|
31
43
|
// keeps both enforcement points agreeing on which tools read no local path.
|
|
32
44
|
const UNSCANNED_TOOLS = new Set(['bash', ...TOOLS_WITHOUT_LOCAL_FILE_OPERANDS])
|
|
33
45
|
const VIRTUAL_FILESYSTEM_ROOTS = ['/proc', '/sys', '/dev', '/run'] as const
|
|
46
|
+
const HARDLINK_SCAN_EXCLUDED_AGENT_DIRS = new Set(['.git', '.gitstore', 'node_modules'])
|
|
34
47
|
|
|
35
48
|
// The bash sandbox hides the role's private surface — the working DIRECTORIES
|
|
36
49
|
// (workspace/, memory/, sessions/), unconditional credential directories, and
|
|
@@ -55,37 +68,25 @@ const VIRTUAL_FILESYSTEM_ROOTS = ['/proc', '/sys', '/dev', '/run'] as const
|
|
|
55
68
|
// shapes like look_at's images[].path and channel_send's attachments[].path —
|
|
56
69
|
// and blocks any string that resolves to (a secret file) or under (a hidden
|
|
57
70
|
// directory) the deny-list.
|
|
58
|
-
export function checkPrivateSurfaceReadGuard(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
71
|
+
export function checkPrivateSurfaceReadGuard(
|
|
72
|
+
options: {
|
|
73
|
+
tool: string
|
|
74
|
+
args: Record<string, unknown>
|
|
75
|
+
agentDir: string
|
|
76
|
+
hidden: HiddenPaths
|
|
77
|
+
fileOperands?: ToolFileOperands
|
|
78
|
+
},
|
|
79
|
+
hooks: PrivateSurfaceIdentityScanHooks = {},
|
|
80
|
+
): SecurityBlock | undefined {
|
|
65
81
|
const { tool, args, agentDir, hidden, fileOperands } = options
|
|
66
82
|
if (UNSCANNED_TOOLS.has(tool)) return undefined
|
|
67
|
-
const deniedDirs =
|
|
68
|
-
...new Set([
|
|
69
|
-
...hidden.dirs,
|
|
70
|
-
...CANONICAL_AGENT_SECRET_DIRS.map((dir) => path.join(agentDir, dir)),
|
|
71
|
-
...CANONICAL_HOME_SECRET_DIRS.map((dir) => path.join(homedir(), dir)),
|
|
72
|
-
]),
|
|
73
|
-
]
|
|
74
|
-
// Keep these runtime-owned credential stores independent of role-derived
|
|
75
|
-
// visibility so a privileged role or partially-wired caller cannot turn raw
|
|
76
|
-
// credentials into model context.
|
|
77
|
-
const deniedFiles = [
|
|
78
|
-
...new Set([
|
|
79
|
-
...hidden.files,
|
|
80
|
-
...CANONICAL_AGENT_SECRET_FILES.map((file) => path.join(agentDir, file)),
|
|
81
|
-
...CANONICAL_HOME_SECRET_FILES.map((file) => path.join(homedir(), file)),
|
|
82
|
-
]),
|
|
83
|
-
]
|
|
83
|
+
const { deniedDirs, deniedFiles } = deniedSurface(agentDir, hidden)
|
|
84
84
|
if (deniedDirs.length === 0 && deniedFiles.length === 0) return undefined
|
|
85
|
+
const identityScanner = createHardlinkIdentityScanner(agentDir, deniedDirs, deniedFiles, hooks)
|
|
85
86
|
|
|
86
87
|
const nonFile = fileOperands?.nonFile === undefined ? undefined : new Set(fileOperands.nonFile)
|
|
87
88
|
for (const candidate of collectPathCandidates(args, tool, nonFile)) {
|
|
88
|
-
const hit = matchHidden(candidate, agentDir, deniedDirs, deniedFiles)
|
|
89
|
+
const hit = matchHidden(candidate, agentDir, deniedDirs, deniedFiles, identityScanner)
|
|
89
90
|
if (hit !== undefined) {
|
|
90
91
|
return {
|
|
91
92
|
block: true,
|
|
@@ -99,6 +100,97 @@ export function checkPrivateSurfaceReadGuard(options: {
|
|
|
99
100
|
return undefined
|
|
100
101
|
}
|
|
101
102
|
|
|
103
|
+
type PrivateSurfaceIdentityOptions = {
|
|
104
|
+
tool: string
|
|
105
|
+
agentDir: string
|
|
106
|
+
hidden: HiddenPaths
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export type PrivateSurfaceIdentityScanHooks = {
|
|
110
|
+
maxEntries?: number
|
|
111
|
+
openDirectory?(directory: string): Pick<Dir, 'readSync' | 'closeSync'>
|
|
112
|
+
lstat?(candidate: string): Stats
|
|
113
|
+
afterEntryLstat?(candidate: string, stats: Stats): void
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type PrivateSurfaceIdentityVerifier = {
|
|
117
|
+
check(identity: Pick<Stats, 'dev' | 'ino' | 'nlink'>): SecurityBlock | undefined
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function createPrivateSurfaceReadIdentityVerifier(
|
|
121
|
+
options: PrivateSurfaceIdentityOptions,
|
|
122
|
+
hooks: PrivateSurfaceIdentityScanHooks = {},
|
|
123
|
+
): PrivateSurfaceIdentityVerifier {
|
|
124
|
+
const { deniedDirs, deniedFiles } = deniedSurface(options.agentDir, options.hidden)
|
|
125
|
+
const scanner = createHardlinkIdentityScanner(options.agentDir, deniedDirs, deniedFiles, hooks)
|
|
126
|
+
return {
|
|
127
|
+
check(identity) {
|
|
128
|
+
if (identity.nlink <= 1) return undefined
|
|
129
|
+
const hit = scanner.check(identity)
|
|
130
|
+
return hit === undefined ? undefined : privateSurfaceBlock(options.tool, hit)
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function checkPrivateSurfaceReadIdentityGuard(
|
|
136
|
+
options: PrivateSurfaceIdentityOptions & { identity: Pick<Stats, 'dev' | 'ino' | 'nlink'> },
|
|
137
|
+
hooks: PrivateSurfaceIdentityScanHooks = {},
|
|
138
|
+
): SecurityBlock | undefined {
|
|
139
|
+
return createPrivateSurfaceReadIdentityVerifier(options, hooks).check(options.identity)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function createHardlinkIdentityScanner(
|
|
143
|
+
agentDir: string,
|
|
144
|
+
deniedDirs: readonly string[],
|
|
145
|
+
deniedFiles: readonly string[],
|
|
146
|
+
hooks: PrivateSurfaceIdentityScanHooks,
|
|
147
|
+
): HardlinkIdentityScanner {
|
|
148
|
+
const lstat = hooks.lstat ?? lstatSync
|
|
149
|
+
return new HardlinkIdentityScanner({
|
|
150
|
+
agentDir,
|
|
151
|
+
deniedDirs,
|
|
152
|
+
deniedFiles,
|
|
153
|
+
maxEntries: hooks.maxEntries ?? MAX_HARDLINK_IDENTITY_ENTRIES,
|
|
154
|
+
openDirectory:
|
|
155
|
+
hooks.openDirectory === undefined
|
|
156
|
+
? openDescriptorAnchoredDirectory
|
|
157
|
+
: createPathDirectoryOpener(hooks.openDirectory, lstat),
|
|
158
|
+
lstat,
|
|
159
|
+
afterEntryLstat: hooks.afterEntryLstat,
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function deniedSurface(agentDir: string, hidden: HiddenPaths): { deniedDirs: string[]; deniedFiles: string[] } {
|
|
164
|
+
return {
|
|
165
|
+
deniedDirs: [
|
|
166
|
+
...new Set([
|
|
167
|
+
...hidden.dirs,
|
|
168
|
+
...CANONICAL_AGENT_SECRET_DIRS.map((dir) => path.join(agentDir, dir)),
|
|
169
|
+
...CANONICAL_HOME_SECRET_DIRS.map((dir) => path.join(homedir(), dir)),
|
|
170
|
+
]),
|
|
171
|
+
],
|
|
172
|
+
// Keep runtime-owned credential stores independent of role-derived
|
|
173
|
+
// visibility so a partially-wired caller cannot expose raw credentials.
|
|
174
|
+
deniedFiles: [
|
|
175
|
+
...new Set([
|
|
176
|
+
...hidden.files,
|
|
177
|
+
...CANONICAL_AGENT_SECRET_FILES.map((file) => path.join(agentDir, file)),
|
|
178
|
+
...CANONICAL_HOME_SECRET_FILES.map((file) => path.join(homedir(), file)),
|
|
179
|
+
]),
|
|
180
|
+
],
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function privateSurfaceBlock(tool: string, hit: string): SecurityBlock {
|
|
185
|
+
return {
|
|
186
|
+
block: true,
|
|
187
|
+
reason: [
|
|
188
|
+
`Guard \`${GUARD_PRIVATE_SURFACE_READ}\` blocked ${tool}: input inode aliases ${hit}, which is not available to LLM tools.`,
|
|
189
|
+
'The bash sandbox masks the same path. Privileged roles cannot bypass canonical agent credential files; use host-side redacted diagnostics instead.',
|
|
190
|
+
].join(' '),
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
102
194
|
// Field names whose values are ALWAYS free text (prose/queries/ids), NEVER a
|
|
103
195
|
// filesystem path, for EVERY tool. Scanning them caused false positives: a
|
|
104
196
|
// guest's `channel_reply({ text: "the memory leak" })` or `web_search({ query:
|
|
@@ -242,6 +334,7 @@ function matchHidden(
|
|
|
242
334
|
agentDir: string,
|
|
243
335
|
deniedDirs: string[],
|
|
244
336
|
deniedFiles: string[],
|
|
337
|
+
identityScanner: HardlinkIdentityScanner,
|
|
245
338
|
): string | undefined {
|
|
246
339
|
const lexicalHit = matchLexicallyDenied(candidate, agentDir, deniedDirs, deniedFiles)
|
|
247
340
|
if (lexicalHit !== undefined) return lexicalHit
|
|
@@ -259,9 +352,10 @@ function matchHidden(
|
|
|
259
352
|
// "\"-joined paths a win32 test runner produces.
|
|
260
353
|
if (resolved === realDir || resolved.startsWith(`${realDir}${path.sep}`)) return dir
|
|
261
354
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
355
|
+
const identity = fileIdentity(resolved)
|
|
356
|
+
if (identity !== undefined && identity.nlink > 1) {
|
|
357
|
+
const hit = identityScanner.check(identity)
|
|
358
|
+
if (hit !== undefined) return hit
|
|
265
359
|
}
|
|
266
360
|
return undefined
|
|
267
361
|
}
|
|
@@ -311,39 +405,505 @@ function virtualFilesystemRoot(candidate: string): string | undefined {
|
|
|
311
405
|
return undefined
|
|
312
406
|
}
|
|
313
407
|
|
|
314
|
-
function
|
|
408
|
+
function fileIdentity(candidate: string): Pick<Stats, 'dev' | 'ino' | 'nlink'> | undefined {
|
|
315
409
|
try {
|
|
316
|
-
const stats =
|
|
317
|
-
return stats.isFile()
|
|
410
|
+
const stats = lstatSync(candidate)
|
|
411
|
+
return stats.isFile() ? stats : undefined
|
|
318
412
|
} catch {
|
|
319
|
-
return
|
|
413
|
+
return undefined
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
type HardlinkDirectory = {
|
|
418
|
+
readSync(): Dirent | null
|
|
419
|
+
statSync(): Stats
|
|
420
|
+
lstatEntrySync(name: string): Stats
|
|
421
|
+
openChildSync(name: string): HardlinkDirectory
|
|
422
|
+
finishSync(): void
|
|
423
|
+
closeSync(): void
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
type OpenedHardlinkDirectory = { reader: HardlinkDirectory; census: Stats }
|
|
427
|
+
|
|
428
|
+
function createPathDirectoryOpener(
|
|
429
|
+
openDirectory: (directory: string) => Pick<Dir, 'readSync' | 'closeSync'>,
|
|
430
|
+
lstat: (candidate: string) => Stats,
|
|
431
|
+
): (directory: string) => HardlinkDirectory {
|
|
432
|
+
const openPath = (directory: string): HardlinkDirectory => {
|
|
433
|
+
const reader = openDirectory(directory)
|
|
434
|
+
let finished = false
|
|
435
|
+
return {
|
|
436
|
+
readSync: () => reader.readSync(),
|
|
437
|
+
statSync: () => lstat(directory),
|
|
438
|
+
lstatEntrySync: (name) => lstat(path.join(directory, name)),
|
|
439
|
+
openChildSync: (name) => openPath(path.join(directory, name)),
|
|
440
|
+
finishSync() {
|
|
441
|
+
if (finished) return
|
|
442
|
+
finished = true
|
|
443
|
+
reader.closeSync()
|
|
444
|
+
},
|
|
445
|
+
closeSync() {
|
|
446
|
+
if (!finished) reader.closeSync()
|
|
447
|
+
finished = true
|
|
448
|
+
},
|
|
449
|
+
}
|
|
320
450
|
}
|
|
451
|
+
return openPath
|
|
321
452
|
}
|
|
322
453
|
|
|
323
|
-
function
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
454
|
+
function openDescriptorAnchoredDirectory(directory: string): HardlinkDirectory {
|
|
455
|
+
if (process.platform !== 'linux') {
|
|
456
|
+
throw new Error('hardlink identity traversal requires Linux descriptor anchoring')
|
|
457
|
+
}
|
|
458
|
+
const descriptor = openSync(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW)
|
|
459
|
+
const anchor = `/proc/self/fd/${descriptor}`
|
|
460
|
+
let reader: Dir
|
|
461
|
+
try {
|
|
462
|
+
reader = opendirSync(anchor)
|
|
463
|
+
} catch (error) {
|
|
464
|
+
closeSync(descriptor)
|
|
465
|
+
throw error
|
|
466
|
+
}
|
|
467
|
+
let closed = false
|
|
468
|
+
let finished = false
|
|
469
|
+
return {
|
|
470
|
+
readSync: () => reader.readSync(),
|
|
471
|
+
statSync: () => fstatSync(descriptor),
|
|
472
|
+
lstatEntrySync: (name) => lstatSync(path.join(anchor, name)),
|
|
473
|
+
openChildSync: (name) => openDescriptorAnchoredDirectory(path.join(anchor, name)),
|
|
474
|
+
finishSync() {
|
|
475
|
+
if (finished) return
|
|
476
|
+
finished = true
|
|
477
|
+
reader.closeSync()
|
|
478
|
+
},
|
|
479
|
+
closeSync() {
|
|
480
|
+
if (closed) return
|
|
481
|
+
closed = true
|
|
482
|
+
let closeError: unknown
|
|
483
|
+
if (!finished) {
|
|
484
|
+
try {
|
|
485
|
+
reader.closeSync()
|
|
486
|
+
} catch (error) {
|
|
487
|
+
closeError = error
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
try {
|
|
491
|
+
closeSync(descriptor)
|
|
492
|
+
} catch (error) {
|
|
493
|
+
closeError ??= error
|
|
494
|
+
}
|
|
495
|
+
if (closeError !== undefined) throw closeError
|
|
496
|
+
},
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
type HardlinkIdentityScannerOptions = {
|
|
501
|
+
agentDir: string
|
|
502
|
+
deniedDirs: readonly string[]
|
|
503
|
+
deniedFiles: readonly string[]
|
|
504
|
+
maxEntries: number
|
|
505
|
+
openDirectory(directory: string): HardlinkDirectory
|
|
506
|
+
lstat(candidate: string): Stats
|
|
507
|
+
afterEntryLstat?(candidate: string, stats: Stats): void
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
class HardlinkIdentityScanner {
|
|
511
|
+
private remaining: number
|
|
512
|
+
private readonly visibleCounts = new Map<string, number>()
|
|
513
|
+
private readonly deniedIdentities = new Map<string, string>()
|
|
514
|
+
private readonly observedEntries = new Set<string>()
|
|
515
|
+
private readonly openedDirectories = new Set<string>()
|
|
516
|
+
private visibleScanned = false
|
|
517
|
+
private deniedScanned = false
|
|
518
|
+
private visibleFailure: string | undefined
|
|
519
|
+
private deniedFailure: string | undefined
|
|
520
|
+
private deniedFilesScanned = false
|
|
521
|
+
|
|
522
|
+
constructor(private readonly options: HardlinkIdentityScannerOptions) {
|
|
523
|
+
this.remaining = options.maxEntries
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
check(identity: Pick<Stats, 'dev' | 'ino' | 'nlink'>): string | undefined {
|
|
527
|
+
const key = identityKey(identity)
|
|
528
|
+
this.scanDeniedFiles()
|
|
529
|
+
if (this.deniedFailure !== undefined) return this.deniedFailure
|
|
530
|
+
const deniedFile = this.deniedIdentities.get(key)
|
|
531
|
+
if (deniedFile !== undefined) return deniedFile
|
|
532
|
+
|
|
533
|
+
this.scanDeniedTrees()
|
|
534
|
+
const deniedAlias = this.deniedIdentities.get(key)
|
|
535
|
+
if (deniedAlias !== undefined) return deniedAlias
|
|
536
|
+
if (this.deniedFailure !== undefined) return this.deniedFailure
|
|
537
|
+
|
|
538
|
+
this.scanVisibleTree()
|
|
539
|
+
if (this.visibleFailure !== undefined) return this.visibleFailure
|
|
540
|
+
if ((this.visibleCounts.get(key) ?? 0) >= identity.nlink) return undefined
|
|
541
|
+
return 'hardlink identities could not be fully accounted within the agent tree'
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
private scanDeniedFiles(): void {
|
|
545
|
+
if (this.deniedFilesScanned) return
|
|
546
|
+
this.deniedFilesScanned = true
|
|
547
|
+
for (const file of this.options.deniedFiles) {
|
|
331
548
|
try {
|
|
332
|
-
|
|
549
|
+
const stats = this.options.lstat(file)
|
|
550
|
+
if (stats.isFile()) this.deniedIdentities.set(identityKey(stats), file)
|
|
551
|
+
} catch (error) {
|
|
552
|
+
if (isNotFoundError(error)) continue
|
|
553
|
+
this.deniedFailure = `denied-file hardlink identity could not be read: ${file}`
|
|
554
|
+
return
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
private scanVisibleTree(): void {
|
|
560
|
+
if (this.visibleScanned) return
|
|
561
|
+
this.visibleScanned = true
|
|
562
|
+
const root = realpathRealIntendedPath(this.options.agentDir)
|
|
563
|
+
const deniedRoots = this.options.deniedDirs.map(realpathRealIntendedPath)
|
|
564
|
+
const deniedFiles = new Set(this.options.deniedFiles.map(realpathRealIntendedPath))
|
|
565
|
+
const rootStats = this.readRootStats(root, 'visible')
|
|
566
|
+
if (rootStats === undefined) return
|
|
567
|
+
const rootDirectory = this.openVerifiedDirectory(root, rootStats, 'visible')
|
|
568
|
+
if (rootDirectory === undefined) return
|
|
569
|
+
const pending: Array<{
|
|
570
|
+
directory: string
|
|
571
|
+
reader: HardlinkDirectory
|
|
572
|
+
identity: Pick<Stats, 'dev' | 'ino'>
|
|
573
|
+
census: Stats
|
|
574
|
+
agentRoot: boolean
|
|
575
|
+
}> = [{ directory: root, ...rootDirectory, identity: rootStats, agentRoot: true }]
|
|
576
|
+
const opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }> = [
|
|
577
|
+
{ directory: root, ...rootDirectory },
|
|
578
|
+
]
|
|
579
|
+
let scanCompleted = false
|
|
580
|
+
try {
|
|
581
|
+
while (pending.length > 0) {
|
|
582
|
+
const current = pending.pop()
|
|
583
|
+
if (current === undefined) break
|
|
584
|
+
const completed = this.forEachEntry(current, 'visible', (entry, stats) => {
|
|
585
|
+
const entryPath = path.join(current.directory, entry.name)
|
|
586
|
+
if (current.agentRoot && HARDLINK_SCAN_EXCLUDED_AGENT_DIRS.has(entry.name)) return true
|
|
587
|
+
if (deniedFiles.has(entryPath) || deniedRoots.some((deniedRoot) => isPathAtOrBelow(entryPath, deniedRoot))) {
|
|
588
|
+
return true
|
|
589
|
+
}
|
|
590
|
+
if (stats.isDirectory()) {
|
|
591
|
+
const child = this.openVerifiedChild(current, entry.name, stats, 'visible')
|
|
592
|
+
if (child === undefined) return false
|
|
593
|
+
const openedChild = { directory: entryPath, ...child }
|
|
594
|
+
pending.push({ ...openedChild, identity: stats, agentRoot: false })
|
|
595
|
+
opened.push(openedChild)
|
|
596
|
+
} else if (stats.isFile()) {
|
|
597
|
+
this.recordVisibleFile(stats)
|
|
598
|
+
}
|
|
599
|
+
return true
|
|
600
|
+
})
|
|
601
|
+
if (!completed) return
|
|
602
|
+
}
|
|
603
|
+
scanCompleted = true
|
|
604
|
+
} finally {
|
|
605
|
+
this.verifyAndCloseDirectories(opened, 'visible', scanCompleted)
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
private scanDeniedTrees(): void {
|
|
610
|
+
if (this.deniedScanned) return
|
|
611
|
+
this.deniedScanned = true
|
|
612
|
+
for (const deniedDir of this.options.deniedDirs) {
|
|
613
|
+
let rootStats: Stats
|
|
614
|
+
let root: string
|
|
615
|
+
try {
|
|
616
|
+
root = realpathRealIntendedPath(deniedDir)
|
|
617
|
+
rootStats = this.options.lstat(root)
|
|
618
|
+
} catch (error) {
|
|
619
|
+
if (isNotFoundError(error)) continue
|
|
620
|
+
this.deniedFailure = `denied-directory hardlink identity could not be read: ${deniedDir}`
|
|
621
|
+
return
|
|
622
|
+
}
|
|
623
|
+
if (!rootStats.isDirectory()) {
|
|
624
|
+
this.deniedFailure = `denied-directory hardlink identity root is not a directory: ${deniedDir}`
|
|
625
|
+
return
|
|
626
|
+
}
|
|
627
|
+
const rootDirectory = this.openVerifiedDirectory(root, rootStats, 'denied')
|
|
628
|
+
if (rootDirectory === undefined) return
|
|
629
|
+
const pending: Array<{
|
|
630
|
+
directory: string
|
|
631
|
+
reader: HardlinkDirectory
|
|
632
|
+
identity: Pick<Stats, 'dev' | 'ino'>
|
|
633
|
+
census: Stats
|
|
634
|
+
}> = [{ directory: root, ...rootDirectory, identity: rootStats }]
|
|
635
|
+
const opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }> = [
|
|
636
|
+
{ directory: root, ...rootDirectory },
|
|
637
|
+
]
|
|
638
|
+
let scanCompleted = false
|
|
639
|
+
try {
|
|
640
|
+
while (pending.length > 0) {
|
|
641
|
+
const current = pending.pop()
|
|
642
|
+
if (current === undefined) break
|
|
643
|
+
const completed = this.forEachEntry(current, 'denied', (entry, stats) => {
|
|
644
|
+
const entryPath = path.join(current.directory, entry.name)
|
|
645
|
+
if (stats.isDirectory()) {
|
|
646
|
+
const child = this.openVerifiedChild(current, entry.name, stats, 'denied')
|
|
647
|
+
if (child === undefined) return false
|
|
648
|
+
const openedChild = { directory: entryPath, ...child }
|
|
649
|
+
pending.push({ ...openedChild, identity: stats })
|
|
650
|
+
opened.push(openedChild)
|
|
651
|
+
} else if (stats.isFile()) {
|
|
652
|
+
this.recordDeniedFile(stats, deniedDir)
|
|
653
|
+
}
|
|
654
|
+
return true
|
|
655
|
+
})
|
|
656
|
+
if (!completed) return
|
|
657
|
+
}
|
|
658
|
+
scanCompleted = true
|
|
659
|
+
} finally {
|
|
660
|
+
this.verifyAndCloseDirectories(opened, 'denied', scanCompleted)
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
private forEachEntry(
|
|
666
|
+
current: {
|
|
667
|
+
directory: string
|
|
668
|
+
reader: HardlinkDirectory
|
|
669
|
+
identity: Pick<Stats, 'dev' | 'ino'>
|
|
670
|
+
},
|
|
671
|
+
surface: 'visible' | 'denied',
|
|
672
|
+
visit: (entry: Dirent, stats: Stats) => boolean,
|
|
673
|
+
): boolean {
|
|
674
|
+
let completed = true
|
|
675
|
+
try {
|
|
676
|
+
while (true) {
|
|
677
|
+
const entry = current.reader.readSync()
|
|
678
|
+
if (entry === null) break
|
|
679
|
+
if (!this.consumeEntry(surface)) {
|
|
680
|
+
completed = false
|
|
681
|
+
break
|
|
682
|
+
}
|
|
683
|
+
if (!this.claimEntryObservation(current.identity, entry.name, surface)) {
|
|
684
|
+
completed = false
|
|
685
|
+
break
|
|
686
|
+
}
|
|
687
|
+
let stats: Stats
|
|
688
|
+
try {
|
|
689
|
+
stats = current.reader.lstatEntrySync(entry.name)
|
|
690
|
+
this.options.afterEntryLstat?.(path.join(current.directory, entry.name), stats)
|
|
691
|
+
} catch {
|
|
692
|
+
this.recordEntryFailure(path.join(current.directory, entry.name), surface)
|
|
693
|
+
completed = false
|
|
694
|
+
break
|
|
695
|
+
}
|
|
696
|
+
if (!visit(entry, stats)) {
|
|
697
|
+
completed = false
|
|
698
|
+
break
|
|
699
|
+
}
|
|
700
|
+
try {
|
|
701
|
+
const confirmed = current.reader.lstatEntrySync(entry.name)
|
|
702
|
+
if (!sameDirectoryEntry(stats, confirmed)) {
|
|
703
|
+
this.recordEntryFailure(path.join(current.directory, entry.name), surface)
|
|
704
|
+
completed = false
|
|
705
|
+
break
|
|
706
|
+
}
|
|
707
|
+
} catch {
|
|
708
|
+
this.recordEntryFailure(path.join(current.directory, entry.name), surface)
|
|
709
|
+
completed = false
|
|
710
|
+
break
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
} catch {
|
|
714
|
+
this.recordDirectoryFailure(current.directory, surface)
|
|
715
|
+
completed = false
|
|
716
|
+
} finally {
|
|
717
|
+
try {
|
|
718
|
+
current.reader.finishSync()
|
|
333
719
|
} catch {
|
|
334
|
-
|
|
720
|
+
this.recordDirectoryFailure(current.directory, surface)
|
|
721
|
+
completed = false
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return completed
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
private readRootStats(directory: string, surface: 'visible' | 'denied'): Stats | undefined {
|
|
728
|
+
try {
|
|
729
|
+
const stats = this.options.lstat(directory)
|
|
730
|
+
if (!stats.isDirectory()) {
|
|
731
|
+
this.recordDirectoryFailure(directory, surface)
|
|
732
|
+
return undefined
|
|
733
|
+
}
|
|
734
|
+
return stats
|
|
735
|
+
} catch {
|
|
736
|
+
this.recordDirectoryFailure(directory, surface)
|
|
737
|
+
return undefined
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
private openVerifiedDirectory(
|
|
742
|
+
directory: string,
|
|
743
|
+
expected: Pick<Stats, 'dev' | 'ino'>,
|
|
744
|
+
surface: 'visible' | 'denied',
|
|
745
|
+
): OpenedHardlinkDirectory | undefined {
|
|
746
|
+
let reader: HardlinkDirectory | undefined
|
|
747
|
+
try {
|
|
748
|
+
reader = this.options.openDirectory(directory)
|
|
749
|
+
const opened = reader.statSync()
|
|
750
|
+
if (!opened.isDirectory() || opened.dev !== expected.dev || opened.ino !== expected.ino) {
|
|
751
|
+
this.recordDirectoryFailure(directory, surface)
|
|
752
|
+
reader.closeSync()
|
|
753
|
+
return undefined
|
|
754
|
+
}
|
|
755
|
+
if (!this.claimDirectoryIdentity(opened, directory, surface)) {
|
|
756
|
+
reader.closeSync()
|
|
757
|
+
return undefined
|
|
335
758
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
759
|
+
return { reader, census: opened }
|
|
760
|
+
} catch {
|
|
761
|
+
try {
|
|
762
|
+
reader?.closeSync()
|
|
763
|
+
} catch {}
|
|
764
|
+
this.recordDirectoryFailure(directory, surface)
|
|
765
|
+
return undefined
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
private openVerifiedChild(
|
|
770
|
+
parent: { directory: string; reader: HardlinkDirectory },
|
|
771
|
+
name: string,
|
|
772
|
+
expected: Pick<Stats, 'dev' | 'ino'>,
|
|
773
|
+
surface: 'visible' | 'denied',
|
|
774
|
+
): OpenedHardlinkDirectory | undefined {
|
|
775
|
+
const directory = path.join(parent.directory, name)
|
|
776
|
+
let reader: HardlinkDirectory | undefined
|
|
777
|
+
try {
|
|
778
|
+
reader = parent.reader.openChildSync(name)
|
|
779
|
+
const opened = reader.statSync()
|
|
780
|
+
if (!opened.isDirectory() || opened.dev !== expected.dev || opened.ino !== expected.ino) {
|
|
781
|
+
this.recordDirectoryFailure(directory, surface)
|
|
782
|
+
reader.closeSync()
|
|
783
|
+
return undefined
|
|
784
|
+
}
|
|
785
|
+
if (!this.claimDirectoryIdentity(opened, directory, surface)) {
|
|
786
|
+
reader.closeSync()
|
|
787
|
+
return undefined
|
|
788
|
+
}
|
|
789
|
+
return { reader, census: opened }
|
|
790
|
+
} catch {
|
|
791
|
+
try {
|
|
792
|
+
reader?.closeSync()
|
|
793
|
+
} catch {}
|
|
794
|
+
this.recordDirectoryFailure(directory, surface)
|
|
795
|
+
return undefined
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
private verifyAndCloseDirectories(
|
|
800
|
+
opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }>,
|
|
801
|
+
surface: 'visible' | 'denied',
|
|
802
|
+
verify: boolean,
|
|
803
|
+
): void {
|
|
804
|
+
for (const current of opened.reverse()) {
|
|
805
|
+
if (verify) {
|
|
806
|
+
try {
|
|
807
|
+
if (!sameDirectoryCensus(current.census, current.reader.statSync())) {
|
|
808
|
+
this.recordDirectoryFailure(current.directory, surface)
|
|
809
|
+
}
|
|
810
|
+
} catch {
|
|
811
|
+
this.recordDirectoryFailure(current.directory, surface)
|
|
342
812
|
}
|
|
343
813
|
}
|
|
814
|
+
try {
|
|
815
|
+
current.reader.closeSync()
|
|
816
|
+
} catch {
|
|
817
|
+
this.recordDirectoryFailure(current.directory, surface)
|
|
818
|
+
}
|
|
344
819
|
}
|
|
345
820
|
}
|
|
346
|
-
|
|
821
|
+
|
|
822
|
+
private recordDirectoryFailure(directory: string, surface: 'visible' | 'denied'): void {
|
|
823
|
+
const reason = `${surface} hardlink identity directory could not be read: ${directory}`
|
|
824
|
+
if (surface === 'visible') this.visibleFailure = reason
|
|
825
|
+
else this.deniedFailure = reason
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
private consumeEntry(surface: 'visible' | 'denied'): boolean {
|
|
829
|
+
if (this.remaining > 0) {
|
|
830
|
+
this.remaining -= 1
|
|
831
|
+
return true
|
|
832
|
+
}
|
|
833
|
+
const reason = `hardlink identity entry limit exceeded (${this.options.maxEntries})`
|
|
834
|
+
if (surface === 'visible') this.visibleFailure = reason
|
|
835
|
+
else this.deniedFailure = reason
|
|
836
|
+
return false
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
private recordEntryFailure(candidate: string, surface: 'visible' | 'denied'): void {
|
|
840
|
+
const reason = `${surface} hardlink identity could not be read: ${candidate}`
|
|
841
|
+
if (surface === 'visible') this.visibleFailure = reason
|
|
842
|
+
else this.deniedFailure = reason
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
private claimDirectoryIdentity(
|
|
846
|
+
identity: Pick<Stats, 'dev' | 'ino'>,
|
|
847
|
+
directory: string,
|
|
848
|
+
surface: 'visible' | 'denied',
|
|
849
|
+
): boolean {
|
|
850
|
+
const key = identityKey(identity)
|
|
851
|
+
if (this.openedDirectories.has(key)) {
|
|
852
|
+
this.recordDirectoryFailure(directory, surface)
|
|
853
|
+
return false
|
|
854
|
+
}
|
|
855
|
+
this.openedDirectories.add(key)
|
|
856
|
+
return true
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
private claimEntryObservation(
|
|
860
|
+
directoryIdentity: Pick<Stats, 'dev' | 'ino'>,
|
|
861
|
+
name: string,
|
|
862
|
+
surface: 'visible' | 'denied',
|
|
863
|
+
): boolean {
|
|
864
|
+
const key = `${identityKey(directoryIdentity)}:${JSON.stringify(name)}`
|
|
865
|
+
if (this.observedEntries.has(key)) {
|
|
866
|
+
this.recordEntryFailure(name, surface)
|
|
867
|
+
return false
|
|
868
|
+
}
|
|
869
|
+
this.observedEntries.add(key)
|
|
870
|
+
return true
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
private recordVisibleFile(stats: Stats): void {
|
|
874
|
+
const key = identityKey(stats)
|
|
875
|
+
this.visibleCounts.set(key, (this.visibleCounts.get(key) ?? 0) + 1)
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
private recordDeniedFile(stats: Stats, deniedRoot: string): void {
|
|
879
|
+
this.deniedIdentities.set(identityKey(stats), deniedRoot)
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
function identityKey(identity: Pick<Stats, 'dev' | 'ino'>): string {
|
|
884
|
+
return `${identity.dev}:${identity.ino}`
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
function sameDirectoryEntry(before: Stats, after: Stats): boolean {
|
|
888
|
+
return (
|
|
889
|
+
before.dev === after.dev && before.ino === after.ino && before.mode === after.mode && before.nlink === after.nlink
|
|
890
|
+
)
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
function sameDirectoryCensus(before: Stats, after: Stats): boolean {
|
|
894
|
+
return (
|
|
895
|
+
before.dev === after.dev &&
|
|
896
|
+
before.ino === after.ino &&
|
|
897
|
+
before.mode === after.mode &&
|
|
898
|
+
before.nlink === after.nlink &&
|
|
899
|
+
before.size === after.size &&
|
|
900
|
+
before.mtimeMs === after.mtimeMs &&
|
|
901
|
+
before.ctimeMs === after.ctimeMs
|
|
902
|
+
)
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
function isPathAtOrBelow(candidate: string, root: string): boolean {
|
|
906
|
+
return candidate === root || candidate.startsWith(`${root}${path.sep}`)
|
|
347
907
|
}
|
|
348
908
|
|
|
349
909
|
function isPathKey(key: string | undefined): boolean {
|