-
Notifications
You must be signed in to change notification settings - Fork 0
test dast static analysis #63
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
base: dast-test1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1 +1,67 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foobar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from flask import Flask, request, jsonify, Response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import sqlite3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app = Flask(__name__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Database file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DATABASE = 'app.db' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_db_connection(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn = sqlite3.connect(DATABASE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return conn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def init_db(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Create table and insert data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn = get_db_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cursor = conn.cursor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Check if table already exists to prevent overwriting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users';") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if cursor.fetchone() is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cursor.execute("INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25), ('Charlie', 35);") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn.commit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn.close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@app.route('/users', methods=['GET']) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_user(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name = request.args.get('name') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn = get_db_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cursor = conn.cursor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Vulnerable SQL Query from raw string concatenation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
query = f"SELECT * FROM users WHERE name = '{name}'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cursor.execute(query) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# # Fixed SQL Query using parameterized queries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# query = "SELECT * FROM users WHERE name = ?" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# cursor.execute(query, (name,)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user = cursor.fetchone() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn.close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if user: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return jsonify({"id": user[0], "name": user[1], "age": user[2]}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return jsonify({"error": "User not found"}), 404 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+25
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Fix SQL injection vulnerability immediately. The current implementation is vulnerable to SQL injection attacks through raw string concatenation. This is a severe security risk that could allow attackers to:
Apply this fix immediately by uncommenting the secure implementation and removing the vulnerable code: - # Vulnerable SQL Query from raw string concatenation
- query = f"SELECT * FROM users WHERE name = '{name}'"
- cursor.execute(query)
+ # Secure SQL Query using parameterized queries
+ query = "SELECT * FROM users WHERE name = ?"
+ cursor.execute(query, (name,)) Additionally, add input validation: def get_user():
name = request.args.get('name')
+ if not name or not isinstance(name, str):
+ return jsonify({"error": "Invalid name parameter"}), 400
conn = get_db_connection() 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: Linters[error] 25-25: SQL Injection vulnerability in /users endpoint using SQLite database |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@app.route('/.env', methods=['GET']) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_env(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
env_content = """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DB_NAME=crapi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DB_USER=crapi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DB_PASSWORD=crapi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DB_HOST=postgresdb | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DB_PORT=5432 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SERVER_PORT=8080 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MONGO_DB_HOST=mongodb | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MONGO_DB_PORT=27017 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MONGO_DB_USER=crapi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MONGO_DB_PASSWORD=crapi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MONGO_DB_NAME=crapi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return Response(env_content, headers={ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Content-Disposition": "attachment; filename=env" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+46
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Remove endpoint exposing sensitive credentials. This endpoint poses a severe security risk by exposing:
-@app.route('/.env', methods=['GET'])
-def get_env():
- env_content = """
-DB_NAME=crapi
-DB_USER=crapi
-DB_PASSWORD=crapi
-DB_HOST=postgresdb
-DB_PORT=5432
-SERVER_PORT=8080
-MONGO_DB_HOST=mongodb
-MONGO_DB_PORT=27017
-MONGO_DB_USER=crapi
-MONGO_DB_PASSWORD=crapi
-MONGO_DB_NAME=crapi
-"""
- return Response(env_content, headers={
- "Content-Disposition": "attachment; filename=env"
- })
import os
from dotenv import load_dotenv
load_dotenv()
DB_NAME = os.getenv('DB_NAME')
DB_USER = os.getenv('DB_USER')
# ... etc 🧰 Tools🪛 GitHub Actions: Linters[error] 46-46: Sensitive information disclosure vulnerability: .env file is publicly accessible at /.env endpoint |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
init_db() # Initialize the database and populate it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.run(host="0.0.0.0", debug=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security: Disable debug mode and restrict network binding. Running with debug=True in production is a security risk as it can expose sensitive information and allow code execution through the debugger. Modify the run configuration: if __name__ == '__main__':
init_db() # Initialize the database and populate it
- app.run(host="0.0.0.0", debug=True)
+ debug_mode = os.getenv('FLASK_DEBUG', 'False').lower() == 'true'
+ app.run(
+ host="127.0.0.1", # Only bind to localhost unless explicitly needed
+ debug=debug_mode, # Control through environment variable
+ ) 📝 Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance database connection handling.
The current implementation lacks proper error handling and connection management.
Consider implementing:
📝 Committable suggestion