fix: keep export files off the public /uploads path #53

Merged
jfro merged 3 commits from jtk/private-exports into main 2026-10-18 22:40:14 +00:00
Owner

Export files land in UPLOAD_PATH/exports, and endpoint.ex serves all of UPLOAD_PATH at /uploads through Plug.Static with no auth, so anyone who guessed an export's file name could download it.

Change

  • New FuzzyCatalogWeb.Plugs.BlockPrivateUploads, plugged in the endpoint just before the /uploads static plug. Anything under /uploads/exports/ gets a 404 and the request is halted.
  • It percent-decodes the segment before checking it, the same way Plug.Static does, so /uploads/%65xports/... is blocked too.
  • Each blocked request is logged as a warning with its path and remote IP.
  • Admin downloads still go through ImportExportController, which reads the file from disk, so nothing that's meant to work changes.

I went with blocking the path rather than moving exports out of UPLOAD_PATH. Moving them would break the exports/... paths already stored on existing jobs and the job_file_path/1 root check, and the issue lists either fix as fine.

Tests

  • Plug unit tests: blocked, percent-encoded, other upload paths, and a non-upload /exports path.
  • Endpoint test: writes a real export file under the uploads dir and checks it comes back 404. It fails without the plug.

mix precommit passes: 542 tests, 0 failures.

Fixes #35

Export files land in `UPLOAD_PATH/exports`, and `endpoint.ex` serves all of `UPLOAD_PATH` at `/uploads` through `Plug.Static` with no auth, so anyone who guessed an export's file name could download it. **Change** - New `FuzzyCatalogWeb.Plugs.BlockPrivateUploads`, plugged in the endpoint just before the `/uploads` static plug. Anything under `/uploads/exports/` gets a 404 and the request is halted. - It percent-decodes the segment before checking it, the same way `Plug.Static` does, so `/uploads/%65xports/...` is blocked too. - Each blocked request is logged as a warning with its path and remote IP. - Admin downloads still go through `ImportExportController`, which reads the file from disk, so nothing that's meant to work changes. I went with blocking the path rather than moving exports out of `UPLOAD_PATH`. Moving them would break the `exports/...` paths already stored on existing jobs and the `job_file_path/1` root check, and the issue lists either fix as fine. **Tests** - Plug unit tests: blocked, percent-encoded, other upload paths, and a non-upload `/exports` path. - Endpoint test: writes a real export file under the uploads dir and checks it comes back 404. It fails without the plug. `mix precommit` passes: 542 tests, 0 failures. Fixes #35
jfro added this to the 0.6.2 milestone 2026-10-18 21:40:07 +00:00
fix: keep export files off the public /uploads path
Some checks failed
CI / test (pull_request) Failing after 51s
13a38c7deb
Export files are written under UPLOAD_PATH/exports, which Plug.Static
serves at /uploads with no auth. Add a plug ahead of the static plug that
404s anything under /uploads/exports (percent-decoded like Plug.Static
does) and logs the blocked request. Admin downloads keep going through
the import/export controller.

Fixes #35
jfro force-pushed jtk/private-exports from 13a38c7deb
Some checks failed
CI / test (pull_request) Failing after 51s
to 81b808be1f
All checks were successful
CI / test (pull_request) Successful in 1m48s
2026-10-18 21:48:31 +00:00
Compare
Author
Owner

Review from Nye (changes requested)

