Flask Code Review
Start Timer
0:00:00
Let’s say we’re reviewing this Flask service before it ships.
Find the security flaws in the code and show how you would fix each one.
import hashlib
import sqlite3
import subprocess
from flask import Flask, request, send_file
app = Flask(__name__)
db = sqlite3.connect("app.db", check_same_thread=False)
@app.route("/login", methods=["POST"])
def login():
user = request.form["user"]
pw_hash = hashlib.md5(request.form["password"].encode()).hexdigest()
row = db.execute(
f"SELECT id FROM users WHERE name = '{user}' AND pw_hash = '{pw_hash}'"
).fetchone()
return ("ok", 200) if row else ("denied", 401)
@app.route("/download")
def download():
return send_file("/srv/uploads/" + request.args["name"])
@app.route("/ping")
def ping():
host = request.args["host"]
return subprocess.check_output(f"ping -c 1 {host}", shell=True)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
.
.
.
.
.
.
.
.
.
Topic
Responsible AI & Security
Tags
Python
Security
User Authentication
Category
Technical Coding
Position
Security Engineer
Difficulty
Medium
Success
N/A
Last asked
This question was asked on
Companies
What did you think?
Comments