Cross-Site Scripting (XSS) Overview
Cross-Site Scripting (XSS) Overview
Cross-Site Scripting (XSS) vulnerabilities occur when applications include untrusted data in web pages without proper validation or escaping. These vulnerabilities allow attackers to inject and execute malicious scripts in victims’ browsers, potentially leading to session hijacking, credential theft, or other client-side attacks.There are three main types of XSS attacks:
- Reflected XSS: Malicious script is reflected off the web server, such as in search results or error messages.
- Stored XSS: Malicious script is stored on the target server, such as in a database, message forum, or comment field.
- DOM-based XSS: Vulnerability exists in client-side code rather than server-side code.
Reflected XSS
Reflected XSS
- Always escape or encode user input before including it in HTML responses
- Use template systems with automatic escaping
- Implement Content Security Policy (CSP) headers
- Validate input against a whitelist of allowed values when possible
- Use frameworks that automatically handle XSS protection
- Consider implementing the X-XSS-Protection header
Stored XSS
Stored XSS
- Sanitize user input before storing in the database
- Escape or encode user-generated content when rendering
- Use HTML sanitization libraries to remove dangerous tags and attributes
- Implement Content Security Policy (CSP) headers
- Consider using a markup language like Markdown instead of allowing HTML
- Validate input against a whitelist of allowed values when possible
DOM-based XSS
DOM-based XSS
- Use safe DOM methods like textContent instead of innerHTML
- Sanitize data before inserting it into the DOM
- Use client-side sanitization libraries like DOMPurify
- Implement Content Security Policy (CSP) headers
- Avoid using dangerous JavaScript functions like eval() or document.write()
- Be cautious when using jQuery’s html() method or similar functions
XSS in JavaScript Frameworks
XSS in JavaScript Frameworks
- Understand the security model of your framework
- Avoid bypassing built-in protections (like React’s dangerouslySetInnerHTML)
- Use framework-specific security best practices
- Still sanitize user input even when using frameworks
- Implement Content Security Policy (CSP) headers
- Keep frameworks and libraries updated
- Use security linters and static analysis tools
XSS in HTML Attributes
XSS in HTML Attributes
- Escape user input before inserting it into HTML attributes
- Be especially careful with attributes that can execute JavaScript (onclick, onerror, etc.)
- Use proper URL encoding for attributes containing URLs
- Avoid inline event handlers and use unobtrusive JavaScript instead
- Consider using template systems with automatic attribute escaping
- Implement Content Security Policy (CSP) headers
XSS in URLs
XSS in URLs
- Validate URLs against a whitelist of allowed domains
- Ensure URLs use safe protocols (https:, http:) and not dangerous ones (javascript:, data:)
- Use proper URL encoding
- Consider implementing URL sanitization libraries
- For redirects, use a server-side whitelist approach
- Implement proper Content Security Policy (CSP) headers
- Use rel=“noopener noreferrer” for external links
Content Security Policy (CSP)
Content Security Policy (CSP)
- Define appropriate directives for different resource types
- Start with a strict policy and relax as needed
- Use nonce or hash-based approaches for inline scripts when necessary
- Implement report-uri directive to monitor violations
- Consider using report-only mode during initial implementation
- Regularly review and update CSP based on application needs
- Test CSP thoroughly to avoid breaking legitimate functionality
XSS in JSON Responses
XSS in JSON Responses
- Set proper Content-Type headers for JSON responses
- Avoid embedding JSON directly in HTML
- Use JSON.parse() for client-side parsing instead of eval()
- Consider using JSON.stringify() with a replacer function to sanitize values
- Implement proper Content Security Policy (CSP) headers
- Use AJAX or fetch to load JSON data asynchronously
- Be cautious with user-controlled values in JSON
XSS in Template Engines
XSS in Template Engines
- Use template engines that escape output by default
- Understand the escaping syntax of your template engine
- Avoid unescaped output unless absolutely necessary
- If unescaped output is needed, sanitize the data first
- Implement proper Content Security Policy (CSP) headers
- Keep template engines updated to benefit from security fixes
- Consider using context-specific escaping for different parts of HTML
XSS Prevention Checklist
XSS Prevention Checklist
- Treat all user input as untrusted
- Apply context-specific encoding when outputting data
- Implement Content Security Policy as an additional layer of defense
- Use modern frameworks with built-in XSS protections
- Regularly test for XSS vulnerabilities
- Keep all dependencies updated
- Implement proper security headers
- Follow the principle of defense in depth