Skip to content

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

Merged
merged 1 commit into from
May 30, 2025

Conversation

cristipufu
Copy link
Member

Problem

Binary files (PNG, PDF, etc.) uploaded via AttachmentsService.upload() and upload_async() were being corrupted with HTTP multipart form data headers embedded in the file content.

Example of corruption:

Content-Disposition: form-data; name="file"; filename="image.png"
Content-Type: image/png

‰PNG
[actual PNG data...]

This made uploaded images and other binary files unusable.

Root Cause

The upload methods were using files={"file": file} parameter with httpx, which automatically wraps the file content in multipart/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} with content=file_content to send raw binary data without multipart wrapping.

Changes Made

  • upload(): Read file content into memory and use content= parameter
  • upload_async(): Read file content into memory and use content= parameter

Copy link

@Copilot Copilot AI left a 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.

Comment on lines +387 to +394
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)
Copy link
Preview

Copilot AI May 30, 2025

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.

Comment on lines +523 to +530
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)
Copy link
Preview

Copilot AI May 30, 2025

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.

@cristipufu cristipufu merged commit e96a99d into main May 30, 2025
14 checks passed
@cristipufu cristipufu deleted the fix/attachments_binary branch May 30, 2025 15:51
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