Skip to content

Commit 318e165

Browse files
committed
Use the same feature name validation rule from Cargo
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 72356e4 commit 318e165

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

src/controllers/krate/publish.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
238238
}
239239

240240
for (key, values) in features.iter() {
241-
if !Crate::valid_feature_name(key) {
242-
return Err(cargo_err(&format!(
243-
"\"{key}\" is an invalid feature name (feature names must contain only letters, numbers, '-', '+', or '_')"
244-
)));
245-
}
246-
241+
Crate::valid_feature_name(key)?;
247242
let num_features = values.len();
248243
if num_features > max_features {
249244
return Err(cargo_err(&format!(

src/models/krate.rs

+39-8
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,38 @@ impl Crate {
220220
.unwrap_or(false)
221221
}
222222

223-
/// Validates the THIS parts of `features = ["THIS", "and/THIS"]`.
224-
pub fn valid_feature_name(name: &str) -> bool {
225-
!name.is_empty()
226-
&& name
227-
.chars()
228-
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '+' || c == '.')
223+
/// Validates the THIS parts of `features = ["THIS", "and/THIS", "dep:THIS", "dep?/THIS"]`.
224+
pub fn valid_feature_name(name: &str) -> AppResult<()> {
225+
if name.is_empty() {
226+
return Err(cargo_err("feature cannot be an empty"));
227+
}
228+
let mut chars = name.chars();
229+
if let Some(ch) = chars.next() {
230+
if !(unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' || ch.is_ascii_digit()) {
231+
return Err(cargo_err(&format!(
232+
"invalid character `{}` in feature `{}`, \
233+
the first character must be a Unicode XID start character or digit \
234+
(most letters or `_` or `0` to `9`)",
235+
ch, name,
236+
)));
237+
}
238+
}
239+
for ch in chars {
240+
if !(unicode_xid::UnicodeXID::is_xid_continue(ch)
241+
|| ch == '-'
242+
|| ch == '+'
243+
|| ch == '.')
244+
{
245+
return Err(cargo_err(&format!(
246+
"invalid character `{}` in feature `{}`, \
247+
characters must be Unicode XID characters, `+`, or `.` \
248+
(numbers, `+`, `-`, `_`, `.`, or most letters)",
249+
ch, name,
250+
)));
251+
}
252+
}
253+
254+
Ok(())
229255
}
230256

231257
/// Validates the prefix in front of the slash: `features = ["THIS/feature"]`.
@@ -241,10 +267,11 @@ impl Crate {
241267
pub fn valid_feature(name: &str) -> bool {
242268
match name.split_once('/') {
243269
Some((dep, dep_feat)) => {
270+
println!("dep: {}, dep_feat: {}", dep, dep_feat);
244271
let dep = dep.strip_suffix('?').unwrap_or(dep);
245-
Crate::valid_feature_prefix(dep) && Crate::valid_feature_name(dep_feat)
272+
Crate::valid_feature_prefix(dep) && Crate::valid_feature_name(dep_feat).is_ok()
246273
}
247-
None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)),
274+
None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)).is_ok(),
248275
}
249276
}
250277

@@ -518,6 +545,10 @@ mod tests {
518545
#[test]
519546
fn valid_feature_names() {
520547
assert!(Crate::valid_feature("foo"));
548+
assert!(Crate::valid_feature("1foo"));
549+
assert!(Crate::valid_feature("_foo"));
550+
assert!(Crate::valid_feature("_foo-_+.1"));
551+
assert!(Crate::valid_feature("_foo-_+.1"));
521552
assert!(!Crate::valid_feature(""));
522553
assert!(!Crate::valid_feature("/"));
523554
assert!(!Crate::valid_feature("%/%"));

src/tests/krate/publish/snapshots/all__krate__publish__features__invalid_feature_name.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "\"~foo\" is an invalid feature name (feature names must contain only letters, numbers, '-', '+', or '_')"
8+
"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`)"
99
}
1010
]
1111
}

0 commit comments

Comments
 (0)