{"record":{"id":"619b5d3776b1bfd5","repo":"gitbutlerapp/gitbutler","slug":"refusing-to-read-relative-path-from-commit","errorCode":null,"errorMessage":"Refusing to read '{relative_path:?}' from commit {commit_id:?} as it's not relative to the worktree","messagePattern":"Refusing to read '(.+?)' from commit (.+?) as it's not relative to the worktree","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-repo/src/commands.rs","lineNumber":342,"sourceCode":"            .filter(|entry| entry.0 > 0)\n            .sorted_by(|a, b| b.0.cmp(&a.0))\n            .take(limit)\n            .map(|(_, path)| path)\n            .collect();\n\n        Ok(scored_files)\n    }\n}\n\n/// Read `relative_path` from the tree of `commit_id` in `repo`, see\n/// [`RepoCommands::read_file_from_commit()`].\nfn read_commit_file(\n    repo: &gix::Repository,\n    commit_id: gix::ObjectId,\n    relative_path: &Path,\n) -> Result<FileInfo> {\n    if !relative_path.is_relative() {\n        bail!(\n            \"Refusing to read '{relative_path:?}' from commit {commit_id:?} as it's not relative to the worktree\"\n        );\n    }\n\n    let tree = repo.find_commit(commit_id)?.tree()?;\n\n    Ok(match tree.lookup_entry_by_path(relative_path)? {\n        Some(entry) => {\n            let blob = repo.find_blob(entry.id())?;\n            FileInfo::from_content(relative_path, &blob.data)\n        }\n        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.","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-repo/src/commands.rs#L324-L360","documentation":"read_commit_file only accepts paths relative to the worktree root. If relative_path is absolute (or otherwise not relative), it refuses to read the file from the commit's tree, blocking path traversal outside the repository.","triggerScenarios":"Calling read_file_from_commit or read_worktree_file with an absolute path (e.g. \"/etc/passwd\", \"C:\\\\repo\\\\file\") or a path with a root/prefix component instead of a repo-relative path like \"src/main.rs\".","commonSituations":"Frontend sends an absolute path taken from an OS file dialog; path built by joining an absolute base; Windows drive-letter paths passed unchanged.","solutions":["Pass a path relative to the worktree root (strip the workdir prefix before calling).","Normalize the input: path.strip_prefix(workdir) or Path::new(p).strip_prefix(base) and use the remainder.","Reject/handle absolute paths at the API boundary with a clear client-side error."],"exampleFix":"// before\nread_file_from_commit(project, commit_id, \"/repo/src/main.rs\")\n// after\nlet rel = Path::new(abs).strip_prefix(workdir)?;\nread_file_from_commit(project, commit_id, rel)","handlingStrategy":"validation","validationCode":"fn ensure_relative(p: &Path) -> Result<&Path, String> {\n    if p.is_relative() && !p.starts_with(\"/\") { Ok(p) } else { Err(format!(\"path must be worktree-relative: {p:?}\")) }\n}","typeGuard":"fn is_worktree_relative(p: &Path) -> bool { p.is_relative() }","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"not relative to the worktree\") => {\n        let rel = Path::new(abs).strip_prefix(workdir)?;\n        retry_with(rel)\n    }\n    other => other,\n}","preventionTips":["Strip the workdir prefix before passing paths to commit-file reads.","Validate paths at the UI/API boundary.","Never pass OS file-dialog absolute paths directly."],"tags":["git","path-validation","security","rust"],"backgroundTag":"invalid-argument-value","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"}