Skip to content

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

Merged
ArshVermaGit merged 1 commit into
mainfrom
alert-autofix-27
May 21, 2026
Merged

Potential fix for code scanning alert no. 23: Uncontrolled data used in path expression#19
ArshVermaGit merged 1 commit into
mainfrom
alert-autofix-27

Conversation

@ArshVermaGit
Copy link
Copy Markdown
Owner

Potential fix for https://github.com/ArshVermaGit/SentinelOps-Autonomous-DevOps-AI/security/code-scanning/23

General fix: ensure user-controlled paths are canonicalized and constrained to an allowlisted root, then avoid constructing secondary paths in ways that can be interpreted as path injection. Prefer direct checks on expected repository metadata paths.

Best fix here (minimal behavior change): in LocalGitService._validate_repo_path_for_linking (in sentinelops-backend/app/services/local_git_service.py), replace the git_dir = realpath(join(normalized, ".git")) + commonpath pattern with a deterministic check of os.path.join(normalized, ".git") and require it to be a real directory via os.path.isdir and not os.path.islink. This prevents accepting repos where .git is a symlink to somewhere else and removes the tainted realpath(join(...)) flow CodeQL highlights.

Edit region: method _validate_repo_path_for_linking, around current lines 139–146.

No new imports or dependencies are required.

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

…in path expression

Co-authored-by: Arsh Verma <arshverma.dev@gmail.com>

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Owner Author

@ArshVermaGit ArshVermaGit left a comment

Choose a reason for hiding this comment

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

This looks like a reasonable minimal-risk refinement. Moving away from the realpath(join(...)) + commonpath pattern toward a deterministic check on os.path.join(normalized, ".git"), combined with requiring .git to be a real directory and not a symlink, makes the validation logic simpler and more explicit. I also like that this directly addresses the CodeQL concern without broadening behavior or adding new complexity. The main tradeoff is intentionally disallowing repos that rely on a symlinked .git directory, but from a security perspective that seems like an acceptable constraint for a linking validator. Overall, this feels like a clean hardening change with clear intent and minimal functional impact.

@ArshVermaGit ArshVermaGit marked this pull request as ready for review May 21, 2026 08:11
@ArshVermaGit ArshVermaGit merged commit 695ddc1 into main May 21, 2026
4 of 5 checks passed
if not os.path.isdir(git_dir):
git_dir = os.path.join(normalized, ".git")
# Require a real .git directory inside the repository path and reject symlinks.
if not os.path.isdir(git_dir) or os.path.islink(git_dir):
if not os.path.isdir(git_dir):
git_dir = os.path.join(normalized, ".git")
# Require a real .git directory inside the repository path and reject symlinks.
if not os.path.isdir(git_dir) or os.path.islink(git_dir):
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