Skip to content

Connection error on Linux when platform.version() contains # character in HTTP header #1368

@chenyuchen993-cmyk

Description

@chenyuchen993-cmyk

What version of Kimi Code CLI is running?

1.17.0

Which open platform/subscription were you using?

/login, kimicode plan

Which model were you using?

No response

What platform is your computer?

No response

What issue are you seeing?

On Ubuntu 22.04 (HWE kernel), running kimi and sending any message immediately fails with:

LLM provider error: Connection error.

The root cause is that platform.version() returns a string containing a # character (e.g.
#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC), which is an illegal character in HTTP
header values per RFC 7230. This string is used as the value of the X-Msh-Os-Version header.

The existing _ascii_header_value() function in kimi_cli/auth/oauth.py only filters non-ASCII (Unicode)
characters, but does not filter HTTP-illegal ASCII characters such as #. As a result, httpx raises a
LocalProtocolError before the request is ever sent, which gets wrapped into APIConnectionError: Connection
error.

What steps can reproduce the bug?

  1. Run on Ubuntu 22.04 with HWE kernel (e.g. 6.8.0-101-generic)
  2. Verify python3 -c "import platform; print(platform.version())" outputs a string starting with #
  3. Run kimi and send any message
  4. Observe LLM provider error: Connection error.

What is the expected behavior?

The # and other HTTP-illegal characters should be stripped from header values before sending, so the
request succeeds normally.

Additional information

Root Cause

In kimi_cli/auth/oauth.py, the _ascii_header_value() function:

  def _ascii_header_value(value: str, *, fallback: str = "unknown") -> str:
      try:
          value.encode("ascii")
          return value  # ← returns value with '#' intact!
      except UnicodeEncodeError:
          sanitized = value.encode("ascii", errors="ignore").decode("ascii").strip()
          return sanitized or fallback

The string #101~22.04.1-Ubuntu... is pure ASCII, so it passes the encode("ascii") check and is returned
as-is — including the illegal # character.

Suggested Fix

Extend _ascii_header_value() to also strip HTTP-illegal characters:

  def _ascii_header_value(value: str, *, fallback: str = "unknown") -> str:
      try:
          value.encode("ascii")
      except UnicodeEncodeError:
          value = value.encode("ascii", errors="ignore").decode("ascii")
      # Strip characters illegal in HTTP header values
      sanitized = "".join(
          c for c in value if ord(c) >= 0x20 and c not in '\x7f"(),/:;<=>?@[\\]{}#'
      ).strip()
      return sanitized or fallback

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions