-
Notifications
You must be signed in to change notification settings - Fork 244
Fix ranged GET signing failure with Cloudflare R2 #452
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
36fedf7
52881b0
c86795c
2936eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,6 +215,38 @@ impl<'a> Command<'a> { | |
| } | ||
| } | ||
|
|
||
| /// Whether this command should include `Content-Length` and `Content-Type` | ||
| /// headers in the signed request. | ||
| /// | ||
| /// Returns `true` for commands that serialize a request body, plus | ||
| /// `InitiateMultipartUpload`. The latter is a `POST` with an empty body | ||
| /// but is included because: | ||
| /// | ||
| /// - Google Cloud Storage rejects the request with HTTP 411 if | ||
| /// `Content-Length` is omitted from a `POST`, even when the body is | ||
| /// empty. | ||
| /// - The `Content-Type` value carried by `InitiateMultipartUpload` is | ||
| /// not a description of the (empty) request body but the content type | ||
| /// to associate with the eventual multipart object on the server. | ||
| /// | ||
| /// Body-less `GET`, `HEAD`, and `DELETE` commands return `false` so that | ||
| /// stray `Content-Length: 0` / `Content-Type: text/plain` headers do | ||
| /// not enter the AWS4-HMAC-SHA256 canonical request, which Cloudflare | ||
| /// R2 rejects as a signature mismatch (notably for ranged `GET`s). | ||
| pub fn has_body(&self) -> bool { | ||
| matches!( | ||
| self, | ||
| Command::PutObject { .. } | ||
| | Command::PutObjectTagging { .. } | ||
| | Command::UploadPart { .. } | ||
| | Command::InitiateMultipartUpload { .. } | ||
| | Command::CompleteMultipartUpload { .. } | ||
| | Command::CreateBucket { .. } | ||
| | Command::PutBucketLifecycle { .. } | ||
| | Command::PutBucketCors { .. } | ||
| ) | ||
| } | ||
|
|
||
| pub fn content_length(&self) -> Result<usize, S3Error> { | ||
| let result = match &self { | ||
| Command::CopyObject { from: _ } => 0, | ||
|
|
@@ -368,3 +400,65 @@ impl<'a> Command<'a> { | |
| Ok(result) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// Body-less `GET`s (notably ranged `GET`) must not advertise body | ||
| /// headers, otherwise Cloudflare R2 rejects the AWS4-HMAC-SHA256 | ||
| /// signature for ranged downloads. | ||
| #[test] | ||
| fn ranged_get_does_not_have_body() { | ||
| let cmd = Command::GetObjectRange { | ||
| start: 0, | ||
| end: Some(1023), | ||
| }; | ||
| assert!(!cmd.has_body()); | ||
| assert!(!Command::GetObject.has_body()); | ||
| assert!(!Command::HeadObject.has_body()); | ||
| assert!(!Command::ListBuckets.has_body()); | ||
| } | ||
|
|
||
| /// `DELETE` and `CopyObject` carry no request body. | ||
| #[test] | ||
| fn delete_and_copy_do_not_have_body() { | ||
| assert!(!Command::DeleteObject.has_body()); | ||
| assert!(!Command::AbortMultipartUpload { upload_id: "u" }.has_body()); | ||
| assert!(!Command::CopyObject { from: "x" }.has_body()); | ||
| } | ||
|
|
||
| /// `InitiateMultipartUpload` is body-less but must still be reported as | ||
| /// having a body so that `Content-Length: 0` is sent. GCS returns HTTP | ||
| /// 411 on `POST` requests without `Content-Length`, even when the body | ||
| /// is empty. | ||
| #[test] | ||
| fn initiate_multipart_upload_has_body_for_gcs_compat() { | ||
| let cmd = Command::InitiateMultipartUpload { | ||
| content_type: "application/octet-stream", | ||
| }; | ||
| assert!(cmd.has_body()); | ||
| assert_eq!(cmd.http_verb(), HttpMethod::Post); | ||
| assert_eq!(cmd.content_length().unwrap(), 0); | ||
| } | ||
|
|
||
| /// Body-bearing commands report `has_body() == true` so signing | ||
| /// includes accurate `Content-Length` / `Content-Type`. | ||
| #[test] | ||
| fn body_bearing_commands_have_body() { | ||
| let put = Command::PutObject { | ||
| content: b"hello", | ||
| content_type: "text/plain", | ||
| custom_headers: None, | ||
| multipart: None, | ||
| }; | ||
| assert!(put.has_body()); | ||
|
|
||
| let upload = Command::UploadPart { | ||
| part_number: 1, | ||
| content: b"data", | ||
| upload_id: "u", | ||
| }; | ||
| assert!(upload.has_body()); | ||
| } | ||
| } | ||
|
Comment on lines
+421
to
+503
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | 🏗️ Heavy lift Please cover the header integration, not just These tests prove the enum classification, but the regression surface is 🤖 Prompt for AI Agents |
||
Uh oh!
There was an error while loading. Please reload this page.