Skip to content

feat(chat): never send a message that holds an API key, and offer to take the key out (W4-05) - #2020

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-key-paste-guard
Nov 3, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-key-paste-guard

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

A message that holds an API key is never sent to Chat (CHAT-16, Wave 4 plan W4-05). A pasted key would otherwise reach the model, the chat's history and the run's records.

  • Composer: text with something shaped like a key (sk-ant-…, sk-or-v1-…, sk-proj-…, sk-…, AIza…) shows one line under the box: "This looks like an API key. Save it securely instead of sending it?". Send and Enter wait, steering too.
    • Remove takes every key out and keeps the rest of the message.
    • Save securely shows only where the page passes onSaveKey. That is W4-06's key form (Enterprise, Admins), so for now only Remove shows.
  • Server: POST /api/chat/runs (message) and POST /api/chat/runs/{id}/answer (other) refuse such text with 400 api_key before the engine sees it. The text is never logged.
  • Shapes are strict enough that ordinary words and slugs don't match (sk-learn, /sk-guide-to-ai-search-in-2026): a plain sk- key needs 20+ characters with a digit and a capital letter, and OpenRouter's is hex.
  • Help: a short "An API key in your message" section in help/chat/chat.md (approved: false).

Behind CHAT_AGENTIC like the rest of Chat; no migration.

Related issue

None. Chat Wave 4 plan, W4-05.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Open Chat and paste a made-up key such as sk-proj- followed by 40 random letters and digits after some text.
  2. The amber line shows and Send is greyed out; Enter does nothing.
  3. Click Remove: the key is gone, the rest of the text stays, and the message sends.
  4. curl the route with a key in message (signed in): 400 with code: "api_key".
  5. cd web && yarn vitest run src/lib/chat/key-paste.test.ts src/app/api/chat/runs src/components/chat/composer.

Screenshot (light and dark, 1024 px): composer with the pasted-key line.

No new environment variables. No migration.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

Screens: shown to the owner in a local preview (1024px, light and dark) and approved on 2026-11-02.

CI passed on this branch (82e66f8), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41489993261): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

…take the key out (W4-05)

The composer finds a pasted key by its provider's shape (sk-ant-, sk-or-v1-,
sk-proj-, sk-, AIza): Send waits, a line says why, and Remove takes the key
out of the text. Save securely shows only where the page offers a key form.
The run routes refuse such a message or typed answer with api_key before
anything keeps it, and never log it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
gkhngyk previously approved these changes Nov 2, 2026
@gkhngyk

gkhngyk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Approved. Small notes:

  • keyPaste.line asks "Save it securely instead of sending it?", but no caller passes onSaveKey yet, so only Remove shows. Neutral copy until W4-06?
  • Google AIza… keys are often public browser keys in page HTML, so pasting page source will be blocked. Acceptable, since Remove works. Stripe sk_live_ isn't caught; worth adding.

gkhngyk
gkhngyk previously approved these changes Nov 3, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@gkhngyk
gkhngyk merged commit 71b6a47 into ansvisor:main Nov 3, 2026
5 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