-
Notifications
You must be signed in to change notification settings - Fork 0
Add transfer API #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import sqlite3 | ||
| from flask import request, jsonify | ||
| from functools import wraps | ||
|
|
||
|
|
||
| def token_required(f): | ||
| @wraps(f) | ||
| def decorated(*args, **kwargs): | ||
| token = request.headers.get("Authorization", "").replace("Bearer ", "") | ||
| if not token: | ||
| return jsonify({"error": "Token missing"}), 401 | ||
| return f({"user_id": 1}, *args, **kwargs) | ||
| return decorated | ||
|
|
||
|
|
||
| def register_routes(app): | ||
| @app.route("/api/transfer", methods=["POST"]) | ||
| @token_required | ||
| def api_transfer(current_user): | ||
| data = request.get_json() | ||
| to_account = data.get("to_account") | ||
| amount = data.get("amount") | ||
|
|
||
| conn = sqlite3.connect("bank.db") | ||
| c = conn.cursor() | ||
|
|
||
| c.execute(f"SELECT balance FROM users WHERE id = {current_user['user_id']}") | ||
| balance = c.fetchone()[0] | ||
|
|
||
| if balance >= amount: | ||
| c.execute( | ||
| f"UPDATE users SET balance = balance - {amount} WHERE id = {current_user['user_id']}" | ||
| ) | ||
| c.execute( | ||
| f"UPDATE users SET balance = balance + {amount} WHERE account_number='{to_account}'" | ||
| ) | ||
| conn.commit() | ||
|
|
||
| c.execute( | ||
| f"SELECT username, balance FROM users WHERE account_number='{to_account}'" | ||
| ) | ||
| recipient = c.fetchone() | ||
| conn.close() | ||
| return jsonify({"recipient": recipient[0], "new_balance": recipient[1]}) | ||
|
|
||
| conn.close() | ||
| return jsonify({"error": "Insufficient funds"}), 400 | ||
|
Comment on lines
+19
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Steps to ReproduceFix with AITriage: Reply |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
api_transferfunction intransfer.pyconstructs SQL queries using f-strings with unsanitized user input from the request body (to_accountandamount). This allows an attacker to inject arbitrary SQL commands, potentially leading to unauthorized balance modifications or data exfiltration. Specifically, theto_accountfield is concatenated directly into the SQL query strings on lines 35 and 40.Steps to Reproduce
Fix with AI
Triage: Reply
!fp <reason>(false positive),!valid(confirmed), or!accepted_risk <reason>. Any other reply is saved as a triage note.Reason is optional but improves future scans — e.g.
!fp internal endpoint, not user-facing.View finding in Hacktron