LLM Output Encoding Security
LLM outputs without encoding security can enable XSS attacks and encoding exploits. Four controls: unicode sanitisation, XSS prevention, encoding validation and safe rendering.
4 Output Encoding Security Controls
Sanitise Unicode characters in LLM outputs to prevent homograph attacks and encoding-based exploits.
# Moltbot unicode sanitisation:
unicode_sanitisation:
enabled: true
# Normalisation:
normalisation:
# Normalise unicode to NFC form
# This prevents homograph attacks using visually similar characters
form: "NFC"
# Character filtering:
filtering:
# Block or replace dangerous unicode characters:
# - Zero-width characters (ZWJ, ZWNJ, ZWSP)
# - Invisible characters
# - Bidirectional override characters
# - Non-printing control characters
block_zero_width: true
block_invisible: true
block_bidi_override: true
block_control: true
# Character replacement:
replacement:
# Replace problematic characters with safe equivalents
# Example: replace homographs with their ASCII equivalents
enabled: truePrevent cross-site scripting (XSS) attacks in LLM outputs. Escape HTML, JavaScript, and other code before rendering.
# Moltbot XSS prevention:
xss_prevention:
enabled: true
# HTML escaping:
html_escaping:
enabled: true
# Escape all HTML special characters:
# - <, >, &, ", '
# Use: <, >, &, ", '
# JavaScript escaping:
js_escaping:
enabled: true
# Escape JavaScript special characters:
# - ", ', , /, <, >
# Use: ", ', \, /, <, >
# Context-aware escaping:
context_aware:
enabled: true
# Escape based on context:
# - HTML body: escape HTML
# - HTML attribute: escape HTML and quotes
# - JavaScript: escape JavaScript
# - CSS: escape CSS
# - URL: escape URL
# Safe rendering:
safe_rendering:
# Use safe rendering libraries:
# - DOMPurify for HTML
# - DOMPurify for SVG
# - Safe JSON parsing for JSON
enabled: trueValidate LLM output encoding before rendering. Ensure outputs use expected character sets and encodings.
# Moltbot output encoding validation:
encoding_validation:
enabled: true
# Character set validation:
charset:
# Validate output character set:
# - UTF-8 (default)
# - ASCII (for code blocks)
# - Reject unexpected character sets
allowed_charsets: ["UTF-8", "ASCII"]
reject_unknown: true
# Encoding validation:
encoding:
# Validate output encoding:
# - UTF-8 (default)
# - Base64 (for encoded content)
# - Reject unexpected encodings
allowed_encodings: ["UTF-8", "Base64"]
reject_unknown: true
# Byte order mark (BOM) handling:
bom_handling:
# Strip BOM from outputs
# BOM can cause rendering issues
strip_bom: true
# Encoding detection:
detection:
# Detect output encoding automatically
# If encoding is unexpected, block or sanitise
enabled: trueRender LLM outputs safely using sandboxed rendering contexts and content security policies.
# Moltbot safe output rendering:
safe_rendering:
enabled: true
# Sandboxed rendering:
sandbox:
# Render outputs in sandboxed iframe
# Prevents: script execution, style injection, frame navigation
enabled: true
sandbox_attributes:
- "allow-scripts"
- "allow-same-origin"
# Avoid: allow-popups, allow-top-navigation
# Content Security Policy (CSP):
csp:
enabled: true
# Apply CSP to rendered outputs:
# - script-src: 'self' or 'none'
# - style-src: 'self'
# - img-src: 'self'
# - default-src: 'self'
policy: "default-src 'self'; script-src 'self'; style-src 'self';"
# Safe DOM APIs:
safe_dom:
# Use safe DOM APIs:
# - textContent instead of innerHTML
# - createElement instead of insertAdjacentHTML
# - setAttribute instead of direct property assignment
enabled: trueFrequently Asked Questions
What is the difference between XSS prevention and output encoding validation?
XSS prevention is about preventing cross-site scripting attacks by escaping or sanitising dangerous content (HTML, JavaScript, CSS) before it's rendered in the browser. It focuses on the security of the rendered output. Output encoding validation is about ensuring the output uses expected character sets and encodings (UTF-8, ASCII) before rendering. It focuses on the technical correctness of the output. Both are necessary: XSS prevention prevents attacks, encoding validation prevents rendering errors and encoding-based exploits. Example: XSS prevention escapes <script> to <script>. Encoding validation ensures the output is valid UTF-8 and not malformed UTF-8 that could be exploited.
How do homograph attacks work and how do I prevent them?
Homograph attacks use visually similar Unicode characters to deceive users. Example: the Cyrillic 'а' looks identical to Latin 'a' but is a different character. Attackers can use homographs to create phishing URLs that look legitimate (e.g., gооgle.com using Cyrillic 'о' instead of Latin 'o'). Prevention: 1) Unicode normalisation — normalise all Unicode to NFC form, which converts composed characters to canonical form. 2) Character filtering — block or replace dangerous Unicode characters (zero-width, invisible, bidirectional override). 3) Domain validation — validate domains against a list of allowed TLDs and character sets. 4) Visual similarity checks — use libraries to detect homograph characters in domains or URLs.
How do I implement context-aware escaping?
Context-aware escaping means escaping content based on where it will be rendered. Different contexts require different escaping: 1) HTML body — escape HTML special characters (<, >, &, ", '). 2) HTML attribute — escape HTML and quotes (additional quote escaping for attribute values). 3) JavaScript — escape JavaScript special characters (", ', \, /, <, >). 4) CSS — escape CSS special characters (quotes, backslashes). 5) URL — escape URL special characters (spaces, quotes, control characters). Use libraries like DOMPurify for HTML, or framework-specific escaping functions (React's JSX auto-escapes, Django's template engine).
What are the risks of unsafe output rendering?
Unsafe output rendering can lead to: 1) XSS attacks — attackers can inject malicious scripts that execute in the user's browser. 2) Data theft — XSS can steal cookies, session tokens, or sensitive data. 3) UI spoofing — attackers can modify the UI to deceive users (phishing, credential harvesting). 4) Frame navigation — attackers can navigate to malicious sites using iframes. 5) Clickjacking — attackers can overlay transparent iframes to trick users into clicking malicious links. 6) Encoding exploits — malformed encodings can bypass security filters. Safe rendering uses sandboxed iframes, CSP, and safe DOM APIs to prevent these risks.