Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Finding eval(), exec(), and Insecure SQL Queries

Injection occurs when untrusted input is treated as code or as part of a command. In Python, this most often manifests in three ways:

Dangerous Dynamic Execution: eval() and exec()

Both eval() and exec() execute strings as Python code.

user_input = input("Enter calculation: ")
result = eval(user_input)
print(result)

If a user enters:

__import__("os").system("rm -rf /")

When using Python Code Audit with do the command:

codeaudit filescan eval_sample.py

You see that the findings are:

sast-findings-input

Both input and eval are detected; since both can be exploited, identifying them is essential. This ensures that when using Python Code Audit, the detection of common injection attacks is fully supported.

You have remote command execution.

What to Look for in SAST findings

Use of:

Since no SAST for Python will detect anything that might cause weakness, be alert on:

Insecure SQL Queries

A common pattern in Python applications using SQLite, MySQL, or PostgreSQL:

query = f"SELECT * FROM users WHERE username = '{username}'"
cursor.execute(query)

If username is:

admin' OR '1'='1

The attacker bypasses authentication.

What to Look for

Secure Alternative

cursor.execute("SELECT * FROM users WHERE username = ?", (username,))

Or with libraries such as SQLAlchemy, using parameterised queries or ORM query builders.

Testing Techniques