Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/issue-1593-glob-key-read-redirect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Security
- Reading renamed private SSH keys with a wildcard is now rated red data risk even when a redirect is glued to the pattern or the command sits in backticks.
27 changes: 25 additions & 2 deletions crates/core/src/risk/classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,8 @@ fn glob_reads_private_key(segment: &str) -> bool {
) {
return false;
}
words.any(|word| {
let word = word.trim_matches(|c| c == '"' || c == '\'' || c == ')');
words.flat_map(split_at_redirects).any(|word| {
let word = word.trim_matches(|c| c == '"' || c == '\'' || c == ')' || c == '`');
let last = word.rsplit('/').next().unwrap_or(word);
if !last.contains(GLOB)
|| !["id_", "rsa", "ed25519", "ecdsa", "dsa"]
Expand Down Expand Up @@ -1046,6 +1046,29 @@ fn glob_reads_private_key(segment: &str) -> bool {
})
}

/// Issue #1593: splits a word at every unquoted redirect operator (`<`, `>`)
/// and returns the non-empty parts, so `backup_id_*>/tmp/x` and
/// `<~/.ssh/backup_id_*` each judge the path part on its own.
fn split_at_redirects(word: &str) -> Vec<&str> {
let mut parts = Vec::new();
let mut quote: Option<char> = None;
let mut start = 0;
for (i, c) in word.char_indices() {
match (quote, c) {
(Some(q), _) if c == q => quote = None,
(None, '"' | '\'') => quote = Some(c),
(None, '<' | '>') => {
parts.push(&word[start..i]);
start = i + c.len_utf8();
}
_ => {}
}
}
parts.push(&word[start..]);
parts.retain(|p| !p.is_empty());
parts
}

/// Heuristik der ersten Fassung: Platzhalter in einem Wort, das auf eine
/// Punktdatei, ein Home-Verzeichnis oder einen Secret-Hinweis zeigt.
fn glob_has_secret_hint(word: &str) -> bool {
Expand Down
28 changes: 28 additions & 0 deletions crates/core/src/risk/tests_ssh_key_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,31 @@ fn secret_path_check_escalates_prefixed_key_globs() {
assert!(super::secret_path_read_reason(c).is_some(), "{c}");
}
}

/// Issue #1593: glued redirects and backticks do not hide a key glob.
#[test]
fn glob_reads_with_glued_redirect_or_backticks_are_red() {
for c in [
"cat ~/.ssh/backup_id_*>/tmp/x",
"cat ~/.ssh/id_*>>/tmp/x",
"cat ~/.ssh/id_*</dev/null",
"echo `cat ~/.ssh/backup_id_*`",
"cat <~/.ssh/backup_id_*",
"cat <~/.ssh/id_*",
"cat 0<~/.ssh/backup_id_*",
] {
assert_eq!(classify(c).data_risk, RiskLevel::Red, "{c}");
}
}

#[test]
fn glob_reads_with_glued_redirect_stay_non_red_for_near_misses() {
for c in [
"cat ~/.ssh/*.pub>/tmp/x",
"cat ~/.ssh/known_*>/tmp/x",
"cat ~/.ssh/rapid_*>/tmp/x",
"cat ~/.ssh/bid_*>/tmp/x",
] {
assert_ne!(classify(c).data_risk, RiskLevel::Red, "{c}");
}
}
Loading