Skip to content

fix(ai): stop API keys leaking into AI context + remove key CLI flags#113

Merged
dubscode merged 2 commits into
mainfrom
feature/dub-85-api-keys-leak-into-ai-context-modern-sk-proj-sk-ant-formats
May 29, 2026
Merged

fix(ai): stop API keys leaking into AI context + remove key CLI flags#113
dubscode merged 2 commits into
mainfrom
feature/dub-85-api-keys-leak-into-ai-context-modern-sk-proj-sk-ant-formats

Conversation

@dubscode

Copy link
Copy Markdown
Contributor

Summary

Closes DUB-85.

API keys could leak verbatim into the AI assistant's context packet (and be sent to the model). Reproduced during local setup: after dub ai env --openai-key sk-proj-…, the assistant itself warned the key was in command history. This PR closes the leak at both layers and removes the root exposure.

1. Redaction gap (fix(ai): redact modern sk-proj-/sk-ant- API keys)

  • redactSensitiveText only matched plain-alphanumeric sk- keys, so modern formats containing -/_ (sk-proj-…, sk-ant-api03-…) slipped through into both recentShellHistory (.zsh_history) and recentHistory (history.jsonl). Broadened the pattern to \bsk-[A-Za-z0-9_-]{12,}.
  • Added --openai-key (spaced + = forms) to sanitizeCommandArgs REDACTED_ARGS, which previously only listed gemini/anthropic/gateway.

2. Root cause — keys as CLI args (fix(ai)!: stop accepting API keys as CLI flags, BREAKING)

  • Passing a secret in argv exposes it in the OS process list and writes it to shell history before dub ever sees it — exposure dub cannot redact. dub ai env now rejects --gemini-key/--anthropic-key/--gateway-key/--openai-key with a helpful error pointing to the masked dub ai setup wizard or a pre-set DUBSTACK_*_API_KEY env var.
  • Safe to remove: at runtime dub reads keys only from DUBSTACK_*_API_KEY env vars (ai-provider.ts:104), so the flag was just a convenience for writing the export line. Non-interactive/CI sets the env var directly instead.
  • The interactive dub ai setup wizard is unaffected (masked password prompt, never echoed/stored).
  • Docs (README, QUICKSTART, apps/docs), skills, and provider/env error hints updated to recommend the wizard / env var.

BREAKING CHANGE

dub ai env no longer accepts --gemini-key, --anthropic-key, --gateway-key, or --openai-key. Use dub ai setup (masked prompt) or export the DUBSTACK_*_API_KEY env var.

Test plan

  • pnpm checks (biome) clean
  • pnpm typecheck passes
  • pnpm test — 1448 tests pass
  • New regression tests: modern sk-proj-/sk-ant- redaction, --openai-key arg redaction (both forms), short sk- branch names NOT over-redacted, and assertNoApiKeyFlags (throws + lists offending flags, no-op when absent)
  • Manual: dub ai env --openai-key sk-… shows the rejection error; dub ai setup still configures a key

Note for users

Keys already typed as CLI args remain in .zsh_history in cleartext — rotate them.

Copilot AI review requested due to automatic review settings May 29, 2026 04:15
@vercel

vercel Bot commented May 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
dubstack Ready Ready Preview, Comment May 29, 2026 4:51am

@github-actions

github-actions Bot commented May 29, 2026

Copy link
Copy Markdown

DubStack AI evals

Cheap-model eval status: skipped: DUBSTACK_GEMINI_API_KEY is not configured

Report artifact: workflow run

Copilot AI 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.

Pull request overview

Closes a leak where modern API key formats (sk-proj-…, sk-ant-api03-…) could slip past redactSensitiveText into AI context (shell + dub history) and removes the root cause by rejecting API-key CLI flags on dub ai env. Users are directed to the masked dub ai setup wizard or pre-set DUBSTACK_*_API_KEY env vars.

Changes:

  • Broaden the sk- redaction regex to cover key bodies containing -/_, and add --openai-key to the arg redaction set.
  • Convert --gemini-key/--anthropic-key/--gateway-key/--openai-key on dub ai env to hidden options that assertNoApiKeyFlags rejects with a helpful DubError; remove key fields from the action's call to configureAiEnv.
  • Update README, QUICKSTART, docs, skills, and provider error hints to recommend dub ai setup or direct env-var exports.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/cli/src/lib/history.ts Add --openai-key redaction (both forms); broaden sk- regex to [A-Za-z0-9_-]{12,}
packages/cli/src/lib/history.test.ts Tests for --openai-key arg redaction, modern key formats, and short sk- branch names
packages/cli/src/index.ts Convert key flags to hidden Options; call assertNoApiKeyFlags and stop forwarding keys to configureAiEnv
packages/cli/src/commands/ai-env.ts New assertNoApiKeyFlags helper + updated "provide at least one" hint
packages/cli/src/commands/ai-env.test.ts Tests for assertNoApiKeyFlags (single, multiple, none)
packages/cli/src/lib/ai-provider.ts Update no-provider / missing-key recovery hints to recommend dub ai setup or env vars
README.md / QUICKSTART.md / apps/docs/**/index.mdx / ai-assistant.mdx Replace key-flag examples with dub ai setup / env-var guidance
skills/dubstack/SKILL.md, .agents/skills/dubstack/SKILL.md Same skill guidance update for the assistant

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

dubscode added 2 commits May 28, 2026 21:49
redactSensitiveText only matched plain-alphanumeric sk- keys, so OpenAI
project keys (sk-proj-…) and Anthropic keys (sk-ant-…) — which contain
'-'/'_' — slipped into recentShellHistory and history.jsonl. Broaden the
pattern to include '-'/'_', and add --openai-key (spaced + = forms) to
sanitizeCommandArgs so dub's own command history is redacted too.

Closes DUB-85
Passing a key to `dub ai env --openai-key …` (and --gemini-key,
--anthropic-key, --gateway-key) leaks it into shell history and the OS
process list — exposure dub cannot redact. Reject those flags with a
helpful error pointing to the masked `dub ai setup` wizard or a pre-set
DUBSTACK_*_API_KEY env var. Same recommendation scrubbed from docs,
skills, and the provider/env error hints that previously suggested them.

BREAKING CHANGE: `dub ai env` no longer accepts --gemini-key,
--anthropic-key, --gateway-key, or --openai-key. Use `dub ai setup`
(masked prompt) or export the DUBSTACK_*_API_KEY env var instead.

Closes DUB-85
@dubscode dubscode force-pushed the feature/dub-85-api-keys-leak-into-ai-context-modern-sk-proj-sk-ant-formats branch from 2ea32c5 to 5b422a6 Compare May 29, 2026 04:51
@dubscode dubscode merged commit f74cc06 into main May 29, 2026
14 checks passed
@dubscode dubscode deleted the feature/dub-85-api-keys-leak-into-ai-context-modern-sk-proj-sk-ant-formats branch May 29, 2026 04:59
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants