From 85366aa1281f0d10bb92d9817885fffb5f91502a Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Fri, 1 May 2026 14:24:54 +0530 Subject: [PATCH] fix: create pgadmin4.log with mode 0o600 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- web/pgadmin/utils/enhanced_log_rotation.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/web/pgadmin/utils/enhanced_log_rotation.py b/web/pgadmin/utils/enhanced_log_rotation.py index ae45978b4..33897b3c3 100644 --- a/web/pgadmin/utils/enhanced_log_rotation.py +++ b/web/pgadmin/utils/enhanced_log_rotation.py @@ -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) \