feat(model): PEP 440 at the boundary where text becomes argv

SECURITY §2 has always said names *and versions* are validated before argv. Names were; versions
were plain strings, so a dock-bay entry of `2.0 ; python_version<'0'` reached argv as one token pip
read as a requirement whose marker is false — a silent no-op install — and requirements import
failed outright on any range, because `build_request` split on `==` alone (`django>=5.0,<6`
became a "package name"; `pkg===1.0` became version `=1.0`).

New `pep440` module, the one place that says what a version and a specifier set may look like: a
hand-written reader of `packaging`'s `VERSION_PATTERN` (epochs, every pre/post/dev spelling, local
labels — nothing legitimate is refused), a specifier-set grammar (`===`, `.*` prefixes, whitespace
only between an operator and its version), and `is_argv_safe` for installed versions that are not
PEP 440, which the guard set now restates with `===` (packaging dropped `LegacyVersion`, so `==`
would be refused).

Wired in at every way text arrives:
- `Spec::parse` for CLI/GUI install specs and imported requirements; `Spec` and `PinnedSpec` now
  deserialize through the same checks, closing the IPC door the way `PkgName` already did.
- The guard set skips an installed version that could be read as more than one token.
- uv's `+ pkg==0.1.0 (from file:///…)` keeps only the version (it reached argv whole).
- pip report changes and freeze lines with such versions are dropped / unrestorable.
- `pins::validated_hold` and `health::validated_version` were the same charset check twice; both
  use `pep440::is_version` now, which also rejects `1.0--break-system-packages`, a string whose
  every character is in a version's alphabet.

`version_req` is normalized to an operator form (`==0.28.1`), which `plan_argv_specs` already
rendered identically. The one golden that moved is `schema-PlanRequest`, for its doc text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5.5 committed 2026-10-17 00:55:27 +07:00
1 parent 3e4f3d0243
commit 6559ebcc84
12 files changed
+656 -54

No files matched your search

