Skip to content

Strengthen prompt injection protection in chat_completion - #1452

Open
0xxreacher wants to merge 1 commit into
meta-llama:mainfrom
0xxreacher:patch-1
Open

Strengthen prompt injection protection in chat_completion#1452
0xxreacher wants to merge 1 commit into
meta-llama:mainfrom
0xxreacher:patch-1

Conversation

@0xxreacher

Copy link
Copy Markdown

Summary

This PR strengthens the prompt injection protection in chat_completion() by addressing multiple bypass vectors:

  1. Unicode evasion - Zero-width characters, full-width characters
  2. Role field injection - Previously unvalidated
  3. Partial/modified tags - Space insertion, character encoding

Changes

  • Add Unicode normalization function to catch evasion attempts
  • Implement comprehensive message validation
  • Add 20+ test cases covering all known bypass vectors
  • Maintain backward compatibility (no API changes)

Vulnerability Addresses

  • Zero-width character bypass (U+200B, U+200C, U+200D)
  • Role field manipulation (was not validated)
  • Partial tag bypass ("[ INST]" with spaces)
  • HTML entity encoding

Testing

All 20+ tests pass:

  • Unicode normalization tests
  • Injection detection tests
  • Dialog validation tests
  • Edge case handling
  • Integration tests

Security Impact

Fixes potential prompt injection vulnerabilities that could bypass the current safety check.

Breaking Changes

None. This is a security hardening with no API changes.

Checklist

  • Code follows project style
  • Tests added and passing
  • No breaking changes
  • Backward compatible
  • Security implications reviewed
  • Documentation updated with docstrings

Added functions to normalize text and check for prompt injection.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant