Skip to content

feat(cosh): add secret redaction for model output and tool results#100

Merged
samchu-zsl merged 2 commits intoalibaba:mainfrom
kongche-jbw:fix/cosh/api-sk-leakage
Apr 7, 2026
Merged

feat(cosh): add secret redaction for model output and tool results#100
samchu-zsl merged 2 commits intoalibaba:mainfrom
kongche-jbw:fix/cosh/api-sk-leakage

Conversation

@kongche-jbw
Copy link
Copy Markdown
Collaborator

@kongche-jbw kongche-jbw commented Apr 5, 2026

Description

Add a secret redaction layer to prevent sensitive credentials from being displayed in the terminal. Introduces secretRedactor.ts with pattern-based detection for common key formats (OpenAI sk-, Anthropic ant-, Alibaba Cloud LTAI/accessKeySecret, Bearer tokens, generic apiKey/password fields, and environment variable assignments).

The redaction is applied at two interception points:

  1. Streamed model output (useGeminiStream.ts): each stream chunk is accumulated into a raw turn buffer and redacted against the full buffer, fixing a streaming-split vulnerability where a secret spanning multiple chunks could partially leak.
  2. Tool execution results (coreToolScheduler.ts): llmContent, returnDisplay (string and ANSI variants) are redacted before further processing.

A Secret Protection rule is also added to the system prompt to instruct the model to refuse revealing secrets at the source.

Related Issue

fixes #83
closes #83

Type of Change

  • New feature (non-breaking change that adds functionality)
  • Bug fix (non-breaking change that fixes an issue)

Scope

  • cosh (copilot-shell)

Checklist

  • I have read the Contributing Guide
  • My code follows the project's code style
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly
  • For cosh: Lint passes, type check passes, and tests pass
  • For agent-sec-core (Rust): cargo clippy -- -D warnings and cargo fmt --check pass
  • For agent-sec-core (Python): Ruff format and pytest pass
  • For os-skills: Skill directory structure is valid and shell scripts pass syntax check
  • Lock files are up to date (package-lock.json / Cargo.lock)

Testing

# Unit tests for redaction patterns
cd src/copilot-shell/packages/core
npx vitest run src/utils/secretRedactor.test.ts

# Snapshot tests for system prompt
npx vitest run src/core/prompts.test.ts

# Manual test: ask the model to output a secret key pattern
# e.g. "请帮我 cat xxx.settiing.json"
# Expected terminal output: sk-********************

@kongche-jbw kongche-jbw self-assigned this Apr 5, 2026
@kongche-jbw kongche-jbw requested a review from samchu-zsl as a code owner April 5, 2026 16:47
@kongche-jbw kongche-jbw added the component:cosh src/copilot-shell/ label Apr 5, 2026
@casparant casparant modified the milestones: sec-core/v0.2, cosh/v2.0.3 Apr 5, 2026
@kongche-jbw kongche-jbw requested a review from casparant April 5, 2026 16:50
@kongche-jbw
Copy link
Copy Markdown
Collaborator Author

image

@kongche-jbw kongche-jbw force-pushed the fix/cosh/api-sk-leakage branch from b7a1695 to 91cd579 Compare April 5, 2026 16:51
Copy link
Copy Markdown
Collaborator

@samchu-zsl samchu-zsl left a comment

Choose a reason for hiding this comment

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

Thanks for the fix. I have a remaining concern.

Since secrets are redacted from the LLM's stream output, could this affect tool calls? Say, if I'm installing Claude Code and provide an API key, would that key be redacted before the write tool is called?

@kongche-jbw
Copy link
Copy Markdown
Collaborator Author

The redaction is purely a terminal display concern — it never mutates tool parameters or blocks writes.

Copy link
Copy Markdown
Collaborator

@samchu-zsl samchu-zsl left a comment

Choose a reason for hiding this comment

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

All concerns addressed. LGTM. Thanks!

- Add secretRedactor.ts with regex patterns for sk-, ant-, LTAI, Bearer, accessKeySecret, env vars
- Apply redactSecrets() to streamed model output
- Fix streaming split vulnerability: accumulate raw turn buffer and redact full content per chunk
- Apply redactPartListUnion/redactAnsiOutput to tool results in coreToolScheduler.ts
- Add Secret Protection instruction to system prompt in prompts.ts
- Extend returnDisplay redaction to cover FileDiff type (WriteFile / EditFile)
- Redact fileDiff, newContent, and originalContent fields to prevent
  secrets from appearing in post-execution diff summaries
@kongche-jbw kongche-jbw force-pushed the fix/cosh/api-sk-leakage branch from 91cd579 to abf76f1 Compare April 7, 2026 06:56
@samchu-zsl samchu-zsl merged commit ce08935 into alibaba:main Apr 7, 2026
9 checks passed
@kongche-jbw kongche-jbw mentioned this pull request Apr 8, 2026
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:cosh src/copilot-shell/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(cosh): 当前cosh有api sk泄漏风险

3 participants