Repository navigation
fix(security): CodeQL-recognized path-injection sanitizer for heatmap embed (PR #283 follow-up) - #287
Closed
cursor[bot] wants to merge 3 commits into
Closed
cursor[bot] wants to merge 3 commits into
cursor[bot] wants to merge 3 commits into
Conversation
…eatmap embed Replace Path.is_relative_to() (not recognized by CodeQL py/path-injection) with the same sanitizer mold already accepted in api/routes.py: - _heatmap_basename: os.path.basename + re.fullmatch on allowlist (whitelist barrier) - _safe_heatmap_path_under_output_dir: realpath base + normpath(join(base, name)) + startswith + isfile (containment under operator-controlled output_dir) The user-influenced heatmap_path argument is never passed to os.path.join or Path.resolve; only the validated basename flows into the join, so CodeQL data-flow stops at the barrier. session_id-derived filenames continue to flow through report.safe_prefix.safe_session_prefix. Backwards-compatible alias _heatmap_path_under_output_dir is kept for callers that imported the previous helper name. Refs PR #283 RCA (Slack thread); see ADR 0044. Co-authored-by: Fabio Tavares Leitão <[email protected]>
…sanitizer Pin the CodeQL py/path-injection sanitizer mold so the same defect class cannot return silently if someone tries to 'simplify' the helper back to Path.is_relative_to or skip the basename allowlist. Covers: - _heatmap_basename: allowlist accept + reject (traversal, off-pattern, oversize) - _safe_heatmap_path_under_output_dir: real-file containment, traversal collapse to safe basename, off-allowlist rejection, missing file, unresolvable output_dir (OSError + ValueError on embedded NUL), and symlinked output_dir realpath 26 tests, all pass alongside existing tests/test_report_path_safety.py and tests/test_safe_prefix.py. Co-authored-by: Fabio Tavares Leitão <[email protected]>
Document the basename allowlist + normpath/startswith/isfile sanitizer mold as the workflow lock-in for both api/routes.py (already accepted by CodeQL) and report/generator.py (this slice). Calls out: - Why Path.is_relative_to is not a CodeQL barrier (PR #283 RCA). - The two-step sanitizer chain that CodeQL recognizes. - safe_session_prefix() for session_id-derived filenames. - Silent fallback (no embed) on rejection per THE_ART_OF_THE_FALLBACK.md. - Zero blast radius on customer DB (DEFENSIVE_SCANNING_MANIFESTO.md clauses unaffected). Index row added to docs/adr/README.md; AGENTS.md 'Current last' bumped to 0044. Co-authored-by: Fabio Tavares Leitão <[email protected]>
|
|
||
| # Backwards-compatible alias kept for tests/external callers that imported the | ||
| # previous helper name. New code should call :func:`_safe_heatmap_path_under_output_dir`. | ||
| _heatmap_path_under_output_dir = _safe_heatmap_path_under_output_dir |
1 of 2 tasks
FabioLeitao
force-pushed
the
cursor/sre-agent-protocol-1f12
branch
from
May 3, 2026 17:52
1428118 to
7625778
Compare
5 tasks
2 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes the CodeQL
py/path-injectionalert that PR #283 left open atreport/generator.py:46. Replaces thePath.is_relative_tocontainment check (not recognized by CodeQL as a sanitizer) with the same two-step mold already accepted by CodeQL inapi/routes.py:_heatmap_basename:os.path.basenamethenre.fullmatch(_HEATMAP_BASENAME_PATTERN, name)(^heatmap_[A-Za-z0-9_-]{4,64}\.png$)._safe_heatmap_path_under_output_dir: realpath-canonical base +os.path.normpath(os.path.join(base, name))+startswith(base)+isfile().The user-influenced
heatmap_pathargument is never passed toos.path.joinorPath.resolve; only the validated basename is joined with the operator-controlledoutput_dir, so CodeQL's data-flow stops at the barrier.session_id-derived report and heatmap filenames continue throughreport.safe_prefix.safe_session_prefix(existing helper).A backwards-compatible alias
_heatmap_path_under_output_diris kept for callers that imported the previous helper name.Why the previous fix failed CI
Path.is_relative_tomay be useful for application logic, but CodeQL'spy/path-injectionquery does not treat it as a barrier. Bandit, Semgrep, Sonar, and the duplicateAnalyze (Python)job in.github/workflows/codeql.ymlwere all green precisely because the issue is strictly Python data-flow in the GHAS CodeQL job — the same point the Slack thread RCA called out.Engineering DNA
docs/ops/inspirations/DEFENSIVE_SCANNING_MANIFESTO.md— "no surprise side effects" extends to the report writer; filenames must never let a tainted segment pivot away from the operator-controlled output directory. Zero impact on database locks: this PR only edits filename validation and image embedding; no SQL, connector, or session lifecycle code is touched. TheWITH (NOLOCK)/ sampling cap / statement-attribution clauses are unaffected.docs/ops/inspirations/THE_ART_OF_THE_FALLBACK.md— when containment fails, the generator degrades silently (no embed) and the Excel still ships. This honours the "diagnostic on fall" rule via the existingtry/except: passaroundOpenpyxlImage, without raising and without writing a sink.RCA (Julia Evans-style)
Tests
tests/test_report_generator_path_safety.py(new): 26 tests covering basename allowlist accept/reject, traversal-collapses-to-safe-basename, off-allowlist rejection, missing file underoutput_dir, unresolvableoutput_dir(OSError+ValueErroron embedded NUL), and symlinkedoutput_dirrealpath.tests/test_report_path_safety.py(5),tests/test_safe_prefix.py(3),tests/test_security.py(22), and the wider report surface (test_report_trends,test_executive_report,test_scan_evidence,test_report_recommendations,test_report_data_source_inventory,test_routes_responses, …).-k "report or generator or path or safe or heatmap or security"); Ruff lint and format clean.Documentation
0044-codeql-path-injection-sanitizer-mold.md— locks in the basename-allowlist + normpath/startswith/isfile mold for bothapi/routes.pyandreport/generator.py, with explicit guidance thatPath.is_relative_tois not a CodeQL barrier.docs/adr/README.mdindex row added;AGENTS.md"Current last" pointer bumped to ADR 0044.Verification checklist (LMDE-style)
gh api repos/FabioLeitao/data-boar/check-runs/73292858146/annotations—report/generator.py:46, "Uncontrolled data used in path expression".Path.is_relative_to) and the accepted sibling pattern inapi/routes.py(_real_file_under_out_dir_str+_HEATMAP_FILENAME_PATTERN).report/generator.py; helper is isolated and named clearly so CodeQL can validate the contract on a tight scope.gh run view).Refs
api/routes.pylines 326–355.DEFENSIVE_SCANNING_MANIFESTO.md,THE_ART_OF_THE_FALLBACK.md.