Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/secfoo/report/code_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Collapse horizontal whitespace so formatting-only edits don't churn the hash.
_WS_RE = re.compile(r"[ \t]+")

_LEADING_SEP_RE = re.compile(r"^(?:\./|/)+")


def _normalize_snippet_line(line: str) -> str:
return _WS_RE.sub(" ", line.rstrip())
Expand All @@ -33,7 +35,9 @@ def hash_code_region(workdir: Path, file_path: str, line: str | int | None) -> s
if center < 1:
return None

rel = file_path.strip().lstrip("./")
# Drop leading "./" and "/" segments, not every leading "." character:
# ".env" and ".github/..." keep their dot.
rel = _LEADING_SEP_RE.sub("", file_path.strip())
if not rel or rel.startswith("..") or "/../" in rel:
return None

Expand Down
23 changes: 23 additions & 0 deletions tests/test_report/test_code_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ def test_hash_code_region_differs_for_different_snippets(tmp_path):
assert hash_code_region(tmp_path, "app/db.py", 5) != hash_code_region(tmp_path, "app/db.py", 13)


def test_hash_code_region_keeps_leading_dot_of_dotfiles_and_dot_dirs(tmp_path):
(tmp_path / ".env").write_text("API_KEY=abc123\n")
workflow = tmp_path / ".github" / "workflows" / "ci.yml"
workflow.parent.mkdir(parents=True)
workflow.write_text("run: echo ${{ github.event.issue.title }}\n")

assert hash_code_region(tmp_path, ".env", 1) is not None
assert hash_code_region(tmp_path, ".github/workflows/ci.yml", 1) is not None
assert hash_code_region(tmp_path, "./.env", 1) == hash_code_region(tmp_path, ".env", 1)


def test_hash_code_region_strips_leading_dot_slash_and_rejects_parent(tmp_path):
path = tmp_path / "app" / "db.py"
path.parent.mkdir(parents=True)
path.write_text('query = "bad"\n')

expected = hash_code_region(tmp_path, "app/db.py", 1)
assert expected is not None
assert hash_code_region(tmp_path, "./app/db.py", 1) == expected
assert hash_code_region(tmp_path, "/app/db.py", 1) == expected
assert hash_code_region(tmp_path / "app", "../app/db.py", 1) is None


def test_findings_with_fingerprints_uses_region_hash_when_workdir_provided(tmp_path):
db = tmp_path / "app" / "db.py"
db.parent.mkdir(parents=True)
Expand Down
Loading