Skip to content

Commit 6a68ccc

Browse files
committed
Modify the codebase
1 parent 8a8727c commit 6a68ccc

File tree

9 files changed

+107
-33
lines changed

9 files changed

+107
-33
lines changed

.github/workflows/cd.yml

Whitespace-only changes.

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Run Tests
33
on:
44
pull_request: # Runs on pull requests
55
branches:
6-
- dev
6+
- main
77

88
jobs:
99
test:
@@ -26,4 +26,4 @@ jobs:
2626
run: |
2727
python -m unittest discover -s tests -p "test_*.py" -v
2828
env:
29-
PYTHONPATH: src # Adds src/ to the module search path
29+
PYTHONPATH: src # Adds src/ to the module search path

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
*.DS_Store

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
## [Unreleased]
66
- Upcoming features and bug fixes.
77

8-
## [1.0.0] - YYYY-MM-DD
8+
## [1.0.0] - 2025-03-19
99
### Added
1010
- Initial project structure with source code.
1111
- Basic functionality in `main.py`.
@@ -16,6 +16,6 @@ All notable changes to this project will be documented in this file.
1616
- Issue and Pull Request templates.
1717
- Open-source license (`LICENSE`).
1818

19-
## [0.1.0] - YYYY-MM-DD
19+
## [1.0.1] - 2025-03-20
2020
### Added
2121
- Project initialization with Git and GitHub setup.

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Use official Python image
2+
FROM python:3.11
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Copy dependencies file and install dependencies
8+
COPY requirements.txt .
9+
RUN pip install --no-cache-dir -r requirements.txt
10+
11+
# Copy the rest of the application files
12+
COPY . .
13+
14+
# Run tests before starting the application
15+
RUN pytest tests/
16+
17+
# Command to run the main application after successful tests
18+
CMD ["python", "src/main.py"]

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# MIT License
2+
3+
Copyright (c) 2025 CodeX
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
"# secure-code-game"
1+
# Secure Code Game
2+
## Welcome to Secure Code Game - CodeX! 👋
3+
To get started, please follow the 🛠️ [set up guide](#setup-guide) (if you haven't already).
4+
Refer to the [Hints](#hints) for helpful information and [Tasks](#time-to-start) to head to the challenge directly.
5+
6+
## Overview
7+
This program validates transactions in systems where orders consist of multiple items and payments. It ensures that the financial records are consistent and highlights any discrepancies.
8+
9+
## 📝 Storyline
10+
In another part of the world, a quaint local bookstore was buzzing with excitement as the annual Book Fair approached. Eager to expand their reach, they hastily launched an online platform, hoping to attract book lovers far and wide. However, in their rush, they overlooked critical security measures, leaving their website vulnerable. Now, with hackers lurking in the shadows, the fate of their online store hangs in the balance. Can you uncover and fix the vulnerabilities before it's too late?
11+
12+
## ⌨️ What's in the repo?
13+
For each level, you will find the same file structure:
14+
- `src/` includes the vulnerable code to be reviewed.
15+
- `tests/` contains the unit tests that should still pass after you have implemented your fix.
16+
17+
## 🚦 Time to start!
18+
- [ ] Observe the **Github Workflow** logs, and identify the failing test cases.
19+
- [ ] Open a new **Github Issue** to address the problem and your observations.
20+
- [ ] Create a new **Git Branch** and work on the fix.
21+
1. Review the code in `src/main.py`. Can you spot the bug(s)?
22+
2. Try to fix the bug(s). Ensure that unit tests are still passing 🟢.
23+
- [ ] Make a new **Pull Request** with a description of what you fixed.
24+
- [ ] Observe the GitHub Workflow logs, and ensure that the code is error free.
25+
- [ ] Once all workflows pass, **merge** the pull request.
26+
27+
> [!NOTE]
28+
> You successfully completed the level when the Github Workflow passes 🟢.
29+
30+
## 💡 Hints
31+
The program currently has vulnerabilities related to floating-point arithmetic. Pay close attention to how decimal values are handled during transaction validation.
32+
33+
<!-- Additional hints will be provided if needed -->
34+
35+
# Setup Guide
36+
## Local Installation
37+
```bash
38+
git clone https://github.com/kgchinthana/secure-code-game.git
39+
cd secure-code-game
40+
```
41+
42+
## Usage
43+
```bash
44+
python src/main.py [arguments]
45+
```
46+
47+
## Running Tests
48+
```bash
49+
python -m pytest
50+
```

src/main.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
from collections import namedtuple
2-
from decimal import Decimal
32

3+
Order = namedtuple("Order", "id, items")
4+
Item = namedtuple("Item", "type, description, amount, quantity")
45

5-
Order = namedtuple('Order', 'id, items')
6-
Item = namedtuple('Item', 'type, description, amount, quantity')
7-
8-
9-
MAX_ITEM_AMOUNT = 100000 # maximum price of item in the shop
10-
MAX_QUANTITY = 100 # maximum quantity of an item in the shop
11-
MIN_QUANTITY = 0 # minimum quantity of an item in the shop
12-
MAX_TOTAL = 1e6 # maximum total amount accepted for an order
13-
14-
15-
def validorder(order):
16-
payments = Decimal('0')
17-
expenses = Decimal('0')
186

7+
def validorder(order: Order):
8+
net = 0
199

2010
for item in order.items:
21-
if item.type == 'payment':
22-
# Sets a reasonable min & max value for the invoice amounts
23-
if -MAX_ITEM_AMOUNT <= item.amount <= MAX_ITEM_AMOUNT:
24-
payments += Decimal(str(item.amount))
25-
elif item.type == 'product':
26-
if type(item.quantity) is int and MIN_QUANTITY < item.quantity <= MAX_QUANTITY and MIN_QUANTITY < item.amount <= MAX_ITEM_AMOUNT:
27-
expenses += Decimal(str(item.amount)) * item.quantity
11+
if item.type == "payment":
12+
net += item.amount
13+
elif item.type == "product":
14+
net -= item.amount * item.quantity
2815
else:
2916
return "Invalid item type: %s" % item.type
30-
31-
if abs(payments) > MAX_TOTAL or expenses > MAX_TOTAL:
32-
return "Total amount payable for an order exceeded"
3317

34-
if payments != expenses:
35-
return "Order ID: %s - Payment imbalance: $%0.2f" % (order.id, payments - expenses)
18+
if net != 0:
19+
return "Order ID: %s - Payment imbalance: $%0.2f" % (order.id, net)
3620
else:
37-
return "Order ID: %s - Full payment received!" % order.id
21+
return "Order ID: %s - Full payment received!" % order.id

tests/test_main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,5 @@ def test_5(self):
5151
order_1 = c.Order(id="1", items=[service])
5252
self.assertEqual(c.validorder(order_1), "Invalid item type: service")
5353

54-
5554
if __name__ == "__main__":
5655
unittest.main()

0 commit comments

Comments
 (0)