Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard interface for handling file uploads in the Dioptra client #708

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

jkglasbrenner
Copy link
Collaborator

@jkglasbrenner jkglasbrenner commented Jan 10, 2025

Addresses client-side solution for #694

Added requests-toolbelt as a new project dependency to support streaming of multi-file uploads.

This branch does not include any new unit tests for this new functionality, as it will start being exercised via the REST API integration tests once we integrate the solutions for #694 with #658. However, in the interest of verifying this new feature for the client works, I created this branch, https://github.com/usnistgov/dioptra/tree/test-client-uploading-support, that adds a simple /echoes endpoint that allows you to upload files, and the server echoes back the name and size of the files you upload. I tested it locally and I also created two simple REST API integration tests based on this endpoint to validate the test client implementation. Both seem to be working as expected.

Todos

  • Add a utility that scans a directory, either with or without recursion, and builds up a list of DioptraFile objects for upload. The filenames will retain relative filepaths with respect to the source directory's root path, and be standardized in the POSIX format.
  • Add a utility that accepts a filepath/list of filepaths and converts them into a DioptraFile object/list of DioptraFile objects to upload. The filenames will be stripped of any directory paths.

@jkglasbrenner jkglasbrenner changed the title Add uploading support to client Add standard interface for handling file uploads in the Dioptra client Jan 10, 2025
@jkglasbrenner jkglasbrenner linked an issue Jan 10, 2025 that may be closed by this pull request
@jkglasbrenner jkglasbrenner marked this pull request as draft January 10, 2025 23:29
@jkglasbrenner jkglasbrenner force-pushed the add-uploading-support-to-client branch 5 times, most recently from ec75e73 to 9374ea7 Compare January 14, 2025 23:47
@jkglasbrenner jkglasbrenner marked this pull request as ready for review January 14, 2025 23:50
This update adds file uploading support to the Dioptra client library through a new DioptraFile
class, supporting utility functions, and extending the client's post method to accept `data` and
`files` arguments. The changes enable users to upload single files, multiple files, or all files
within a directory while enforcing strict path validation in the associated filenames to prevent
server-side directory traversal attacks.

The new functionality includes:

- A DioptraFile class that validates filenames to ensure they are normalized, POSIX-compliant
  relative paths without traversal components (e.g., "..").
- File selection utility functions:
  - select_files_in_directory: Recursively selects files from a directory with optional regex
    pattern filtering
  - select_one_or_more_files: Selects individual files with optional renaming support to handle
    filename collisions
- Extended POST request in the client to handle multipart form data that contains regular form
  fields and file uploads. All file uploads, whether they're single file or multiple files, are
  streamed to the server.

The DioptraSession implementation for the requests-based client and the Flask test client have both
been updated to support the new functionality.
@jkglasbrenner jkglasbrenner force-pushed the add-uploading-support-to-client branch from 9374ea7 to 3164d79 Compare January 15, 2025 17:54
@jkglasbrenner jkglasbrenner merged commit db9b5a5 into dev Jan 15, 2025
11 checks passed
@jkglasbrenner jkglasbrenner deleted the add-uploading-support-to-client branch January 15, 2025 22:53
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.

Add general support for uploading files to the Dioptra Client
3 participants