Skip to content

fix(auth): strip whitespace from HTTP header values to prevent h11 rejection#1229

Open
privatejava wants to merge 2 commits intoMoonshotAI:mainfrom
Workharu:fix/strip-trailing-whitespace-from-header-values
Open

fix(auth): strip whitespace from HTTP header values to prevent h11 rejection#1229
privatejava wants to merge 2 commits intoMoonshotAI:mainfrom
Workharu:fix/strip-trailing-whitespace-from-header-values

Conversation

@privatejava
Copy link

@privatejava privatejava commented Feb 24, 2026

Related Issue

Resolve #1226 #1227, #1220

Description

Kimi throws an error LLM Provider error when trying from Ubuntu 22.04.5 LTS .

On some Linux systems, platform.version() returns a kernel version string with a trailing space (e.g. "#100~22.04.1-Ubuntu SMP ... UTC "). This trailing whitespace violates RFC 7230 field-value grammar, causing h11 to raise LocalProtocolError: Illegal header value and making the CLI unable to connect to the API.

The existing _ascii_header_value() only stripped whitespace in the UnicodeEncodeError fallback path. This change ensures .strip() is always applied, matching the behavior that was already intended for the non-ASCII path.

Here is the detailed logs from ~/.kimi/logs/kimi.log

2026-02-23 19:14:39.432 | ERROR    | kimi_cli.ui.shell:run_soul_command:253 - LLM provider error:
Traceback (most recent call last):

  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpx/_transports/default.py", line 101, in map_httpcore_exceptions
    yield
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpx/_transports/default.py", line 394, in handle_async_request
    resp = await self._pool.handle_async_request(req)
                 │    │     │                    └ <Request [b'POST']>
                 │    │     └ <function AsyncConnectionPool.handle_async_request at 0x7f9ca92f0540>
                 │    └ <AsyncConnectionPool [Requests: 0 active, 0 queued | Connections: 0 active, 0 idle]>
                 └ <httpx.AsyncHTTPTransport object at 0x7f9ca9425fd0>
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_async/connection_pool.py", line 256, in handle_async_request
    raise exc from None
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_async/connection_pool.py", line 236, in handle_async_request
    response = await connection.handle_async_request(
                     │          └ <function AsyncHTTPConnection.handle_async_request at 0x7f9ca92e7600>
                     └ <AsyncHTTPConnection ['https://api.kimi.com:443', HTTP/1.1, CLOSED, Request Count: 1]>
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_async/connection.py", line 103, in handle_async_request
    return await self._connection.handle_async_request(request)
                 │    │           │                    └ <Request [b'POST']>
                 │    │           └ <function AsyncHTTP11Connection.handle_async_request at 0x7f9ca92e63e0>
                 │    └ <AsyncHTTP11Connection ['https://api.kimi.com:443', CLOSED, Request Count: 1]>
                 └ <AsyncHTTPConnection ['https://api.kimi.com:443', HTTP/1.1, CLOSED, Request Count: 1]>
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_async/http11.py", line 136, in handle_async_request
    raise exc
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_async/http11.py", line 86, in handle_async_request
    await self._send_request_headers(**kwargs)
          │    │                       └ {'request': <Request [b'POST']>}
          │    └ <function AsyncHTTP11Connection._send_request_headers at 0x7f9ca92e6480>
          └ <AsyncHTTP11Connection ['https://api.kimi.com:443', CLOSED, Request Count: 1]>
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_async/http11.py", line 144, in _send_request_headers
    with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
         │               │   │                   └ <class 'httpcore.LocalProtocolError'>
         │               │   └ <class 'h11._util.LocalProtocolError'>
         │               └ <module 'h11' from '/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/h11/__init__.py'>
         └ <function map_exceptions at 0x7f9ca942fa60>
  File "/home/narayan/.local/share/uv/python/cpython-3.13.11-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    │    │   │     └ LocalProtocolError("Illegal header value b'#100~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 19 17:10:19 UTC '")
    │    │   └ <method 'throw' of 'generator' objects>
    │    └ <generator object map_exceptions at 0x7f9c8fe932e0>
    └ <contextlib._GeneratorContextManager object at 0x7f9c8feb4ec0>
  File "/home/narayan/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
          └ <class 'httpcore.LocalProtocolError'>

httpcore.LocalProtocolError: Illegal header value b'#100~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 19 17:10:19 UTC '

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

Open with Devin

…jection

On some Linux systems, `platform.version()` returns a kernel version
string with a trailing space (e.g. "MoonshotAI#100~22.04.1-Ubuntu SMP ... UTC ").
This trailing whitespace violates RFC 7230 field-value grammar, causing
h11 to raise `LocalProtocolError: Illegal header value` and making the
CLI unable to connect to the API.

The existing `_ascii_header_value()` only stripped whitespace in the
UnicodeEncodeError fallback path. This change ensures `.strip()` is
always applied, matching the behavior that was already intended for the
non-ASCII path.
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@leonardobrunelli-OX
Copy link

You can manually patch the OAuth module in your installation.

edit this file:

Edit the oauth.py file in your uv tools installation

nano /home/USERFOLDER/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/kimi_cli/auth/oauth.py
Find the _common_headers() function (around line 155) and change:

Before (broken):

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

After (fixed):

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

@gurudk
Copy link

gurudk commented Feb 26, 2026

这个预计在哪个版本发布解决,试了1.14还有这个问题


In which version is this expected to be released? I tried 1.14 and still have this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LLM provider error: Connection error.

3 participants