Hmbown/CodeWhale · error

The Codewhale service returned an invalid device…

Error message

The Codewhale service returned an invalid device authorization response

What it means

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.

Solutions

  1. Make the server return a 43-character unpadded base64url encoding of a 32-byte random value ([A-Za-z0-9_-]{43})
  2. If the server emits standard base64, switch it to base64url without padding
  3. Log the raw device_code value and check for added/removed characters (URL-encoding, quotes) in transit

Example fix

// before
{ "device_code": "aGVsbG8=" }
// after
{ "device_code": "GhKq3_x9PpLmNqRsTuVwXyZ0123456789abcdefghijKLMN" }
Defensive patterns

Strategy: validation

Validate before calling

fn is_valid_device_code(code: &str) -> bool {
    code.len() == 43
        && code.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_'))
}

Type guard

fn device_code_shape_ok(code: &str) -> bool {
    is_valid_device_code(code)
}

Try / catch

match run_device_login() {
    Err(e) if e.to_string().contains("invalid device authorization response") => {
        eprintln!("device_code is not 43-char base64url; check server encoder and proxies.");
    }
    other => return other,
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/f1beae27741d354f. Report an issue: GitHub.

Appendix: source

Thrown at crates/cli/src/cloud.rs:1127

        || bytes[4] != b'-'
        || bytes[9] != b'-'
        || bytes
            .iter()
            .enumerate()
            .any(|(index, byte)| !matches!(index, 4 | 9) && !ALPHABET.contains(byte))
    {
        bail!("The Codewhale service returned an invalid user code");
    }
    Ok(())
}

fn validate_device_code(code: &str) -> Result<()> {
    if code.len() != 43
        || !code
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
    {
        bail!("The Codewhale service returned an invalid device authorization response");
    }
    Ok(())
}

fn validate_api_key(key: &str) -> Result<()> {
    let bytes = key.len();
    if bytes < MIN_API_KEY_BYTES || bytes as u64 > MAX_API_KEY_BYTES {
        bail!("API key must be {MIN_API_KEY_BYTES}-{MAX_API_KEY_BYTES} UTF-8 bytes");
    }
    if key.chars().any(is_ascii_control) {
        bail!("API key contains invalid control characters");
    }
    Ok(())
}

fn validate_label(label: &str) -> Result<String> {
    let label = label.split_whitespace().collect::<Vec<_>>().join(" ");
    if label.is_empty()

View on GitHub (pinned to 73e0f67d83)