{"record":{"id":"f1beae27741d354f","repo":"Hmbown/CodeWhale","slug":"the-codewhale-service-returned-an-invalid-device-a","errorCode":null,"errorMessage":"The Codewhale service returned an invalid device authorization response","messagePattern":"The Codewhale service returned an invalid device authorization response","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/cloud.rs","lineNumber":1127,"sourceCode":"        || 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 {\n        bail!(\"API key must be {MIN_API_KEY_BYTES}-{MAX_API_KEY_BYTES} UTF-8 bytes\");\n    }\n    if key.chars().any(is_ascii_control) {\n        bail!(\"API key contains invalid control characters\");\n    }\n    Ok(())\n}\n\nfn validate_label(label: &str) -> Result<String> {\n    let label = label.split_whitespace().collect::<Vec<_>>().join(\" \");\n    if label.is_empty()","sourceCodeStart":1109,"sourceCodeEnd":1145,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/cli/src/cloud.rs#L1109-L1145","documentation":"Thrown by `validate_device_code` in crates/cli/src/cloud.rs:1127 when the device_code in the device-authorization response is not exactly 43 characters of ASCII alphanumerics, `-`, or `_` — i.e. not a valid base64url-encoded 32-byte value. The CLI rejects it before polling the token endpoint, since a malformed device code can never complete the flow.","triggerScenarios":"The endpoint returns a device_code that is empty, shorter/longer than 43 chars, base64-standard (`+`/`/` and `=` padding) instead of base64url, hex-encoded, or JWT-shaped (contains dots).","commonSituations":"A mock server generating device codes with a different encoder; a proxy or JSON layer mangling/URL-encoding the code; server upgrade changing the token format while the CLI still expects 43-char base64url.","solutions":["Make the server return a 43-character unpadded base64url encoding of a 32-byte random value ([A-Za-z0-9_-]{43})","If the server emits standard base64, switch it to base64url without padding","Log the raw device_code value and check for added/removed characters (URL-encoding, quotes) in transit"],"exampleFix":"// before\n{ \"device_code\": \"aGVsbG8=\" }\n// after\n{ \"device_code\": \"GhKq3_x9PpLmNqRsTuVwXyZ0123456789abcdefghijKLMN\" }","handlingStrategy":"validation","validationCode":"fn is_valid_device_code(code: &str) -> bool {\n    code.len() == 43\n        && code.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_'))\n}","typeGuard":"fn device_code_shape_ok(code: &str) -> bool {\n    is_valid_device_code(code)\n}","tryCatchPattern":"match run_device_login() {\n    Err(e) if e.to_string().contains(\"invalid device authorization response\") => {\n        eprintln!(\"device_code is not 43-char base64url; check server encoder and proxies.\");\n    }\n    other => return other,\n}","preventionTips":["Encode device codes as unpadded base64url of 32 random bytes","Avoid standard base64 (+, /, =) in URL-adjacent tokens","Check proxies for token mangling or URL-encoding","Share an encoder utility between server and any test doubles"],"tags":["device-login","format-validation","base64url","device-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"}