Skip to content

fix(report): parse line ranges and multi-site locations in SAST and secret scanning - #137

Closed
kushaldave002 wants to merge 2 commits into
secfoo-com:mainfrom
kushaldave002:fix/sast-parse-multi-range-locations
Closed

kushaldave002 wants to merge 2 commits into
secfoo-com:mainfrom
kushaldave002:fix/sast-parse-multi-range-locations

Conversation

@kushaldave002

Copy link
Copy Markdown
Contributor

Summary

Fixes #112.

When SAST or secret scanning agents report findings with line ranges (e.g. server.js:78-102), bare line continuations (app/data/user-dao.js:25, :61), or multi-file locations (server.js:135-137, app/views/layout.html:75), the entire raw string was previously placed into location_file with location_line = NULL. In SARIF output, this produced invalid or single-point regions, and web UI templates could not navigate individual sites.

This PR addresses all parser requirements, SARIF formatting, UI display, and crucially addresses maintainer review feedback on PR #129 regarding fingerprint migration, duplicate collapse, and reviewer sign-off preservation.

Key Changes

  1. Location Parser (parse_locations):
    • Parses single lines (file.py:10), line ranges (file.py:10-25), bare line continuations (file.py:10, :25), and multi-file locations (file1.py:10, file2.py:20).
    • Normalizes location_file to the primary path and location_line to the primary start line.
  2. Schema & Migration (_migrate_finding_locations):
    • Backfills structured locations JSON column on sast_findings and secret_findings.
    • Recomputes fingerprints for existing rows from the primary-path hash, ensuring fingerprints match the parser output.
    • Deterministic row collapse & review preservation: When pre-existing rows collapse onto the same (project_id, CWE/secret_type, primary_path):
      • The primary row is deterministically selected (active review_status > status == 'open' > earliest first_seen_at > id).
      • Any human reviewer sign-off (review_status, review_note, reviewed_by, reviewed_at) is preserved onto the primary row.
      • Collapsed duplicate rows are closed and safely rekeyed to <new_fp>:merged:<id> so they satisfy the uniqueness constraint without colliding on future scans.
      • Rescans against migrated rows match the primary row with zero churn and keep review status intact.
  3. SARIF Export:
    • Formats SARIF locations[] for each site with physicalLocation.region.startLine and endLine.
  4. Dashboard / Web UI:
    • Templates display all sites with line ranges and links.

Verification

  • Added unit and property-based parser tests in tests/test_property_parsers.py.
  • Added SARIF export tests in tests/test_report/test_sarif.py.
  • Added migration tests in tests/test_storage/test_structured_findings_storage.py:
    • test_migration_re_derives_locations_for_existing_sast_and_secret_rows
    • test_migration_collapses_duplicate_findings_preserving_review
    • test_migration_rescan_causes_no_churn
  • Ran full test suite (pytest) and linter (ruff check) cleanly.

…ecret scanning

- Parse line ranges, bare line continuations, and multi-file locations into structured locations
- Recompute primary-path fingerprints in migration with deterministic row collapse
- Preserve reviewer sign-off status across migration and subsequent scans
- Format SARIF locations[] per site with region startLine/endLine

Fixes secfoo-com#112
Comment thread src/secfoo/storage/repository.py Fixed
Comment thread src/secfoo/storage/repository.py Fixed
@vsn411

vsn411 commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this, @kushaldave002. The parser part is solid: all four real NodeGoat locations from #112 parse correctly, and Windows paths (C:\app\x.py:12, C:\app\x.py:12-14, :20, \\server\share\x.py:3, C:/app/x.py:12) split cleanly. I'm closing the PR because the upgrade migration damages existing users' findings, and that needs a different approach rather than a small patch. #112 stays open, and you're welcome to pick it up again with a new PR (or reopen this one after force-pushing the fix).

Blocking: the migration rewrites fingerprints without the line-bucket and region-hash parts

_migrate_finding_locations (repository.py ~L226–L370, checked at 562ac46) recomputes every fingerprint with hashlib from skill + CWE + path (+ code_region_hash), but the scanner's own fingerprint_for_row also adds linebucket:N when there is no region hash. Reproduced by building a store on main, opening it with this branch, then rescanning:

  1. No workdir: app/db.py:10 and app/db.py:120 (same CWE, both reviewed false_positive) are merged into one row by the migration. The next scan produces line-bucket fingerprints matching neither, so both close and two new open rows appear without their reviews. Secret rows server.js:3 and server.js:90 behave the same.
  2. With workdir: old rows app/db.py:10-12 and app/db.py:120-125 are merged; on rescan the second reopens as a new row without its review.
  3. Secrets: an open config/env/all.js:8-9 row is closed and reopened as a new row (lost first-seen date), because secrets have no legacy rekey step like SAST does.

Suggested fix

  • Keep the stored fingerprint whenever the re-parsed first path equals the stored location_file (that's every single-site row; Bug: line ranges and multi-file locations become bogus file paths (breaks fingerprints and SARIF locations) #112 doesn't affect them).
  • For rows whose path did change, call fingerprint_for_row (or a shared helper) with the same line-bucket and region rules instead of a copied formula, or add a legacy rekey for secrets.
  • Factor the SAST and secret blocks into one helper; they're ~100-line near-copies.
  • Add a migration test with two findings sharing a CWE and file, plus a range row, asserting ids and reviews survive a rescan.

Also worth fixing in the next PR

  • _TRAILING_SPAN_RE reads any trailing :digits as a line, so https://example.com:443 or db.internal:5432 (common in secret scanning) become line 443 / 5432.
  • Search now runs LIKE over the raw locations JSON, so searching path, line, start or null matches every finding.
  • Location cells with no path (:61, …) become location_file = '' in the parser but keep the raw text in the migration, so the two disagree; two such findings with one CWE also collide.
  • The migration runs on every RunRepository() open (the web UI opens one per request) with unindexed WHERE locations IS NULL scans. Gate it behind a schema version.
  • from typing_extensions import Self isn't a declared dependency (it's only there transitively via typer). Use from __future__ import annotations with a string return type, or declare it.
  • The PR body says ruff check src passes cleanly; it reports ~110 errors on both this branch and main (tracked in Make lint and security scanning blocking in CI: ruff has 133 errors behind || true, bandit findings are unreviewed, pip-audit never runs #114), so please drop that claim.

Happy to look at the next version.

@vsn411 vsn411 closed this Nov 17, 2026
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.

Bug: line ranges and multi-file locations become bogus file paths (breaks fingerprints and SARIF locations)

3 participants