Skip to content

Commit 5e3ef88

Browse files
author
TomacGG
committed
Fixed SQL Injection Vulnerabilities
1 parent 4209b7a commit 5e3ef88

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

content/app.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
def log_request():
2020
log_file.write(f"{request.method} {request.path} {dict(request.form) if request.form else ''}\n")
2121

22-
2322
# Set user_id on request if user is logged in, or else set it to None.
2423
@app.before_request
2524
def check_authentication():
@@ -48,15 +47,28 @@ def get_comments_page(quote_id):
4847
@app.route("/quotes", methods=["POST"])
4948
def post_quote():
5049
with db:
51-
db.execute(f"""insert into quotes(text,attribution) values("{request.form['text']}","{request.form['attribution']}")""")
50+
db.execute("""
51+
INSERT INTO quotes (text, attribution)
52+
VALUES (:text, :attribution)
53+
""", {"text": request.form['text'], "attribution": request.form['attribution']}
54+
)
55+
5256
return redirect("/#bottom")
5357

5458

5559
# Post a new comment
5660
@app.route("/quotes/<int:quote_id>/comments", methods=["POST"])
5761
def post_comment(quote_id):
5862
with db:
59-
db.execute(f"""insert into comments(text,quote_id,user_id) values("{request.form['text']}",{quote_id},{request.user_id})""")
63+
db.execute("""
64+
INSERT INTO comments (text, quote_id, user_id)
65+
VALUES (:text, :quote_id, :user_id)
66+
""", {
67+
"text": request.form['text'],
68+
"quote_id": quote_id,
69+
"user_id": request.user_id
70+
}
71+
)
6072
return redirect(f"/quotes/{quote_id}#bottom")
6173

6274

@@ -65,16 +77,30 @@ def post_comment(quote_id):
6577
def signin():
6678
username = request.form["username"].lower()
6779
password = request.form["password"]
80+
81+
user = db.execute("""
82+
SELECT id, password
83+
FROM users
84+
WHERE name = :username
85+
""", {"username": username}
86+
).fetchone()
6887

69-
user = db.execute(f"select id, password from users where name='{username}'").fetchone()
7088
if user: # user exists
7189
if password != user['password']:
7290
# wrong! redirect to main page with an error message
7391
return redirect('/?error='+urllib.parse.quote("Invalid password!"))
7492
user_id = user['id']
7593
else: # new sign up
7694
with db:
77-
cursor = db.execute(f"insert into users(name,password) values('{username}', '{password}')")
95+
cursor = db.execute("""
96+
INSERT INTO users (name, password)
97+
VALUES (:username, :password)
98+
""", {
99+
"username": username,
100+
"password": password
101+
}
102+
)
103+
78104
user_id = cursor.lastrowid
79105

80106
response = make_response(redirect('/'))

0 commit comments

Comments
 (0)