PR #53, fix: keep export files off the public /uploads path (13a38c7). Static review against Plug 1.20.3's Plug.Static source; I didn't run the suite here.

  1. (Medium, blocker) Case-insensitive filesystems get around the block. The plug compares the decoded segment to "exports" exactly, so /uploads/EXPORTS/ or /uploads/Exports/ (or /uploads/%45xports/...) passes through, and Plug.Static resolves it to UPLOAD_PATH/exports/ on a case-insensitive filesystem: macOS APFS, Docker Desktop bind mounts on macOS/Windows, NTFS. On NTFS, "exports." and "exports " also resolve to "exports". Fix: normalize before comparing, e.g. dir |> URI.decode() |> String.downcase() |> String.trim_trailing(". "). Add tests for "EXPORTS", "%45xports" and "exports.".
  2. (Low, test gap) Add endpoint-level tests for the cases this is supposed to cover: HEAD /uploads/exports/ gives 404, /uploads/exports/ with a trailing slash gives 404, //uploads//exports/ gives 404, and /uploads/x/../exports/ doesn't serve the file (Plug.Static raises InvalidPathError there, which is a 400). Those cases all work today, but tests would keep them working.
  3. (Suggestion, not blocking) A path denylist is easy to get wrong over time. Your reasoning for not moving exports in this PR makes sense. Consider a follow-up issue (post-0.6.2 is fine) to write exports to a private dir set by an env var (EXPORT_PATH), with a fallback that reads legacy "exports/..." job paths. job_file_root/1 is already the single place that would need to change.

Checked and OK:

  • Plug order: BlockPrivateUploads runs before both static plugs. The root Plug.Static (only: uploads, from priv/static) also serves /uploads when UPLOAD_PATH is unset, and it's covered too.
  • //uploads//exports: path_info drops empty segments, so it's blocked.
  • Dot segments, and encoded / \ : NUL (%2e%2e, exports%2Ffile): Plug.Static's invalid_path? raises a 400. Not served.
  • Double encoding (%2565xports): Plug.Static decodes once, so it looks for a literal "%65xports" dir. Not served.
  • HEAD and other methods: the plug matches on path regardless of method.
  • Admin download: ImportExportController.download reads job_file_path/1 and calls send_file, so it isn't affected by the plug.
  • The Logger warning includes the path and remote IP. The title is conventional, the body has "Fixes #35", there's no version bump, and the endpoint test fails without the plug.
Review from Nye (changes requested) PR #53, fix: keep export files off the public /uploads path (13a38c7). Static review against Plug 1.20.3's Plug.Static source; I didn't run the suite here. 1. (Medium, blocker) Case-insensitive filesystems get around the block. The plug compares the decoded segment to "exports" exactly, so /uploads/EXPORTS/<file> or /uploads/Exports/<file> (or /uploads/%45xports/...) passes through, and Plug.Static resolves it to UPLOAD_PATH/exports/<file> on a case-insensitive filesystem: macOS APFS, Docker Desktop bind mounts on macOS/Windows, NTFS. On NTFS, "exports." and "exports " also resolve to "exports". Fix: normalize before comparing, e.g. `dir |> URI.decode() |> String.downcase() |> String.trim_trailing(". ")`. Add tests for "EXPORTS", "%45xports" and "exports.". 2. (Low, test gap) Add endpoint-level tests for the cases this is supposed to cover: HEAD /uploads/exports/<file> gives 404, /uploads/exports/ with a trailing slash gives 404, //uploads//exports/<file> gives 404, and /uploads/x/../exports/<file> doesn't serve the file (Plug.Static raises InvalidPathError there, which is a 400). Those cases all work today, but tests would keep them working. 3. (Suggestion, not blocking) A path denylist is easy to get wrong over time. Your reasoning for not moving exports in this PR makes sense. Consider a follow-up issue (post-0.6.2 is fine) to write exports to a private dir set by an env var (EXPORT_PATH), with a fallback that reads legacy "exports/..." job paths. job_file_root/1 is already the single place that would need to change. Checked and OK: - Plug order: BlockPrivateUploads runs before both static plugs. The root Plug.Static (only: uploads, from priv/static) also serves /uploads when UPLOAD_PATH is unset, and it's covered too. - //uploads//exports: path_info drops empty segments, so it's blocked. - Dot segments, and encoded / \ : NUL (%2e%2e, exports%2Ffile): Plug.Static's invalid_path? raises a 400. Not served. - Double encoding (%2565xports): Plug.Static decodes once, so it looks for a literal "%65xports" dir. Not served. - HEAD and other methods: the plug matches on path regardless of method. - Admin download: ImportExportController.download reads job_file_path/1 and calls send_file, so it isn't affected by the plug. - The Logger warning includes the path and remote IP. The title is conventional, the body has "Fixes #35", there's no version bump, and the endpoint test fails without the plug.
fix: block case and trailing-dot variants of /uploads/exports
All checks were successful
CI / test (pull_request) Successful in 1m46s
01f226e333
Case-insensitive filesystems (macOS APFS, Docker Desktop bind mounts, NTFS)
resolve EXPORTS/Exports to exports, and NTFS also resolves 'exports.' and
'exports '. Normalize the segment before comparing, and add endpoint tests for
HEAD, trailing slash, doubled slashes and dot segments.
Author
Owner

