-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_app.py
More file actions
371 lines (355 loc) · 18.4 KB
/
Copy pathsecure_app.py
File metadata and controls
371 lines (355 loc) · 18.4 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from flask import Flask, request, render_template_string, session, redirect, url_for
from werkzeug.security import generate_password_hash, check_password_hash
from markupsafe import escape
import sqlite3
import os
import subprocess
import re
import platform
app = Flask(__name__)
# FIX 1: Secrets should be loaded from environment variables, never hardcoded.
# Example: export SECRET_KEY="your-production-secret-key"
app.secret_key = os.environ.get("SECRET_KEY", "default-dev-key")
def init_db():
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute('''DROP TABLE IF EXISTS users''') # Drop to ensure fresh schema with hashed passwords
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT)''')
# FIX 5: Hash passwords using PBKDF2 (Werkzeug default)
hashed_pw = generate_password_hash('admin123')
cursor.execute('''INSERT INTO users (id, username, password) VALUES (1, 'admin', ?)''', (hashed_pw,))
conn.commit()
conn.close()
@app.route('/')
def index():
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secure App Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; border-bottom: 1px solid #edf2f7; padding: 15px 20px; }
.card-body { padding: 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Secure Dashboard</div>
<div class="card-body">
<p class="mb-4">Welcome to the secure app! Select a module to test:</p>
<div class="d-grid gap-2">
<a href="/login" class="btn btn-outline-success">Login (Secure)</a>
<a href="/greet" class="btn btn-outline-success">Greet (Secure)</a>
<a href="/ping" class="btn btn-outline-success">Ping (Secure)</a>
<a href="/admin" class="btn btn-outline-success">Admin (Secure)</a>
</div>
</div>
</div>
</div>
</body>
</html>
'''
# FIX 2 & FIX 5: Prevent SQL Injection using parameterized queries & verify hashed password
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
# Secure SQL query using parameterized query (?) - fetch by username
query = "SELECT * FROM users WHERE username = ?"
cursor.execute(query, (username,))
user = cursor.fetchone()
# Verify the hash securely using check_password_hash
if user and check_password_hash(user[2], password):
session['logged_in'] = True
session['username'] = username
return redirect(url_for('admin'))
else:
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Failed</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Login Failed</div>
<div class="card-body text-center p-4">
<h4 class="text-danger mb-3">Invalid credentials.</h4>
<a href="/login" class="btn btn-secondary mt-3">Try Again</a>
</div>
</div>
</div>
</body>
</html>
'''
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 500px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Account Login</div>
<div class="card-body p-4">
<form method="POST">
<div class="mb-3">
<label class="form-label text-muted">Username</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="mb-4">
<label class="form-label text-muted">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<button type="submit" class="btn btn-success w-100 mb-2">Sign In</button>
<div class="text-center">
<a href="/" class="text-decoration-none text-muted small">Return to Dashboard</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
'''
# FIX 3: Prevent XSS using templating engine variables (Jinja2 auto-escaping)
@app.route('/greet')
def greet():
name = request.args.get('name', 'Guest')
# Secure against XSS: Using Jinja2 context variables ensures auto-escaping
template = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Greet</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Greeting Service</div>
<div class="card-body text-center p-4">
<h3 class="mb-4">Hello, {{ name }}!</h3>
<p class="text-muted small mb-4">Try adding ?name=<script>alert(1)</script> to the URL. It will be safely escaped.</p>
<a href="/" class="btn btn-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
'''
return render_template_string(template, name=name)
# FIX 4: Prevent Command Injection by using subprocess with argument lists and input validation
@app.route('/ping')
def ping():
ip = request.args.get('ip', '127.0.0.1')
# Input validation: Ensure the input looks like an IP address
if not re.match(r"^\d{1,3}(\.\d{1,3}){3}$", ip):
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ping Error</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: black; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Network Ping Tool</div>
<div class="card-body text-center p-4">
<h4 class="text-danger mb-3">Invalid IP address format.</h4>
<a href="/" class="btn btn-secondary mt-3">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
''', 400
try:
# Secure: Using subprocess.run with a list of arguments avoids shell=True risks
count_flag = "-n" if platform.system() == "Windows" else "-c"
result = subprocess.run(['ping', count_flag, '1', ip], capture_output=True, text=True, timeout=5)
response_text = escape(result.stdout)
return f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ping Utility</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {{ background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }}
.banner {{ background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }}
.container-main {{ max-width: 700px; margin: 40px auto; }}
.card {{ border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }}
.card-header {{ background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }}
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Network Ping Tool</div>
<div class="card-body p-4">
<p class="text-muted small mb-3">Ping results for IP: {ip}</p>
<div class="bg-light p-3 rounded border mb-4">
<pre class="mb-0" style="white-space: pre-wrap;">{response_text}</pre>
</div>
<div class="text-center">
<a href="/" class="btn btn-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</div>
</body>
</html>
'''
except Exception as e:
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ping Error</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Network Ping Tool</div>
<div class="card-body text-center p-4">
<h4 class="text-danger mb-3">An error occurred during ping.</h4>
<a href="/" class="btn btn-secondary mt-3">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
'''
# FIX 6: Prevent Missing Authentication by requiring a valid session
@app.route('/admin')
def admin():
if not session.get('logged_in'):
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unauthorized</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Admin Access</div>
<div class="card-body text-center p-4">
<h4 class="text-danger mb-3">Unauthorized. Please login first.</h4>
<a href="/login" class="btn btn-secondary mt-3">Go to Login</a>
</div>
</div>
</div>
</body>
</html>
''', 401
username = escape(session.get('username'))
return f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {{ background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }}
.banner {{ background-color: #cff4fc; color: #055160; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #b6effb; }}
.container-main {{ max-width: 600px; margin: 40px auto; }}
.card {{ border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }}
.card-header {{ background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }}
</style>
</head>
<body>
<div class="banner">✅ This application demonstrates secure coding practices.</div>
<div class="container container-main">
<div class="card border-success">
<div class="card-header bg-success text-white">Admin Dashboard</div>
<div class="card-body p-4">
<h5 class="text-success mb-3">Authentication Successful</h5>
<p class="mb-4">Welcome to the secure admin area, <strong>{username}</strong>.</p>
<a href="/" class="btn btn-outline-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
'''
if __name__ == '__main__':
init_db()
app.run(debug=True)