Harden SSH host checks for plugin marketplaces (#63806)

## Why

Git and SSH can interpret ambiguous remote URLs differently from the URL parser
used by the marketplace allowlist. A host pattern can therefore match a
different host from the one Git would contact.

## What changed

Validate the SSH authority and repository path before matching a
`host_pattern`. Reject ambiguous host syntax and unsafe usernames in
`ssh://` URLs, and check the host and path in scp-style `git@host:path`
remotes.

## Testing

Add regression cases that reject ambiguous remotes during marketplace add
and verify that host patterns still accept supported SSH usernames,
bracketed IPv6 addresses, and scp-style remotes.

GitOrigin-RevId: 0b7c040c0db2f2f7a802373aeb073969f48cbcd2
This commit is contained in:
viyatb-oai
2026-11-25 02:14:25 +00:00
committed by copyberry
parent cb491754f6
commit e5c2440d5e
3 changed files with 77 additions and 7 deletions
+11 -1
View File
@@ -281,13 +281,23 @@ url = "https://github.com/example/allowed.git"
[marketplaces.allowed_sources.internal]
source = "host_pattern"
host_pattern = '^allowed\.test$'
host_pattern = '^(allowed\.test|\[2001:db8::7\])$'
"#,
);
for source in [
"https://github.com/example/blocked.git",
"https://127.0.0.1:18767/r.git",
"https://allowed.test@127.0.0.1:18767/r.git",
"ssh://allowed.test?user@denied.test/r.git",
"ssh://alice@allowed.test@denied.test/r.git",
"ssh://denied.test%2fuser@allowed.test/r.git",
"ssh://git@allowed.test:x@[denied.test]/r.git",
"git@allowed.test:x@[denied.test]:r.git",
"ssh://allowed.test/a%40%5bdenied.test%5D/r.git",
"ssh://git@[denied.test]@allowed.test/r.git",
"ssh://[2001:db8::7]/team/plugins@[denied.test]/r.git",
"ssh://%2Doption@allowed.test/r.git",
"ssh://git%09user@allowed.test/r.git",
r"http://allowed.test\@127.0.0.1:18767/r.git",
r"https://allowed.test\@127.0.0.1:18767/r.git",
r"http://allowed.test\anything@127.0.0.1:18767/r.git",
@@ -599,12 +599,65 @@ fn git_hostname(url: &str) -> Option<String> {
return None;
}
}
if let Ok(url) = Url::parse(url) {
return url.host_str().map(str::to_ascii_lowercase);
// Git looks for the first @[ and a closing ] even past the repository separator.
let has_bracketed_host = |path: &str| {
path.split_once("@[")
.is_some_and(|(_, rest)| rest.contains(']'))
};
let ssh_name_byte = |b: u8| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-');
if let Some(remote) = url.strip_prefix("git@") {
let (hostname, path) = remote.split_once(':')?;
if hostname.is_empty()
|| hostname.starts_with('-')
|| !hostname.bytes().all(ssh_name_byte)
|| has_bracketed_host(path)
{
return None;
}
return Some(hostname.to_ascii_lowercase());
}
let (_, host_and_path) = url.split_once('@')?;
let (hostname, _) = host_and_path.split_once(':')?;
(!hostname.is_empty()).then(|| hostname.to_ascii_lowercase())
let parsed = Url::parse(url).ok()?;
if let Some(remote) = url.strip_prefix("ssh://") {
// Git decodes escapes before splitting at / and treats ? as part of the authority.
// OpenSSH splits at the last @, so earlier literal or encoded @s belong to the username.
let (authority, path) = remote.split_once('/')?;
let (user, host_port) = authority.rsplit_once('@').unwrap_or(("", authority));
for (i, b) in user.bytes().enumerate() {
let b = if b == b'%' {
u8::from_str_radix(user.get(i + 1..i + 3)?, 16).ok()?
} else {
b
};
if (i == 0 && b == b'-')
|| !(ssh_name_byte(b) || matches!(b, b'+' | b'~' | b'\\' | b'@'))
{
return None;
}
}
// Let Url validate ports and normalize bracketed IPv6 addresses.
if host_port.starts_with('-')
|| !host_port
.bytes()
.all(|b| ssh_name_byte(b) || matches!(b, b':' | b'[' | b']'))
{
return None;
}
// Only user@[IPv6] consumes the first @[ before the path. A bare
// [IPv6] host can still be displaced by a bracketed host in the path.
if !authority.contains("@[") {
let decoded = path.contains('%').then(|| {
path.to_ascii_lowercase()
.replace("%40", "@")
.replace("%5b", "[")
.replace("%5d", "]")
});
if has_bracketed_host(decoded.as_deref().unwrap_or(path)) {
return None;
}
}
}
parsed.host_str().map(str::to_ascii_lowercase)
}
#[cfg(test)]
@@ -157,7 +157,7 @@ restrict_to_allowed_sources = true
[marketplaces.allowed_sources.internal]
source = "host_pattern"
host_pattern = '^git\.example\.com$'
host_pattern = '^(git\.example\.com|\[2001:db8::7\])$'
url = "https://github.com/example/ignored.git"
ref = "ignored"
"#,
@@ -172,6 +172,13 @@ ref = "ignored"
r"https://git.example.com/team\@plugins.git",
r"https://git.example.com?repository=team\@plugins.git",
"ssh://git@git.example.com/team/plugins.git",
"ssh://alice%40example.com@git.example.com/team/plugins.git",
"ssh://alice@example.com@git.example.com/team/plugins.git",
"ssh://team%2Bci@git.example.com:02222/team/my%20plugins.git",
"ssh://DOMAIN%5Cgit@git.example.com/team/plugins.git",
"ssh://git@[2001:db8:0:0:0:0:0:7]/team/plugins@[stable].git",
"ssh://git@git.example.com/team/plugins%40%5Bnext.git",
"git@git.example.com:team/plugins@[next.git",
"git@git.example.com:team/plugins.git",
] {
assert_eq!(