T05 · Unauthorized Access and Privilege Escalation
- Location
gitguard_skill/entropy.py:195- Finding
Repository Boundary Escape Through Symbolic-Link File Scanning
- Content
View full analysis
Vulnerability Details
File Location:
gitguard_skill/entropy.py, lines 195-212
Vulnerability Type: Symbolic-link traversal and unauthorized local-file read
Risk Level: MediumVulnerable Code
python def scan_repo(repo_path: str, entropy_threshold: float = 4.0, max_files: int = 5000) -> list[SecretFinding]: """Walk a repository and scan all text files for exposed secrets.""" root = Path(repo_path) findings: list[SecretFinding] = [] scanned = 0 for p in root.rglob("*"): if scanned >= max_files: break if not p.is_file(): continue if any(part in SKIP_DIR_NAMES for part in p.parts): continue if p.suffix.lower() not in SCANNABLE_EXTENSIONS and p.name != ".env": continue scanned += 1 findings.extend(scan_file(p, entropy_threshold)) return findingsThe file is subsequently read by
scan_file():python def scan_file(path: Path, entropy_threshold: float = 4.0) -> list[SecretFinding]: findings: list[SecretFinding] = [] try: text = path.read_text(errors="ignore") except (UnicodeDecodeError, OSError): return findingsTechnical Analysis
The repository scanner recursively discovers paths using
Path.rglob(), checks them withPath.is_file(), and reads accepted paths usingPath.read_text(). Bothis_file()andread_text()follow symbolic links. The implementation does not:- Reject symbolic links.
- Resolve candidate paths before reading them.
- Verify that a resolved candidate remains beneath the resolved repository root.
- Open files with operating-system controls that prohibit symbolic-link following.
As a result, a repository can contain a symbolic link with a supported extension—or named
.env—whose target is outside the selected repository. The scanner will treat the link as an ordinary file and read the external target using the privileges of the GitGuard pr ...[truncated 2098 chars]- Remediation
View remediation
Remediation Suggestions
-
Reject symbolic links explicitly
Check
p.is_symlink()before callingis_file()or reading the candidate:python if p.is_symlink(): continue -
Enforce containment using resolved paths
Resolve the repository root once, resolve every candidate, and require the candidate to remain inside that root:
python root = Path(repo_path).resolve() for p in root.rglob("*"): if p.is_symlink(): continue try: resolved = p.resolve(strict=True) resolved.relative_to(root) except (OSError, ValueError): continue if not resolved.is_file(): continueOn supported Python versions,
resolved.is_relative_to(root)may be used instead of catchingValueError. -
Prevent time-of-check/time-of-use link replacement
A candidate could theoretically be replaced with a symbolic link after validation but before reading. Where supported, open files using operating-system facilities equivalent to
O_NOFOLLOW, validate the opened file descriptor, and read through that descriptor rather than reopening the pathname. -
Minimize disclosure in findings
Consider fully masking secret values instead of preserving the first and last four characters. A safer preview can include only the secret length or pattern classification.
-
Apply filesystem least privilege
Run the scanner under an account or sandbox that can read only the repositories intended for analysis. Avoid granting the service broad access to home directories, deployment secrets, SSH material, or other sensitive files.
-
Add regression tests
Add tests covering:
- A file symlink pointing outside the repository.
- A
.envsymlink pointing outside the repository. - A valid internal regular file.
- Broken symbolic links.
- Symbolic-link replacement between validation and reading, where practical.
-
