File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,10 +60,27 @@ class CacheDB:
6060 * All values are stored as plain text; callers handle serialisation.
6161 """
6262
63+ @staticmethod
64+ def _ensure_private_db_file (path : str ) -> None :
65+ """Ensure an on-disk SQLite DB file is owner-readable/writable only.
66+
67+ This is a best-effort hardening step for local cache data. For new
68+ files, create the file first so we can set restrictive permissions
69+ before SQLite opens it. For existing files, tighten permissions.
70+ """
71+ try :
72+ fd = os .open (path , os .O_CREAT | os .O_EXCL | os .O_WRONLY , 0o600 )
73+ except FileExistsError :
74+ os .chmod (path , 0o600 )
75+ else :
76+ os .close (fd )
77+ os .chmod (path , 0o600 )
78+
6379 def __init__ (self , db_path : str | Path | None = None ) -> None :
6480 path = str (db_path or _DEFAULT_DB_PATH )
6581 if path != ":memory:" :
6682 Path (path ).parent .mkdir (parents = True , exist_ok = True )
83+ self ._ensure_private_db_file (path )
6784
6885 self ._conn = sqlite3 .connect (path , check_same_thread = False )
6986 self ._lock = threading .Lock ()
You can’t perform that action at this time.
0 commit comments