fix(security): remediate audit vulnerabilities SEC-001 through SEC-023
Deploy Canina / deploy (push) Successful in 2m25s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s

This commit is contained in:
parsa committed 2026-09-23 23:00:25 +03:30
1 parent dc88677169
commit bd49fbc4a0
31 files changed
+867 -114

No files matched your search

+19 -19
View File
@@ -147,6 +147,25 @@ export class OrdersController {
return this.ordersService.findAllByUser(req.user.id, query);
}
@Post(':id/retry-payment')
@ApiOperation({
summary: 'پرداخت مجدد و تکمیل سفارش با امکان انتخاب روش پرداخت',
})
@ApiOkResponse({
description: 'درخواست پرداخت مجدد با موفقیت پردازش شد',
})
retryPayment(
@Req() req: { user: { id: string } },
@Param('id') id: string,
@Body() body: { paymentMethod?: string },
) {
return this.ordersService.retryPayment(
req.user.id,
id,
body?.paymentMethod,
);
}
@Get(':id')
@ApiOperation({ summary: 'دریافت جزئیات یک سفارش خاص' })
@ApiOkResponse({
@@ -186,25 +205,6 @@ export class OrdersController {
},
},
})
@Post(':id/retry-payment')
@ApiOperation({
summary: 'پرداخت مجدد و تکمیل سفارش با امکان انتخاب روش پرداخت',
})
@ApiOkResponse({
description: 'درخواست پرداخت مجدد با موفقیت پردازش شد',
})
retryPayment(
@Req() req: { user: { id: string } },
@Param('id') id: string,
@Body() body: { paymentMethod?: string },
) {
return this.ordersService.retryPayment(
req.user.id,
id,
body?.paymentMethod,
);
}
findOne(@Req() req: { user: { id: string } }, @Param('id') id: string) {
return this.ordersService.findOne(id, req.user.id);
}
+24 -23
View File
@@ -196,20 +196,20 @@ export class OrdersService {
if (!user) {
throw new NotFoundException('کاربر یافت نشد');
}
const userBalance = new Prisma.Decimal(user.walletBalance || 0);
if (userBalance.lessThan(finalAmount)) {
throw new BadRequestException(
'موجودی کیف پول برای پرداخت این سفارش کافی نیست',
);
}
const createdWalletOrder = await this.prisma.$transaction(async (tx) => {
await tx.user.update({
where: { id: userId },
data: {
walletBalance: { decrement: Number(finalAmount) },
},
});
// SEC-002: Atomic decrement preventing race condition & overdraft
const updatedCount = await tx.$executeRaw`
UPDATE "users"
SET "wallet_balance" = "wallet_balance" - ${Number(finalAmount)}::decimal
WHERE "id" = ${userId}::uuid AND "wallet_balance" >= ${Number(finalAmount)}::decimal
`;
if (updatedCount === 0) {
throw new BadRequestException(
'موجودی کیف پول برای پرداخت این سفارش کافی نیست',
);
}
await tx.walletTransaction.create({
data: {
@@ -581,18 +581,19 @@ export class OrdersService {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (!user) throw new NotFoundException('کاربر یافت نشد');
const userBalance = Number(user.walletBalance || 0);
if (userBalance < finalAmount) {
throw new BadRequestException(
`موجودی کیف پول شما (${userBalance.toLocaleString('fa-IR')} تومان) کمتر از مبلغ سفارش (${finalAmount.toLocaleString('fa-IR')} تومان) است.`,
);
}
await this.prisma.$transaction(async (tx) => {
await tx.user.update({
where: { id: userId },
data: { walletBalance: { decrement: finalAmount } },
});
// SEC-002: Atomic decrement preventing race condition & overdraft
const updatedCount = await tx.$executeRaw`
UPDATE "users"
SET "wallet_balance" = "wallet_balance" - ${finalAmount}::decimal
WHERE "id" = ${userId}::uuid AND "wallet_balance" >= ${finalAmount}::decimal
`;
if (updatedCount === 0) {
throw new BadRequestException(
`موجودی کیف پول شما برای پرداخت این سفارش (${finalAmount.toLocaleString('fa-IR')} تومان) کافی نیست.`,
);
}
await tx.walletTransaction.create({
data: {