Added input sanitization middleware to prevent stored XSS attacks in user-provided content, specifically the reason field in verification reports.
- File:
backend/src/sanitizer.ts - Purpose: XSS prevention via:
- Stripping HTML tags (script, iframe, etc.)
- Encoding HTML entities
- Removing event handlers (onerror=, onclick=, etc.)
- File:
backend/src/database.ts - Changes:
- Added
asset_reportstable to store individual reports with reason for audit trail - Added
saveAssetReport()function to persist sanitized reasons
- Added
- File:
backend/src/api.ts - Endpoint:
POST /api/verification/report - Changes:
- Imported
sanitizeInputfrom sanitizer module - Imported
saveAssetReportfrom database - Added sanitization of
reasonfield before storing - Stores sanitized reason in
asset_reportstable
- Imported
- File:
backend/src/__tests__/sanitizer.test.ts - Coverage: 11 test cases covering:
- Null/undefined handling
- Script tag removal
- Iframe tag removal
- HTML entity encoding
- Event handler removal
- Safe content preservation
- Stored XSS attacks via
reasonfield in verification reports - Script injection through HTML tags
- Event handler exploitation (onerror, onclick, etc.)
- JavaScript URL injection
# Before (vulnerable):
POST /api/verification/report
{ "assetCode": "USDC", "issuer": "...", "reason": "<script>alert(1)</script>" }
# After (sanitized):
POST /api/verification/report
{ "assetCode": "USDC", "issuer": "...", "reason": "alert1" }
# Run tests
cd backend
npm test
# Test specific module
npx vitest run sanitizer- Create sanitizer module with XSS protection
- Update database schema with asset_reports table
- Add saveAssetReport database function
- Integrate sanitization in verification report endpoint
- Add unit tests for sanitizer
- Verify no TypeScript errors
- Issue #260: HMAC Signature Verification for Webhooks
- Issue #261: Contract Upgrade Authorization with Timelock