(Possible) Cross site scripting
Description
This endpoint may be vulnerable to Cross-Site Scripting (XSS), where user-supplied input is reflected in the response without proper sanitization.
Important: While the response uses a JSON Content-Type header, which prevents direct browser exploitation, this vulnerability may still be exploitable if client-side JavaScript processes the response and inserts it into the DOM without proper encoding. Manual verification is required to confirm whether the application's client-side code safely handles this data.
Cross-Site Scripting allows attackers to inject malicious scripts into web pages viewed by other users. When executed, these scripts run in the victim's browser context with full access to cookies, session tokens, and page content, enabling session hijacking, credential theft, and other attacks.
Remediation
Implement comprehensive input validation and context-aware output encoding to prevent XSS attacks:
1. Server-Side Mitigation:
- Validate and sanitize all user input on the server side before processing
- Encode output based on the context where data will be used (HTML, JavaScript, URL, CSS)
- Use established security libraries for encoding (e.g., OWASP Java Encoder, Microsoft AntiXSS)
// Bad - Unsafe reflection of user input
res.json({ message: userInput });
// Good - Sanitize input before including in response
const sanitizeHtml = require('sanitize-html');
const cleanInput = sanitizeHtml(userInput, {
allowedTags: [],
allowedAttributes: {}
});
res.json({ message: cleanInput });
2. Client-Side Mitigation:- When processing JSON responses, avoid using dangerous methods like
innerHTML,document.write(), oreval() - Use safe DOM methods like
textContentorinnerTextfor inserting data - If HTML rendering is required, use a trusted sanitization library like DOMPurify
// Bad - Vulnerable to XSS
document.getElementById('output').innerHTML = jsonResponse.message;
// Good - Safe text insertion
document.getElementById('output').textContent = jsonResponse.message;
// If HTML is needed - sanitize first
const clean = DOMPurify.sanitize(jsonResponse.message);
document.getElementById('output').innerHTML = clean;
3. Additional Security Headers:- Implement Content Security Policy (CSP) headers to restrict script execution
- Use X-Content-Type-Options: nosniff to prevent MIME type sniffing