fix: strip whitespace from oauth header values#1361
fix: strip whitespace from oauth header values#13610xsirsaif wants to merge 1 commit intoMoonshotAI:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR prevents invalid HTTP header values by stripping leading/trailing whitespace from the OAuth “common” headers before requests are sent, addressing httpx/httpcore.LocalProtocolError failures caused by platform strings with trailing spaces.
Changes:
- Strip leading/trailing whitespace in
_ascii_header_value()and ensure empty/whitespace-only values fall back to"unknown". - Apply
_ascii_header_value()normalization across all_common_headers()values. - Add tests covering whitespace stripping and the
X-Msh-Os-Versiontrailing-whitespace regression.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/kimi_cli/auth/oauth.py |
Normalizes header values by trimming whitespace and applying a fallback for empty results. |
tests/utils/test_utils_environment.py |
Adds regression/unit tests validating header value trimming and the X-Msh-Os-Version fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def test_ascii_header_value_strips_ascii_whitespace(): | ||
| assert _ascii_header_value(" value ") == "value" | ||
| assert _ascii_header_value(" ") == "unknown" | ||
|
|
||
|
|
||
| def test_common_headers_strip_os_version(monkeypatch): | ||
| monkeypatch.setattr(platform, "node", lambda: "host") |
There was a problem hiding this comment.
The new OAuth header sanitization tests are placed in test_utils_environment.py, which makes them harder to discover/maintain since they are unrelated to environment detection. Consider moving these tests into a dedicated auth/oauth test module (e.g., tests/auth/test_oauth_headers.py or similar) and keeping this file focused on Environment.detect().
f9e7734 to
8a9c829
Compare
|
Fixed on 1.20.0 via #1401 |
Related Issue
Resolve #886, ##414
Description
Strip leading and trailing whitespace from OAuth/common header values before sending requests. This fixes X-Msh-Os-Version when platform.version() includes trailing whitespace and prevents httpx.LocalProtocolError.
Observed in logs:
httpcore.LocalProtocolError: Illegal header value
b'#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC 'httpx.LocalProtocolError: Illegal header value
b'#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC 'openai.APIConnectionError: Connection error.
kosong.chat_provider.APIConnectionError: Connection error.
Checklist