Skip to content

Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression#7

Merged
PRATHAM777P merged 1 commit into
mainfrom
alert-autofix-9
Apr 27, 2026
Merged

Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression#7
PRATHAM777P merged 1 commit into
mainfrom
alert-autofix-9

Conversation

@PRATHAM777P
Copy link
Copy Markdown
Owner

Potential fix for https://github.com/PRATHAM777P/IPWatchdog/security/code-scanning/9

Use a stricter, explicit path-allowlist validation with pathlib.Path.resolve() and enforce containment against configured roots using relative-path checks. Then reduce race risk by opening exactly the validated resolved path and removing redundant pre-checks.

Best fix in this snippet:

  1. In app.py, update _validated_monitor_log_path:
    • Resolve user path with Path(user_path).resolve(strict=True) (must exist).
    • Resolve each allowed root with Path(...).resolve(strict=True) and ensure it is a directory.
    • Accept only when resolved_user.relative_to(resolved_root) succeeds.
    • Return a string path.
  2. In monitor_start, replace os.path.isfile(...) check with error handling around validation/open lifecycle:
    • Keep validation errors as 400/403.
    • Treat missing file as 404.
  3. In tail_worker, keep using the validated resolved path.

This preserves behavior (monitoring allowed local log files) while making sanitization clearer and stronger for both security and static analysis.

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

…n path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@PRATHAM777P PRATHAM777P marked this pull request as ready for review April 27, 2026 22:52
@PRATHAM777P PRATHAM777P merged commit 8c81c95 into main Apr 27, 2026
1 of 2 checks passed
Comment thread app.py

real_log_path = os.path.realpath(user_path)
try:
real_log_path = Path(user_path).resolve(strict=True)
@PRATHAM777P PRATHAM777P deleted the alert-autofix-9 branch April 27, 2026 22:52
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