Skip to content

fix: support UUID generation in insecure browser contexts - #5173

Merged
liruifengv merged 3 commits into
release/2026-11-20from
fix/no-crypto-random-uuid-lint
Nov 20, 2026
Merged

liruifengv merged 3 commits into
release/2026-11-20from
fix/no-crypto-random-uuid-lint

Conversation

@kimi-agent-bot

@kimi-agent-bot kimi-agent-bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

通过局域网 HTTP 访问 CLI Web 时,Chrome 将页面视为非安全上下文,crypto.randomUUID 不可用,空会话的 Composer 会完全消失。这个 PR 让 UUID 生成在安全与非安全上下文都可用,并新增 lint 防护,避免同类调用再次进入仓库。

Bug Reproduction Steps

  1. 启动 CLI 2.7.0 Web,并通过 http://<局域网 IP>:<端口> 访问,而不是 localhost 或 HTTPS。
  2. 打开空会话页面,确认 window.isSecureContext === false 且 typeof crypto.randomUUID === 'undefined'。
  3. 页面只剩 Logo 和项目入口,没有 Composer;控制台出现 TypeError: crypto.randomUUID is not a function,调用栈经过菜单注册和 Composer setup。

Root Cause

browserOverlayMenu 在注册菜单时无条件调用 crypto.randomUUID()。Composer setup 注册权限菜单和模型菜单时进入该路径,异常使组件挂载失败。crypto.randomUUID() 只在安全上下文可用,但代码把它当成了所有浏览器环境都提供的能力。

这是根本修复:统一使用仓库已有的 [email protected]。原生 randomUUID 存在时继续走原生路径,否则通过 crypto.getRandomValues 生成 RFC 4122 UUID v4;同时用 lint 禁止新的直接调用,而不是只修一个报错点。

Code Changes

核心路径如下,调用方不再自行判断运行环境,也不使用 Math.random 或时间戳充当随机源:

flowchart LR
    A[UI、VS Code、协议解码调用方] --> B[uuid.v4]
    B --> C{原生 randomUUID 可用?}
    C -->|是| D[使用原生 UUID]
    C -->|否| E[getRandomValues 生成 UUID v4]
Loading
决策 选择 没选 理由
UUID 实现 复用现有 [email protected] 新写共享随机算法或新增工作区包 仓库已有依赖、浏览器/Node 分支和降级测试,改动更小且可审计
迁移范围 29 处生产直接调用、2 处自制降级、1 处测试调用 机械替换全部 node:crypto 用法 只处理跨端问题;Node 内置 randomUUID 不受浏览器安全上下文影响
防回退 新增 kimi/no-crypto-random-uuid AST 规则 只靠人工评审或文本搜索 覆盖普通调用、可选链、计算属性、解构和 Vue script,同时放行 node:crypto 与 uuid.v4
回归验证 菜单单元回归 + 真实 HTTP 浏览器验证 只验证 import 能解析 直接覆盖本次 Composer 消失的真实入口

