Skip to content

修复:sessionStorage 二进制控制字符 + normalizeMessagesForAPI 数组保护#594

Open
oadank wants to merge 2 commits into
NanmiCoder:mainfrom
oadank:pr/upstream-fixes
Open

修复:sessionStorage 二进制控制字符 + normalizeMessagesForAPI 数组保护#594
oadank wants to merge 2 commits into
NanmiCoder:mainfrom
oadank:pr/upstream-fixes

Conversation

@oadank
Copy link
Copy Markdown

@oadank oadank commented May 24, 2026

问题描述

问题 1:二进制输出破坏 JSONL session log

  • 执行 cat /usr/bin/ls 等命令时,二进制输出(含 null bytes)进入 Bash tool stdout
  • null bytes 导致 JSONL session log 解析失败,后续会话恢复时报错

问题 2:LiteLLM 错误响应导致 CLI 崩溃

  • LiteLLM 代理返回错误响应时,message.message.content 可能不是数组(字符串或对象)
  • normalizeMessagesForAPI 直接调用 .map() 导致 TypeError: content.map is not a function

修复内容

Commit 1:fix(sessionStorage): sanitize binary control chars from tool output

  • 新增 sanitizeJsonlReplacer 函数,在 JSON.stringify 时过滤控制字符
  • -�, , , �-� 替换为 [U+XXXX] 文本
  • 保留 , , (正常换行/制表符)
  • 附带完整单元测试(含真实 ELF 二进制测试)

Commit 2:fix(messages): guard against non-array content in normalizeMessagesForAPI

  • normalizeMessagesForAPI 处理 assistant 消息时加 Array.isArray 检查
  • 非数组时用空数组 [] 兜底,避免 .map() 崩溃
  • 同步修复 stripCallerFieldFromAssistantMessage 里的同类问题

测试情况

  • 已在本地运行验证,session log 正常写入、会话恢复无报错
  • 修复后飞书 Claude 桥接稳定运行,不再出现 content.map is not a function 错误

合并建议

两个修复独立、无依赖关系,可以一起合并或分开审查。

Claude Code and others added 2 commits May 24, 2026 15:45
Prevent null bytes (e.g. from `cat /usr/bin/ls`) from corrupting
JSONL session logs by filtering control characters via a JSON.stringify
replacer. Replaces \x00-\x08, \x0b, \x0c, \x0e-\x1f with safe [U+XXXX]
text while preserving \n, \r, \t.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels May 24, 2026
@github-actions
Copy link
Copy Markdown

PR quality triage

Changed areas: area:cli-core

CLI core policy: Blocked by policy until a maintainer applies allow-cli-core-change and approves the PR.

Missing-test policy: No missing-test policy block detected.

Coverage baseline policy: No coverage-baseline policy block detected.

CLI core files:

  • src/utils/__tests__/sessionStorageSanitizer.test.ts
  • src/utils/messages.ts
  • src/utils/sessionStorage.ts

Coverage policy files:

  • none

Expected checks:

  • change-policy
  • server-checks
  • coverage-checks

Test coverage signals:

  • Agent/model runtime path changed: use mock/request-shape tests in PR and maintainer live-model smoke before release.

Risk notes:

  • No special risk notes from changed paths.

Hard merge gates still come from GitHub Actions, not AI review.

Dosu handoff: Dosu can be used as the AI reviewer for risk explanation, missing-test prompts, and maintainer Q&A. If it does not comment automatically from the PR template, ask:

@dosubot review this PR for changed-area risk, missing tests, docs impact, desktop startup risk, and CLI core impact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli-core bug Something isn't working needs-maintainer-approval size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant