ActiveStorage direct upload fails to sign content-length header for S3 service
Rails' ActiveStorage direct upload code passes a content-length value when generating a presigned S3 URL, but the AWS SDK silently drops that header, so the signed URL never enforces the size. An attacker could therefore upload a file of any size despite a developer's intended size limit, potentially causing storage costs or other harm.
- Triaged in 19 hours
- Fixed in 3 months
- Paid in 3 months
- Disclosed after 3 months
When a user makes a direct upload using ActiveStorage, the browser makes a request to the DirectUploadsController containing the direct_upload parameters filename, content_type, byte_size, and checksum. These are used to generate a presigned url that is then passed back to the browser, allowing the user to upload directly to S3.
In particular, the byte_size parameter is intended to be encoded in the url for content-length, preventing the user from uploading a file of a different size. Although Rails does not currently provide any built in validations, developers have been encouraged to modify the controller or provide their own controller if they want to create a validation. For example, a developer might decide to prohibit uploads greater than 10MB in size.
in all current version of Rails with ActiveStorage and direct uploads active_storage/lib/active_storage/service/s3_service.rb, the code generates the presigned_url as follows:
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key: key do |payload|
generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i,
content_type: content_type, content_length: content_length, content_md5: checksum
payload[:url] = generated_url
generated_url
end
end
However, the aws-sdk-s3 gem silently blacklists the "content-length" header:
https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-s3/lib/aws-sdk-s3/presigner.rb#L22
This issue is also raised here: https://github.com/aws/aws-sdk-ruby/issues/2098
As a result, the content-length header is never actually part of the presigned url. As a result, a malicious user can select a file of arbitrary size, tell the direct uploads controller that the file is a different size, and then proceed to upload the file, bypassing the intended protection of the signed url.
The solution is to add the whitelist_headers argument:
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key: key do |payload|
generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i,
content_type: content_type, content_length: content_length, content_md5: checksum,
whitelist_headers: ['content-length']
payload[:url] = generated_url
generated_url
end
end
After this is added, the content-length will be included in the presigned url and the client will be unable to upload a file of arbitrary size.
Impact
The attacker could upload a file of any size, unless the S3 service is configured separately to prevent this, whereas the developer believes they have protected themselves against this. This could allow an attacker to upload a very large file to S3, incurring additional costs to the website owner or causing other harm.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the AWS SDK's presigner silently blacklists the content-length header, so the size constraint passed by Rails never ends up in the signed URL.
Impact
The attacker could upload a file of any size, unless S3 is separately configured to prevent it, potentially incurring extra costs for the site owner.
What the attacker needed
ActiveStorage direct uploads to S3; developer relies on a size limit
The fix
The fix is to pass a whitelist_headers argument listing content-length so the SDK includes it in the presigned URL.
How it was found
- A developer sets up ActiveStorage direct uploads to S3 and relies on a size limit.
- The browser asks the direct uploads controller for a presigned URL, sending filename, content type, byte size and checksum.
- The controller generates the URL, but the size header is dropped by the SDK.
- The attacker uploads a file of arbitrary size using the returned URL.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you upload a file straight to cloud storage, the server gives your browser a temporary signed link. That link is supposed to lock in details like the file size, so you cannot swap in a bigger file. Here the size detail was quietly ignored by the library building the link, so the lock was never applied.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.