Skip to content

Commit

Permalink
Use the same feature name validation rule from Cargo
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Nov 7, 2023
1 parent 72356e4 commit 9cda4d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
}

for (key, values) in features.iter() {
if !Crate::valid_feature_name(key) {
return Err(cargo_err(&format!(
"\"{key}\" is an invalid feature name (feature names must contain only letters, numbers, '-', '+', or '_')"
)));
}

Crate::valid_feature_name(key)?;
let num_features = values.len();
if num_features > max_features {
return Err(cargo_err(&format!(
Expand Down
45 changes: 38 additions & 7 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,37 @@ impl Crate {
}

/// Validates the THIS parts of `features = ["THIS", "and/THIS"]`.
pub fn valid_feature_name(name: &str) -> bool {
!name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '+' || c == '.')
pub fn valid_feature_name(name: &str) -> AppResult<()> {
if name.is_empty() {
return Err(cargo_err("feature cannot be an empty"));
}
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if !(unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' || ch.is_ascii_digit()) {
return Err(cargo_err(&format!(
"invalid character `{}` in feature `{}`, \
the first character must be a Unicode XID start character or digit \
(most letters or `_` or `0` to `9`)",
ch, name,
)));
}
}
for ch in chars {
if !(unicode_xid::UnicodeXID::is_xid_continue(ch)
|| ch == '-'
|| ch == '+'
|| ch == '.')
{
return Err(cargo_err(&format!(
"invalid character `{}` in feature `{}`, \
characters must be Unicode XID characters, `+`, or `.` \
(numbers, `+`, `-`, `_`, `.`, or most letters)",
ch, name,
)));
}
}

Ok(())
}

/// Validates the prefix in front of the slash: `features = ["THIS/feature"]`.
Expand All @@ -241,10 +267,11 @@ impl Crate {
pub fn valid_feature(name: &str) -> bool {
match name.split_once('/') {
Some((dep, dep_feat)) => {
println!("dep: {}, dep_feat: {}", dep, dep_feat);
let dep = dep.strip_suffix('?').unwrap_or(dep);
Crate::valid_feature_prefix(dep) && Crate::valid_feature_name(dep_feat)
Crate::valid_feature_prefix(dep) && Crate::valid_feature_name(dep_feat).is_ok()
}
None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)),
None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)).is_ok(),
}
}

Expand Down Expand Up @@ -518,6 +545,10 @@ mod tests {
#[test]
fn valid_feature_names() {
assert!(Crate::valid_feature("foo"));
assert!(Crate::valid_feature("1foo"));
assert!(Crate::valid_feature("_foo"));
assert!(Crate::valid_feature("_foo-_+.1"));
assert!(Crate::valid_feature("_foo-_+.1"));
assert!(!Crate::valid_feature(""));
assert!(!Crate::valid_feature("/"));
assert!(!Crate::valid_feature("%/%"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: response.into_json()
{
"errors": [
{
"detail": "\"~foo\" is an invalid feature name (feature names must contain only letters, numbers, '-', '+', or '_')"
"detail": "invalid character `~` in feature `~foo`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"
}
]
}

0 comments on commit 9cda4d0

Please sign in to comment.