Skip to content

feat: prompt injection defense & chat security hardening - #58

Merged
silver-snoopy merged 2 commits into
masterfrom
feature/chat-security-hardening
Apr 4, 2026
Merged

feat: prompt injection defense & chat security hardening#58
silver-snoopy merged 2 commits into
masterfrom
feature/chat-security-hardening

Conversation

@silver-snoopy

Copy link
Copy Markdown
Owner

Summary

  • User scoping: All conversation queries now filter by user_id (IDOR prevention). Unified user ID across REST ('default') and WebSocket (UUID) routes with data migration
  • Prompt injection defense: System prompt hardened with anti-extraction instructions. Suspicious input detection flags override attempts with soft model warnings
  • Information leakage prevention: Error messages sanitized to generic responses. Tool results shaped to strip internal fields (id, userId, source, collectedAt). UUID validation on conversation IDs prevents DB error leakage
  • Resource abuse prevention: Rate limiting via @fastify/rate-limit (60 req/min global, 15 req/min on POST /api/chat). Tool input bounds checking (730-day date span, 100 item limit, 200-char exercise name, 20 metrics max). Conversation history truncated to 50 messages

Addresses OWASP LLM Top 10: LLM01 (Prompt Injection), LLM07 (System Prompt Leakage), LLM10 (Unbounded Consumption).

Files changed (16)

  • conversations.ts — added userId to 4 query functions
  • chat.ts / ws-chat.ts — unified userId, UUID validation, error sanitization, rate limit
  • conversation-service.ts — injection detection, history truncation
  • chat-persona.md — Security Boundaries section
  • tool-executor.ts — bounds checking, result shaping, error sanitization
  • app.ts — global rate limiting
  • New: uuid.ts (shared utility), 009_unify_user_id.sql (migration)

Test plan

  • npm run build passes
  • npm run lint — 0 errors
  • npm run format:check — all files clean
  • npm test — 339 tests passing (287 backend + 52 frontend)
  • E2E tests — 47 passed (16 pre-existing failures, none introduced)
  • Live verification: prompt injection blocked ("I can't do that"), normal data queries work with tool calls
  • Live verification: existing conversations load after user ID migration

🤖 Generated with Claude Code

Defense-in-depth security improvements for the chat feature:
- Scope conversation queries to user_id (IDOR prevention)
- Unify user ID across REST and WebSocket routes
- Add UUID validation on conversation IDs
- Harden system prompt with anti-extraction instructions
- Add suspicious input detection with soft flagging
- Sanitize error messages to prevent internal detail leakage
- Add tool input bounds checking (date span, limits, lengths)
- Shape tool result outputs to strip internal fields
- Add conversation history truncation (50 messages max)
- Add rate limiting via @fastify/rate-limit (global + chat-specific)
- Add data migration to unify existing 'default' user IDs

Addresses OWASP LLM Top 10: LLM01 (Prompt Injection),
LLM07 (System Prompt Leakage), LLM10 (Unbounded Consumption)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Apr 4, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
vitals Ignored Ignored Apr 4, 2026 0:39am

- Narrow /you are now/i regex to avoid false positives on legitimate messages
- Fix misleading "authenticated user" wording in system prompt
- Bound query_exercise_progress dates when only one is provided
- Differentiate validation vs infrastructure errors in tool-executor catch
- Fix chatStream JSON parse silent fallback — now returns error to AI model
- Add chatStream tests for history truncation and injection detection
- Add comment explaining injection detection is defense-in-depth heuristic

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@silver-snoopy
silver-snoopy merged commit 7f07410 into master Apr 4, 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.

1 participant