@@ -16,7 +16,7 @@ expression: out
"description": "Normalized distribution name."
},
"versionReq": {
"description": "PEP 440 specifier such as `\">=2,<3\"`, or `None` for \"latest\".",
"description": "PEP 440 specifier such as `\">=2,<3\"` (or a bare version, read as `==`), or `None` for\n\"latest\". Validated on every way in — [`Spec::parse`] and deserialization — so it is\nnever prose, a marker or an option by the time it reaches argv.",
"type": [
"string",
"null"
+33
View File
@@ -297,6 +297,11 @@ pub fn plan_requirements(req: &PlanRequest, installed: &[crate::model::Dist]) ->
installed
.iter()
.filter(|d| !moving.contains(&d.name))
// A version that could be read as more than a version — whitespace, a marker, a URL —
// never reaches argv. pip itself cannot install such a distribution, so restating it
// would only fail the resolve; leaving it out leaves it untouched, which is what a guard
// is for.
.filter(|d| crate::pep440::is_argv_safe(&d.version.0))
.map(|d| PinnedSpec {
name: d.name.clone(),
version: d.version.clone(),
@@ -576,4 +581,32 @@ mod tests {
};
assert!(plan_requirements(&req, &[]).is_empty());
}
#[test]
fn the_guard_set_leaves_out_a_version_that_is_not_argv_safe() {
let installed = [
crate::model::Dist {
name: PkgName::parse("good").expect("name"),
version: crate::model::Version("1.0".into()),
requires_dist: Vec::new(),
requires_python: None,
size_bytes: None,
},
crate::model::Dist {
name: PkgName::parse("odd").expect("name"),
version: crate::model::Version("1.0 ; python_version < '0'".into()),
requires_dist: Vec::new(),
requires_python: None,
size_bytes: None,
},
];
let req = PlanRequest {
upgrades: Vec::new(),
installs: Vec::new(),
strategy: crate::plan::Strategy::Compatible,
};
let guards = plan_requirements(&req, &installed);
assert_eq!(guards.len(), 1);
assert_eq!(guards[0].to_requirement(), "good==1.0");
}
}
+29 -7
View File
@@ -106,12 +106,16 @@ pub fn pip_report(stdout: &str, stderr: &str, installed: &[Dist]) -> Result<Pars
let Ok(name) = PkgName::parse(raw_name) else {
continue;
};
let to = Version(
meta.get("version")
.and_then(serde_json::Value::as_str)
.unwrap_or_default()
.to_owned(),
);
let raw_version = meta
.get("version")
.and_then(serde_json::Value::as_str)
.unwrap_or_default();
// pip normalizes report versions to PEP 440; anything else is not something PipDock will
// turn into an argv token, and the change is left out rather than mis-rendered.
if !crate::pep440::is_argv_safe(raw_version) {
continue;
}
let to = Version(raw_version.to_owned());
if entry.get("is_yanked").and_then(serde_json::Value::as_bool) == Some(true) {
yanked.push(YankWarning {
@@ -244,7 +248,16 @@ pub fn uv_plan(stdout: &str, stderr: &str, installed: &[Dist]) -> Result<ParsedP
let Ok(name) = PkgName::parse(name.trim()) else {
continue;
};
let version = Version(version.trim().to_owned());
// `+ pkg==0.1.0 (from file:///…)` for a local or direct-URL source: the version is
// the first token, and the rest is uv explaining where it comes from. Kept whole,
// it reached argv as `pkg==0.1.0 (from file:///…)`.
let Some(version) = version.split_whitespace().next() else {
continue;
};
if !crate::pep440::is_argv_safe(version) {
continue;
}
let version = Version(version.to_owned());
if sign == "-" {
removals.insert(name, version);
} else {
@@ -665,4 +678,13 @@ mod tests {
"a yank must not make the plan dirty"
);
}
#[test]
fn a_uv_line_with_a_source_suffix_keeps_only_the_version() {
// `+ pkg==0.1.0 (from file:///…)` reached argv whole as the version.
let text = "Resolved 1 package in 5ms\nWould install 1 package\n + localpkg==0.1.0 (from file:///C:/src/localpkg)\n";
let plan = uv_plan("", text, &[]).expect("parses");
assert_eq!(plan.report.changes.len(), 1);
assert_eq!(plan.report.changes[0].to.0, "0.1.0");
}
}
+58 -8
View File
@@ -803,13 +803,7 @@ fn build_request(
upgrades: Vec::new(),
installs: specs
.iter()
.map(|raw| {
let (name, version_req) = match raw.split_once("==") {
Some((n, v)) => (n, Some(v.to_owned())),
None => (raw.as_str(), None),
};
PkgName::parse(name).map(|name| Spec { name, version_req })
})
.map(|raw| Spec::parse(raw))
.collect::<Result<_>>()?,
strategy: Strategy::Compatible,
},
@@ -918,10 +912,66 @@ mod tests {
)
.expect("builds");
assert_eq!(req.installs.len(), 2);
assert_eq!(req.installs[0].version_req.as_deref(), Some("0.28.1"));
assert_eq!(req.installs[0].version_req.as_deref(), Some("==0.28.1"));
assert_eq!(req.installs[1].version_req, None);
}
#[test]
fn every_spec_a_requirements_file_produces_builds_a_request() {
// The seam P1-B left open: `requirements::parse` emitted `django>=5.0,<6` and the flow
// split on `==` only, so every range failed with PD-PKG-002 and `pkg===1.0` became
// version `=1.0`. Fed through for real, not with hand-written specs.
let parsed = crate::requirements::parse(
"django>=5.0,<6\nrequests==2.32.3\nnumpy ~= 2.1\nlegacy===1.0-final\nidna\n",
);
assert!(parsed.skipped.is_empty(), "{:?}", parsed.skipped);
let (req, _) = build_request(
&Intent::Install {
specs: parsed.specs,
},
&[],
&[],
)
.expect("every parsed spec builds");
let got: Vec<(String, Option<String>)> = req
.installs
.iter()
.map(|s| (s.name.to_string(), s.version_req.clone()))
.collect();
assert_eq!(
got,
[
("django".to_owned(), Some(">=5.0,<6".to_owned())),
("requests".to_owned(), Some("==2.32.3".to_owned())),
("numpy".to_owned(), Some("~=2.1".to_owned())),
("legacy".to_owned(), Some("===1.0-final".to_owned())),
("idna".to_owned(), None),
]
);
}
#[test]
fn a_version_that_is_not_one_never_reaches_argv() {
// SECURITY §2 says versions are validated before argv; they were not. This one is a
// single token pip reads as a requirement whose marker is false — a silent no-op install.
for bad in [
"pkg==2.0 ; python_version<'0'",
"pkg==1.0 --break-system-packages",
"pkg @ https://example.com/pkg.whl",
"pkg is not a requirement",
] {
let err = build_request(
&Intent::Install {
specs: vec![bad.into()],
},
&[],
&[],
)
.expect_err(bad);
assert_eq!(err.code, Code::PkgNotFound, "{bad}");
}
}
#[test]
fn a_malformed_name_is_refused_before_any_engine_runs() {
// SECURITY §2: names are validated before they can reach argv, and both heads inherit
+1 -8
View File
@@ -426,14 +426,7 @@ fn validated_version(raw: &str) -> Result<Version> {
format!("invalid version in tools-requirements.txt: {raw:?}"),
)
};
let bytes = raw.as_bytes();
if bytes.is_empty() || !bytes[0].is_ascii_alphanumeric() {
return Err(invalid());
}
if !bytes
.iter()
.all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_' | b'+' | b'!'))
{
if !crate::pep440::is_version(raw) {
return Err(invalid());
}
Ok(Version(raw.to_owned()))
+1
View File
@@ -37,6 +37,7 @@ pub mod graph;
pub mod health;
pub mod index;
pub mod model;
pub mod pep440;
pub mod pins;
pub mod plan;
pub mod report;
+145 -7
View File
@@ -211,19 +211,87 @@ impl std::fmt::Display for Version {
}
/// What the user asked for: a name, optionally constrained.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[derive(Debug, Clone, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Spec {
/// Normalized distribution name.
pub name: PkgName,
/// PEP 440 specifier such as `">=2,<3"`, or `None` for "latest".
/// PEP 440 specifier such as `">=2,<3"` (or a bare version, read as `==`), or `None` for
/// "latest". Validated on every way in — [`Spec::parse`] and deserialization — so it is
/// never prose, a marker or an option by the time it reaches argv.
pub version_req: Option<String>,
}
impl Spec {
/// Parse what a user typed, or what a requirements file said: `name`, `name==1.0`,
/// `name>=5.0,<6`, `name ~= 1.4`, `name===legacy`.
///
/// The flow used to split on `==` alone, so every imported range failed — `django>=5.0,<6`
/// became the "package name" `django>=5.0,<6` and PD-PKG-002 — and `pkg===1.0` became
/// `pkg` at version `=1.0`.
///
/// # Errors
/// `PD-PKG-002` when the name is not a distribution name or the rest is not a PEP 440
/// specifier set.
pub fn parse(raw: &str) -> Result<Self> {
let raw = raw.trim();
let end = raw
.find(|c: char| matches!(c, '=' | '<' | '>' | '!' | '~') || c.is_whitespace())
.unwrap_or(raw.len());
let name = PkgName::parse(raw[..end].trim())?;
let rest = raw[end..].trim();
if rest.is_empty() {
return Ok(Self {
name,
version_req: None,
});
}
if !crate::pep440::is_specifier_set(rest) {
return Err(PdError::new(
Code::PkgNotFound,
format!("not a version specifier: {rest:?}"),
));
}
Ok(Self {
name,
version_req: Some(crate::pep440::normalize_specifier_set(rest)),
})
}
}
/// Deserialize through the same checks as [`Spec::parse`] — the IPC door, as for [`PkgName`].
impl<'de> serde::Deserialize<'de> for Spec {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct Raw {
name: PkgName,
version_req: Option<String>,
}
let raw = Raw::deserialize(deserializer)?;
let version_req = match raw.version_req {
None => None,
Some(v) if crate::pep440::is_version(&v) => Some(v),
Some(v) if crate::pep440::is_specifier_set(&v) => {
Some(crate::pep440::normalize_specifier_set(&v))
}
Some(v) => {
return Err(serde::de::Error::custom(format!(
"not a version specifier: {v:?}"
)));
}
};
Ok(Self {
name: raw.name,
version_req,
})
}
}
/// A resolved, exact `name==version` pair. Only these reach a mutating engine command.
#[derive(
Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
)]
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, schemars::JsonSchema)]
pub struct PinnedSpec {
/// Normalized distribution name.
pub name: PkgName,
@@ -232,10 +300,43 @@ pub struct PinnedSpec {
}
impl PinnedSpec {
/// Render as the `name==version` argv token.
/// Render as the argv token: `name==version`, or `name===version` for an installed version
/// that is not PEP 440 (packaging dropped `LegacyVersion`, so `==` would be refused).
///
/// Every producer of a `PinnedSpec` admits only a [`crate::pep440::is_argv_safe`] version —
/// the parsers, the guard set, the snapshot reader and deserialization all check — so this
/// cannot render a marker, a URL or whitespace into one token.
#[must_use]
pub fn to_requirement(&self) -> String {
format!("{}=={}", self.name, self.version)
if crate::pep440::is_version(&self.version.0) {
format!("{}=={}", self.name, self.version)
} else {
format!("{}==={}", self.name, self.version)
}
}
}
/// Deserialize with the version checked, the same door [`PkgName`] closes for names.
impl<'de> serde::Deserialize<'de> for PinnedSpec {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
#[derive(serde::Deserialize)]
struct Raw {
name: PkgName,
version: Version,
}
let raw = Raw::deserialize(deserializer)?;
if !crate::pep440::is_argv_safe(&raw.version.0) {
return Err(serde::de::Error::custom(format!(
"not a version: {:?}",
raw.version.0
)));
}
Ok(Self {
name: raw.name,
version: raw.version,
})
}
}
@@ -439,4 +540,41 @@ mod tests {
};
assert_eq!(spec.to_requirement(), "typing-extensions==4.12.2");
}
#[test]
fn a_spec_from_the_ipc_door_is_validated_like_one_from_a_parser() {
let ok: Spec = serde_json::from_str(r#"{"name":"Django","versionReq":">=5.0, <6"}"#)
.expect("a range deserializes");
assert_eq!(ok.name.as_str(), "django");
assert_eq!(ok.version_req.as_deref(), Some(">=5.0,<6"));
let bare: Spec = serde_json::from_str(r#"{"name":"idna","versionReq":"3.10"}"#)
.expect("a bare version deserializes");
assert_eq!(bare.version_req.as_deref(), Some("3.10"));
for junk in [
r#"{"name":"x","versionReq":"2.0 ; python_version<'0'"}"#,
r#"{"name":"x","versionReq":"--break-system-packages"}"#,
] {
assert!(serde_json::from_str::<Spec>(junk).is_err(), "{junk}");
}
}
#[test]
fn a_pinned_spec_refuses_a_version_that_could_be_more_than_one() {
assert!(serde_json::from_str::<PinnedSpec>(r#"{"name":"x","version":"1.0 ; x"}"#).is_err());
let ok: PinnedSpec =
serde_json::from_str(r#"{"name":"x","version":"1.0"}"#).expect("a version");
assert_eq!(ok.to_requirement(), "x==1.0");
}
#[test]
fn a_legacy_installed_version_is_restated_with_arbitrary_equality() {
// packaging dropped LegacyVersion, so `==1.0-SNAPSHOT` is refused; `===` compares strings.
let spec = PinnedSpec {
name: PkgName::parse("old").expect("name"),
version: Version("1.0-SNAPSHOT".into()),
};
assert_eq!(spec.to_requirement(), "old===1.0-SNAPSHOT");
}
}
+359
View File
@@ -0,0 +1,359 @@
//! PEP 440 at the boundary where text becomes argv (SECURITY §2).
//!
//! SECURITY §2 has always said names *and versions* are validated before they reach an argv array.
//! Names were — [`crate::model::PkgName`] is the only way one gets there — but versions were plain
//! strings: a dock-bay entry of `2.0 ; python_version<'0'` became one argv token pip accepted as a
//! requirement whose marker is false, so the install silently did nothing, and an imported
//! `django>=5.0,<6` failed outright because the flow split on `==` alone. This module is the one
//! place that decides what a version and a specifier set may look like.
//!
//! It is a **grammar check**, not an ordering: nothing here compares versions (that is
//! [`crate::compat`]'s job, and the engines'). It accepts what `packaging`'s `VERSION_PATTERN`
//! accepts — epochs, pre/post/dev segments in all their spellings, local labels — so a legitimate
//! version is never refused, and it rejects everything else, which is what keeps whitespace,
//! markers (`;`), URLs (`@`) and option-looking tokens out of argv.
/// Is `raw` a PEP 440 version (the permissive `VERSION_PATTERN`, case-insensitive)?
#[must_use]
pub fn is_version(raw: &str) -> bool {
Parser::new(raw).version()
}
/// Is `raw` safe to put after `===` (PEP 440 "arbitrary equality")?
///
/// Installed metadata can carry versions that are not PEP 440 — pip still lists them — and the
/// guard set must restate them. `===` compares strings, so the only requirement is that the token
/// cannot be read as anything *but* a version: ASCII alphanumerics and `. _ + ! -`, starting with
/// an alphanumeric. No whitespace, `;`, `@`, quotes, commas or operators.
#[must_use]
pub fn is_argv_safe(raw: &str) -> bool {
let bytes = raw.as_bytes();
!bytes.is_empty()
&& bytes[0].is_ascii_alphanumeric()
&& bytes
.iter()
.all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'+' | b'!' | b'-'))
}
/// Is `raw` a PEP 440 specifier set: `op version` clauses separated by commas?
///
/// Whitespace is allowed around commas and between an operator and its version (`~= 1.4`), never
/// inside a version. `==` and `!=` may end in `.*`; `===` takes any [`is_argv_safe`] token. An
/// empty string is **not** a specifier set — callers that allow "any version" check for that
/// themselves, so an empty clause like `>=1,` cannot slip through as one.
#[must_use]
pub fn is_specifier_set(raw: &str) -> bool {
!raw.trim().is_empty() && raw.split(',').all(is_clause)
}
/// The normalized form of a specifier set: whitespace removed around operators and commas.
///
/// Only call on something [`is_specifier_set`] accepted. `~= 1.4, <2` becomes `~=1.4,<2`, a single
/// argv-safe token.
#[must_use]
pub fn normalize_specifier_set(raw: &str) -> String {
raw.split(',')
.map(|clause| {
let clause = clause.trim();
let (op, rest) = split_operator(clause).unwrap_or(("", clause));
format!("{op}{}", rest.trim())
})
.collect::<Vec<_>>()
.join(",")
}
fn is_clause(clause: &str) -> bool {
let clause = clause.trim();
let Some((op, version)) = split_operator(clause) else {
return false;
};
let version = version.trim();
if version.is_empty() || version.contains(char::is_whitespace) {
return false;
}
match op {
"===" => is_argv_safe(version),
"==" | "!=" => {
// `==1.4.*` is a prefix match; the wildcard may only replace the final release part.
version
.strip_suffix(".*")
.map_or_else(|| is_version(version), is_release_prefix)
}
// `~=` needs at least two release segments (PEP 440); the grammar check is enough here
// and the engine will say so if it disagrees.
_ => is_version(version),
}
}
fn split_operator(clause: &str) -> Option<(&'static str, &str)> {
// Longest first, so `===` is not read as `==` followed by `=1.0`.
["===", "==", "!=", "<=", ">=", "~=", "<", ">"]
.into_iter()
.find_map(|op| clause.strip_prefix(op).map(|rest| (op, rest)))
}
/// `1`, `1.4`, `1!2.0` — what may precede `.*` in a prefix match.
fn is_release_prefix(raw: &str) -> bool {
let mut p = Parser::new(raw);
p.epoch();
p.release() && p.done()
}
/// A recursive-descent reader over `packaging`'s `VERSION_PATTERN`, lowercased.
struct Parser {
s: Vec<u8>,
at: usize,
}
impl Parser {
/// Not trimmed: a version is one argv token, and `" 1.0"` is not one. Callers that read a
/// clause with spaces around it (`~= 1.4`) trim the clause, not the version inside it.
fn new(raw: &str) -> Self {
Self {
s: raw.to_ascii_lowercase().into_bytes(),
at: 0,
}
}
fn done(&self) -> bool {
self.at == self.s.len()
}
fn peek(&self) -> Option<u8> {
self.s.get(self.at).copied()
}
fn eat(&mut self, b: u8) -> bool {
if self.peek() == Some(b) {
self.at += 1;
true
} else {
false
}
}
fn eat_word(&mut self, words: &[&str]) -> bool {
// Longest first is the caller's job; `words` is ordered that way below.
for w in words {
if self.s[self.at..].starts_with(w.as_bytes()) {
self.at += w.len();
return true;
}
}
false
}
fn digits(&mut self) -> bool {
let start = self.at;
while self.peek().is_some_and(|b| b.is_ascii_digit()) {
self.at += 1;
}
self.at > start
}
fn sep(&mut self) {
if matches!(self.peek(), Some(b'-' | b'_' | b'.')) {
self.at += 1;
}
}
/// `(N!)?` — only consumed when the digits really are followed by `!`.
fn epoch(&mut self) {
let save = self.at;
if !(self.digits() && self.eat(b'!')) {
self.at = save;
}
}
/// `N(.N)*`
fn release(&mut self) -> bool {
if !self.digits() {
return false;
}
loop {
let save = self.at;
if self.eat(b'.') && self.digits() {
continue;
}
self.at = save;
return true;
}
}
/// An optional segment `[-_.]? label [-_.]? N?`; restores the position when absent.
fn labelled(&mut self, labels: &[&str]) -> bool {
let save = self.at;
self.sep();
if self.eat_word(labels) {
let after = self.at;
self.sep();
if !self.digits() {
// The separator after a label is only part of the segment when digits follow.
self.at = after;
}
true
} else {
self.at = save;
false
}
}
fn post(&mut self) {
let save = self.at;
// `-N` is an implicit post-release.
if self.eat(b'-') && self.digits() {
return;
}
self.at = save;
self.labelled(&["post", "rev", "r"]);
}
fn local(&mut self) -> bool {
if !self.eat(b'+') {
return true;
}
let part = |p: &mut Self| {
let start = p.at;
while p.peek().is_some_and(|b| b.is_ascii_alphanumeric()) {
p.at += 1;
}
p.at > start
};
if !part(self) {
return false;
}
while matches!(self.peek(), Some(b'-' | b'_' | b'.')) {
self.at += 1;
if !part(self) {
return false;
}
}
true
}
fn version(mut self) -> bool {
if self.s.is_empty() {
return false;
}
self.eat(b'v');
self.epoch();
if !self.release() {
return false;
}
self.labelled(&["alpha", "beta", "preview", "pre", "rc", "a", "b", "c"]);
self.post();
self.labelled(&["dev"]);
self.local() && self.done()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn real_versions_are_accepted_in_every_spelling() {
for v in [
"1",
"1.0",
"2.32.3",
"1!2.0",
"1.0a1",
"1.0.alpha.1",
"1.0-beta2",
"1.0rc1",
"1.0c3",
"1.0.post1",
"1.0-1",
"1.0.rev2",
"1.0.dev0",
"1.0a1.dev2",
"1.0+ubuntu.1",
"1.0+abc-def_ghi",
"v1.2",
"2026.10.16",
"1.0.post",
"1.0.dev",
] {
assert!(is_version(v), "{v} is PEP 440");
}
}
#[test]
fn anything_that_is_not_a_version_is_refused() {
for v in [
"",
" ",
"2.0 ; python_version<'0'",
"1.0 --break-system-packages",
"1.0@https://evil",
"latest",
"1..0",
"1.0+",
"1.0+a..b",
">=1.0",
"1.0,2.0",
"1.0 2.0",
"'1.0'",
"1.0;",
"SNAPSHOT",
" 1.0",
"1.0 ",
] {
assert!(!is_version(v), "{v:?} must not pass as a version");
}
}
#[test]
fn specifier_sets_accept_what_requirements_files_contain() {
for s in [
"==2.32.3",
">=5.0,<6",
">=5.0, <6",
"~= 1.4",
"!=1.0.*",
"==1.4.*",
"===1.0-legacy",
">1,!=1.5,<2",
"<=2026.1",
] {
assert!(is_specifier_set(s), "{s:?} is a specifier set");
}
}
#[test]
fn specifier_sets_refuse_prose_markers_urls_and_options() {
for s in [
"",
">=1,",
"is not a requirement",
"==1.0 ; python_version<'3'",
"==1.0 --break-system-packages",
"@ https://example.com/x.whl",
"==",
"1.0",
">=1.0 <2",
"==1.*.0",
"~=1.0.*",
"===has space",
] {
assert!(!is_specifier_set(s), "{s:?} must be refused");
}
}
#[test]
fn normalizing_removes_only_the_whitespace_pep_440_allows() {
assert_eq!(normalize_specifier_set("~= 1.4, < 2"), "~=1.4,<2");
assert_eq!(normalize_specifier_set(">=5.0,<6"), ">=5.0,<6");
}
#[test]
fn arbitrary_equality_takes_a_legacy_version_but_nothing_that_could_be_more() {
assert!(is_argv_safe("1.0-SNAPSHOT"));
assert!(is_argv_safe("2.0.0.final"));
assert!(!is_argv_safe("1.0 ; x"));
assert!(!is_argv_safe("-1.0"));
assert!(!is_argv_safe("1.0@x"));
assert!(!is_argv_safe(""));
}
}
+2 -8
View File
@@ -118,14 +118,8 @@ fn validated_hold(version: &Version) -> Result<&str> {
format!("invalid version for a pin: {raw:?}"),
)
};
let bytes = raw.as_bytes();
if bytes.is_empty() || !bytes[0].is_ascii_alphanumeric() {
return Err(invalid());
}
if !bytes
.iter()
.all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_' | b'+' | b'!'))
{
// The one PEP 440 check, shared with the tools ledger and every argv boundary.
if !crate::pep440::is_version(raw) {
return Err(invalid());
}
Ok(raw)
+4 -13
View File
@@ -190,25 +190,16 @@ pub fn parse(text: &str) -> ParsedRequirements {
/// uninstallable, while the job here is only to reject whitespace and anything that is not part
/// of a specifier's alphabet. An empty constraint is valid and means "any version".
fn is_specifier(raw: &str) -> bool {
if raw.is_empty() {
return true;
}
raw.starts_with(['<', '>', '=', '!', '~'])
&& raw.chars().all(|c| {
c.is_ascii_alphanumeric()
|| matches!(
c,
'<' | '>' | '=' | '!' | '~' | '.' | ',' | '*' | '-' | '_' | '+'
)
})
// The shape check this used to be accepted `>=1.0--break-system-packages` (every character is
// in a specifier's alphabet); the grammar does not.
raw.is_empty() || crate::pep440::is_specifier_set(raw)
}
/// Render a parsed requirement back as a spec `Intent::Install` accepts.
///
/// **The marker is dropped, and the extras with it.** A marker is a condition on the *installing*
/// environment that the resolver evaluates itself, and PipDock's job here is to say which packages
/// to ask for. Keeping either would put a token into argv that
/// `plan::build_request`'s `name==version` split does not understand.
/// to ask for. What is rendered is exactly what [`crate::model::Spec::parse`] reads back.
fn render(req: &Requirement) -> String {
if req.constraint.is_empty() {
req.name.as_str().to_owned()
+18 -1
View File
@@ -171,7 +171,13 @@ fn pin_of(line: &str) -> Option<(PkgName, Version)> {
return None;
}
let name = PkgName::parse(name.trim()).ok()?;
Some((name, Version(version.trim().to_owned())))
let version = version.trim();
// A freeze line whose version could be read as more than one — `pkg==1.0 ; marker` from a
// hand-edited snapshot — is unrestorable rather than restored into argv.
if !crate::pep440::is_argv_safe(version) {
return None;
}
Some((name, Version(version.to_owned())))
}
/// Lines a freeze contained that [`parse_freeze`] could not turn into a restorable pin.
@@ -741,4 +747,15 @@ mod tests {
"ids must be filename-safe: {earlier}"
);
}
#[test]
fn a_freeze_line_whose_version_is_not_one_is_unrestorable() {
let freeze = "good==1.0\nodd==1.0 ; python_version < '0'\n";
let pins = parse_freeze(freeze);
assert_eq!(pins.len(), 1, "{pins:?}");
assert_eq!(
unrestorable_lines(freeze),
vec!["odd==1.0 ; python_version < '0'".to_owned()]
);
}
}
+5 -1
View File
@@ -949,7 +949,11 @@ export interface SourceLocation {
export interface Spec {
/** Normalized distribution name. */
name: PkgName;
/** PEP 440 specifier such as `">=2,<3"`, or `None` for "latest". */
/**
* PEP 440 specifier such as `">=2,<3"` (or a bare version, read as `==`), or `None` for
* "latest". Validated on every way in — [`Spec::parse`] and deserialization — so it is
* never prose, a marker or an option by the time it reaches argv.
*/
versionReq?: string | null;
}