Skip to content

HTTP header validation error: trailing whitespace in platform.version() causes Connection error #1266

@asamawi

Description

@asamawi

Bug Report: HTTP Header Validation Error Due to Trailing Whitespace in platform.version()

Environment

  • kimi-cli version: 1.15.0
  • Python version: 3.13.12
  • OS: Ubuntu 22.04.5 LTS (Jammy)
  • Kernel: 6.8.0-101-generic

Problem Description

The CLI fails with a Connection error on certain Linux systems because platform.version() returns a string with a trailing space, which violates HTTP header validation in the h11 library.

Error Message

httpcore.LocalProtocolError: Illegal header value b'#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC '

Root Cause

In kimi_cli/auth/oauth.py, the _ascii_header_value() function does not strip whitespace from header values:

def _ascii_header_value(value: str, *, fallback: str = "unknown") -> str:
    try:
        value.encode("ascii")
        return value  # <-- Missing .strip() here
    except UnicodeEncodeError:
        sanitized = value.encode("ascii", errors="ignore").decode("ascii").strip()
        return sanitized or fallback

On some Linux systems, platform.version() returns:

'#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC '
#                                                                    ^
#                                                            trailing space!

This value is used in X-Msh-Os-Version header via _common_headers():

"X-Msh-Os-Version": platform.version(),

The h11 library (used by httpx/httpcore) validates headers against RFC 7230 and rejects values with trailing whitespace.

Steps to Reproduce

  1. Install kimi-cli on Ubuntu 22.04 with kernel 6.8.0-101-generic
  2. Run kimi login to authenticate
  3. Run any command that makes an API call:
    echo "hi" | kimi --print --quiet
  4. Observe Connection error

Workaround / Fix

Add .strip() to the return value in _ascii_header_value():

def _ascii_header_value(value: str, *, fallback: str = "unknown") -> str:
    try:
        value.encode("ascii")
        return value.strip()  # Fixed: strip whitespace
    except UnicodeEncodeError:
        sanitized = value.encode("ascii", errors="ignore").decode("ascii").strip()
        return sanitized or fallback

Verification

After applying the fix locally:

$ echo "hi" | kimi --print --quiet
Hey there! 👋

I'm Kimi, ready to help you with whatever you need. What's on your mind today?

Additional Context

  • This issue is specific to certain kernel builds where uname -v includes trailing whitespace
  • The bug affects the X-Msh-Os-Version header and potentially other headers if they contain user/system data with whitespace
  • Related: h11's strict validation of field-value pattern in RFC 7230

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions