{"record":{"id":"5ce29c8a96d86a60","repo":"Hmbown/CodeWhale","slug":"patch-path-must-be-workspace-relative","errorCode":null,"errorMessage":"patch path must be workspace-relative","messagePattern":"patch path must be workspace-relative","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/eval.rs","lineNumber":690,"sourceCode":"    content.push_str(line);\n    content.push('\\n');\n    fs::write(path, content).with_context(|| format!(\"failed to write {}\", path.display()))\n}\n\nfn apply_patch(root: &Path, patch: &str) -> Result<()> {\n    let mut lines = patch.lines();\n\n    let begin = lines.next().unwrap_or_default();\n    if begin != \"*** Begin Patch\" {\n        return Err(anyhow!(\"patch missing *** Begin Patch header\"));\n    }\n\n    let header = lines.next().unwrap_or_default();\n    let file_rel = header\n        .strip_prefix(\"*** Update File: \")\n        .ok_or_else(|| anyhow!(\"only *** Update File patches are supported\"))?;\n    if file_rel.contains(\"..\") {\n        return Err(anyhow!(\"patch path must be workspace-relative\"));\n    }\n\n    let file_path = root.join(file_rel);\n    let original = read_workspace_file(&file_path)?;\n    let had_trailing_newline = original.ends_with('\\n');\n    let mut file_lines: Vec<String> = original.lines().map(|l| l.to_string()).collect();\n\n    let mut cursor = 0usize;\n    for raw_line in lines {\n        if raw_line == \"*** End Patch\" {\n            break;\n        }\n        if raw_line.starts_with(\"*** \") {\n            return Err(anyhow!(\"unexpected patch directive: {raw_line}\"));\n        }\n        if raw_line.starts_with(\"@@\") {\n            continue;\n        }","sourceCodeStart":672,"sourceCodeEnd":708,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/eval.rs#L672-L708","documentation":"As a path-safety guard, apply_patch rejects any `*** Update File:` target containing `..`, preventing patches from escaping the workspace root. Any file path segment with `..` triggers this error before the file is read or written.","triggerScenarios":"Patch header like `*** Update File: ../../etc/passwd` or any path containing a `..` component, including innocent ones like `foo..bar`.","commonSituations":"Model-generated patch referencing a path outside the workspace; adversarial prompt output attempting path traversal; legitimate filenames containing `..` substrings are also rejected (false positive).","solutions":["Use a path relative to the workspace root without any `..` components.","Move the target file inside the workspace and patch it there.","If a real filename contains `..`, rename the file to avoid the false positive."],"exampleFix":"// before\n*** Update File: ../shared/lib.rs\n// after\n*** Update File: shared/lib.rs","handlingStrategy":"validation","validationCode":"let header = patch.lines().nth(1).unwrap_or_default();\nif let Some(rel) = header.strip_prefix(\"*** Update File: \") {\n    if rel.contains(\"..\") {\n        eprintln!(\"patch path escapes workspace: {rel}\");\n        return;\n    }\n}","typeGuard":"fn is_workspace_relative(rel: &str) -> bool {\n    !rel.contains(\"..\") && !rel.starts_with('/')\n}","tryCatchPattern":"match apply_patch(&root, patch) {\n    Err(e) if e.to_string().contains(\"workspace-relative\") => {\n        eprintln!(\"reject patch: target path must stay inside the workspace\");\n    }\n    other => other?,\n}","preventionTips":["Treat `..` in patch paths as a hard reject; never normalize it away.","Sanitize model-generated file paths against the workspace root.","Avoid filenames containing `..` substrings in workspaces."],"tags":["security","path-traversal","patch"],"backgroundTag":"path-traversal-blocked","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}