Hmbown/CodeWhale · error · anyhow::Error

patch path must be workspace-relative

Error message

patch path must be workspace-relative

What it means

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.

Solutions

  1. Use a path relative to the workspace root without any `..` components.
  2. Move the target file inside the workspace and patch it there.
  3. If a real filename contains `..`, rename the file to avoid the false positive.

Example fix

// before
*** Update File: ../shared/lib.rs
// after
*** Update File: shared/lib.rs
Defensive patterns

Strategy: validation

Validate before calling

let header = patch.lines().nth(1).unwrap_or_default();
if let Some(rel) = header.strip_prefix("*** Update File: ") {
    if rel.contains("..") {
        eprintln!("patch path escapes workspace: {rel}");
        return;
    }
}

Type guard

fn is_workspace_relative(rel: &str) -> bool {
    !rel.contains("..") && !rel.starts_with('/')
}

Try / catch

match apply_patch(&root, patch) {
    Err(e) if e.to_string().contains("workspace-relative") => {
        eprintln!("reject patch: target path must stay inside the workspace");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Patch header like `*** Update File: ../../etc/passwd` or any path containing a `..` component, including innocent ones like `foo..bar`.

Common situations: Model-generated patch referencing a path outside the workspace; adversarial prompt output attempting path traversal; legitimate filenames containing `..` substrings are also rejected (false positive).

Understand the failure class

Background: Path traversal blocked: "path escapes the workspace" and "outside site root" errors when a path will not stay inside its allowed directory — this error's family across 26 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/5ce29c8a96d86a60. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/eval.rs:690

    content.push_str(line);
    content.push('\n');
    fs::write(path, content).with_context(|| format!("failed to write {}", path.display()))
}

fn apply_patch(root: &Path, patch: &str) -> Result<()> {
    let mut lines = patch.lines();

    let begin = lines.next().unwrap_or_default();
    if begin != "*** Begin Patch" {
        return Err(anyhow!("patch missing *** Begin Patch header"));
    }

    let header = lines.next().unwrap_or_default();
    let file_rel = header
        .strip_prefix("*** Update File: ")
        .ok_or_else(|| anyhow!("only *** Update File patches are supported"))?;
    if file_rel.contains("..") {
        return Err(anyhow!("patch path must be workspace-relative"));
    }

    let file_path = root.join(file_rel);
    let original = read_workspace_file(&file_path)?;
    let had_trailing_newline = original.ends_with('\n');
    let mut file_lines: Vec<String> = original.lines().map(|l| l.to_string()).collect();

    let mut cursor = 0usize;
    for raw_line in lines {
        if raw_line == "*** End Patch" {
            break;
        }
        if raw_line.starts_with("*** ") {
            return Err(anyhow!("unexpected patch directive: {raw_line}"));
        }
        if raw_line.starts_with("@@") {
            continue;
        }

View on GitHub (pinned to 73e0f67d83)