-
Notifications
You must be signed in to change notification settings - Fork 7
Fix: Prevent multipart form data corruption in binary file uploads #398
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes binary file corruption during uploads by sending raw binary content instead of multipart form data.
- Update upload() and upload_async() to read the file content into memory and pass it as raw binary data using the content parameter.
- Bump the project version from 2.0.62 to 2.0.63.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/uipath/_services/attachments_service.py | Updated upload methods to prevent multipart form data wrapping in binary file uploads. |
pyproject.toml | Updated project version to reflect the new changes. |
file_content = file.read() | ||
if result["BlobFileAccess"]["RequiresAuth"]: | ||
self.request( | ||
"PUT", upload_uri, headers=headers, files={"file": file} | ||
"PUT", upload_uri, headers=headers, content=file_content | ||
) | ||
else: | ||
with httpx.Client() as client: | ||
client.put(upload_uri, headers=headers, files={"file": file}) | ||
client.put(upload_uri, headers=headers, content=file_content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the entire file content into memory may lead to increased memory usage for very large files. Consider using a streaming approach if the HTTP client supports it to improve performance.
Copilot uses AI. Check for mistakes.
file_content = file.read() | ||
if result["BlobFileAccess"]["RequiresAuth"]: | ||
await self.request_async( | ||
"PUT", upload_uri, headers=headers, files={"file": file} | ||
"PUT", upload_uri, headers=headers, content=file_content | ||
) | ||
else: | ||
with httpx.Client() as client: | ||
client.put(upload_uri, headers=headers, files={"file": file}) | ||
client.put(upload_uri, headers=headers, content=file_content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the complete file into memory here could impact performance with large files. Evaluate if a streaming solution might be appropriate given the usage context.
Copilot uses AI. Check for mistakes.
Problem
Binary files (PNG, PDF, etc.) uploaded via
AttachmentsService.upload()
andupload_async()
were being corrupted with HTTP multipart form data headers embedded in the file content.Example of corruption:
This made uploaded images and other binary files unusable.
Root Cause
The upload methods were using
files={"file": file}
parameter withhttpx
, which automatically wraps the file content inmultipart/form-data
encoding. UiPath's blob storage was incorrectly storing the entire multipart payload instead of extracting just the file content.Solution
Replace files=
{"file": file}
withcontent=file_content
to send raw binary data without multipart wrapping.Changes Made
upload()
: Read file content into memory and usecontent=
parameterupload_async()
: Read file content into memory and usecontent=
parameter