Skip to content

[BUG] [v0.0.7] cortex login status panics on non-ASCII CORTEX_AUTH_TOKEN — byte-slice at char boundary in cortex-login/utils.rs:11 #51202

Description

@CherryPop-999

Description

When the CORTEX_AUTH_TOKEN environment variable is set to a value containing non-ASCII UTF-8 characters where byte position 8 falls inside a multi-byte character, cortex login status panics immediately.

The safe_format_key() function in cortex-login/src/utils.rs checks if key.len() <= 13 and then slices &key[..8] as a raw byte slice. If byte 8 is inside a multi-byte UTF-8 character, this panics.

Steps to Reproduce

$env:CORTEX_AUTH_TOKEN = "aaaaaaa" + [char]233 + "aaaaa"
& "C:\...\cortex.exe" login status

The token is 7 ASCII chars + é (U+00E9, 2 UTF-8 bytes at positions 7-8) + 5 ASCII chars = 14 bytes. This passes the <= 13 guard, then &key[..8] slices mid-character.

Actual Behavior

Panic in thread 'main' at src\cortex-login\src\utils.rs:11:22

thread 'main' (27028) panicked at src\cortex-login\src\utils.rs:11:22:
byte index 8 is not a char boundary; it is inside 'é' (bytes 7..9) of `aaaaaaaéaaaaa`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected Behavior

Should safely mask the token without panicking, even if it contains non-ASCII characters.

Root Cause

In src/cortex-login/src/utils.rs:11:

pub fn safe_format_key(key: &str) -> String {
    if key.len() <= 13 {
        return "***".to_string();
    }
    let prefix = &key[..8];           // panics if byte 8 is inside a multi-byte char
    let suffix = &key[key.len() - 5..];
    format!("{prefix}***{suffix}")
}

Should use key.floor_char_boundary(8) or iterate via char_indices() for safe slicing.

Screenshot

cortex login status UTF-8 panic

Environment

  • Cortex version: v0.0.7
  • OS: Windows 11
  • Shell: PowerShell

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions