Files
goodgo-platform/apps/api/src/modules/shared/infrastructure/middleware/sanitize-input.middleware.ts
Ho Ngoc Hai c920934fb6 fix(lint): enforce consistent-type-imports and fix import ordering across codebase
Auto-fix 862 lint errors: convert value imports used only as types to
`import type`, fix import group ordering in seed.ts and du-an-api.ts,
remove unused imports in auth controller, and clean up stale eslint-disable
comments referencing non-existent rules.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-16 05:13:56 +07:00

55 lines
1.7 KiB
TypeScript

import { Injectable, type NestMiddleware } from '@nestjs/common';
import { type NextFunction, type Request, type Response } from 'express';
import sanitizeHtml from 'sanitize-html';
const SANITIZE_OPTIONS: sanitizeHtml.IOptions = {
allowedTags: [],
allowedAttributes: {},
disallowedTagsMode: 'recursiveEscape',
};
function sanitizeValue(value: unknown): unknown {
if (typeof value === 'string') {
return sanitizeHtml(value, SANITIZE_OPTIONS);
}
if (Array.isArray(value)) {
return value.map(sanitizeValue);
}
if (value !== null && typeof value === 'object') {
return sanitizeObject(value as Record<string, unknown>);
}
return value;
}
function sanitizeObject(obj: Record<string, unknown>): Record<string, unknown> {
const sanitized: Record<string, unknown> = {};
for (const [key, val] of Object.entries(obj)) {
sanitized[key] = sanitizeValue(val);
}
return sanitized;
}
/**
* Strips HTML tags from all string values in request body, query, and params
* to prevent stored XSS attacks.
*/
@Injectable()
export class SanitizeInputMiddleware implements NestMiddleware {
use(req: Request, _res: Response, next: NextFunction): void {
if (req.body && typeof req.body === 'object') {
req.body = sanitizeObject(req.body as Record<string, unknown>);
}
if (req.query && typeof req.query === 'object') {
for (const [key, val] of Object.entries(req.query)) {
(req.query as Record<string, unknown>)[key] = sanitizeValue(val);
}
}
if (req.params && typeof req.params === 'object') {
for (const [key, val] of Object.entries(req.params)) {
(req.params as Record<string, unknown>)[key] = sanitizeValue(val);
}
}
next();
}
}