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.

Solutions

  1. Set cachePath to an absolute path (e.g. /var/lib/app/cache) in your CacheConfig
  2. Remove any '..' segments from the configured path
  3. If a relative path is desired, resolve it to an absolute path before passing it (path.resolve(process.cwd(), rel))
  4. 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

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


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/94f67c81ca60db37. Report an issue: GitHub.

Appendix: 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)