How to Protect System Prompts: Best Practices for Secure Prompt Engineering
To protect
system prompts, restrict access to prompt configurations, sanitize user inputs to prevent injection, and use environment variables or secure storage for sensitive prompt data. Always validate and monitor prompt usage to avoid unintended prompt manipulation.Syntax
Protecting system prompts involves these key parts:
- Access Control: Limit who can view or edit system prompts.
- Input Sanitization: Clean user inputs to avoid harmful injections.
- Secure Storage: Store prompts in protected environments or encrypted storage.
- Monitoring: Track prompt usage and changes for suspicious activity.
python
def sanitize_input(user_input: str) -> str: """Remove dangerous characters from user input.""" dangerous_chars = ['"', '\'', ';', '--'] for char in dangerous_chars: user_input = user_input.replace(char, '') return user_input # Example of restricting prompt access class PromptManager: def __init__(self): self._system_prompt = "System prompt text" self._authorized_users = {'admin'} def get_prompt(self, user: str) -> str: if user in self._authorized_users: return self._system_prompt else: raise PermissionError("Access denied")
Example
This example shows how to protect a system prompt by sanitizing user input and restricting access to the prompt text.
python
def sanitize_input(user_input: str) -> str: dangerous_chars = ['"', '\'', ';', '--'] for char in dangerous_chars: user_input = user_input.replace(char, '') return user_input class PromptManager: def __init__(self): self._system_prompt = "Welcome to the secure system prompt." self._authorized_users = {'admin', 'moderator'} def get_prompt(self, user: str) -> str: if user in self._authorized_users: return self._system_prompt else: raise PermissionError("Access denied") # Simulate user input and access user_input = 'Hello; DROP TABLE users;' safe_input = sanitize_input(user_input) pm = PromptManager() try: prompt_text = pm.get_prompt('admin') print(f"Prompt for admin: {prompt_text}") print(f"Sanitized user input: {safe_input}") prompt_text = pm.get_prompt('guest') except PermissionError as e: print(e)
Output
Prompt for admin: Welcome to the secure system prompt.
Sanitized user input: Hello DROP TABLE users
Access denied
Common Pitfalls
Common mistakes when protecting system prompts include:
- Allowing unrestricted access to prompt configurations, which can lead to prompt injection.
- Not sanitizing user inputs, enabling attackers to manipulate prompts.
- Storing prompts in plain text or unsecured files.
- Failing to monitor prompt usage and changes, missing suspicious activities.
python
class UnsafePromptManager: def __init__(self): self.system_prompt = "Open prompt" def get_prompt(self): return self.system_prompt # Unsafe usage pm = UnsafePromptManager() print(pm.get_prompt()) # Anyone can access and modify # Safe usage class SafePromptManager: def __init__(self): self._system_prompt = "Protected prompt" self._authorized_users = {'admin'} def get_prompt(self, user: str): if user in self._authorized_users: return self._system_prompt else: raise PermissionError("Access denied") pm_safe = SafePromptManager() try: print(pm_safe.get_prompt('guest')) except PermissionError as e: print(e)
Output
Open prompt
Access denied
Quick Reference
- Restrict Access: Only trusted users can view or edit system prompts.
- Sanitize Inputs: Remove or escape dangerous characters from user inputs.
- Use Secure Storage: Store prompts in encrypted or environment-protected locations.
- Monitor Changes: Log and review prompt modifications regularly.
- Validate Outputs: Check model outputs to detect prompt injection attempts.
Key Takeaways
Always restrict access to system prompts to trusted users only.
Sanitize all user inputs to prevent prompt injection attacks.
Store system prompts securely using encryption or protected environments.
Monitor and log prompt usage and changes to detect suspicious activity.
Validate model outputs to catch unintended prompt manipulations early.