{"record":{"id":"f66da0dd03240cf7","repo":"santifer/career-ops","slug":"refusing-to-write-the-cover-letter-outside-output","errorCode":null,"errorMessage":"Refusing to write the cover letter outside output/: (empty path)","messagePattern":"Refusing to write the cover letter outside output/: \\(empty path\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"generate-cover-letter.mjs","lineNumber":41,"sourceCode":"import { isMainModule } from \"./lib/is-main-module.mjs\";\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst OUTPUT_ROOT = resolve(__dirname, \"output\");\n\n/**\n * Resolve a requested cover-letter output path.\n *\n * Paths that stay inside `output/` keep their relative subdirectory (the\n * application-bundle layout `generate-pdf.mjs` already supports). Paths that\n * would escape `output/` — `..` traversal or an absolute path outside it —\n * are rejected instead of being silently flattened to `output/<basename>`.\n *\n * @param {string} raw - Caller-supplied --out / payload.output_path value.\n * @returns {string} Absolute path inside OUTPUT_ROOT.\n */\nexport function safeOutputPath(raw) {\n  if (raw == null || String(raw).trim() === \"\") {\n    throw new Error(\"Refusing to write the cover letter outside output/: (empty path)\");\n  }\n  const trimmed = String(raw).trim();\n\n  const asWritten = resolve(trimmed);\n  if (containedInOutput(asWritten)) return asWritten;\n\n  // Absolute paths and any `..` segment already chose a location; if that\n  // location is not inside output/, refuse instead of rewriting to a basename.\n  if (isAbsolute(trimmed) || /(^|[\\\\/])\\.\\.([\\\\/]|$)/.test(trimmed)) {\n    throw new Error(`Refusing to write the cover letter outside output/: ${raw}`);\n  }\n\n  // Bare filename or a relative path that is not already under output/\n  // (e.g. --out cover.pdf, or --out output/foo/bar.pdf from another cwd).\n  const posix = trimmed.replace(/\\\\/g, \"/\").replace(/^\\.\\//, \"\");\n  const relativeToRoot = posix === \"output\" || posix === \"output/\"\n    ? \"\"\n    : posix.startsWith(\"output/\")","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/generate-cover-letter.mjs#L23-L59","documentation":"safeOutputPath in generate-cover-letter.mjs validates the caller-supplied --out / output_path before any file write: an empty, null, or whitespace-only path cannot be proven to live inside OUTPUT_ROOT, so it refuses with a fixed message rather than writing to an arbitrary location. All cover letters must land under output/.","triggerScenarios":"Calling safeOutputPath(null/undefined/'') or `generate-cover-letter.mjs --out \"\"`; a script variable holding the output path that resolved to empty; a JSON payload with output_path missing or set to \"\".","commonSituations":"An automation pipeline where `OUT_FILE=$(...)` produced an empty string; omitting --out while assuming a default exists; hand-built payload JSON where output_path was left blank.","solutions":["Pass a real relative filename, e.g. `--out cover-letter.md` (it will be placed inside output/).","In scripts, guard before invoking: `[ -n \"$OUT\" ] || { echo 'OUT is empty'; exit 1; }`.","In JSON payloads, set output_path to a non-empty relative path like \"output/cover.md\"."],"exampleFix":"// before\nnode generate-cover-letter.mjs --out \"\"\n// after\nnode generate-cover-letter.mjs --out cover-letter-064.md","handlingStrategy":"validation","validationCode":"if (!outPath || !String(outPath).trim()) { throw new Error('--out requires a non-empty path'); }","typeGuard":"const hasOutputPath = (p) => p != null && String(p).trim() !== '';","tryCatchPattern":"try { const p = safeOutputPath(opts.out); } catch (e) { if (/\\(empty path\\)/.test(e.message)) { opts.out = defaultCoverLetterName(); } else throw e; }","preventionTips":["Always pass an explicit --out filename in scripts.","Guard shell variables for emptiness before interpolation.","Default output_path in payload builders to a generated filename instead of blank."],"tags":["path","validation","cli"],"backgroundTag":"empty-required-field","analyzedSha":"aac998c7ed7248ea853b720ceeb1fdbeb322fc5d","analyzedAt":"2026-09-16T06:35:29.214Z","contentChangedAt":"2026-09-16T06:35:29.214Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}