Fix/64 prompt injection newline sanitization - #1027
Open
sansin112 wants to merge 8 commits into
Open
Conversation
added 8 commits
July 21, 2026 22:50
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
This PR resolves a prompt injection vulnerability where newline-triggered control vectors were not properly neutralized during resume ingestion (#64). Previously,
PromptDefense.sanitize()only stripped angle brackets and template delimiters, allowing multi-line role-switching tags (such as\nSystem:) and fake section separators (such as\n---) to pass through unescaped into the LLM context. This change updatesPromptDefense.sanitize()to actively neutralize role switches and section delimiters, updatesINJECTION_PATTERNSregex matching, and ensures raw resume text is properly sanitized prior to chunking and embedding.Issue
Closes #64
Changes
PromptDefense.sanitize()insafety/prompt_defense.pyto strip and replace multi-line role-switching vectors (\nSystem:,\nHuman:,\nAssistant:) with[sanitized-role]:and section separators (---,===) with[sanitized-delimiter].INJECTION_PATTERNSto cover flexible whitespace and newline injection boundaries without breaking legitimate job titles (e.g., "System Administrator").tests/unit/test_prompt_defense.pywith comprehensive test coverage for multi-line sanitization, regression checks for legitimate resume text, and explicitmypyreturn type annotations (-> None).Testing
make test-unit)make test-integration)make lint)make typecheck)Screenshots / Demo
N/A (Backend defense and sanitization logic update)
Notes for Reviewers
PromptDefense.sanitize()effectively neutralizes\nSystem:role switches without causing false positives on common resume phrases or bullet points.tests/unit/test_prompt_defense.pyare passing, and static analysis checks (ruff,black,mypy) have completed with zero errors.