[Bug] kimi web 拒绝带方括号的 IPv6 Host 头([2409:…]:port 返回 403),IPv6 访问不可用

Open Beginner friendly
#5,539 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript
Domain
backend, security

Research direction

Start with packages/kap-server/src/middleware/hostnames.ts, especially stripPort() and isAllowedHost(), then check how the hostname tests cover bracketed IPv6 addresses. Reproduce the reported case with [2409:893d:7a39:2564::1]:58628 and compare it with the unbracketed form. Done means bracketed IPv6 hosts are accepted while the existing host allowlist behavior remains covered by tests.

Written by the indexing model from the issue text.

Description

你运行的 Kimi Code 版本是?

2.8.0

你使用的哪个开放平台/订阅?

第三方聚合网关提供的 Anthropic 兼容接口(ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN)。这一项与本问题无关——请求在 Host 校验阶段就被拒了,还没走到模型调用。

你使用的模型?

MiniMax M3.1 Flash(同样无关)

你的电脑平台是?

与平台无关。在仓库里直接调用 isAllowedHost() 就能复现(下面有输出),不需要特定系统。

你遇到了什么问题?

用 IPv6 地址访问 kimi web 时,请求被 Host 校验拒绝,返回 403:

$ curl -g -H "Host: [2409:893d:7a39:2564::1]:58628" http://127.0.0.1:58628/
{"code":40301,"msg":"Invalid Host header: [2409:893d:7a39:2564::1]; allow this host with
KIMI_CODE_ALLOWED_HOSTS=[2409:893d:7a39:2564::1] or 'kimi web --allowed-host [2409:893d:7a39:2564::1]'.","data":null,"request_id":"..."}

复现步骤?

  1. kimi web --host :: --no-open(绑定 IPv6,双栈)
  2. 用客户端访问 http://[2409:…]:58628/

浏览器发过来的 Host 头就是带方括号的 IPv6 字面量(RFC 3986/7230 要求 IPv6 字面量在 authority 里带方括号),因此每个请求都会 403。

也可以不启服务,直接调用仓库里的函数验证:

import { isAllowedHost, stripPort } from './packages/kap-server/src/middleware/hostnames';

isAllowedHost('[2409:893d:7a39:2564::1]:58628', {}); // false
isAllowedHost('2409:893d:7a39:2564::1', {});         // true

实际输出:

Host=[2409:893d:7a39:2564::1]:58628   stripPort=[2409:893d:7a39:2564::1]   allowed=false
Host=[2409:893d:7a39:2564::1]         stripPort=[2409:893d:7a39:2564::1]   allowed=false
Host=2409:893d:7a39:2564::1           stripPort=2409:893d:7a39:2564::1     allowed=true
Host=[::1]:58628                      stripPort=[::1]                      allowed=true
Host=10.141.197.199:58628             stripPort=10.141.197.199              allowed=true
Host=garbage                          stripPort=garbage                     allowed=false

期望的行为是什么?

带方括号的 IPv6 字面量应当被识别为合法 Host。目前只有 [::1] 因为写进了白名单特例才没被拒,其它 IPv6 地址一律 403。

补充信息?

问题出在 packages/kap-server/src/middleware/hostnames.ts:

  • stripPort() 对以 [ 开头的 Host 会保留方括号(return host.slice(0, end + 1).toLowerCase()),这是为了匹配白名单里的 [::1];
  • 但 isAllowedHost() 随后用 net.isIP(h) 判断是不是 IP 地址,而 net.isIP('[2409:893d:7a39:2564::1]') 返回 0(方括号不是合法 IP 文本),于是落到"不是 IP、不是 localhost、不在 extra 白名单" → 拒绝。

对照上面输出可以看出:同一个 IPv6 地址,带方括号被拒、不带方括号通过——stripPort() 保留方括号、isAllowedHost() 却按无括号形式判断,两边对不上。

影响范围(避免误解,这里说清楚):

场景 是否受影响
默认 --host 127.0.0.1 否(只有 IPv4 监听,不会产生 IPv6 连接)
--host 0.0.0.0(仅 IPv4) 否
--host ::(双栈)或 --host <IPv6>,客户端走 IPv6 是,403
用主机名访问(反向代理 / hosts) 403,但这是设计行为,用 --allowed-host 放行

建议的修法方向(以维护者判断为准):判断"是不是 IP 地址"时把方括号剥掉再交给 net.isIP(),同时保留 stripPort() 给白名单比较用的带括号形式;例如在 isAllowedHost() 里加一步对去括号形式的 net.isIP() 判断。

在修好之前可以用报错里提示的方式绕过:

kimi web --host :: --allowed-host "[2409:893d:7a39:2564::1]"

我可以自己提交修复此 bug 的 PR(请先等待维护者在本 issue 中批准)。

Dominant language
TypeScript
Stars
8.3k
Forks
1.4k
Avg merge
12h 6m
Merged PRs (30d)
596

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MoonshotAI/kimi-code

All issues in MoonshotAI/kimi-code

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.