fix: prevent root-owned database files when running with sudo

When start.sh runs via sudo, chown instance/ and data/ back to the
invoking user so the SQLite DB stays accessible without sudo. Also
adds a clear error message in get_connection() when the DB can't be
opened due to permissions.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Smittix
2026-03-02 09:59:41 +00:00
parent 56ec2bcba4
commit 17a70beb3a
2 changed files with 38 additions and 21 deletions
+9
View File
@@ -78,6 +78,15 @@ done
export INTERCEPT_HOST="$HOST" export INTERCEPT_HOST="$HOST"
export INTERCEPT_PORT="$PORT" export INTERCEPT_PORT="$PORT"
# ── Fix ownership of user data dirs when run via sudo ────────────────────────
if [[ "$(id -u)" -eq 0 && -n "${SUDO_USER:-}" ]]; then
for dir in instance data; do
if [[ -d "$SCRIPT_DIR/$dir" ]]; then
chown -R "$SUDO_USER" "$SCRIPT_DIR/$dir"
fi
done
fi
# ── Dependency check (delegate to intercept.py) ───────────────────────────── # ── Dependency check (delegate to intercept.py) ─────────────────────────────
if [[ "$CHECK_DEPS" -eq 1 ]]; then if [[ "$CHECK_DEPS" -eq 1 ]]; then
exec "$PYTHON" intercept.py --check-deps exec "$PYTHON" intercept.py --check-deps
+12 -4
View File
@@ -35,10 +35,18 @@ def get_connection() -> sqlite3.Connection:
"""Get a thread-local database connection.""" """Get a thread-local database connection."""
if not hasattr(_local, 'connection') or _local.connection is None: if not hasattr(_local, 'connection') or _local.connection is None:
db_path = get_db_path() db_path = get_db_path()
_local.connection = sqlite3.connect(str(db_path), check_same_thread=False) try:
_local.connection.row_factory = sqlite3.Row _local.connection = sqlite3.connect(str(db_path), check_same_thread=False)
# Enable foreign keys _local.connection.row_factory = sqlite3.Row
_local.connection.execute('PRAGMA foreign_keys = ON') # Enable foreign keys
_local.connection.execute('PRAGMA foreign_keys = ON')
except sqlite3.OperationalError as e:
logger.error(
f"Cannot open database at {db_path}: {e}. "
f"If the file is owned by root, fix with: "
f"sudo chown -R $(whoami) {DB_DIR}"
)
raise
return _local.connection return _local.connection