Fix traversal checks that crash instead of rejecting

What does this MR do and why?

Helpers.validate_traversal raises ValueError on a bad path and never returns a falsy value. Several callers used it as if not validate_traversal(...), so their rejection branches never ran. A bad path then returned a generic 500 instead of a clean rejection.

This does not fix any security issues, just how the app responds when it detects traversal.

  • Add Helpers.is_path_within(), a boolean wrapper, and use it in reject_traversal and the import checks in main_controller.py.
  • Route the file API and login photo checks through reject_traversal, which removes the duplicated error blocks.
  • Make reject_traversal send 403 instead of 400
  • Validate every path before acting in multi-item delete, copy, and move. Before, a bad path in a later item failed the request after earlier items were already deleted or moved.

Screenshots or screen recordings

N/A

How to set up and validate locally

  1. Create keep.txt and a dest directory in a server's root directory.
  2. Send a delete with one good path and one bad path: curl -k -X DELETE -H "Authorization: Bearer $TOKEN" https://localhost:8443/api/v2/servers/$SERVER/files -d '{"file_system_objects":[{"filename":"keep.txt"},{"filename":"../outside.txt"}]}'
  3. Confirm the response is 403 TRAVERSAL DETECTED and keep.txt still exists. On dev, the response is a 500 and keep.txt is deleted.
  4. Send a move with one good target and one bad target: curl -k -X POST -H "Authorization: Bearer $TOKEN" https://localhost:8443/api/v2/servers/$SERVER/files/move -d '{"file_system_objects":[{"source_path":"keep.txt","target_path":"dest"},{"source_path":"keep.txt","target_path":"../x"}]}'
  5. Confirm the response is 403 and keep.txt is still in the root, not in dest.
  6. Delete, move, and download a valid file in the panel file manager. All three work as before.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

  • Have you checked this doesn't interfere/conflict/duplicate someone elses work?
  • Have you fully tested your changes?
  • Have you resolved any lint issues?
  • Have you assigned a reviewer?
  • Have you applied correct labels?
Edited by Iain Powrie

Merge request reports

Loading
Loading