Skip to content

Commit 0e6cb16

Browse files
compare to semgrep
1 parent a441fbc commit 0e6cb16

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

.github/workflows/dast.yml

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
---
2-
name: Linters
3-
1+
name: Security Checks
42
on:
5-
workflow_dispatch:
63
pull_request:
74
push:
85

96
jobs:
10-
"dast-test":
11-
runs-on: ubuntu-latest
12-
container: python:3.11
13-
14-
steps:
15-
- uses: actions/checkout@v2
16-
17-
- name: Install dependencies
18-
run: cat results.sarif && exit 1
7+
semgrep:
8+
runs-on: ubuntu-latest
9+
container:
10+
image: returntocorp/semgrep:latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Run Semgrep
15+
env:
16+
SEMGREP_RULES: >-
17+
p/security-audit
18+
p/owasp-top-ten
19+
p/javascript
20+
p/python
21+
run: semgrep ci
22+

app.py

+67-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
1-
foobar
1+
from flask import Flask, request, jsonify, Response
2+
import sqlite3
3+
4+
app = Flask(__name__)
5+
6+
# Database file
7+
DATABASE = 'app.db'
8+
9+
def get_db_connection():
10+
conn = sqlite3.connect(DATABASE)
11+
return conn
12+
13+
def init_db():
14+
# Create table and insert data
15+
conn = get_db_connection()
16+
cursor = conn.cursor()
17+
# Check if table already exists to prevent overwriting
18+
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users';")
19+
if cursor.fetchone() is None:
20+
cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);')
21+
cursor.execute("INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25), ('Charlie', 35);")
22+
conn.commit()
23+
conn.close()
24+
25+
@app.route('/users', methods=['GET'])
26+
def get_user():
27+
name = request.args.get('name')
28+
conn = get_db_connection()
29+
cursor = conn.cursor()
30+
31+
# Vulnerable SQL Query from raw string concatenation
32+
query = f"SELECT * FROM users WHERE name = '{name}'"
33+
cursor.execute(query)
34+
35+
# # Fixed SQL Query using parameterized queries
36+
# query = "SELECT * FROM users WHERE name = ?"
37+
# cursor.execute(query, (name,))
38+
39+
user = cursor.fetchone()
40+
conn.close()
41+
if user:
42+
return jsonify({"id": user[0], "name": user[1], "age": user[2]})
43+
else:
44+
return jsonify({"error": "User not found"}), 404
45+
46+
@app.route('/.env', methods=['GET'])
47+
def get_env():
48+
env_content = """
49+
DB_NAME=crapi
50+
DB_USER=crapi
51+
DB_PASSWORD=crapi
52+
DB_HOST=postgresdb
53+
DB_PORT=5432
54+
SERVER_PORT=8080
55+
MONGO_DB_HOST=mongodb
56+
MONGO_DB_PORT=27017
57+
MONGO_DB_USER=crapi
58+
MONGO_DB_PASSWORD=crapi
59+
MONGO_DB_NAME=crapi
60+
"""
61+
return Response(env_content, headers={
62+
"Content-Disposition": "attachment; filename=env"
63+
})
64+
65+
if __name__ == '__main__':
66+
init_db() # Initialize the database and populate it
67+
app.run(host="0.0.0.0", debug=True)

0 commit comments

Comments
 (0)