-
Notifications
You must be signed in to change notification settings - Fork 831
fix(oauth): strip whitespace from HTTP header values on Linux #1401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||
| """Tests for _ascii_header_value and _common_headers in oauth module. | ||||||||||||
|
|
||||||||||||
| Regression tests for the issue where Linux kernel version strings containing | ||||||||||||
| trailing whitespace/newlines (e.g. platform.version() returning | ||||||||||||
| "#101-Ubuntu SMP ...\n") would be included in HTTP headers, causing | ||||||||||||
| connection errors. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| from unittest.mock import patch | ||||||||||||
|
|
||||||||||||
| from kimi_cli.auth.oauth import _ascii_header_value, _common_headers | ||||||||||||
|
|
||||||||||||
|
Comment on lines
+1
to
+12
|
||||||||||||
|
|
||||||||||||
| class TestAsciiHeaderValue: | ||||||||||||
| """Test cases for _ascii_header_value.""" | ||||||||||||
|
|
||||||||||||
| def test_plain_ascii(self) -> None: | ||||||||||||
| assert _ascii_header_value("hello") == "hello" | ||||||||||||
|
|
||||||||||||
| def test_strips_trailing_newline(self) -> None: | ||||||||||||
| """Regression: Linux platform.version() may contain trailing newline.""" | ||||||||||||
| assert _ascii_header_value("6.8.0-101\n") == "6.8.0-101" | ||||||||||||
|
|
||||||||||||
| def test_non_ascii_sanitized(self) -> None: | ||||||||||||
| assert _ascii_header_value("héllo") == "hllo" | ||||||||||||
|
|
||||||||||||
| def test_all_non_ascii_returns_fallback(self) -> None: | ||||||||||||
| assert _ascii_header_value("你好") == "unknown" | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class TestCommonHeaders: | ||||||||||||
| """Test that _common_headers returns clean header values.""" | ||||||||||||
|
|
||||||||||||
| @patch("kimi_cli.auth.oauth.platform") | ||||||||||||
| @patch("kimi_cli.auth.oauth.get_device_id", return_value="abc123") | ||||||||||||
| def test_no_whitespace_in_header_values(self, _mock_device_id, mock_platform) -> None: | ||||||||||||
| """All header values must be free of leading/trailing whitespace.""" | ||||||||||||
| mock_platform.node.return_value = "myhost" | ||||||||||||
| mock_platform.version.return_value = "#101-Ubuntu SMP\n" | ||||||||||||
|
||||||||||||
| mock_platform.version.return_value = "#101-Ubuntu SMP\n" | |
| mock_platform.version.return_value = "#101-Ubuntu SMP\n" | |
| mock_platform.system.return_value = "Linux" | |
| mock_platform.machine.return_value = "x86_64" | |
| mock_platform.release.return_value = "6.8.0-101" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ascii_header_value() now strips whitespace for ASCII-only values, but unlike the Unicode-sanitization path it does not fall back when the stripped result becomes empty (e.g., value is only whitespace). Consider returning
fallbackwhenvalue.strip()is empty as well, to keep behavior consistent and avoid emitting empty header values.