{"record":{"id":"f4131caafd3e130e","repo":"JuliusBrussee/caveman","slug":"kilo-db-contains-an-invalid-path-character","errorCode":null,"errorMessage":"KILO_DB contains an invalid path character","messagePattern":"KILO_DB contains an invalid path character","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":12036,"sourceCode":"  try {\n    preferenceFiles.unshift(join(\"/Library/Managed Preferences\", userInfo().username, \"ai.opencode.managed.plist\"));\n  } catch {\n    // System-level preference remains covered; an unreadable username is itself\n    // unusual, but no user path can be resolved safely enough to claim absence.\n    return true;\n  }\n  return preferenceFiles.some((path) => existsSync(path));\n}\n\nfunction kiloDatabasePath(): string | null {\n  const rawDataRoot = process.env.XDG_DATA_HOME || join(homedir(), \".local\", \"share\");\n  const cleanedDataRoot = rawDataRoot.replace(/[\\r\\n]+/g, \"\");\n  if (!cleanedDataRoot) throw new Error(\"Kilo data root resolves to an empty path\");\n  const dataRoot = normalize(isAbsolute(cleanedDataRoot) ? cleanedDataRoot : resolve(cleanedDataRoot));\n  const configured = process.env.KILO_DB;\n  if (configured === \":memory:\") return null;\n  if (configured) {\n    if (/[\\0\\r\\n]/.test(configured)) throw new Error(\"KILO_DB contains an invalid path character\");\n    return normalize(isAbsolute(configured) ? configured : join(dataRoot, \"kilo\", configured));\n  }\n  return join(dataRoot, \"kilo\", \"kilo.db\");\n}\n\nfunction kiloActiveOrganizationState(): \"none\" | \"active\" | \"unknown\" {\n  let path: string | null;\n  try {\n    path = kiloDatabasePath();\n    if (path === null) return \"none\";\n    const stat = statSync(path);\n    if (!stat.isFile()) return \"unknown\";\n  } catch (error) {\n    if ((error as NodeJS.ErrnoException).code === \"ENOENT\") return \"none\";\n    return \"unknown\";\n  }\n  const script = [\n    'const { DatabaseSync } = require(\"node:sqlite\");',","sourceCodeStart":12018,"sourceCodeEnd":12054,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L12018-L12054","documentation":"When the KILO_DB environment variable is set (and not ':memory:'), it is used to build the Kilo database path. The value is rejected if it contains NUL, CR, or LF characters, since these make filesystem paths invalid or exploitable. This prevents path injection through the environment.","triggerScenarios":"Exporting KILO_DB with embedded newlines or a NUL byte, e.g. KILO_DB=$'foo\\nbar' or a value read from a file/secret store that includes a trailing newline or \\0.","commonSituations":"Reading the env var from a Docker secret or CI variable that appends a newline; scripted env injection with unescaped control characters; copy-paste errors into shell profiles.","solutions":["Remove control characters from the KILO_DB value, e.g. KILO_DB=\"$(printf '%s' \"$KILO_DB\" | tr -d '\\r\\n\\0')\"","Set KILO_DB to a plain path like /home/user/.local/share/kilo/kilo.db","Unset KILO_DB to fall back to the default database location"],"exampleFix":"// before\nexport KILO_DB=\"/tmp/kilo$(printf '\\n').db\"\n// after\nexport KILO_DB=\"/tmp/kilo.db\"","handlingStrategy":"validation","validationCode":"const db = process.env.KILO_DB;\nif (db && db !== ':memory:' && /[\\0\\r\\n]/.test(db)) {\n  process.env.KILO_DB = db.replace(/[\\0\\r\\n]/g, '');\n}","typeGuard":"function isValidKiloDbPath(v: string | undefined): v is string {\n  return v === undefined || v === ':memory:' || !/[\\0\\r\\n]/.test(v);\n}","tryCatchPattern":"try {\n  resolveKiloDatabase();\n} catch (e) {\n  if (e instanceof Error && e.message.includes('invalid path character')) {\n    process.env.KILO_DB = process.env.KILO_DB!.replace(/[\\0\\r\\n]/g, '');\n    return resolveKiloDatabase();\n  }\n  throw e;\n}","preventionTips":["Strip trailing newlines when loading KILO_DB from files or secret stores","Avoid echoing KILO_DB from command substitution that appends newlines","Validate secret-injected env vars for control characters at container startup","Keep KILO_DB as a simple absolute path without embedded whitespace"],"tags":["environment","path","kilo","invalid-characters"],"backgroundTag":"invalid-env-var-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}