Skip to content

Commit 71c4bd8

Browse files
qchappCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent dcd5c43 commit 71c4bd8

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/ai_agent/utils/cache_db.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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()

0 commit comments

Comments
 (0)