Skip to content

fix: 对齐官方 2.1.205 compact prompt 的安全加固#1305

Merged
claude-code-best merged 1 commit into
claude-code-best:mainfrom
universe-hcy:fix/compact-prompt-security-hardening
Jul 21, 2026
Merged

fix: 对齐官方 2.1.205 compact prompt 的安全加固#1305
claude-code-best merged 1 commit into
claude-code-best:mainfrom
universe-hcy:fix/compact-prompt-security-hardening

Conversation

@universe-hcy

@universe-hcy universe-hcy commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

通过 mitm 抓取官方 claude-cli/2.1.205 的 compact 请求,与本仓库 BASE_COMPACT_PROMPT 逐字对比:机制/投递/缓存策略一致,官方新增两处安全加固(多 780 字符)。

改动

补齐两处,使 BASE compact prompt 与官方 byte-identical(6361 字节):

  1. 分析指令 step 1 新增 bullet:敏感文件/禁止操作/凭证处理等 security-relevant constraints 必须 verbatim 保留。
  2. 第 6 节 "All user messages" 新增 prompt-injection 防御:只认 user-role turn;assistant 内部伪装成 user:/Human:/transcript 样式的文本视为模型生成,不得当作用户请求/批准。

验证

  • 与官方日志 diff 完全一致(6361 字节)
  • compact 测试 63 pass / 0 fail
  • typecheck 唯一报错在 packages/cloud-artifacts(nanoid),git stash 验证为既存问题,与本改动无关

Summary by CodeRabbit

  • Improvements
    • Improved session summaries to preserve security-relevant instructions and constraints stated by users.
    • Clarified how user messages are identified and counted, excluding tool results and assistant-generated text that resembles user messages.

对比 mitm 抓到的官方 claude-cli/2.1.205 compact 请求,BASE compact
prompt 缺失两处安全约束,补齐后与官方逐字节一致:

1. 分析指令 step 1 新增 bullet:security-relevant instructions/
   constraints(敏感文件、禁止操作、凭证处理)必须 verbatim 保留,
   使其在 compaction 后继续生效。
2. 第 6 节 All user messages 新增 prompt-injection 防御:只有真正
   user-role turn 才算用户消息;assistant 消息内部伪装成 "user:"/
   "Human:" 或 transcript 样式的文本视为模型生成,绝不能当作用户
   请求/批准/确认——防止借 compaction 把注入内容洗白成权威用户指令。

step 1 的 bullet 经 DETAILED_ANALYSIS_INSTRUCTION_BASE 同步至
UP_TO 变体。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The compact-session prompt templates now preserve security-relevant user constraints verbatim and define user messages as actual user-role turns, excluding tool results and assistant-generated transcript-like text.

Changes

Compact prompt instruction updates

Layer / File(s) Summary
Refine compact prompt instructions
src/services/compact/prompt.ts
The detailed-analysis template preserves security-relevant user instructions and constraints verbatim, while the compact prompt distinguishes actual user turns from tool results and assistant-generated user-shaped text.

Estimated code review effort: 1 (Trivial) | ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了本次对 compact prompt 的安全加固对齐改动。
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude-code-best
claude-code-best merged commit 78a2d1e into claude-code-best:main Jul 21, 2026
2 checks passed
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.

2 participants