hcengineering/platform · error
Invalid cache path
Error message
Invalid cache path
What it means
createCache validates the configured cachePath before constructing a DiskCache. If the resolved path is not absolute or contains '..' (path traversal), it throws 'Invalid cache path'. This guards against writing cache files to unintended directories.
Source
Thrown at pods/preview/src/cache.ts:216
try {
const chunks: Buffer[] = []
for await (const chunk of data) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
}
return Buffer.concat(chunks)
} finally {
data.destroy()
}
}
export function createCache (ctx: MeasureContext, options: CacheConfig): Cache {
if (options.enabled && options.cachePath !== undefined) {
try {
const cachePath = resolve(normalize(options.cachePath))
if (cachePath.includes('..') || !isAbsolute(cachePath)) {
throw new Error('Invalid cache path')
}
ctx.info('using disk cache', { cachePath })
return new DiskCache(ctx, { ...options, cachePath })
} catch (err: any) {
ctx.error('Failed to create cache', { path: options.cachePath, error: err })
}
}
ctx.info('using no cache')
return new NoopCache()
}
export async function withCache (
ctx: MeasureContext,
cache: Cache,
key: string,
fn: () => Promise<PreviewFile>View on GitHub (pinned to 63e28dc964)
Solutions
- Set cachePath to an absolute path (e.g. /var/lib/app/cache) in your CacheConfig
- Remove any '..' segments from the configured path
- If a relative path is desired, resolve it to an absolute path before passing it (path.resolve(process.cwd(), rel))
- Or set enabled: false to skip the disk cache entirely
Example fix
// before
createCache(ctx, { enabled: true, cachePath: './data/cache' })
// after
createCache(ctx, { enabled: true, cachePath: '/var/lib/myapp/cache' }) Defensive patterns
Strategy: validation
Validate before calling
import { resolve, normalize, isAbsolute } from 'path'
function assertValidCachePath (p?: string): void {
if (p === undefined) return
const abs = resolve(normalize(p))
if (abs.includes('..') || !isAbsolute(abs)) {
throw new Error(`cachePath must be absolute without '..': ${p}`)
}
}
assertValidCachePath(options.cachePath) Type guard
function isValidCachePath (p: unknown): p is string {
return typeof p === 'string' && p.length > 0 && isAbsolute(resolve(normalize(p))) && !resolve(normalize(p)).includes('..')
} Try / catch
try {
const cache = createCache(ctx, options)
} catch (err) {
ctx.error('cache init failed, continuing without disk cache', { path: options.cachePath, error: err })
// fall back to in-memory/no cache
} Prevention
- Always configure absolute paths via path.resolve(__dirname, ...) or a known root
- Never build cache paths from untrusted user input
- Add a startup config validation step that checks cachePath before createCache
- Keep '..' out of path templates; join segments instead of concatenating strings
When it happens
Trigger: createCache(ctx, { enabled: true, cachePath: <path> }) where the normalized absolute path either is relative after resolution or contains a '..' segment.
Common situations: Config file or env var holding a relative path like './cache' or 'var/cache'; misconfigured defaults on Windows where the path lacks a drive root; path interpolation injecting '..' segments.
Related errors
- No screen access granted
- Unable to find ${RUSH_JSON_FILENAME}.
- Accounts url not specified
- Workspace ${options.workspace} not found
- Failed to fetch config
AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29).
Data as JSON: /api/errors/94f67c81ca60db37.
Report an issue: GitHub.