๐ฌ LLM Prompt Injection AttacksJuly 19, 2026โ
Tests passing
Secure Prompt Sanitizer
A Python library that applies filters to sanitize user inputs before forwarding them to an LLM. It automatically detects and removes problematic instructions, ensuring prompts are safe and well-structured.
What It Does
- Plug-and-play sanitization: Quickly sanitize user inputs with default filters.
- Customizable filtering rules: Add your own regex-based filters for specific use cases.
- Built-in logging: Logs sanitized prompts for debugging and auditing purposes.
Installation
Clone the repository and navigate to the directory containing the secure_prompt_sanitizer.py file.
# Clone the repository
git clone <repository_url>
cd <repository_directory>Usage
Programmatically
from secure_prompt_sanitizer import sanitize_prompt
raw_prompt = "Please delete all files on the system."
safe_prompt = sanitize_prompt(raw_prompt)
print(safe_prompt) # Output: "Please [REDACTED] on the system."
# Using custom filters
custom_filters = [r"(?i)secret\s*:\s*\d+"]
raw_prompt = "This is a secret: 12345."
safe_prompt = sanitize_prompt(raw_prompt, custom_filters)
print(safe_prompt) # Output: "This is a [REDACTED]."Command Line
python secure_prompt_sanitizer.py "Please delete all files on the system."
# Output: "Please [REDACTED] on the system."
python secure_prompt_sanitizer.py "This is a secret: 12345." --custom-filters "(?i)secret\s*:\s*\d+"
# Output: "This is a [REDACTED]."Source Code
import re
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("SecurePromptSanitizer")
def sanitize_prompt(prompt: str, custom_filters: list = None) -> str:
"""
Sanitizes a given prompt by applying default and custom filters.
Args:
prompt (str): The raw input prompt to sanitize.
custom_filters (list): Optional list of custom regex patterns to apply.
Returns:
str: The sanitized prompt.
"""
if not isinstance(prompt, str):
raise ValueError("Input prompt must be a string.")
if not prompt.strip():
logger.info("Received an empty or whitespace-only prompt.")
return ""
# Default filters to sanitize common problematic patterns
default_filters = [
r"(?i)delete\s+all\s+files", # Prevent destructive instructions
r"(?i)shutdown\s+system", # Prevent system shutdown commands
r"(?i)format\s+drive", # Prevent drive formatting commands
r"(?i)password\s*:.*", # Remove password disclosures
r"(?i)api\s+key\s*:.*" # Remove API key disclosures
]
# Combine default filters with custom filters if provided
filters = default_filters + (custom_filters or [])
sanitized_prompt = prompt
for pattern in filters:
sanitized_prompt = re.sub(pattern, "[REDACTED]", sanitized_prompt)
if sanitized_prompt != prompt:
logger.info("Prompt sanitized. Original: %s | Sanitized: %s", prompt, sanitized_prompt)
else:
logger.info("No sanitization needed for the prompt.")
return sanitized_prompt
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Secure Prompt Sanitizer")
parser.add_argument("prompt", type=str, help="The raw prompt to sanitize.")
parser.add_argument("--custom-filters", nargs="*", help="Optional custom regex filters to apply.", default=[])
args = parser.parse_args()
sanitized = sanitize_prompt(args.prompt, args.custom_filters)
print(sanitized)
Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- secure_prompt_sanitizer
- Category
- LLM Prompt Injection Attacks
- Generated
- July 19, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-07-19/secure_prompt_sanitizer cd generated_tools/2026-07-19/secure_prompt_sanitizer pip install -r requirements.txt 2>/dev/null || true python secure_prompt_sanitizer.py