{"record":{"id":"18e95d95a7f759f8","repo":"santifer/career-ops","slug":"refusing-to-write-the-cover-letter-outside-output-18e95d","errorCode":null,"errorMessage":"Refusing to write the cover letter outside output/: ${raw}","messagePattern":"Refusing to write the cover letter outside output/: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"generate-cover-letter.mjs","lineNumber":51,"sourceCode":" * 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/\")\n      ? posix.slice(\"output/\".length)\n      : posix;\n  const candidate = resolve(OUTPUT_ROOT, relativeToRoot);\n  if (containedInOutput(candidate)) return candidate;\n\n  throw new Error(`Refusing to write the cover letter outside output/: ${raw}`);\n}\n\n/** True when absPath is a file (not output/ itself) still inside OUTPUT_ROOT. */\nfunction containedInOutput(absPath) {","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/generate-cover-letter.mjs#L33-L69","documentation":"safeOutputPath rejects paths that explicitly chose a location outside OUTPUT_ROOT: absolute paths, or relative paths containing a `..` segment, that resolve outside output/. Rather than silently rewriting such a path to a basename, it refuses with the raw value embedded, enforcing that generated cover letters can only be written into the output directory.","triggerScenarios":"`--out /home/user/cover.md` (absolute, outside output/); `--out ../cover.md` or `--out output/../secret.md` (traversal segment resolving outside OUTPUT_ROOT); payloads with output_path set to an absolute temp dir.","commonSituations":"A user tries to write the letter directly to ~/Desktop or a shared drive; a script builds a path with .. to a sibling directory; integration tests passing /tmp paths.","solutions":["Use a bare filename or an output/-relative path: `--out cover.md` or `--out output/cover.md` from the repo root.","Generate inside output/ and copy/move the file yourself afterwards.","If you need a different destination directory, change OUTPUT_ROOT configuration rather than passing paths outside it."],"exampleFix":"// before\nnode generate-cover-letter.mjs --out /home/user/cover.md\n// after\nnode generate-cover-letter.mjs --out cover.md && cp output/cover.md /home/user/","handlingStrategy":"validation","validationCode":"import {isAbsolute} from 'path';\nfunction isSafeRelOut(p) { return typeof p === 'string' && !isAbsolute(p) && !/(^|[\\\\/])\\.\\.([\\\\/]|$)/.test(p); }","typeGuard":"const isInsideOutput = (abs, root) => { const rel = relative(root, abs); return rel !== '' && !rel.startsWith('..') && !isAbsolute(rel); };","tryCatchPattern":"try { const p = safeOutputPath(raw); } catch (e) { if (/Refusing to write the cover letter outside output/.test(e.message)) { console.error('Use a bare filename; it will be placed under output/'); process.exit(2); } throw e; }","preventionTips":["Treat output/ as the only writable destination; copy elsewhere afterwards.","Never construct --out values with .. segments.","In integrations, always pass bare filenames and let the tool resolve inside OUTPUT_ROOT."],"tags":["path","security","validation"],"backgroundTag":"path-traversal-blocked","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"}