gitbutlerapp/gitbutler · error

Path to read from at

Error message

Path to read from at '{}' isn't in the worktree directory '{}'

What it means

read_worktree_file canonicalizes (realpath) the joined workdir+path and then verifies the result is still inside the worktree directory. If the path escapes the worktree — because it was absolute, or traversed symlinks — it bails with this message rather than reading arbitrary files.

Solutions

  1. Provide a path that resolves inside the worktree; remove ../ segments and out-of-repo symlinks.
  2. Pass a repo-relative path instead of an absolute one.
  3. If symlinks are intentional, relocate the target inside the worktree or read the file through another approved API.

Example fix

// before
read_file_from_workspace(project, "../secrets.txt")
// after
read_file_from_workspace(project, "config/settings.json")
Defensive patterns

Strategy: validation

Validate before calling

let canonical = gix::path::realpath(workdir.join(path))?;
if !canonical.starts_with(&canonical_workdir) {
    return Err("path escapes worktree");
}

Type guard

fn inside_worktree(p: &Path, workdir: &Path) -> bool {
    gix::path::realpath(workdir.join(p)).map(|c| c.starts_with(workdir)).unwrap_or(false)
}

Try / catch

match result {
    Err(e) if e.to_string().contains("isn't in the worktree directory") => {
        // reject the request or sanitize the path
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling read_worktree_file (via get_workspace_file_from_source or read_file_from_workspace) with an absolute path outside the project dir, or a relative path containing ../ segments or symlinked directories that resolve outside the worktree.

Common situations: Symlinked source directories (e.g. node_modules or shared folders) pointing outside the repo; user-supplied absolute paths; path traversal attempts via ../.

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 gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/6d8443b1a3ea0b7b. Report an issue: GitHub.

Appendix: source

Thrown at crates/gitbutler-repo/src/commands.rs:372

        None => FileInfo::deleted(),
    })
}

/// Read `path` from the checkout of `repo`, see [`RepoCommands::read_file_from_workspace()`].
/// `path` can be relative or absolute, and is validated to be inside the worktree.
/// A linked worktree opened from disk reads the same way as the main worktree.
pub fn read_worktree_file(repo: &gix::Repository, path: &Path) -> Result<FileInfo> {
    let workdir = repo
        .workdir()
        .ok_or_else(|| anyhow!("Cannot currently work in repositories without a worktree"))?;
    let canonical_workdir = gix::path::realpath(workdir)?;
    let path = gix::path::realpath(canonical_workdir.join(path))?;
    // Double-check that the path is still in the worktree - this might not be the case
    // if it was absolute to begin with, or leads through symlinks.
    let relative_path = match path.strip_prefix(&canonical_workdir) {
        Ok(relative_path) => relative_path.to_owned(),
        Err(_) => {
            bail!(
                "Path to read from at '{}' isn't in the worktree directory '{}'",
                path.display(),
                canonical_workdir.display()
            );
        }
    };

    // Refuse `.git` in every spelling an OS can map onto it (`.GIT`,
    // `GIT~1`, `.git.`). Runs before the stat: `.git` can be a file
    // (linked worktrees) and must be refused all the same.
    if relative_path.components().any(|component| {
        matches!(
            gix::validate::path::component(
                component.as_os_str().as_encoded_bytes().as_bstr(),
                None,
                Default::default(),
            ),
            Err(gix::validate::path::component::Error::DotGitDir)

View on GitHub (pinned to 58e5313667)