Skip to content

fix(security): validate report/heatmap basenames under output_dir - #98

Merged
FabioLeitao merged 7 commits into
mainfrom
chore/security-api-report-path-hardening
Mar 20, 2026
Merged

FabioLeitao merged 7 commits into
mainfrom
chore/security-api-report-path-hardening

Conversation

@FabioLeitao

Copy link
Copy Markdown
Collaborator

Summary

Security (API): Harden GET /report and heatmap download paths:

  • Relatorio_Auditoria_*.xlsx and heatmap_*.png basename patterns (_REPORT_FILENAME_PATTERN, _HEATMAP_FILENAME_PATTERN).
  • _safe_report_output_path / _safe_heatmap_output_path resolve only under configured report.output_dir; no trusting arbitrary paths then Path().exists() on attacker-controlled strings beyond basename validation.
  • New test: reject not_a_report.xlsx even when under output_dir.

Note: Old branch pr/api-report-path-hardening was far behind main; this work is cherry-picked commit b415c79 onto current main, with test merge resolution.

Chore: Ruff format tests/test_scripts.py (pre-commit).

Tests

  • .\scripts\check-all.ps1

…ts()

- Strict Relatorio_Auditoria_*.xlsx / heatmap_*.png under output_dir only

- Regenerate report/heatmap when stored path is unsafe or missing

- Test: reject non-standard report basename inside output_dir

Made-with: Cursor
Comment thread api/routes.py Fixed
Comment thread api/routes.py Fixed
Comment thread api/routes.py Fixed
Comment thread api/routes.py Fixed
Replace Path(stored_path).name with _validated_output_basename() using
os.path.basename(os.fspath) and regex before out_dir/filename.resolve().
Addresses py/path-injection on refs/pull/98/merge.

Made-with: Cursor
… tests/docs

- _resolved_existing_file_under_out_dir: os.path.join/normpath/realpath + prefix check
- Heatmap flow via _heatmap_png_path_under_out_dir and _safe_report_path_for_heatmap
- Extend test_report_path_safety; document in TESTING; rule/skill for path safety

Made-with: Cursor
Comment thread api/routes.py Fixed
Comment thread api/routes.py Fixed
… heatmap bytes response

- _real_file_under_out_dir_str: normpath(join)+startswith(base_path)+isfile (doc-aligned)
- Base dir from _report_output_dir_resolved(engine) only, not report_path.parent
- GET /heatmap and /heatmap/{id}: _heatmap_png_response + open(safe_path) avoids FileResponse path sink
- Tests + rule/skill updates

Made-with: Cursor
@FabioLeitao
FabioLeitao merged commit 1c45087 into main Mar 20, 2026
2 checks passed
@FabioLeitao
FabioLeitao deleted the chore/security-api-report-path-hardening branch March 20, 2026 18:18
FabioLeitao added a commit that referenced this pull request Mar 20, 2026
- S4 table + progress: link #101, status in progress
- Footer: #98-#100 on main

Made-with: Cursor
FabioLeitao added a commit that referenced this pull request Mar 20, 2026
* chore(deps): bump certifi floor and refresh lock + requirements

Batch security hygiene for Dependabot alignment (PEM bundle / advisory churn).

Made-with: Cursor

* docs(plans): track A1 deps PR #101 in MAINTENANCE_FRONT_OF_WORK

- S4 table + progress: link #101, status in progress
- Footer: #98-#100 on main

Made-with: Cursor
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.

2 participants