Repository navigation
security(analysis): reject path traversal in diff parser (#2099) #2174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1793e49
1db8967
f72c7b6
cadbcb0
c1c3ad6
2508539
e19533a
2c5a74d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| use std::path::PathBuf; | ||
| use std::path::{Component, Path, PathBuf}; | ||
|
|
||
| pub(super) fn parse_new_path_marker(raw: &str) -> Option<PathBuf> { | ||
| let marker = raw.strip_prefix("+++ ")?; | ||
|
|
@@ -7,7 +7,12 @@ pub(super) fn parse_new_path_marker(raw: &str) -> Option<PathBuf> { | |
| return None; | ||
| } | ||
| let path = path.strip_prefix("b/").unwrap_or(&path); | ||
| Some(PathBuf::from(path)) | ||
| let parsed = PathBuf::from(path); | ||
| if is_path_safe(&parsed) { | ||
| Some(parsed) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn parse_old_path_marker(raw: &str) -> bool { | ||
|
|
@@ -28,6 +33,19 @@ fn is_plausible_unquoted_diff_path(path: &str) -> bool { | |
| !path.is_empty() && !path.chars().any(char::is_whitespace) | ||
| } | ||
|
|
||
| /// Reject paths that traverse outside the workspace root or are absolute. | ||
| /// A git diff path like `b/../../../etc/passwd` would otherwise be joined | ||
| /// to the workspace root at the consumer site (`root.join(changed.path)`), | ||
| /// producing a path outside root. This guard stops the traversal at the | ||
| /// parser boundary so every downstream consumer inherits the protection. | ||
| /// See #2099. | ||
| fn is_path_safe(path: &Path) -> bool { | ||
| !path.is_absolute() | ||
| && !path | ||
| .components() | ||
| .any(|c| matches!(c, Component::ParentDir | Component::RootDir)) | ||
|
Comment on lines
+42
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: rustup run 1.95.0 cargo test -p ripr parse_new_path_marker_rejects_windows_drive_relative_pathRepository: EffortlessMetrics/ripr-swarm Length of output: 285 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '== diff/path.rs ==\n'
cat -n crates/ripr/src/analysis/diff/path.rs | sed -n '1,220p'
printf '\n== parse.rs relevant slice ==\n'
cat -n crates/ripr/src/analysis/diff/parse.rs | sed -n '90,150p'Repository: EffortlessMetrics/ripr-swarm Length of output: 8182 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '== search for Prefix handling ==\n'
rg -n "Component::Prefix|is_path_safe|parse_new_path_marker|join\\(" crates/ripr/src -SRepository: EffortlessMetrics/ripr-swarm Length of output: 50385 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '== config.rs slice ==\n'
cat -n crates/ripr/src/config.rs | sed -n '620,650p'
printf '\n== lsp/gap_artifacts.rs slice ==\n'
cat -n crates/ripr/src/lsp/gap_artifacts.rs | sed -n '1200,1235p'
printf '\n== diff parser call sites ==\n'
rg -n "parse_new_path_marker\\(|parse_old_path_marker\\(" crates/ripr/src -SRepository: EffortlessMetrics/ripr-swarm Length of output: 3858 🏁 Script executed: #!/bin/bash
set -euo pipefail
python3 - <<'PY'
from pathlib import Path
import ntpath, posixpath
samples = ["C:outside", "C:/outside", r"C:\outside", r"\\server\share\file", "../secret", "src/lib.rs"]
for s in samples:
print(s)
print(" posix isabs:", posixpath.isabs(s), "normpath:", posixpath.normpath(s))
print(" nt isabs :", ntpath.isabs(s), "normpath:", ntpath.normpath(s))
# simulate the current filter conceptually
parts = [p for p in ntpath.normpath(s).split(ntpath.sep) if p]
print()
PYRepository: EffortlessMetrics/ripr-swarm Length of output: 760 🏁 Script executed: #!/bin/bash
set -euo pipefail
python3 - <<'PY'
from pathlib import PureWindowsPath
samples = ["C:outside", "C:/outside", r"C:\outside", r"\\server\share\file", "../secret", "src/lib.rs"]
for s in samples:
p = PureWindowsPath(s)
print(s)
print(" is_absolute:", p.is_absolute())
print(" drive:", p.drive, "root:", p.root, "anchor:", p.anchor)
print(" parts:", p.parts)
print()
PYRepository: EffortlessMetrics/ripr-swarm Length of output: 749 🌐 Web query:
💡 Result: In Rust, when using the std::path::Path::join method on Windows, a path argument that consists of only a Windows drive prefix (e.g., "C:") is treated as an absolute path that replaces the base path entirely [1][2][3]. This behavior occurs because, under Windows path semantics, a path containing a prefix but no root (such as "C:outside") is technically considered to be an absolute path (or more specifically, a path that overrides the current working directory state of the specified drive) [2][4]. The Rust standard library's documentation for join and PathBuf::push explicitly states: "if path has a prefix but no root, self is ignored and path is returned" [1][5][3]. As a result, calling Citations:
🌐 Web query:
💡 Result: In Rust, the behavior of Path::join on Windows is governed by whether the joined path is considered absolute [1][2]. For Windows, a path containing a prefix but no root (such as "C:foo") is treated as an absolute path by the join operation, causing it to replace the original path entirely rather than appending to it [1][2]. Specifically, the Rust documentation defines the following behavior for Path::join on Windows when joining Citations:
🏁 Script executed (no clone): Length of output: 433 🌐 Web query:
💡 Result: On Windows, the method std::path::Path::is_absolute returns false for a path like C:outside because it lacks a root [1][2][3]. According to the official Rust documentation, a path on Windows is considered absolute only if it meets two criteria: it must have a prefix and it must start with the root [1][2]. While C:outside possesses a disk prefix (C:), it does not have a root component [1][3]. A path on Windows has a root if it satisfies any of the following conditions: - It has no prefix and begins with a separator (e.g., \windows) [1][2][3]. - It has a prefix followed by a separator (e.g., c:\windows) [1][2][3]. - It has any non-disk prefix (e.g., \server\share) [1][2][3]. Because C:outside does not have a separator following the disk prefix, it is considered relative to the current directory of the specified drive, and therefore is_absolute returns false [1][2]. Citations:
Path Traversal (CWE-22): Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') Reachability: External Reject Windows drive-prefix paths. 🤖 Prompt for AI AgentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Failure mode: on Windows, a crafted explicit diff path such as Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| fn parse_diff_path_token(raw: &str) -> Option<String> { | ||
| let raw = raw.trim_end_matches('\r'); | ||
| if let Some(quoted) = raw.strip_prefix('"') { | ||
|
|
@@ -91,3 +109,42 @@ where | |
|
|
||
| char::from_u32(value).unwrap_or('\u{FFFD}') | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::parse_new_path_marker; | ||
| use std::path::PathBuf; | ||
|
|
||
| #[test] | ||
| fn parse_new_path_marker_accepts_normal_relative_path() -> Result<(), String> { | ||
| let path = parse_new_path_marker("+++ b/src/lib.rs") | ||
| .ok_or_else(|| "expected Some for normal relative path".to_string())?; | ||
| assert_eq!(path, PathBuf::from("src/lib.rs")); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_new_path_marker_rejects_parent_dir_traversal() { | ||
| // `+++ b/../../../etc/passwd` must be rejected — the path would | ||
| // escape the workspace root when joined at the consumer site. | ||
| assert_eq!(parse_new_path_marker("+++ b/../../../etc/passwd"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_new_path_marker_rejects_single_parent_dir() { | ||
| assert_eq!(parse_new_path_marker("+++ ../secret"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_new_path_marker_rejects_absolute_path() { | ||
| assert_eq!(parse_new_path_marker("+++ /etc/passwd"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_new_path_marker_accepts_nested_subdirectory() -> Result<(), String> { | ||
| let path = parse_new_path_marker("+++ b/src/nested/deep/file.rs") | ||
| .ok_or_else(|| "expected Some for nested path".to_string())?; | ||
| assert_eq!(path, PathBuf::from("src/nested/deep/file.rs")); | ||
| Ok(()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failure mode: when a plain unified diff has a valid file followed by an unsafe
+++marker, returningNonehere makes the boundary lookahead inparse.rsfail, so the parser stays in the previous hunk and consumes the second file's---,+++, and hunk lines as changes to the prior safe path instead of dropping the unsafe section. Fix direction: distinguish “recognized but unsafe path marker” from “not a path marker”, close/skip that file section, and add a regression next toplain_diff_two_file_sections_while_in_hunk_recognized_as_boundary; validate withcargo test -p ripr --lib analysis::diff. Confidence: High based on the current state machine flow and the newNoneresult for traversal paths.Useful? React with 👍 / 👎.