fix(security): remediate audit vulnerabilities SEC-001 through SEC-023
This commit is contained in:
1 parent
dc88677169
commit
bd49fbc4a0
31 files changed
+867
-114
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in new issue
Block a user