Skip to content

Ensure uploaded validator files are always deleted - #228

Merged
kflemin merged 4 commits into
developfrom
fix/delete-uploaded-files
Oct 9, 2026
Merged

kflemin merged 4 commits into
developfrom
fix/delete-uploaded-files

Conversation

@kflemin

@kflemin kflemin commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Uploaded files for the validator are never stored in the database or on a persistent volume, but the temporary copies were not reliably deleted:

  • API, ZIP upload: the uploaded temp file was never unlinked.
  • API, single XML: the temp file was not unlinked if validation raised.
  • Web form: the temp file was not unlinked if validation raised.

This PR adds a saved_upload context manager that always removes the temp file, uses it in the API and web form, extracts ZIPs in a TemporaryDirectory context manager, and splits the API handler into _validate_zip / _validate_xml helpers. It also adds a notice on the validator page that the site is managed by NLR and files are not retained.

Testing

Ran against a temporary local Postgres with DJANGO_SETTINGS_MODULE=bsyncviewer.settings.gh_actions python manage.py test bsyncviewer.tests.test_api bsyncviewer.tests.test_views: 10 tests, OK.

New tests point tempfile.tempdir at a private directory and assert it is empty after an XML upload, a ZIP upload, and a validation error. On the old views.py the ZIP and error tests fail (leftover temp file), confirming the leak.

Notes

  • The schematron runner (testsuite.validate_sch) works in memory here since result_path is not passed, so it writes no files.
  • Infrastructure-level logging (load balancer, web server) is not covered by this change.
  • Please review the privacy wording on the validator page.

Uploaded XML/ZIP files were written to a NamedTemporaryFile(delete=False)
and only unlinked on the single-XML success path. The API ZIP path never
removed the upload, and exceptions in any path skipped cleanup, leaving
files in /tmp.

- Add a saved_upload context manager that always deletes the temp file
- Use it in the API (XML and ZIP) and the web validator form
- Extract API ZIP contents in a TemporaryDirectory context manager
- Add a privacy notice to the validator page
- Add tests asserting no files remain after XML, ZIP, and error cases

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coveralls

coveralls commented Oct 9, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 37963916403

Coverage increased (+0.4%) to 83.107%

Details

  • Coverage increased (+0.4%) from the base build.
  • Patch coverage: 12 uncovered changes across 1 file (67 of 79 lines covered, 84.81%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
bsyncviewer/views.py 53 41 77.36%
Total (2 files) 79 67 84.81%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2356
Covered Lines: 1958
Line Coverage: 83.11%
Coverage Strength: 0.83 hits per line

💛 - Coveralls

@kflemin
kflemin merged commit 11f2f6e into develop Oct 9, 2026
4 checks passed
@kflemin
kflemin deleted the fix/delete-uploaded-files branch October 9, 2026 17:24
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