Skip to content

Commit d07cb77

Browse files
eatradishMingcongBai
authored andcommitted
fix: fix validate_username follow Linux username rules.
The comment translation was carried out using the DeepSeek v4 Flush model - Maximum 32 characters - Must start with a lowercase letter (a-z) or underscore (_) - Emaining characters: lowercase letters (a-z), digits (0-9), underscores (_), or hyphens (-)
1 parent 14de5cf commit d07cb77

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎i18n/en-US/dkcli.ftl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ swap-size = Size of the swapfile (GiB)
4242
hostname-illegal = The specified hostname contains invalid character(s): { $c }
4343
username-illegal = The specified username contains invalid character(s): { $c }
4444
username-illegal-starts-with-number = The specified UNIX user name cannot begin with a number.
45+
username-illegal-starts-with = The specified username starts with invalid character(s): { $c }
46+
username-illegal-too-long = The specified username is too long.
4547
hostname-illegal-starts-with = The specified hostname starts with invalid character(s): { $c }
4648
hostname-illegal-ends-with = The specified hostname ends with invalid character(s): { $c }
4749
hostname-illegal-too-loong = The specified hostname is too long.

‎i18n/zh-CN/dkcli.ftl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ hostname-illegal = 指定的主机名中包含无效字符:{ $c }
4343
hostname-illegal-startswith = 指定的主机名以无效字符字符开头:{ $c }
4444
username-illegal = 指定的 UNIX 用户名中包含无效字符:{ $c }
4545
username-illegal-starts-with-number = 指定的 UNIX 用户名中开头不能是数字。
46+
username-illegal-starts-with = 指定的 UNIX 用户名以无效字符开头:{ $c }
47+
username-illegal-too-long = 指定的 UNIX 用户名过长。
4648
fullname-illegal = 指定的用户全名中包含无效字符:':'
4749
squashfs-empty = 系统发行元数据文件中未包含 `squashfs',该元数据文件可能已损坏。
4850
confirm-password = 确认密码

‎src/main.rs‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,16 +822,33 @@ fn validate_hostname(input: &str) -> std::result::Result<Validation, Box<dyn Err
822822
}
823823

824824
fn validate_username(input: &str) -> std::result::Result<Validation, Box<dyn Error + Send + Sync>> {
825-
if input.starts_with(|x: char| x.is_ascii_digit()) {
825+
if input.is_empty() {
826+
return Ok(Validation::Invalid(fl!("username-illegal", c = "").into()));
827+
}
828+
829+
// Maximum 32 characters
830+
if input.chars().count() > 32 {
831+
return Ok(Validation::Invalid(fl!("username-illegal-too-long").into()));
832+
}
833+
834+
// Must start with a lowercase letter (a-z) or underscore (_)
835+
let first = input.chars().next().unwrap();
836+
if first.is_ascii_digit() {
826837
return Ok(Validation::Invalid(
827838
fl!("username-illegal-starts-with-number").into(),
828839
));
829840
}
841+
if !first.is_ascii_lowercase() && first != '_' {
842+
return Ok(Validation::Invalid(
843+
fl!("username-illegal-starts-with", c = first.to_string()).into(),
844+
));
845+
}
830846

831-
for i in input.chars() {
832-
if !i.is_ascii_lowercase() && !i.is_ascii_digit() {
847+
// Emaining characters: lowercase letters (a-z), digits (0-9), underscores (_), or hyphens (-)
848+
for c in input.chars().skip(1) {
849+
if !c.is_ascii_lowercase() && !c.is_ascii_digit() && c != '_' && c != '-' {
833850
return Ok(Validation::Invalid(
834-
fl!("username-illegal", c = i.to_string()).into(),
851+
fl!("username-illegal", c = c.to_string()).into(),
835852
));
836853
}
837854
}
@@ -1148,6 +1165,9 @@ fn test_hostname_validation() {
11481165
fn test_username_validation() {
11491166
assert_eq!(validate_username("foo").unwrap(), Validation::Valid);
11501167
assert_eq!(validate_username("cth451").unwrap(), Validation::Valid);
1168+
assert_eq!(validate_username("foo_bar").unwrap(), Validation::Valid);
1169+
assert_eq!(validate_username("foo-bar").unwrap(), Validation::Valid);
1170+
assert_eq!(validate_username("_foo").unwrap(), Validation::Valid);
11511171
assert!(matches!(
11521172
validate_username("老白").unwrap(),
11531173
Validation::Invalid(..)
@@ -1160,4 +1180,12 @@ fn test_username_validation() {
11601180
validate_username("123bai").unwrap(),
11611181
Validation::Invalid(..)
11621182
));
1183+
assert!(matches!(
1184+
validate_username("-foobar").unwrap(),
1185+
Validation::Invalid(..)
1186+
));
1187+
assert!(matches!(
1188+
validate_username(&"a".repeat(33)).unwrap(),
1189+
Validation::Invalid(..)
1190+
));
11631191
}

0 commit comments

Comments
 (0)