Skip to content

Commit 6aa81f4

Browse files
SCG:1.0.0:NA #2-workflowTest Fixed vulnerabilities
1 parent 46017c5 commit 6aa81f4

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/main.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
from collections import namedtuple
2+
from decimal import Decimal
23

34
Order = namedtuple("Order", "id, items")
45
Item = namedtuple("Item", "type, description, amount, quantity")
56

7+
MAX_ITEM_AMOUNT = 100000 # maximum price of item in the shop
8+
MAX_QUANTITY = 100 # maximum quantity of an item in the shop
9+
MIN_QUANTITY = 0 # minimum quantity of an item in the shop
10+
MAX_TOTAL = 1e6 # maximum total amount accepted for an order
611

7-
def validorder(order: Order):
8-
net = 0
12+
13+
def validorder(order):
14+
payments = Decimal("0")
15+
expenses = Decimal("0")
916

1017
for item in order.items:
1118
if item.type == "payment":
12-
net += item.amount
19+
# Sets a reasonable min & max value for the invoice amounts
20+
if -MAX_ITEM_AMOUNT <= item.amount <= MAX_ITEM_AMOUNT:
21+
payments += Decimal(str(item.amount))
1322
elif item.type == "product":
14-
net -= item.amount * item.quantity
23+
if (
24+
type(item.quantity) is int
25+
and MIN_QUANTITY < item.quantity <= MAX_QUANTITY
26+
and MIN_QUANTITY < item.amount <= MAX_ITEM_AMOUNT
27+
):
28+
expenses += Decimal(str(item.amount)) * item.quantity
1529
else:
1630
return "Invalid item type: %s" % item.type
1731

18-
if net != 0:
19-
return "Order ID: %s - Payment imbalance: $%0.2f" % (order.id, net)
32+
if abs(payments) > MAX_TOTAL or expenses > MAX_TOTAL:
33+
return "Total amount payable for an order exceeded"
34+
35+
if payments != expenses:
36+
return "Order ID: %s - Payment imbalance: $%0.2f" % (
37+
order.id,
38+
payments - expenses,
39+
)
2040
else:
2141
return "Order ID: %s - Full payment received!" % order.id

0 commit comments

Comments
 (0)