fix: create pgadmin4.log with mode 0o600

Override EnhancedRotatingFileHandler._open() so the active log file
and rotated backups are created owner-only on POSIX. The parent
DATA_DIR is already 0o700, so this is defense-in-depth — but the
log file can contain sensitive context (auth events, query
fragments, paths) and should not be world/group readable on its own.

Pre-existing log files keep their current permissions; new installs
and rotated files pick up the tighter mode. Windows uses the default
open path since os.open mode is ignored there.
This commit is contained in:
Ashesh Vashi committed 2026-05-01 16:38:49 +05:30
1 parent 6f4f28def7
commit 85366aa128
1 file changed
+15
@@ -7,6 +7,7 @@
#
##########################################################################
import os
import re
from logging import handlers
@@ -46,6 +47,20 @@ class EnhancedRotatingFileHandler(handlers.TimedRotatingFileHandler,
backupCount=backup_count,
encoding=encoding)
# Create new log files with mode 0o600 so they are not world/group
# readable. Pre-existing files keep their permissions; the parent
# DATA_DIR is already 0o700 on POSIX, so this is defense-in-depth.
# On Windows the mode arg to os.open is ignored — fall back to the
# default behavior there.
def _open(self):
if os.name == 'nt':
return super()._open()
flags = os.O_WRONLY | os.O_CREAT | (
os.O_APPEND if self.mode == 'a' else os.O_TRUNC)
fd = os.open(self.baseFilename, flags, 0o600)
return os.fdopen(fd, self.mode, encoding=self.encoding,
errors=getattr(self, 'errors', None))
# Time & Size combined rollover
def shouldRollover(self, record):
return handlers.TimedRotatingFileHandler.shouldRollover(self, record) \