-
Notifications
You must be signed in to change notification settings - Fork 826
由于系统内核中变量问题,导致kimi cli整个失效(kimi cli未作防御性清洗,导致整个服务失效) || Due to variable problems in the system kernel, kimi cli failed entirely (kimi The cli was not cleaned defensively, causing the entire service to fail) #1321
Description
What version of Kimi Code CLI is running?
kimi, version 1.16.0
Which open platform/subscription were you using?
/setup
Which model were you using?
k2.5
What platform is your computer?
Linux 6.8.0-101-generic x86_64 x86_64
What issue are you seeing?
claude code 通过~/.kimi/logs/kimi.log 排查结果
核心错误
错误类型: httpcore.LocalProtocolError: Illegal header value
错误位置:
- 文件: /home/ming/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/kosong/chat_provider/kimi.py:165
- 调用链: kimi_cli → kosong.chat_provider.kimi.Kimi.generate() → OpenAI API → httpx 请求
具体原因
非法的 HTTP 请求头值被发送到 Moonshot API:
b'#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC '
这看起来像是Linux 内核版本信息被错误地设置为 HTTP 请求头。
What steps can reproduce the bug?
#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC
header里面拿内核信息的时候,没有做异常字符串的处理
What is the expected behavior?
这里给出临时修复方案
~/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/kosong/chat_provider/kimi.py 165行开始
# 原版(已注释)
# self._client_kwargs: dict[str, Any] = dict(client_kwargs)
self._client_kwargs: dict[str, Any] = copy.deepcopy(dict(client_kwargs))
# 2. 清理 default_headers 中的非法字符(如换行符、回车符)
# 这是解决 httpcore.LocalProtocolError: Illegal header value 的关键
default_headers = self._client_kwargs.get("default_headers", {})
cleaned_headers: dict[str, str] = {}
for key, value in default_headers.items():
if isinstance(value, str):
# 移除所有控制字符(特别是 \n 和 \r),并去除首尾空格
# 内核版本字符串有时会带有换行符导致报错
cleaned_value = value.replace("\n", "").replace("\r", "").strip()
cleaned_headers[key] = cleaned_value
else:
cleaned_headers[key] = str(value)
# 3. 强制设置 User-Agent,覆盖底层库可能自动添加的包含内核版本的字符串
# 这是为了防止 create_openai_client 内部使用 platform.uname() 注入非法值
cleaned_headers.setdefault("User-Agent", "kosong-kimi-cli/1.0")
self._client_kwargs["default_headers"] = cleaned_headers
# 4. 创建客户端
self.client: AsyncOpenAI = create_openai_client(
api_key=self._api_key,
base_url=self._base_url,
client_kwargs=self._client_kwargs,
)
"""The underlying `AsyncOpenAI` client."""
self._generation_kwargs: Kimi.GenerationKwargs = {}
Additional information
一直让排查变量信息的污染问题,但是并没有找到这个变量信息污染的源头,目前有三台设备在使用kimi cli,这台设备报错了,可能是内核的变量有问题,少见的case,希望官方在下个版本中做好防御性的清洗,这个报错直接导致kimi cli无法使用,多次重装都无法使用,卡了很久
What version of Kimi Code CLI is running?
kimi, version 1.16.0
Which open platform/subscription were you using?
/setup
Which model were you using?
k2.5
What platform is your computer?
Linux 6.8.0-101-generic x86_64 x86_64
What issue are you seeing?
claude code Check the results through ~/.kimi/logs/kimi.log
core error
Error type: httpcore.LocalProtocolError: Illegal header value
Error location:
- File: /home/ming/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/kosong/chat_provider/kimi.py:165
- Call chain: kimi_cli → kosong.chat_provider.kimi.Kimi.generate() → OpenAI API → httpx request
specific reasons
Illegal HTTP request header value sent to Moonshot API:
b'#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC '
This looks like the Linux kernel version information is incorrectly set as an HTTP request header.
What steps can reproduce the bug?
#101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC
When getting the kernel information in the header, no exception string processing is done.
What is the expected behavior?
Here is a temporary fix
~/.local/share/uv/tools/kimi-cli/lib/python3.13/site-packages/kosong/chat_provider/kimi.py starts at line 165
#Original (commented)
# self._client_kwargs: dict[str, Any] = dict(client_kwargs)
self._client_kwargs: dict[str, Any] = copy.deepcopy(dict(client_kwargs))
# 2. Clean up illegal characters (such as line feeds and carriage returns) in default_headers
# This is the key to solving httpcore.LocalProtocolError: Illegal header value
default_headers = self._client_kwargs.get("default_headers", {})
cleaned_headers: dict[str, str] = {}
for key, value in default_headers.items():
if isinstance(value, str):
# Remove all control characters (especially \n and \r) and strip leading and trailing spaces
# The kernel version string sometimes contains newlines, causing errors.
cleaned_value = value.replace("\n", "").replace("\r", "").strip()
cleaned_headers[key] = cleaned_value
else:
cleaned_headers[key] = str(value)
# 3. Forcibly set User-Agent, overwriting the string containing the kernel version that may be automatically added by the underlying library.
# This is to prevent create_openai_client from using platform.uname() internally to inject illegal values.
cleaned_headers.setdefault("User-Agent", "kosong-kimi-cli/1.0")
self._client_kwargs["default_headers"] = cleaned_headers
# 4. Create client
self.client: AsyncOpenAI = create_openai_client(
api_key=self._api_key,
base_url=self._base_url,
client_kwargs=self._client_kwargs,
)
"""The underlying `AsyncOpenAI` client."""
self._generation_kwargs: Kimi.GenerationKwargs = {}
Additional information
I have been investigating the pollution problem of variable information, but the source of this variable information pollution has not been found. Currently, three devices are using kimi cli. This device reported an error. It may be that there is a problem with the kernel variables. It is a rare case. I hope the official will do a defensive cleaning in the next version. This error directly caused kimi cli to be unusable, and it was unusable after multiple reinstalls.