Repository navigation
fix(web): raise an error when a files_upload_v2 file exceeds 1 GB - #2049
Conversation
|
Thanks for the contribution! Before we can merge this, we need @60507 to sign the Salesforce Inc. Contributor License Agreement. |
| if file_size > MAX_V2_FILE_SIZE: | ||
| filename = upload_file.get("filename") or os.path.basename(os.fspath(file)) | ||
| raise SlackRequestError( | ||
| f"Maximum upload file size is 1GB (file size: {file_size} bytes for filename: {filename})" |
There was a problem hiding this comment.
suggestion (non-blocking): The hardcoded "1GB" is easy to miss if MAX_V2_FILE_SIZE changes later. Could we build the message from the constant, here and in the len(data) check below, and update the test assertions to match?
| f"Maximum upload file size is 1GB (file size: {file_size} bytes for filename: {filename})" | |
| f"Maximum upload file size is {MAX_V2_FILE_SIZE} bytes (file size: {file_size} bytes for filename: {filename})" |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2049 +/- ##
=======================================
Coverage 84.51% 84.52%
=======================================
Files 118 118
Lines 14037 14044 +7
=======================================
+ Hits 11864 11871 +7
Misses 2173 2173 ☔ View full report in Codecov by Harness. |
Signed-off-by: Aymane El Homaïdi <60507@etu.he2b.be>
|
@60507 The Lint check is failing on formatting in |
Summary
Fixes #1993
When uploading files larger than Slack's 1 GB upload limit via
files_upload_v2, Slack'sfiles.getUploadURLExternalAPI returnsok: truewith a valid upload URL. The upload POST then fails partway through because Slack closes the connection, surfacing an opaqueurllib.error.URLError: <urlopen error EOF occurred in violation of protocol>SSL error rather than an actionable message.This PR adds a client-side size check in
_to_v2_file_upload_item(slack_sdk.web.internal_utils):isinstance(file, (str, os.PathLike))), checksos.path.getsize(os.fsencode(file)) > MAX_V2_FILE_SIZEbefore opening and reading file bytes into memory.file,content), verifies thatlen(data) <= MAX_V2_FILE_SIZE(1 GB /1_073_741_824bytes).SlackRequestErrorwith the file size and 1 GB limit if exceeded.Testing
Run unit tests:
Added:
test_to_v2_file_upload_item_raises_when_file_path_exceeds_1gb: verifiesSlackRequestErroris raised with the size and 1 GB limit message when file path size exceeds 1 GB without loading the file into memory.test_to_v2_file_upload_item_raises_when_content_exceeds_1gb: verifiesSlackRequestErroris raised when content length exceeds the maximum allowed size.Category
/docs(Documents)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements
python -m unittest tests/slack_sdk/web/test_internal_utils.py).