This document outlines the XSS (Cross-Site Scripting) protection measures implemented in the TradeFlow API.
The TradeFlow API now includes XSS protection middleware to prevent malicious script injection through query parameters and user input.
Added xss-clean library to package.json:
{
"dependencies": {
"xss-clean": "^0.1.1"
}
}The XSS protection middleware is applied globally in src/main.ts:
import * as xssClean from 'xss-clean';
// Apply XSS protection middleware globally
app.use(xssClean());This middleware:
- Sanitizes request body, query parameters, and URL parameters
- Removes dangerous HTML tags and JavaScript code
- Prevents XSS attacks through user input
Created test endpoints in src/tokens/tokens.controller.ts:
- URL:
/api/v1/tokens - Method: GET
- Query Parameter:
search - Protection: XSS middleware sanitizes input
- URL:
/api/v1/tokens/vulnerable - Method: GET
- Query Parameter:
search - Purpose: Demonstrates vulnerability without protection
- Start the server:
npm install
npm run start:dev- Test with malicious payloads:
# Test script tag injection
curl "http://localhost:3000/api/v1/tokens?search=<script>alert('XSS')</script>"
# Test vulnerable endpoint
curl "http://localhost:3000/api/v1/tokens/vulnerable?search=<script>alert('XSS')</script>"Run the test script:
node test-xss-protection.jsThe following malicious payloads are tested:
- Basic script tag:
<script>alert("XSS")</script> - IMG tag with onerror:
<img src="x" onerror="alert('XSS')"> - JavaScript protocol:
javascript:alert("XSS") - SVG tag:
<svg onload="alert('XSS')"> - HTML entity encoded:
<script>alert("XSS")</script>
- Malicious HTML tags are removed or escaped
- JavaScript code is sanitized
- Safe content is returned to the client
- Malicious content is reflected back unchanged
- Potential for script execution in browsers
- Security vulnerability
- Input Sanitization: Removes dangerous characters and tags
- Global Protection: Applied to all routes automatically
- Minimal Performance Impact: Lightweight middleware
- Automatic Defense: No manual sanitization required per endpoint
- Always validate input in addition to XSS protection
- Use parameterized queries for database operations
- Implement Content Security Policy headers
- Keep dependencies updated for latest security patches
- Regular security testing with various attack vectors
The xss-clean middleware can be configured with options:
app.use(xssClean({
// Custom configuration options
whiteList: [], // Allowed HTML tags
stripIgnoreTag: false, // Keep content of ignored tags
stripIgnoreTagBody: ['script'] // Remove body of ignored tags
}));This implementation helps address:
- OWASP Top 10 - A03:2021 - Injection
- Security best practices for REST APIs
- Input validation and sanitization requirements
Monitor application logs for:
- Unusual request patterns
- Rejected malicious input
- Security events and alerts
- Regularly update
xss-cleandependency - Review security advisories
- Test with new attack vectors
- Monitor middleware performance impact