You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Run a python script from the command linepython3<<ENDimportastcode="""while b != 0: if a > b: a = a - b else: b = b - areturn a"""tree=ast.parse(code)
# Pretty-printed output of the ASTprint(ast.dump(tree, indent=4))
END
Common Code Security Pitfalls
Hardcoded Secrets and Credentials
# Hardcoded API keyAPI_KEY="my-super-secret-api-key"# Hardcoded database passwordDB_PASSWORD="password123"
importos# Fetch from environment variablesAPI_KEY=os.getenv("API_KEY")
# Fetch from a secure vaultDB_PASSWORD=os.getenv("DB_PASSWORD")
SQL Injection Vulnerabilities
importsqlite3defget_user(username):
conn=sqlite3.connect("users.db")
cursor=conn.cursor()
query=f"SELECT * FROM users WHERE username = '{username}'"# Dangerous: vulnerable to SQL injectioncursor.execute(query)
returncursor.fetchall()
importsqlite3defget_user(username):
conn=sqlite3.connect("users.db")
cursor=conn.cursor()
query="SELECT * FROM users WHERE username = ?"# Parameterized query prevents SQL injectioncursor.execute(query, (username,))
returncursor.fetchall()
Insufficient Input Validation
importosdefexecute_command(user_input):
# Dangerous: could allow arbitrary command executionos.system("echo "+user_input)
@app.route("/admin")defadmin_dashboard():
# No authentication required - anyone can access!return"Admin Panel"
fromflaskimportabort@app.route("/admin")# Requires authentication using the right decorator@login_requireddefadmin_dashboard():
return"Admin Panel"
Bandit: A Security Linter from Python Code Quality Authority (PyCQA)
# Activate the virtual environment
workon menu
# Install Bandit
pip install bandit==1.8.3
bandit -r $HOME/RestQR/
app.run(host="0.0.0.0", port=5001, debug=True)
>> Issue: [B201:flask_debug_true] A Flask app appears to be run with debug=True, which exposes the Werkzeug debugger and allows the execution of arbitrary code.
Severity: High Confidence: Medium
CWE: CWE-94 (https://cwe.mitre.org/data/definitions/94.html)
More Info: https://bandit.readthedocs.io/en/1.8.3/plugins/b201_flask_debug_true.html
Location: /root/RestQR/qr/app.py:30:4
29 if __name__ == "__main__":
30 app.run(host="0.0.0.0", port=5001, debug=True)
--------------------------------------------------
>> Issue: [B104:hardcoded_bind_all_interfaces] Possible binding to all interfaces.
Severity: Medium Confidence: Medium
CWE: CWE-605 (https://cwe.mitre.org/data/definitions/605.html)
More Info: https://bandit.readthedocs.io/en/1.8.3/plugins/b104_hardcoded_bind_all_interfaces.html
Location: /root/RestQR/qr/app.py:30:17
29 if __name__ == "__main__":
30 app.run(host="0.0.0.0", port=5001, debug=True)
[tool.bandit]# List of target folders to scantargets = ["menu", "qr"]
# List of directories and files to excludeexclude_dirs = ["tests", "venv"]
# List of tests to runtests = ["B201", "B301"]
# List of tests to skipskips = ["B101", "B601"]
# This line will not trigger Bandit issuesapp.run(host="0.0.0.0", port=5001, debug=True) # nosec