{"record":{"id":"6d8443b1a3ea0b7b","repo":"gitbutlerapp/gitbutler","slug":"path-to-read-from-at-isn-t-in-the-worktree-di","errorCode":null,"errorMessage":"Path to read from at '{}' isn't in the worktree directory '{}'","messagePattern":"Path to read from at '(.+?)' isn't in the worktree directory '(.+?)'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-repo/src/commands.rs","lineNumber":372,"sourceCode":"        None => FileInfo::deleted(),\n    })\n}\n\n/// Read `path` from the checkout of `repo`, see [`RepoCommands::read_file_from_workspace()`].\n/// `path` can be relative or absolute, and is validated to be inside the worktree.\n/// A linked worktree opened from disk reads the same way as the main worktree.\npub fn read_worktree_file(repo: &gix::Repository, path: &Path) -> Result<FileInfo> {\n    let workdir = repo\n        .workdir()\n        .ok_or_else(|| anyhow!(\"Cannot currently work in repositories without a worktree\"))?;\n    let canonical_workdir = gix::path::realpath(workdir)?;\n    let path = gix::path::realpath(canonical_workdir.join(path))?;\n    // Double-check that the path is still in the worktree - this might not be the case\n    // if it was absolute to begin with, or leads through symlinks.\n    let relative_path = match path.strip_prefix(&canonical_workdir) {\n        Ok(relative_path) => relative_path.to_owned(),\n        Err(_) => {\n            bail!(\n                \"Path to read from at '{}' isn't in the worktree directory '{}'\",\n                path.display(),\n                canonical_workdir.display()\n            );\n        }\n    };\n\n    // Refuse `.git` in every spelling an OS can map onto it (`.GIT`,\n    // `GIT~1`, `.git.`). Runs before the stat: `.git` can be a file\n    // (linked worktrees) and must be refused all the same.\n    if relative_path.components().any(|component| {\n        matches!(\n            gix::validate::path::component(\n                component.as_os_str().as_encoded_bytes().as_bstr(),\n                None,\n                Default::default(),\n            ),\n            Err(gix::validate::path::component::Error::DotGitDir)","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-repo/src/commands.rs#L354-L390","documentation":"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.","triggerScenarios":"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.","commonSituations":"Symlinked source directories (e.g. node_modules or shared folders) pointing outside the repo; user-supplied absolute paths; path traversal attempts via ../.","solutions":["Provide a path that resolves inside the worktree; remove ../ segments and out-of-repo symlinks.","Pass a repo-relative path instead of an absolute one.","If symlinks are intentional, relocate the target inside the worktree or read the file through another approved API."],"exampleFix":"// before\nread_file_from_workspace(project, \"../secrets.txt\")\n// after\nread_file_from_workspace(project, \"config/settings.json\")","handlingStrategy":"validation","validationCode":"let canonical = gix::path::realpath(workdir.join(path))?;\nif !canonical.starts_with(&canonical_workdir) {\n    return Err(\"path escapes worktree\");\n}","typeGuard":"fn inside_worktree(p: &Path, workdir: &Path) -> bool {\n    gix::path::realpath(workdir.join(p)).map(|c| c.starts_with(workdir)).unwrap_or(false)\n}","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"isn't in the worktree directory\") => {\n        // reject the request or sanitize the path\n    }\n    other => other,\n}","preventionTips":["Reject user input containing ../ or absolute paths.","Avoid symlinks that point outside the repository.","Canonicalize and verify containment before any file read."],"tags":["git","path-traversal","security","symlink","rust"],"backgroundTag":"path-traversal-blocked","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}