Skip to content

Potential fix for code scanning alert no. 1: Uncontrolled command line#82

Merged
karam-ajaj merged 1 commit into
mainfrom
alert-autofix-1
Jan 2, 2026
Merged

Potential fix for code scanning alert no. 1: Uncontrolled command line#82
karam-ajaj merged 1 commit into
mainfrom
alert-autofix-1

Conversation

@karam-ajaj
Copy link
Copy Markdown
Owner

Potential fix for https://github.com/karam-ajaj/atlas/security/code-scanning/1

In general, to fix uncontrolled command line usage, any user-provided value that influences a command should be either (a) selected from a hard-coded allowlist or (b) passed through strict validation that limits it to safe characters and reasonable length, and commands should be invoked with shell=False and argument lists.

In this file, the Docker container branch in stream_log already uses validate_container_name before building cmd. The remaining untrusted influence is the filename path segment for log files. We should introduce a validate_log_filename helper that mirrors the container name validation but is tailored to filenames, and apply it before constructing the filepath and cmd. The helper should reject empty names, overly long names, names with path separators (/ or \), and characters outside a conservative set such as letters, digits, dots, underscores, and hyphens. This keeps existing behavior (reading logs from within LOGS_DIR) while ensuring that only simple file names are accepted; deeper directory traversal remains blocked by the existing os.path.commonpath check.

Concretely:

  • Add a new function validate_log_filename(name: str) -> str near validate_container_name that raises HTTPException with 400 for invalid input.
  • In stream_log, inside the else branch (non-container: case), call validate_log_filename(filename) and use the returned safe value when building the path.
  • Keep the rest of the logic (path normalization, commonpath check, existence check, tail invocation) unchanged.

All required imports (HTTPException, re, os, subprocess) are already present; no new imports are necessary.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@karam-ajaj karam-ajaj marked this pull request as ready for review January 2, 2026 19:03
Copilot AI review requested due to automatic review settings January 2, 2026 19:03
@karam-ajaj karam-ajaj merged commit 6ecfe69 into main Jan 2, 2026
5 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a code scanning security alert by adding input validation to prevent uncontrolled command line injection in the log streaming endpoint. The fix introduces a new validation function for log filenames and applies it before constructing file paths and passing them to subprocess calls.

  • Adds validate_log_filename() function following the same pattern as existing validate_container_name() validation
  • Applies validation to the stream_log endpoint's non-container branch before file path construction
  • Uses character whitelisting and path separator rejection to prevent directory traversal and command injection

Comment thread config/scripts/app.py
Comment on lines +268 to +269
if not re.fullmatch(r"[a-zA-Z0-9._-]+", name):
raise HTTPException(status_code=400, detail="Invalid log filename format")
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation regex allows dots in filenames, which could potentially be used for path traversal via sequences like .. (parent directory). While the os.path.normpath and os.path.commonpath checks on lines 296-298 provide defense in depth, it would be safer to either disallow consecutive dots or add explicit validation to reject names containing .. sequences before the path construction step. This would provide earlier detection of malicious input.

Copilot uses AI. Check for mistakes.
@karam-ajaj karam-ajaj deleted the alert-autofix-1 branch January 2, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants