From 5514fdd7aab5ae58ded6bca862e7901e42803c09 Mon Sep 17 00:00:00 2001 From: Semgrep Autofix Date: Tue, 17 Mar 2026 09:28:09 +0000 Subject: [PATCH] Fix SQL injection vulnerability in user registration Fix SQL injection vulnerability in `flask_webgoat/users.py` by replacing string formatting with parameterized queries. ## Changes - Replaced manual SQL string construction using `%` formatting with parameterized query placeholders (`?`) - Passed user inputs (`username`, `password`, `access_level`) as parameters to `query_db` instead of embedding them directly in the query string ## Why The original code used string formatting to construct the SQL INSERT statement, which allowed user-controlled input to be directly interpolated into the query. This created a SQL injection vulnerability where an attacker could manipulate the query structure by providing malicious input values. By using parameterized queries, the database driver handles proper escaping and keeps user data separate from the SQL command structure, preventing injection attacks. ## Semgrep Finding Details Detected user input used to manually construct a SQL string. This is usually bad practice because manual construction could accidentally result in a SQL injection. An attacker could use a SQL injection to steal or modify contents of the database. Instead, use a parameterized query which is available by default in most database engines. Alternatively, consider using the Django object-relational mappers (ORM) instead of raw SQL queries. @267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169013) from the detection rule [python.django.security.injection.tainted-sql-string.tainted-sql-string](https://semgrep.dev/r/python.django.security.injection.tainted-sql-string.tainted-sql-string). --- flask_webgoat/users.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/flask_webgoat/users.py b/flask_webgoat/users.py index a72e698e..cc9aa4ff 100644 --- a/flask_webgoat/users.py +++ b/flask_webgoat/users.py @@ -34,14 +34,10 @@ def create_user(): 402, ) - # vulnerability: SQL Injection - query = ( - "INSERT INTO user (username, password, access_level) VALUES ('%s', '%s', %d)" - % (username, password, int(access_level)) - ) + query = "INSERT INTO user (username, password, access_level) VALUES (?, ?, ?)" try: - query_db(query, [], False, True) + query_db(query, [username, password, int(access_level)], False, True) return jsonify({"success": True}) except sqlite3.Error as err: return jsonify({"error": "could not create user:" + err})