-
Notifications
You must be signed in to change notification settings - Fork 655
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
validate put object content length #2690
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "d32ee107-7541-48db-a31c-d1944f3ffcbf", | ||
"type": "bugfix", | ||
"description": "Add client-side validation to ensure PutObject requests have a derivable content length.", | ||
"modules": [ | ||
"service/s3" | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
...oftware/amazon/smithy/aws/go/codegen/customization/s3/ValidatePutObjectContentLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package software.amazon.smithy.aws.go.codegen.customization.s3; | ||
|
||
import software.amazon.smithy.go.codegen.integration.GoIntegration; | ||
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar; | ||
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
import java.util.List; | ||
|
||
import static software.amazon.smithy.go.codegen.SymbolUtils.buildPackageSymbol; | ||
|
||
/** | ||
* Adds validation to PutObject to ensure that content-length is derivable _somehow_ (either through the body being | ||
* seekable or a length being set) - which is required for the operation to function since the service doesn't support | ||
* chunked transfer-encoding. | ||
*/ | ||
public class ValidatePutObjectContentLength implements GoIntegration { | ||
private static final ShapeId PUT_OBJECT_SHAPE_ID = ShapeId.from("com.amazonaws.s3#PutObject"); | ||
|
||
private static final MiddlewareRegistrar MIDDLEWARE = MiddlewareRegistrar.builder() | ||
.resolvedFunction(buildPackageSymbol("addValidatePutObjectContentLength")) | ||
.build(); | ||
|
||
@Override | ||
public List<RuntimeClientPlugin> getClientPlugins() { | ||
return List.of( | ||
RuntimeClientPlugin.builder() | ||
.operationPredicate((model, service, operation) -> operation.getId().equals(PUT_OBJECT_SHAPE_ID)) | ||
.registerMiddleware(MIDDLEWARE) | ||
.build() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package s3 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
presignedurl "github.com/aws/aws-sdk-go-v2/service/internal/presigned-url" | ||
"github.com/aws/smithy-go/middleware" | ||
) | ||
|
||
var errNoContentLength = errors.New( | ||
"The operation input had an undefined content length. PutObject MUST have a " + | ||
"derivable content length from either (1) an explicit value for the " + | ||
"ContentLength input member (2) the Body input member implementing io.Seeker " + | ||
"such that the SDK can derive a value.", | ||
) | ||
|
||
// PutObject MUST have a derivable content length for the body in some form, | ||
// since the service does not implement chunked transfer-encoding (and | ||
// aws-chunked encoding requires the length anyway). | ||
// | ||
// We gate this constraint at the client level through additional validation | ||
// rather than letting the request through, which would fail with a 501. | ||
type validatePutObjectContentLength struct{} | ||
|
||
func (*validatePutObjectContentLength) ID() string { | ||
return "validatePutObjectContentLength" | ||
} | ||
|
||
func (*validatePutObjectContentLength) HandleInitialize( | ||
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler, | ||
) ( | ||
out middleware.InitializeOutput, metadata middleware.Metadata, err error, | ||
) { | ||
if presignedurl.GetIsPresigning(ctx) { // won't have a body | ||
return next.HandleInitialize(ctx, in) | ||
} | ||
|
||
input, ok := in.Parameters.(*PutObjectInput) | ||
if !ok { | ||
return out, metadata, fmt.Errorf("unknown input parameters type %T", in.Parameters) | ||
} | ||
|
||
_, ok = input.Body.(io.Seeker) | ||
if !ok && input.ContentLength == nil { | ||
return out, metadata, errNoContentLength | ||
} | ||
return next.HandleInitialize(ctx, in) | ||
} | ||
|
||
func addValidatePutObjectContentLength(stack *middleware.Stack) error { | ||
return stack.Initialize.Add(&validatePutObjectContentLength{}, middleware.After) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package s3 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
) | ||
|
||
type reader struct { | ||
p []byte | ||
read bool | ||
} | ||
|
||
func (r *reader) Read(p []byte) (int, error) { | ||
if r.read { | ||
return 0, io.EOF | ||
} | ||
|
||
r.read = true | ||
copy(p, r.p) | ||
return len(r.p), nil | ||
} | ||
|
||
func TestValidatePutObjectContentLength(t *testing.T) { | ||
for name, cs := range map[string]struct { | ||
Input *PutObjectInput | ||
ExpectErr bool | ||
}{ | ||
"noseek,nolen": { | ||
Input: &PutObjectInput{ | ||
Bucket: aws.String("bucket"), | ||
Key: aws.String("key"), | ||
Body: &reader{p: []byte("foo")}, | ||
}, | ||
ExpectErr: true, | ||
}, | ||
"noseek,len": { | ||
Input: &PutObjectInput{ | ||
Bucket: aws.String("bucket"), | ||
Key: aws.String("key"), | ||
Body: &reader{p: []byte("foo")}, | ||
ContentLength: aws.Int64(3), | ||
}, | ||
ExpectErr: false, | ||
}, | ||
"seek,nolen": { | ||
Input: &PutObjectInput{ | ||
Bucket: aws.String("bucket"), | ||
Key: aws.String("key"), | ||
Body: bytes.NewReader([]byte("foo")), | ||
}, | ||
ExpectErr: false, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
svc := New(Options{ | ||
Region: "us-east-1", | ||
}) | ||
|
||
_, err := svc.PutObject(context.Background(), cs.Input) | ||
if cs.ExpectErr && !errors.Is(err, errNoContentLength) { | ||
t.Errorf("expected errNoContentLength, got %v", err) | ||
} | ||
if !cs.ExpectErr && errors.Is(err, errNoContentLength) { | ||
t.Errorf("expected no errNoContentLength but got it") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice error message