Skip to main content

users/
names.rs

1//! Account name and home rules shared by every account store.
2
3/// Longest accepted user name, in bytes (the same limit as Linux `useradd`).
4pub const MAXIMUM_USER_NAME_LENGTH: usize = 32;
5
6/// Home of the built-in root account, which has no account record.
7pub const ROOT_HOME: &str = "/users/root";
8
9/// Name of the group whose members administer the device, for example by
10/// running a command as another user. Seeded images create it with the
11/// `administrator` account as its member.
12pub const ADMINISTRATOR_GROUP_NAME: &str = "administrator";
13
14/// Whether `name` may be given to a new or renamed account.
15///
16/// A user name becomes the final component of its account record path and of
17/// its default home (`/users/<name>`), so the accepted set is deliberately
18/// small: an ASCII letter followed by at most 31 ASCII letters, digits, `_` or
19/// `-`. This rejects empty and overlong names, `/`, `.` and `..`, NUL and
20/// control characters, whitespace and non-ASCII text. The leading letter also
21/// keeps a name from colliding with the numeric `/users/<identifier>` homes of
22/// accounts created before homes were named after users.
23///
24/// Names are case-sensitive. Existing accounts are not revalidated when they
25/// are loaded, so older names keep working.
26pub fn is_valid_user_name(name: &str) -> bool {
27    let mut bytes = name.bytes();
28    let Some(first) = bytes.next() else {
29        return false;
30    };
31    name.len() <= MAXIMUM_USER_NAME_LENGTH
32        && first.is_ascii_alphabetic()
33        && bytes.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
34}
35
36/// Whether `home` is an acceptable recorded home: a single entry directly
37/// below `/users`, such as `/users/alice` or the `/users/1000` of an older
38/// account. The entry may not be `.` or `..`, nor contain `/`, NUL or control
39/// characters, and is at most 255 bytes long.
40pub fn is_valid_user_home(home: &str) -> bool {
41    let Some(entry) = home.strip_prefix("/users/") else {
42        return false;
43    };
44    !entry.is_empty()
45        && entry.len() <= 255
46        && !matches!(entry, "." | "..")
47        && !entry
48            .chars()
49            .any(|character| character == '/' || character.is_control())
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn accepts_the_documented_character_set() {
58        for name in ["a", "administrator", "Alice", "build_bot-2", "z0"] {
59            assert!(is_valid_user_name(name), "{name}");
60        }
61        assert!(is_valid_user_name(&"a".repeat(MAXIMUM_USER_NAME_LENGTH)));
62    }
63
64    #[test]
65    fn rejects_names_that_could_escape_or_collide() {
66        for name in [
67            "",
68            ".",
69            "..",
70            ".hidden",
71            "a/b",
72            "/",
73            "../root",
74            "a\0b",
75            "a\nb",
76            "tab\t",
77            "with space",
78            "1000",
79            "0",
80            "-option",
81            "_private",
82            "a.b",
83            "é",
84            "naïve",
85        ] {
86            assert!(!is_valid_user_name(name), "{name:?}");
87        }
88        assert!(!is_valid_user_name(
89            &"a".repeat(MAXIMUM_USER_NAME_LENGTH + 1)
90        ));
91    }
92
93    #[test]
94    fn homes_are_single_entries_below_users() {
95        for home in ["/users/alice", "/users/1000", ROOT_HOME] {
96            assert!(is_valid_user_home(home), "{home}");
97        }
98        for home in [
99            "",
100            "/users",
101            "/users/",
102            "/users/.",
103            "/users/..",
104            "/users/a/b",
105            "/users/a/",
106            "/users/a\0",
107            "/users/a\n",
108            "/system/users/alice",
109            "users/alice",
110            "/usersalice",
111        ] {
112            assert!(!is_valid_user_home(home), "{home:?}");
113        }
114    }
115}