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
@@ -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