Helmet: HTTP security headers middleware — ngăn chặn XSS, clickjacking, sniffing:
import helmet from 'helmet';
app.use(helmet()); // Thêm vào main.tsCORS: chỉ allow origins cụ thể:
app.enableCors({
origin: ['https://yourdomain.com'],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
});Rate Limiting với @nestjs/throttler:
ThrottlerModule.forRoot([
{ name: 'short', ttl: 1000, limit: 3 }, // 3 req/s
{ name: 'medium', ttl: 10000, limit: 20 }, // 20 req/10s
{ name: 'long', ttl: 60000, limit: 100 }, // 100 req/min
])Input sanitization: class-validator + ValidationPipe với whitelist: true ngăn chặn mass assignment.
Dùng sanitize-html cho user-generated content. SQL Injection: TypeORM parameterized queries tự động escape — không bao giờ dùng raw string interpolation trong queries.
Helmet: HTTP security headers — prevents XSS, clickjacking: app.use(helmet())
CORS: allow specific origins:
app.enableCors({ origin: ['https://yourdomain.com'], credentials: true });Rate Limiting with @nestjs/throttler:
ThrottlerModule.forRoot([
{ name: 'short', ttl: 1000, limit: 3 },
{ name: 'long', ttl: 60000, limit: 100 },
])Input sanitization: ValidationPipe with whitelist: true prevents mass assignment.
TypeORM parameterized queries prevent SQL injection — never use raw string interpolation.