-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecure Code.py
More file actions
79 lines (66 loc) · 2.6 KB
/
Copy pathSecure Code.py
File metadata and controls
79 lines (66 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Vulnerable Flask Application - FOR EDUCATIONAL PURPOSES ONLY
from flask import Flask, request, render_template, redirect, session
import sqlite3
app = Flask(__name__)
# VULNERABILITY: Hardcoded secret key
# This is a major security risk as it can be easily exposed.
app.secret_key = 'this-is-a-very-insecure-hardcoded-secret-key'
# Insecure database connection function
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
# A simple user table
# In a real app, passwords would be hashed and salted.
def create_table():
conn = get_db_connection()
conn.execute('CREATE TABLE IF NOT EXISTS users (username TEXT, password TEXT, bio TEXT)')
conn.commit()
conn.close()
create_table()
# VULNERABILITY: SQL Injection
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = get_db_connection()
# VULNERABILITY: Directly concatenating user input into the SQL query
# An attacker can use ' OR 1=1 -- to bypass authentication.
sql_query = f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'"
user = conn.execute(sql_query).fetchone()
conn.close()
if user:
session['logged_in'] = True
session['username'] = user['username']
return redirect('/profile')
else:
return 'Invalid credentials'
return render_template('login.html')
# VULNERABILITY: Cross-Site Scripting (XSS)
@app.route('/profile')
def profile():
if not session.get('logged_in'):
return redirect('/login')
conn = get_db_connection()
user = conn.execute('SELECT * FROM users WHERE username = ?', (session['username'],)).fetchone()
conn.close()
# VULNERABILITY: The bio field is rendered without sanitization.
# An attacker can store a script in their bio that will execute on this page.
profile_html = f"""
<h1>Welcome, {user['username']}</h1>
<p>Bio: {user['bio']}</p>
"""
return profile_html # DANGEROUS: This returns raw HTML, which is a major XSS risk.
# A simple HTML template for login
@app.route('/login.html')
def login_page():
return """
<form method="post" action="/login">
Username: <input name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
"""
if __name__ == '__main__':
app.run(debug=True)