fix(security): resolve all critical, high, and medium security vulnerabilities
This commit is contained in:
1 parent
da26dd1c8d
commit
dc88677169
33 files changed
+438
-85
No files matched your search
@@ -52,5 +52,18 @@ export function validateEnv(config: Record<string, unknown>) {
|
||||
throw new Error(`Environment Validation Error: ${messages}`);
|
||||
}
|
||||
|
||||
if (validatedConfig.NODE_ENV === 'production') {
|
||||
if (!validatedConfig.JWT_ACCESS_SECRET) {
|
||||
throw new Error(
|
||||
'Environment Validation Error: JWT_ACCESS_SECRET is strictly required in production!',
|
||||
);
|
||||
}
|
||||
if (!validatedConfig.JWT_REFRESH_SECRET) {
|
||||
throw new Error(
|
||||
'Environment Validation Error: JWT_REFRESH_SECRET is strictly required in production!',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return validatedConfig;
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import { ROLES_KEY } from '../decorators/roles.decorator';
|
||||
interface RequestWithUser {
|
||||
user?: {
|
||||
role?: string;
|
||||
isApiKey?: boolean;
|
||||
scopes?: string[];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -33,11 +35,23 @@ export class RolesGuard implements CanActivate {
|
||||
throw new ForbiddenException('شما دسترسی لازم برای این بخش را ندارید');
|
||||
}
|
||||
|
||||
// If request originates from an API key, verify it has admin scope for admin-guarded routes
|
||||
if (user.isApiKey) {
|
||||
const scopes = Array.isArray(user.scopes) ? user.scopes : [];
|
||||
const hasAdminScope =
|
||||
scopes.includes('*') ||
|
||||
scopes.includes('admin') ||
|
||||
scopes.includes('all');
|
||||
if (!hasAdminScope) {
|
||||
throw new ForbiddenException(
|
||||
'کلید API ارائهشده فاقد اسکوپ مدیریتی لازم برای این عملیات است',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const userRoleLower = user.role.toLowerCase();
|
||||
const hasRole = requiredRoles.some(
|
||||
(r) =>
|
||||
r.toLowerCase() === userRoleLower ||
|
||||
(userRoleLower.includes('admin') && r.toLowerCase().includes('admin')),
|
||||
(r) => r.toLowerCase() === userRoleLower,
|
||||
);
|
||||
if (!hasRole) {
|
||||
throw new ForbiddenException('سطح دسترسی شما کافی نیست');
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { Controller, Get, Res } from '@nestjs/common';
|
||||
import { Controller, Get, Res, UseGuards } from '@nestjs/common';
|
||||
import { ApiExcludeController } from '@nestjs/swagger';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from './guards/roles.guard';
|
||||
import { Roles } from './decorators/roles.decorator';
|
||||
import type { Response } from 'express';
|
||||
|
||||
@ApiExcludeController()
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin', 'SUPER_ADMIN')
|
||||
@Controller('metrics')
|
||||
export class MetricsController {
|
||||
private static requestCount = 0;
|
||||
|
||||
Reference in new issue
Block a user