Skip to content

Commit

Permalink
fix invalid signature generation caused by unawareness of host prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
moriyoshi committed Oct 29, 2020
1 parent 6ecaaba commit 65f5b4f
Show file tree
Hide file tree
Showing 236 changed files with 246 additions and 243 deletions.
2 changes: 1 addition & 1 deletion aws/signer/v4/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (s *SignHTTPRequestMiddleware) HandleFinalize(ctx context.Context, in middl
return out, metadata, &SigningError{Err: fmt.Errorf("failed to retrieve credentials: %w", err)}
}

err = s.signer.SignHTTP(ctx, credentials, req.Request, payloadHash, signingName, signingRegion, sdk.NowTime())
err = s.signer.SignHTTP(ctx, credentials, req, payloadHash, signingName, signingRegion, sdk.NowTime())
if err != nil {
return out, metadata, &SigningError{Err: fmt.Errorf("failed to sign http request, %w", err)}
}
Expand Down
2 changes: 1 addition & 1 deletion aws/signer/v4/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestComputePayloadHashMiddleware(t *testing.T) {

type httpSignerFunc func(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time) error

func (f httpSignerFunc) SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time) error {
func (f httpSignerFunc) SignHTTP(ctx context.Context, credentials aws.Credentials, r *smithyhttp.Request, payloadHash string, service string, region string, signingTime time.Time) error {
return f(ctx, credentials, r, payloadHash, service, region, signingTime)
}

Expand Down
19 changes: 11 additions & 8 deletions aws/signer/v4/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
v4Internal "github.com/aws/aws-sdk-go-v2/aws/signer/internal/v4"
"github.com/awslabs/smithy-go/httpbinding"
smithyhttp "github.com/awslabs/smithy-go/transport/http"
)

const (
Expand All @@ -67,7 +68,7 @@ const (

// HTTPSigner is an interface to a SigV4 signer that can sign HTTP requests
type HTTPSigner interface {
SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time) error
SignHTTP(ctx context.Context, credentials aws.Credentials, r *smithyhttp.Request, payloadHash string, service string, region string, signingTime time.Time) error
}

type keyDerivator interface {
Expand Down Expand Up @@ -107,7 +108,7 @@ func NewSigner(optFns ...func(signer *Signer)) *Signer {
}

type httpSigner struct {
Request *http.Request
Request *smithyhttp.Request
ServiceName string
Region string
Time v4Internal.SigningTime
Expand Down Expand Up @@ -137,7 +138,7 @@ func (s *httpSigner) Build() (signedRequest, error) {
sort.Strings(query[key])
}

v4Internal.SanitizeHostForHeader(req)
v4Internal.SanitizeHostForHeader(req.Request)

credentialScope := s.buildCredentialScope()
credentialStr := s.Credentials.AccessKeyID + "/" + credentialScope
Expand All @@ -154,9 +155,11 @@ func (s *httpSigner) Build() (signedRequest, error) {
}
}

host := req.URL.Host
var host string
if len(req.Host) > 0 {
host = req.Host
} else {
host = req.HostPrefix + req.URL.Host
}

signedHeaders, signedHeadersStr, canonicalHeaderStr := s.buildCanonicalHeaders(host, v4Internal.IgnoredHeaders, unsignedHeaders, s.Request.ContentLength)
Expand Down Expand Up @@ -197,7 +200,7 @@ func (s *httpSigner) Build() (signedRequest, error) {
req.URL.RawQuery = rawQuery.String()

return signedRequest{
Request: req,
Request: req.Request,
SignedHeaders: signedHeaders,
CanonicalString: canonicalString,
StringToSign: strToSign,
Expand Down Expand Up @@ -241,7 +244,7 @@ func buildAuthorizationHeader(credentialStr, signedHeadersStr, signingSignature
// will not be lost.
//
// The passed in request will be modified in place.
func (v4 Signer) SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time) error {
func (v4 Signer) SignHTTP(ctx context.Context, credentials aws.Credentials, r *smithyhttp.Request, payloadHash string, service string, region string, signingTime time.Time) error {
signer := &httpSigner{
Request: r,
PayloadHash: payloadHash,
Expand Down Expand Up @@ -292,11 +295,11 @@ func (v4 Signer) SignHTTP(ctx context.Context, credentials aws.Credentials, r *h
//
// This method does not modify the provided request.
func (v4 *Signer) PresignHTTP(
ctx context.Context, credentials aws.Credentials, r *http.Request,
ctx context.Context, credentials aws.Credentials, r *smithyhttp.Request,
payloadHash string, service string, region string, signingTime time.Time,
) (signedURI string, signedHeaders http.Header, err error) {
signer := &httpSigner{
Request: r.Clone(r.Context()),
Request: r.Clone(),
PayloadHash: payloadHash,
ServiceName: service,
Region: region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void writerSignerInterface(GoWriter writer) {
writer.addUseImports(AwsGoDependency.AWS_CORE);
writer.addUseImports(SmithyGoDependency.NET_HTTP);
writer.addUseImports(SmithyGoDependency.TIME);
writer.write("SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, "
writer.write("SignHTTP(ctx context.Context, credentials aws.Credentials, r *smithyhttp.Request, "
+ "payloadHash string, service string, region string, signingTime time.Time) error");
});
}
Expand Down
2 changes: 1 addition & 1 deletion internal/protocoltest/jsonrpc/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/accessanalyzer/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/acm/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/acmpca/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/alexaforbusiness/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/amplify/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/apigateway/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/apigatewaymanagementapi/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/apigatewayv2/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/appconfig/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/appflow/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/applicationautoscaling/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/applicationdiscoveryservice/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/applicationinsights/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/appmesh/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/appstream/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/appsync/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/athena/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/autoscaling/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/autoscalingplans/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/backup/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/batch/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/braket/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/budgets/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/chime/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/cloud9/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/clouddirectory/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/cloudformation/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/cloudfront/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/cloudhsm/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/cloudhsmv2/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 65f5b4f

Please sign in to comment.