Skip to content

Patched results for branch: main #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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
}

def get_data_by_config_value(value):
# This might look suspicious due to string concatenation with values from CONFIG.
query = "SELECT * FROM " + CONFIG["default_table"] + " WHERE " + CONFIG["default_column"] + " = '" + value + "'"
# Use a parameterized query to prevent SQL injection
query = f"SELECT * FROM {CONFIG['default_table']} WHERE {CONFIG['default_column']} = ?"

connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute(query)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')

Avoiding SQL string concatenation: untrusted input concatenated with raw SQL query can result in SQL Injection. In order to execute raw query safely, prepared statement should be used. SQLAlchemy provides TextualSQL to easily used prepared statement with named parameters. For complex SQL composition, use SQL Expression Language or Schema Definition Language. In most cases, SQLAlchemy ORM will be a better option.

cursor.execute(query, (value,))
result = cursor.fetchall()
connection.close()

return result

# Test
print(get_data_by_config_value("admin"))
print(get_data_by_config_value("admin"))