建议阅读顺序:

  1. scripts/no-crypto-random-uuid-lint-plugin.mjs:看禁止和放行的边界。
  2. apps/desktop/src/renderer/lib/browserOverlayMenu.ts 及其测试:看本次故障点和无原生 randomUUID 的回归。
  3. apps/desktop/src/shared/browser.ts、apps/desktop/src/renderer/api/config.ts:看两处原有自制降级如何统一到 CSPRNG。
  4. packages/core/src/llm/builtin/protocol/*/decode.ts:看上游已有 tool call ID 仍优先,只有缺失时才生成。
  5. 其余 VS Code 与 app-* 文件均为相同的机械迁移;pnpm-lock.yaml 只补齐直接依赖。

Screenshots / Recordings

同一个非安全 HTTP 场景中,修复前只有 Logo 和项目入口,修复后 Composer 正常显示;两图均来自真实发布/开发产物运行。

Before After
修复前 Composer 消失 修复后 Composer 恢复

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
非安全 HTTP 中的 Web 初始化和菜单注册 抛出 randomUUID is not a function,Composer 可能消失 通过 CSPRNG 生成 UUID,Composer 和菜单正常工作 使用局域网 IP 访问 CLI Web 的用户 原路径已修复;也可使用 localhost 或 HTTPS
安全上下文中的 UUID 生成 直接调用原生 crypto.randomUUID() 仍由 uuid 库优先调用原生 randomUUID Desktop、localhost/HTTPS Web、VS Code 与协议解码 无行为变化
createBrowserId 与 Web client ID 的降级 分别使用 Date.now + 计数器、Math.random 统一使用 CSPRNG 生成的 UUID v4 非安全上下文中的新 browser/client ID 不改变 ID 前缀和持久化方式;无需迁移
协议事件已有 ID 保留上游 ID,缺失时才生成 保留相同优先级和复合 ID 格式 OpenAI、OpenAI Responses、Anthropic、Google GenAI 的流式响应 无行为变化,由 core 测试覆盖
Node 内置 randomUUID node:crypto 直接导入 保持不变 CLI、服务端、Desktop main 等 Node 路径 无行为变化
新增直接 crypto.randomUUID 调用 只能依赖人工发现 pnpm lint 报错并要求使用 uuid.v4 后续开发者和 CI 使用 import { v4 as randomUUID } from 'uuid'

受影响模块与覆盖:Desktop/Web 菜单由 browser-overlay-menu.test.ts 覆盖;app-composer/app-components 由对应包测试覆盖;VS Code 由扩展与 webview 构建、类型检查和目标测试覆盖;四种协议由 core 测试覆盖;lint 规则由独立 Node 测试覆盖。

实际验证结果
  • pnpm lint:通过,包含新规则及配置守卫。
  • lint 检查测试:41/41 通过,覆盖直接调用、可选链、计算属性、解构、Vue、Node/uuid/自定义对象及 Node namespace/局部绑定放行。
  • Desktop 与工作区测试:239/239 通过。
  • app-composer/app-components:1515/1515 通过。
  • @moonshot-ai/core:164/164 通过。
  • Desktop、VS Code、core 类型检查:通过;sherif:通过。
  • Desktop tsdown、VS Code extension/webview 构建、CLI sync:web:通过。
  • 真实 Chrome + 局域网 HTTP:isSecureContext=false、crypto.randomUUID === undefined,Composer 正常显示且控制台无相关错误。
  • Windows 专项真机测试:未运行;由 PR CI 继续验证。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue or explained why none is needed: this PR comes directly from a reproducible Web bug report.
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fa515a2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@moonshot-ai/core Patch
@moonshot-ai/app-composer Patch
@moonshot-ai/app-components Patch
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T05:02:27.018756Z fa515a2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@fa515a2
npx https://pkg.pr.new/@moonshot-ai/kimi-code@fa515a2

commit: fa515a2

@github-actions

Copy link
Copy Markdown
Contributor

❌ Nix build failed

Hash mismatch in pnpmDeps:

Hash
specified sha256-JiwS/wfwbQxWy0sR/AauFxE5aYOOCINqONAux2fU++8=
got sha256-UdhZRvMdn3Js0zHmCKtRE+2gA0IqkGeH714h00HVCGg=

Please update flake.nix with the got hash.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3642d89dc1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +23 to +24
if (target.type === 'Identifier') return target.name === 'crypto';
return target.type === 'MemberExpression' && memberName(target) === 'crypto' && isGlobalObject(target.object);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resolve the global binding before rejecting crypto

When Node code uses the common namespace form import * as crypto from 'node:crypto'; crypto.randomUUID(), this predicate classifies the imported identifier as the browser global and fails pnpm lint, despite the rule intentionally allowing Node crypto. Conversely, browser spellings such as self.crypto.randomUUID() evade the guard. Determine whether crypto resolves to the global binding (and recognize browser global aliases) rather than relying only on the identifier text.

Useful? React with 👍 / 👎.

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa515a2779

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv

Copy link
Copy Markdown
Collaborator

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: fa515a2779

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv
liruifengv merged commit 7b20daf into release/2026-11-20 Nov 20, 2026
29 of 30 checks passed
@liruifengv
liruifengv deleted the fix/no-crypto-random-uuid-lint branch November 20, 2026 05:03
Grapedge added a commit that referenced this pull request Nov 23, 2026
* fix(core): add Claude 5 output token ceilings (#5096)

* fix(core): add Claude 5 output token ceilings

* chore: add Claude 5 output ceiling changeset

* fix(markdown): keep table actions inside clipped containers (#5098)

* fix(markdown): keep table actions inside clipped containers

* fix(markdown): observe table action clipping ancestors

---------

Co-authored-by: kimi-agent-bot <[email protected]>

* fix(hosts): keep project lists usable with SSH hosts (#5102)

* ci: release packages (#5094)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix: preserve first prompt through project creation (#5161)

Co-authored-by: kimi-agent-bot <[email protected]>

* fix(agent-core-v2): align fork slicing with restored turn ids (#5165)

* fix(agent-core-v2): align fork slicing with restored turn ids

* chore: add restored fork fix changeset

* fix(core): prevent AJV diagnostics from polluting TUI input (#5168)

* refactor(core): route AJV logs through utils logger

* chore(core): add AJV logger changeset

* chore(core): clarify AJV logger changeset

* fix: support UUID generation in insecure browser contexts (#5173)

* fix: support UUID generation in insecure browser contexts

* chore: update pnpm deps hash

* fix: resolve crypto bindings in UUID lint rule

---------

Co-authored-by: kimi-agent-bot <[email protected]>

* fix(client): keep draft thinking effort after sending (#5170)

* fix(client): keep draft thinking effort after sending

* chore: add cli changeset for draft thinking fix

* test(client): use function stub for websocket in thinking test

* fix(client): preserve thinking effort during model normalization

* fix(client): reconcile profile updates and model fallbacks

* fix(client): scope draft thinking preservation

* fix(tui): defer queued prompts until the loop drains (#5166)

* fix(tui): defer queued prompts until the loop drains

- add core and v2 drained/idle lifecycle events
- consume reminder-only queues without starting a model turn
- gate TUI queue dispatch and completion notifications on lifecycle state
- document notification-handoff queue behavior and add a CLI patch changeset

* fix(tui): close loop lifecycle races

- emit idle only after the current macro is revalidated
- keep queued input pending through compaction and delayed dispatch
- reject stale idle after new work starts

* fix(loop): correlate lifecycle events by generation

- track macro generations in the core and v2 loop state
- replace TUI macro inference with event-generation correlation
- preserve compaction guards for queued dispatch

* fix(desktop): simplify multi-select folder picker actions (#5174)

* fix(kap-server): report :wait settled only after the session is on disk (#5176)

:wait settled as soon as the turn left the in-memory activity view, but
the engine writes wire records through a batched log that flushes in a
later task. A server killed right after :wait reported idle could lose
the end of the finished turn.

Before it replies with a settled session, :wait now flushes the wire of
every agent in the session and drains pending metadata writes. A failed
write surfaces as an error instead of a settled reply. The KAP guide
states the guarantee. A new test in sessions.test.ts slows down the disk
write of the turn end and checks that the record is on disk when :wait
returns.

(cherry picked from commit fc33c6d)

Co-authored-by: kai <[email protected]>

* ci: release packages (#5163)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix(client): keep config-derived draft thinking effort after sending (#5191)

Co-authored-by: liruifengv <[email protected]>

* ci: release packages (#5192)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix(kap-server): resume sessions with unavailable models (#5212)

* fix(kap-server): resume sessions with unavailable models

* fix: let users replace unavailable session models

* fix: scope unavailable model recovery state

---------

Co-authored-by: kimi-agent-bot <[email protected]>

* fix(plugins): detect MCP OAuth with targeted probes (#5197)

* fix(plugins): detect MCP OAuth with targeted probes

* fix(plugins): use the MCP inspect route

* fix(plugins): preserve OAuth requirements across inspection and disconnect

* fix(chat): show pending MCP browser authorization (#5246)

* fix(chat): show pending MCP browser authorization

* fix(chat): resolve MCP authorization lint findings

* fix(chat): preserve MCP authorization failure details

* fix(mcp): preserve cancellation during OAuth reconnect

* fix(desktop): cache feedback form per host and let reply reasons wrap (#5303)

* ci: release packages (#5232)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* test: port removed-model recovery coverage to testkit

* test: dedupe anthropic output ceiling cases

---------

Co-authored-by: Haozhe <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: 7Sageer <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: liruifengv <[email protected]>
Co-authored-by: qer <[email protected]>
Co-authored-by: kai <[email protected]>
Co-authored-by: Luyu Cheng <[email protected]>
Co-authored-by: grapedge <[email protected]>
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.

4 participants