10 / 70 · 07 Security Testing for AI Apps · Insecure Output Handling and Model Denial of Service← prev⊞ allnext →☰ Read as one page
2.4Output Validation Framework
# output_sanitizer.py
import re
import html
from urllib.parse import urlparse
class LLMOutputSanitizer:
"""Sanitize LLM output before use in downstream operations."""
@staticmethod
def for_html(text: str) -> str:
"""Sanitize for HTML rendering."""
sanitized = html.escape(text)
# Also strip javascript: URLs
sanitized = re.sub(r'javascript:', '', sanitized, flags=re.IGNORECASE)
return sanitized
@staticmethod
def for_sql_value(text: str) -> str:
"""Sanitize for use as a SQL value (prefer parameterized queries)."""
# This is a last resort -- always use parameterized queries instead
return text.replace("'", "''").replace(";", "").replace("--", "")
@staticmethod
def for_filename(text: str) -> str:
"""Sanitize for use as a filesystem path."""
# Remove path traversal and shell metacharacters
sanitized = re.sub(r'[;|`$(){}\\]', '', text)
sanitized = sanitized.replace("..", "")
sanitized = sanitized.replace("/", "_")
return sanitized
@staticmethod
def for_url(text: str) -> str:
"""Validate and sanitize URLs from LLM output."""
parsed = urlparse(text)
if parsed.scheme not in ("http", "https"):
raise ValueError(f"Invalid URL scheme: {parsed.scheme}")
if parsed.hostname and parsed.hostname.endswith(".internal"):
raise ValueError(f"Internal URL not allowed: {text}")
return text