Skip to content

Commit a58cb20

Browse files
katspaughclaude
andcommitted
refactor: extract path validation to shared utility
Extract isTempDirectory() function to ensure consistent path validation across all safety checks. Now consistently checks tmpdir(), /tmp (Unix), and \Temp (Windows) in all locations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6932d3c commit a58cb20

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/tests/helpers/test-storage.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ interface TestStorageContext {
1515
cleanup: () => void
1616
}
1717

18+
/**
19+
* Check if a path is in a temporary directory
20+
* Validates against tmpdir(), /tmp (Unix), and \Temp (Windows)
21+
*/
22+
export function isTempDirectory(path: string): boolean {
23+
const tmp = tmpdir()
24+
return path.includes(tmp) || path.includes('/tmp') || path.includes('\\Temp')
25+
}
26+
1827
/**
1928
* Create isolated storage directories for testing
2029
* ALWAYS call cleanup() in afterEach to remove test data
@@ -24,8 +33,8 @@ export function createTestStorage(prefix: string = 'safe-cli-test'): TestStorage
2433
const dataDir = mkdtempSync(join(tmpdir(), `${prefix}-data-`))
2534

2635
// Verify we're in a temporary directory (safety check)
27-
if (!configDir.includes(tmpdir()) || !dataDir.includes(tmpdir())) {
28-
throw new Error('CRITICAL: Test storage must be in temp directory!')
36+
if (!isTempDirectory(configDir) || !isTempDirectory(dataDir)) {
37+
throw new Error(`CRITICAL: Test storage must be in temp directory! Got: ${configDir}`)
2938
}
3039

3140
const cleanup = () => {
@@ -73,11 +82,11 @@ export function enforceTestEnvironment(): void {
7382
const xdgConfig = process.env.XDG_CONFIG_HOME
7483
const xdgData = process.env.XDG_DATA_HOME
7584

76-
if (xdgConfig && !xdgConfig.includes(tmpdir()) && !xdgConfig.includes('/tmp')) {
85+
if (xdgConfig && !isTempDirectory(xdgConfig)) {
7786
throw new Error(`CRITICAL: XDG_CONFIG_HOME must point to temp directory, got: ${xdgConfig}`)
7887
}
7988

80-
if (xdgData && !xdgData.includes(tmpdir()) && !xdgData.includes('/tmp')) {
89+
if (xdgData && !isTempDirectory(xdgData)) {
8190
throw new Error(`CRITICAL: XDG_DATA_HOME must point to temp directory, got: ${xdgData}`)
8291
}
8392
}
@@ -102,7 +111,7 @@ export function safeCleanupTestData(
102111
}
103112
): void {
104113
// Verify we're working with test directories
105-
if (!context.configDir.includes(tmpdir())) {
114+
if (!isTempDirectory(context.configDir)) {
106115
throw new Error('CRITICAL: Can only cleanup test storage, not production config!')
107116
}
108117

0 commit comments

Comments
 (0)