Skip to content

Commit

Permalink
Validating Log File Path for cli function
Browse files Browse the repository at this point in the history
  • Loading branch information
rajithkrishnegowda committed Nov 4, 2024
1 parent 7aca44e commit 71c6d2d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions openfl/interface/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,17 @@ def cli(context, log_level, no_warnings):
# This will be overridden later with user selected debugging level
disable_warnings()
log_file = os.getenv("LOG_FILE")
# Validate log_file using allow list approach
if log_file and not re.match(r"^[\w\-.]+$", log_file):
raise ValueError("Invalid log file path")
# Validate log_file with tighter restrictions
if log_file:
log_file = os.path.normpath(log_file)
if not re.match(r"^logs/[\w\-.]+$", log_file) or ".." in log_file or log_file.startswith("/"):
raise ValueError("Invalid log file path")

# Ensure the log file is in the 'logs' directory
allowed_directory = Path("logs").resolve()
full_path = (allowed_directory / log_file).resolve()
if not str(full_path).startswith(str(allowed_directory)):
raise ValueError("Log file path is not allowed")
setup_logging(log_level, log_file)
sys.stdout.reconfigure(encoding="utf-8")

Expand Down

0 comments on commit 71c6d2d

Please sign in to comment.