feat: prompt injection defense & chat security hardening - #58
Merged
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
- 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>
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
user_id(IDOR prevention). Unified user ID across REST ('default') and WebSocket (UUID) routes with data migrationid,userId,source,collectedAt). UUID validation on conversation IDs prevents DB error leakage@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 messagesAddresses OWASP LLM Top 10: LLM01 (Prompt Injection), LLM07 (System Prompt Leakage), LLM10 (Unbounded Consumption).
Files changed (16)
conversations.ts— added userId to 4 query functionschat.ts/ws-chat.ts— unified userId, UUID validation, error sanitization, rate limitconversation-service.ts— injection detection, history truncationchat-persona.md— Security Boundaries sectiontool-executor.ts— bounds checking, result shaping, error sanitizationapp.ts— global rate limitinguuid.ts(shared utility),009_unify_user_id.sql(migration)Test plan
npm run buildpassesnpm run lint— 0 errorsnpm run format:check— all files cleannpm test— 339 tests passing (287 backend + 52 frontend)🤖 Generated with Claude Code