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 <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Nov 10, 2023
1 parent 57b2616 commit 2765157
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 45 deletions.
16 changes: 3 additions & 13 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +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 {
Expand All @@ -257,9 +253,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
}

for value in values.iter() {
if !Crate::valid_feature(value) {
return Err(cargo_err(&format!("\"{value}\" is an invalid feature name")));
}
Crate::valid_feature(value)?;
}
}

Expand Down Expand Up @@ -597,11 +591,7 @@ pub fn validate_dependency(dep: &EncodableCrateDependency) -> AppResult<()> {
}

for feature in &dep.features {
if !Crate::valid_feature(feature) {
return Err(cargo_err(&format_args!(
"\"{feature}\" is an invalid feature name",
)));
}
Crate::valid_feature(feature)?;
}

if let Some(registry) = &dep.registry {
Expand Down
112 changes: 83 additions & 29 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,6 @@ impl Crate {
.unwrap_or(false)
}

/// 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 == '+')
}

/// Validates the prefix in front of the slash: `features = ["THIS/feature"]`.
/// Normally this corresponds to the crate name of a dependency.
fn valid_feature_prefix(name: &str) -> bool {
Expand All @@ -237,14 +229,68 @@ impl Crate {
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}

/// Validates a whole feature string, `features = ["THIS", "ALL/THIS"]`.
pub fn valid_feature(name: &str) -> bool {
match name.split_once('/') {
Some((dep, dep_feat)) => {
let dep = dep.strip_suffix('?').unwrap_or(dep);
Crate::valid_feature_prefix(dep) && Crate::valid_feature_name(dep_feat)
/// Validates the THIS parts of `features = ["THIS", "and/THIS", "dep:THIS", "dep?/THIS"]`.
/// 1. The name must be non-empty.
/// 2. The first character must be a Unicode XID start character, `_`, or a digit.
/// 3. The remaining characters must be Unicode XID characters, `_`, `+`, `-`, or `.`.
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 a whole feature string, `features = ["THIS", "and/THIS", "dep:THIS", "dep?/THIS"]`.
pub fn valid_feature(name: &str) -> AppResult<()> {
if let Some((dep, dep_feat)) = name.split_once('/') {
let dep = dep.strip_suffix('?').unwrap_or(dep);
if !Crate::valid_dependency_name(dep) {
return Err(cargo_err(&format_args!(
"\"{dep}\" is an invalid dependency name (dependency \
names must start with a letter or underscore, contain only \
letters, numbers, hyphens, or underscores and have at most \
{MAX_NAME_LENGTH} characters)"
)));
}
None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)),
Crate::valid_feature_name(dep_feat)
} else if let Some((_, dep)) = name.split_once("dep:") {
if !Crate::valid_dependency_name(dep) {
return Err(cargo_err(&format_args!(
"\"{dep}\" is an invalid dependency name (dependency \
names must start with a letter or underscore, contain only \
letters, numbers, hyphens, or underscores and have at most \
{MAX_NAME_LENGTH} characters)"
)));
}
return Ok(());
} else {
Crate::valid_feature_name(name)
}
}

Expand Down Expand Up @@ -517,19 +563,27 @@ mod tests {

#[test]
fn valid_feature_names() {
assert!(Crate::valid_feature("foo"));
assert!(!Crate::valid_feature(""));
assert!(!Crate::valid_feature("/"));
assert!(!Crate::valid_feature("%/%"));
assert!(Crate::valid_feature("a/a"));
assert!(Crate::valid_feature("32-column-tables"));
assert!(Crate::valid_feature("c++20"));
assert!(Crate::valid_feature("krate/c++20"));
assert!(!Crate::valid_feature("c++20/wow"));
assert!(Crate::valid_feature("foo?/bar"));
assert!(Crate::valid_feature("dep:foo"));
assert!(!Crate::valid_feature("dep:foo?/bar"));
assert!(!Crate::valid_feature("foo/?bar"));
assert!(!Crate::valid_feature("foo?bar"));
assert!(Crate::valid_feature("foo").is_ok());
assert!(Crate::valid_feature("1foo").is_ok());
assert!(Crate::valid_feature("_foo").is_ok());
assert!(Crate::valid_feature("_foo-_+.1").is_ok());
assert!(Crate::valid_feature("_foo-_+.1").is_ok());
assert!(Crate::valid_feature("").is_err());
assert!(Crate::valid_feature("/").is_err());
assert!(Crate::valid_feature("%/%").is_err());
assert!(Crate::valid_feature("a/a").is_ok());
assert!(Crate::valid_feature("32-column-tables").is_ok());
assert!(Crate::valid_feature("c++20").is_ok());
assert!(Crate::valid_feature("krate/c++20").is_ok());
assert!(Crate::valid_feature("c++20/wow").is_err());
assert!(Crate::valid_feature("foo?/bar").is_ok());
assert!(Crate::valid_feature("dep:foo").is_ok());
assert!(Crate::valid_feature("dep:foo?/bar").is_err());
assert!(Crate::valid_feature("foo/?bar").is_err());
assert!(Crate::valid_feature("foo?bar").is_err());
assert!(Crate::valid_feature("bar.web").is_ok());
assert!(Crate::valid_feature("foo/bar.web").is_ok());
assert!(Crate::valid_feature("dep:0foo").is_err());
assert!(Crate::valid_feature("0foo?/bar.web").is_err());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: response.into_json()
{
"errors": [
{
"detail": "\"🍺\" is an invalid feature name"
"detail": "invalid character `🍺` in feature `🍺`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: response.into_json()
{
"errors": [
{
"detail": "\"!bar\" is an invalid feature name"
"detail": "invalid character `!` in feature `!bar`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"
}
]
}
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 2765157

Please sign in to comment.