-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_bad.py
More file actions
52 lines (45 loc) · 1.57 KB
/
Copy pathsample_bad.py
File metadata and controls
52 lines (45 loc) · 1.57 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
import logging
import sqlite3
# 1) Logging PII directly without masking
def log_user_info(user):
# Logs name, email and IP address in plain text
logging.info(f"User connected: name={user['name']}, email={user['email']}, ip={user['ip_address']}")
# 2) Storing personal data in a plain-text file
def save_user_to_file(user):
with open("user_data.csv", "a") as f:
# Stores name and email without consent or encryption
f.write(f"{user['name']},{user['email']}\n")
# 3) Inserting PII directly into a database without parameterization (and no deletion path)
def store_user_in_db(user):
conn = sqlite3.connect("users.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
address TEXT
)
""")
# Direct string interpolation — vulnerable to injection and storing PII unsafely
query = f"""
INSERT INTO users (name, email, address)
VALUES ('{user['name']}', '{user['email']}', '{user['address']}')
"""
cursor.execute(query)
conn.commit()
conn.close()
def main():
# Sample user with PII
user = {
"name": "Alice Smith",
"email": "alice.smith@example.com",
"ip_address": "203.0.113.42",
"address": "123 Main St, Springfield"
}
log_user_info(user)
save_user_to_file(user)
store_user_in_db(user)
# NOTE: There is no function here to delete or anonymize user data → violates "right to be forgotten"
if __name__ == "__main__":
main()