{"record":{"id":"955f2f9afdd44049","repo":"Hmbown/CodeWhale","slug":"the-codewhale-service-returned-an-invalid-user-cod","errorCode":null,"errorMessage":"The Codewhale service returned an invalid user code","messagePattern":"The Codewhale service returned an invalid user code","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/cloud.rs","lineNumber":1116,"sourceCode":"        .unwrap_or(host);\n    host.eq_ignore_ascii_case(\"localhost\")\n        || host\n            .parse::<IpAddr>()\n            .is_ok_and(|address| address.is_loopback())\n}\n\nfn validate_user_code(code: &str) -> Result<()> {\n    const ALPHABET: &[u8] = b\"ABCDEFGHJKLMNPQRSTUVWXYZ23456789\";\n    let bytes = code.as_bytes();\n    if bytes.len() != 14\n        || bytes[4] != b'-'\n        || bytes[9] != b'-'\n        || bytes\n            .iter()\n            .enumerate()\n            .any(|(index, byte)| !matches!(index, 4 | 9) && !ALPHABET.contains(byte))\n    {\n        bail!(\"The Codewhale service returned an invalid user code\");\n    }\n    Ok(())\n}\n\nfn validate_device_code(code: &str) -> Result<()> {\n    if code.len() != 43\n        || !code\n            .bytes()\n            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))\n    {\n        bail!(\"The Codewhale service returned an invalid device authorization response\");\n    }\n    Ok(())\n}\n\nfn validate_api_key(key: &str) -> Result<()> {\n    let bytes = key.len();\n    if bytes < MIN_API_KEY_BYTES || bytes as u64 > MAX_API_KEY_BYTES {","sourceCodeStart":1098,"sourceCodeEnd":1134,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/cli/src/cloud.rs#L1098-L1134","documentation":"Thrown by `validate_user_code` in crates/cli/src/cloud.rs:1116 when the user_code returned by the device-authorization endpoint does not match the expected format: exactly 14 characters with hyphens at positions 4 and 9 (format XXXX-XXXX-XXXX) and every other character drawn from the unambiguous alphabet ABCDEFGHJKLMNPQRSTUVWXYZ23456789 (no 0, 1, I, O). The CLI validates it before showing the code to the user or polling the token endpoint.","triggerScenarios":"The service returns a user code like `ABCD-EFGH-IJKL` (contains forbidden I/1/O/0), `ABCDEF-GHJK` (wrong hyphen positions), a 13- or 15-character string, a lowercase code `abcd-efgh-jklm`, or an empty/null code field.","commonSituations":"A mock/staging server generating codes with the full alphanumeric set or the standard base32; a legacy server using a different grouping (e.g. 4-4-4 with different hyphen positions); serialization that lowercases the code.","solutions":["Fix the device-authorization endpoint to emit codes in the XXXX-XXXX-XXXX shape using only ABCDEFGHJKLMNPQRSTUVWXYZ23456789","Check for accidental lowercasing/normalization of the code field and preserve its case","Verify the code field in the raw JSON response is populated and not truncated"],"exampleFix":"// before\n{ \"user_code\": \"abcd-efgh-jklm\" }\n// after\n{ \"user_code\": \"ABCD-EFGH-JKLM\" }","handlingStrategy":"validation","validationCode":"const ALPHABET: &[u8] = b\"ABCDEFGHJKLMNPQRSTUVWXYZ23456789\";\nfn is_valid_user_code(code: &str) -> bool {\n    let b = code.as_bytes();\n    b.len() == 14\n        && b[4] == b'-' && b[9] == b'-'\n        && b.iter().enumerate()\n            .all(|(i, c)| matches!(i, 4 | 9) || ALPHABET.contains(c))\n}","typeGuard":"fn user_code_shape_ok(code: &str) -> bool {\n    is_valid_user_code(code)\n}","tryCatchPattern":"match run_device_login() {\n    Err(e) if e.to_string().contains(\"invalid user code\") => {\n        eprintln!(\"Server user_code format mismatch; expected XXXX-XXXX-XXXX from ABCDEFGHJKLMNPQRSTUVWXYZ23456789.\");\n    }\n    other => return other,\n}","preventionTips":["Generate codes with the same 32-char unambiguous alphabet as the CLI","Never lowercase or case-normalize the code field in transport","Share the format constant between server and CLI","Test mock code generators against the real validator"],"tags":["device-login","format-validation","user-code"],"backgroundTag":"invalid-argument-format","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}