Review from Nye (approved) — re-review at 01f226e

The blocker is fixed: the segment is URI-decoded, downcased and stripped of trailing dots/spaces; malformed %-encoding falls through to Plug.Static's 400. New endpoint tests for HEAD, the bare dir and dot segments are good.

  • Low (fix on the branch, no re-review needed): String.downcase doesn't fold U+017F LONG S. Unicode case folding maps ſ → s (CaseFolding.txt 017F; C; 0073), which case-insensitive filesystems using Unicode folding (APFS) can apply, so /uploads/export%C5%BF/<file> may still resolve. Use :string.casefold/1 instead of String.downcase/1 in normalize/1 and add "/uploads/export%C5%BF/x.json" to the variants list.
  • Low (test): "doubled slashes are blocked" doesn't test what it says. Plug's test adapter runs the path through URI.parse, and //uploads//exports/x parses uploads as the HOST, so path_info is ["exports", x] and the 404 comes from the router, not the plug. Use "/uploads//exports/#{name}" (single leading slash).
Review from Nye (approved) — re-review at 01f226e The blocker is fixed: the segment is URI-decoded, downcased and stripped of trailing dots/spaces; malformed %-encoding falls through to Plug.Static's 400. New endpoint tests for HEAD, the bare dir and dot segments are good. - **Low** (fix on the branch, no re-review needed): `String.downcase` doesn't fold U+017F LONG S. Unicode case folding maps ſ → s (CaseFolding.txt `017F; C; 0073`), which case-insensitive filesystems using Unicode folding (APFS) can apply, so `/uploads/export%C5%BF/<file>` may still resolve. Use `:string.casefold/1` instead of `String.downcase/1` in `normalize/1` and add `"/uploads/export%C5%BF/x.json"` to the variants list. - **Low** (test): "doubled slashes are blocked" doesn't test what it says. Plug's test adapter runs the path through `URI.parse`, and `//uploads//exports/x` parses `uploads` as the HOST, so path_info is `["exports", x]` and the 404 comes from the router, not the plug. Use `"/uploads//exports/#{name}"` (single leading slash).
fix: Unicode case-fold /uploads segments so ſ can't spell exports
All checks were successful
CI / test (pull_request) Successful in 1m58s
60356d6a69
Author
Owner

Review from Nye (approved) — re-review at 60356d6

I re-reviewed 60356d6: both follow-ups are done. normalize uses :string.casefold, and the test for export%C5%BF passes. The doubled-slash test now really exercises /uploads//exports/…. I checked in Elixir 1.18 that :string.casefold raises ArgumentError on invalid UTF-8 (%FF), and the existing rescue returns the raw segment, so it can't become a 500 or a bypass. No further changes.

Review from Nye (approved) — re-review at 60356d6 I re-reviewed 60356d6: both follow-ups are done. normalize uses :string.casefold, and the test for export%C5%BF passes. The doubled-slash test now really exercises /uploads//exports/…. I checked in Elixir 1.18 that :string.casefold raises ArgumentError on invalid UTF-8 (%FF), and the existing rescue returns the raw segment, so it can't become a 500 or a bypass. No further changes.
jfro merged commit 8539dfa79a into main 2026-10-18 22:40:14 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/fuzzy_catalog!53
No description provided.