perf(main): bound authorized-external-path set with LRU eviction (#7556)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -8,8 +8,11 @@ import type * as RepoWorktrees from '../repo-worktrees'
|
||||
import { listRepoWorktrees } from '../repo-worktrees'
|
||||
import type { FolderWorkspace, GitWorktreeInfo, ProjectGroup, Repo } from '../../shared/types'
|
||||
import {
|
||||
AUTHORIZED_EXTERNAL_PATHS_MAX,
|
||||
authorizeExternalPath,
|
||||
invalidateAuthorizedRootsCache,
|
||||
isDescendantOrEqual,
|
||||
isPathAllowed,
|
||||
rebuildAuthorizedRootsCache,
|
||||
resolveAuthorizedPath,
|
||||
resolveRegisteredWorktreePath,
|
||||
@@ -336,3 +339,42 @@ describe('filesystem-auth path containment', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('filesystem-auth authorized external path bound', () => {
|
||||
// Empty allow-list store, so a path is allowed only if it (or an ancestor) is
|
||||
// in the session-authorized external-path set.
|
||||
const emptyStore = makeStore([])
|
||||
const flood = (n: number): string =>
|
||||
resolve(`/leak-audit-ext/flood-${String(n).padStart(6, '0')}`)
|
||||
|
||||
it('bounds the authorized external path set with LRU eviction', () => {
|
||||
const keep = resolve('/leak-audit-ext/keep')
|
||||
authorizeExternalPath(keep)
|
||||
|
||||
// Flood past the cap with distinct external paths, re-authorizing `keep`
|
||||
// periodically so LRU keeps it hot.
|
||||
const total = AUTHORIZED_EXTERNAL_PATHS_MAX + 200
|
||||
for (let i = 0; i < total; i += 1) {
|
||||
authorizeExternalPath(flood(i))
|
||||
if (i % 250 === 0) {
|
||||
authorizeExternalPath(keep)
|
||||
}
|
||||
}
|
||||
|
||||
// The oldest never-re-touched entries fell out of the bounded set...
|
||||
expect(isPathAllowed(flood(0), emptyStore)).toBe(false)
|
||||
// ...while the periodically re-authorized path and the most recent survive.
|
||||
expect(isPathAllowed(keep, emptyStore)).toBe(true)
|
||||
expect(isPathAllowed(flood(total - 1), emptyStore)).toBe(true)
|
||||
})
|
||||
|
||||
it('re-authorizes an evicted path on next use (self-healing)', () => {
|
||||
const path = resolve('/leak-audit-ext/evicted-then-reused')
|
||||
for (let i = 0; i < AUTHORIZED_EXTERNAL_PATHS_MAX + 50; i += 1) {
|
||||
authorizeExternalPath(flood(100_000 + i))
|
||||
}
|
||||
expect(isPathAllowed(path, emptyStore)).toBe(false)
|
||||
authorizeExternalPath(path)
|
||||
expect(isPathAllowed(path, emptyStore)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,6 +13,14 @@ import type { FolderWorkspace, ProjectGroup, Repo } from '../../shared/types'
|
||||
|
||||
export const PATH_ACCESS_DENIED_MESSAGE =
|
||||
'Access denied: path resolves outside allowed directories. If this blocks a legitimate workflow, please file a GitHub issue.'
|
||||
// Why: authorized external paths accumulate for the whole session (file drops,
|
||||
// terminal link opens, editor/composer/notebook opens). Bound the set with LRU
|
||||
// eviction — mirrors rememberUnwatchableRoot in filesystem-watcher.ts — so a long
|
||||
// session touching many distinct external files cannot grow it (or the O(n) auth
|
||||
// scan in isPathAllowed) without limit. Every caller re-authorizes a path right
|
||||
// before operating on it, so evicting a stale entry is self-healing and does not
|
||||
// weaken the security boundary.
|
||||
export const AUTHORIZED_EXTERNAL_PATHS_MAX = 4096
|
||||
const authorizedExternalPaths = new Set<string>()
|
||||
const registeredWorktreeRoots = new Set<string>()
|
||||
const registeredWorktreeRootsByRepo = new Map<string, Set<string>>()
|
||||
@@ -23,12 +31,26 @@ const AUTHORIZED_ROOTS_REBUILD_CONCURRENCY = 8
|
||||
type FolderScopeStore = Pick<Store, 'getRepos'> &
|
||||
Partial<Pick<Store, 'getProjectGroups' | 'getFolderWorkspaces'>>
|
||||
|
||||
function rememberAuthorizedExternalPath(path: string): void {
|
||||
// Delete-then-add keeps re-authorized (actively used) paths most-recent so
|
||||
// eviction only sheds the oldest never-re-touched entries.
|
||||
authorizedExternalPaths.delete(path)
|
||||
authorizedExternalPaths.add(path)
|
||||
while (authorizedExternalPaths.size > AUTHORIZED_EXTERNAL_PATHS_MAX) {
|
||||
const oldest = authorizedExternalPaths.keys().next().value
|
||||
if (oldest === undefined) {
|
||||
break
|
||||
}
|
||||
authorizedExternalPaths.delete(oldest)
|
||||
}
|
||||
}
|
||||
|
||||
export function authorizeExternalPath(targetPath: string): void {
|
||||
const resolvedTarget = resolve(targetPath)
|
||||
authorizedExternalPaths.add(resolvedTarget)
|
||||
rememberAuthorizedExternalPath(resolvedTarget)
|
||||
try {
|
||||
// Why: macOS canonicalizes /tmp to /private/tmp during read authorization.
|
||||
authorizedExternalPaths.add(realpathSync(resolvedTarget))
|
||||
rememberAuthorizedExternalPath(realpathSync(resolvedTarget))
|
||||
} catch {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user