Skip to content

Commit f7b4bcc

Browse files
author
Open Source Contributor
committed
security: add whitelist validation for SQLite PRAGMA settings
- Add allowed_journal_modes and allowed_sync_modes whitelists - Validate Config.db.journal_mode and Config.db.synchronous_mode against whitelists - Raise ValueError if invalid values are provided This prevents potential SQL injection in PRAGMA statements.
1 parent 65e27fb commit f7b4bcc

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

nettacker/database/db.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ def create_connection():
6060
cursor = connection.cursor()
6161

6262
# Performance enhancing configurations. Put WAL cause that helps with concurrency.
63-
cursor.execute(f"PRAGMA journal_mode={Config.db.journal_mode}")
64-
cursor.execute(f"PRAGMA synchronous={Config.db.synchronous_mode}")
63+
allowed_journal_modes = {"DELETE", "WAL", "MEMORY", "OFF"}
64+
allowed_sync_modes = {"OFF", "NORMAL", "FULL", "EXTRA"}
65+
journal_mode = Config.db.journal_mode
66+
sync_mode = Config.db.synchronous_mode
67+
if journal_mode not in allowed_journal_modes:
68+
raise ValueError(f"Invalid journal_mode: {journal_mode}")
69+
if sync_mode not in allowed_sync_modes:
70+
raise ValueError(f"Invalid synchronous_mode: {sync_mode}")
71+
cursor.execute(f"PRAGMA journal_mode={journal_mode}")
72+
cursor.execute(f"PRAGMA synchronous={sync_mode}")
6573

6674
return connection, cursor
6775
except Exception as e:

0 commit comments

Comments
 (0)