Skip to content

fix(report): keep the leading dot of dot paths in the code region hash - #234

Open
nabeelbaghoor wants to merge 1 commit into
secfoo-com:mainfrom
nabeelbaghoor:fix/sast-code-region-hash-drops-the-leading-dot
Open

nabeelbaghoor wants to merge 1 commit into
secfoo-com:mainfrom
nabeelbaghoor:fix/sast-code-region-hash-drops-the-leading-dot

Conversation

@nabeelbaghoor

Copy link
Copy Markdown
Contributor

What does this change, and why?

hash_code_region normalised the reported path with file_path.strip().lstrip("./"). lstrip removes any run of . and / characters, not a ./ prefix, so .env became env and .github/workflows/ci.yml became github/workflows/ci.yml. The lookup then missed the file and the function returned None, so a secret found in .env (or .env.local, .npmrc, ...) and a SAST finding under .github/ never got a code region hash and fell back to the path and line key that region hashing was added to replace.

This strips only leading ./ and / segments, so ./app/db.py and /app/db.py still resolve to app/db.py as before, and dot files and dot directories keep their name. ../ paths are still rejected.

sast._normalize_path and the only_files scope in storage/repository.py use the same lstrip("./"), but only as a normalised key on both sides of a comparison, and changing it there would change existing fingerprints, so I left them alone.

How to reproduce

from pathlib import Path
from secfoo.report.code_region import hash_code_region

root = Path("/tmp/demo"); root.mkdir(exist_ok=True)
(root / ".env").write_text("API_KEY=abc123\n")
print(hash_code_region(root, ".env", 1))

Before: None. After: a SHA-256 hex digest. parse_location already returns (".env", "1") for a .env:1 location, so the dot was lost only inside hash_code_region.

Testing

  • pytest tests passes: 2426 passed, 11 failed. The same 11 tests fail on untouched main in my environment (Rich table wrapping in CLI output, mermaid not vendored, the Claude capture subprocess, one model column check), none of them near this code.
  • ruff check . passes for the changed files (ruff check src/secfoo/report/code_region.py tests/test_report/test_code_region.py: all checks passed); bandit -r src/secfoo/report/code_region.py -c .bandit -ll: no findings.
  • Added/updated tests for the behavior change: test_hash_code_region_keeps_leading_dot_of_dotfiles_and_dot_dirs fails on main (assert None is not None for .env) and passes with this change; test_hash_code_region_strips_leading_dot_slash_and_rejects_parent pins the ./, / and ../ handling that must not change.

@nabeelbaghoor
nabeelbaghoor requested a review from vsn411 as a code owner December 1, 2026 12:09
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.

1 participant