Successfully implemented comprehensive input validation to prevent prompt injection attacks across the entire travel-agent application.
File: input_validator.py
A robust validation module that:
- ✅ Validates user queries (max 1000 characters)
- ✅ Detects 20+ prompt injection patterns
- ✅ Blocks suspicious characters and control characters
- ✅ Validates locations (airport codes, city names)
- ✅ Validates price ranges ($0 - $100,000)
- ✅ Validates date formats (YYYY-MM-DD)
- ✅ Checks for excessive special character usage (>30%)
- ✅ Provides detailed logging for security monitoring
File: agents.py
Added validation to all agent nodes:
- ✅
entry_node- Validates initial user query - ✅
web_search_node- Double-checks query before web search - ✅
flight_search_node- Validates query, origin, destination, and price - ✅
conversational_node- Validates conversational queries
File: gradio_app.py
Added frontend validation:
- ✅ Validates all inputs before processing
- ✅ Returns user-friendly error messages
- ✅ Prevents malicious inputs from reaching LLM
File: test_input_validation.py
Created test suite with 18 test cases covering:
- ✅ Valid queries (normal travel questions)
- ✅ Prompt injection attempts (ignore instructions, system prompts, etc.)
- ✅ Length attacks (>1000 characters)
- ✅ Special character attacks (>30% special chars)
- ✅ Control character attacks (null bytes, etc.)
- ✅ Location validation (XSS attempts blocked)
- ✅ Price validation (negative/excessive prices blocked)
All 18 tests passed! 🎉
- "ignore previous instructions"
- "System: you are..."
- "### system"
- "[system]"
- "You are now DAN"
- "Act as a..."
- "Pretend to be..."
- "jailbreak"
- And 12+ more injection patterns
- "Find hotels in Paris"
- "I need a flight from NYC to London"
- "Best hotels under $200"
- "Paris, France"
- "New York-JFK"
- Prevents Prompt Injection: Blocks attempts to manipulate LLM behavior
- Data Integrity: Ensures clean, validated data flows through the system
- Audit Trail: Logs all validation failures for security monitoring
- User Experience: Provides clear error messages when invalid input detected
- Defense in Depth: Multiple validation layers (frontend + backend)
from input_validator import InputValidator, InputValidationError
try:
clean_query = InputValidator.validate_user_query(user_input)
# Proceed with clean query
except InputValidationError as e:
# Handle validation error
print(f"Invalid input: {e}")from input_validator import validate_user_input
try:
validated = validate_user_input(
query="Find hotels in Paris",
origin="NYC",
destination="CDG",
max_price=1000
)
except InputValidationError as e:
print(f"Validation failed: {e}")Test the validation module:
python3 test_input_validation.pyExpected output: All tests should pass with green checkmarks ✅
The input validation is now fully implemented and tested. Consider:
- ✅ DONE: Input validation to prevent prompt injection
- 🔄 TODO: Add request timeouts and SSL verification to API calls
- 🔄 TODO: Fix file permissions on
.env(chmod 600) - 🔄 TODO: Implement rate limiting
- 🔄 TODO: Update vulnerable dependencies
- 🔄 TODO: Remove debug print statements
Monitor logs for these WARNING messages:
Potential prompt injection detectedQuery exceeds maximum lengthExcessive special charactersSuspicious character detected
These indicate potential attack attempts that were successfully blocked.
For questions or issues with the validation system:
- Check the test suite:
test_input_validation.py - Review patterns in
InputValidator.INJECTION_PATTERNS - Adjust
MAX_QUERY_LENGTHor other limits as needed
Status: ✅ COMPLETE - Input validation successfully implemented and tested Date: 2025-11-18 Security Level: HIGH - Application now protected against prompt injection attacks