M0: reject invalid prerelease and overflowing versions (#2834)

Reject numeric prerelease identifiers with leading zeros and core components larger than u64 before replacing the workspace version. Extend the existing validator and replacement tests.
This commit is contained in:
Mex authored and GitHub committed 2026-11-15 14:05:55 +00:00
1 parent b2518507b5
commit ec1cc4d37a
1 file changed
+42 -4
+42 -4
View File
@@ -45,11 +45,19 @@ pub fn validate(v: &str) -> Result<(), String> {
None => (v, None),
};
let nums: Vec<&str> = core.split('.').collect();
if nums.len() != 3 || nums.iter().any(|n| n.is_empty() || !n.bytes().all(|b| b.is_ascii_digit()) || (n.len() > 1 && n.starts_with('0'))) {
// Cargo requires every core component to fit in u64 before accepting the manifest.
if nums.len() != 3
|| nums.iter().any(|n| n.parse::<u64>().is_err() || !n.bytes().all(|b| b.is_ascii_digit()) || (n.len() > 1 && n.starts_with('0')))
{
return bad();
}
if let Some(p) = pre
&& (p.is_empty() || p.split('.').any(|id| id.is_empty() || !id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')))
&& (p.is_empty()
|| p.split('.').any(|id| {
id.is_empty()
|| !id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
|| (id.len() > 1 && id.starts_with('0') && id.bytes().all(|b| b.is_ascii_digit()))
}))
{
return bad();
}
@@ -135,11 +143,41 @@ mod tests {
#[test]
fn validates_versions() {
for ok in ["0.1.0", "1.20.300", "1.0.0-rc.1", "1.0.0-alpha", "1.0.0-x-y.2"] {
for ok in [
"0.1.0",
"1.20.300",
"1.0.0-rc.1",
"1.0.0-alpha",
"1.0.0-x-y.2",
"1.0.0-0",
"1.0.0-rc.0",
"1.0.0-01a",
"1.0.0-0-1",
"1.0.0-18446744073709551616",
"18446744073709551615.18446744073709551615.18446744073709551615",
] {
assert!(validate(ok).is_ok(), "{ok}");
assert_eq!(read(&replace(MANIFEST, ok).unwrap()).unwrap(), ok);
}
for bad in ["1.0", "1.0.0.0", "v1.0.0", "01.0.0", "1.0.0-", "1.0.0-a..b", "1.0.0+meta", "1.a.0", ""] {
for bad in [
"1.0",
"1.0.0.0",
"v1.0.0",
"01.0.0",
"1.0.0-",
"1.0.0-a..b",
"1.0.0+meta",
"1.a.0",
"",
"1.2.3-00",
"1.2.3-01",
"1.2.3-rc.01",
"18446744073709551616.0.0",
"0.18446744073709551616.0",
"0.0.18446744073709551616",
] {
assert!(validate(bad).is_err(), "{bad}");
assert!(replace(MANIFEST, bad).is_err(), "{bad}");
}
assert!(replace(MANIFEST, "nope").is_err());
}