| 44 | namespace validation { |
| 45 | |
| 46 | Option<Error> validateID(const string& id) |
| 47 | { |
| 48 | if (id.empty()) { |
| 49 | return Error("ID must not be empty"); |
| 50 | } |
| 51 | |
| 52 | if (id.length() > NAME_MAX) { |
| 53 | return Error( |
| 54 | "ID must not be greater than " + |
| 55 | stringify(NAME_MAX) + " characters"); |
| 56 | } |
| 57 | |
| 58 | // The ID cannot be exactly these special path components. |
| 59 | if (id == "." || id == "..") { |
| 60 | return Error("'" + id + "' is disallowed"); |
| 61 | } |
| 62 | |
| 63 | // Rules on invalid characters in the ID: |
| 64 | // - Control characters are obviously not allowed. |
| 65 | // - Slashes are disallowed as IDs are likely mapped to directories in Mesos. |
| 66 | auto invalidCharacter = [](char c) { |
| 67 | return iscntrl(c) || |
| 68 | c == os::POSIX_PATH_SEPARATOR || |
| 69 | c == os::WINDOWS_PATH_SEPARATOR; |
| 70 | }; |
| 71 | |
| 72 | if (std::any_of(id.begin(), id.end(), invalidCharacter)) { |
| 73 | return Error("'" + id + "' contains invalid characters"); |
| 74 | } |
| 75 | |
| 76 | return None(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // These IDs are valid as long as they meet the common ID requirements |
no test coverage detected