Skip to content
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

Presigned links: Unable to set Content-Length in X-Amz-SignedHeaders #1418

Closed
3 tasks done
jalandis opened this issue Sep 16, 2021 · 3 comments · Fixed by #1464
Closed
3 tasks done

Presigned links: Unable to set Content-Length in X-Amz-SignedHeaders #1418

jalandis opened this issue Sep 16, 2021 · 3 comments · Fixed by #1464
Labels
bug This issue is a bug.

Comments

@jalandis
Copy link

Confirm by changing [ ] to [x] below to ensure that it's a bug:

Describe the bug
Upgrading from v1 to v2, I am unable to set X-Amz-SignedHeaders=content-length when creating pre-signed upload links. After setting ContentLength in the s3.PutObjectInput struct, I expected this to be included in the signed headers as it was in v1. I was expecting it to work the same as setting ContentType in the s3.PutObjectInput struct which does add content-type to the signed headers.

Version of AWS SDK for Go?

github.com/aws/aws-sdk-go-v2 v1.9.0
github.com/aws/aws-sdk-go-v2/config v1.8.1
github.com/aws/aws-sdk-go-v2/credentials v1.4.1
github.com/aws/aws-sdk-go-v2/service/s3 v1.15.1

Version of Go (go version)?
go version go1.17 darwin/amd64

To Reproduce (observed behavior)

	client := s3.NewPresignClient(s3.NewFromConfig(config), func(options *s3.PresignOptions) {
		options.Expires = timeout
	})
	req, err := client.PresignPutObject(
		context.Background(),
		&s3.PutObjectInput{
			Bucket:        ptr.String(bucket),
			Key:           ptr.String(fileIdentifier),
			ContentLength: 100,
		},
	)
	fmt.Println(req.URL)

Expected behavior
Content length set in signed headers:

X-Amz-SignedHeaders=content-length&host

Additional context

@jalandis jalandis added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 16, 2021
@ANair2
Copy link

ANair2 commented Oct 15, 2021

This issue is pretty much reproducible and a breaker from the previous version.
There has been 2 releases after this is and this issue has not been addressed

@skmcgrail skmcgrail removed the needs-triage This issue or PR still needs to be triaged. label Oct 15, 2021
@skmcgrail
Copy link
Member

As a temporary work-around setting http.NoBody will cause the header to be signed. Still need to investigate further on why the header is being excluded.

package main

import (
	"bytes"
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
	"io"
	"math/rand"
	"net/http"
	"net/url"
	"strconv"
)

func main() {
	cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion("us-west-2"))
	if err != nil {
		panic(err)
	}

	client := s3.NewFromConfig(cfg)

	presignClient := s3.NewPresignClient(client)

	httpRequest, err := presignClient.PresignPutObject(context.Background(), &s3.PutObjectInput{
		Bucket:        aws.String("mcgrails-test-data"),
		Key:           aws.String("workarond"),
		ContentLength: 100,
		Body:          http.NoBody,
	}, func(options *s3.PresignOptions) {
		options.ClientOptions = append(options.ClientOptions, func(options *s3.Options) {
			options.ClientLogMode |= aws.LogSigning
		})
	})
	if err != nil {
		panic(err)
	}

	data := make([]byte, 100)

	_, err = rand.Read(data)
	if err != nil {
		panic(err)
	}

	p, err := url.Parse(httpRequest.URL)
	if err != nil {
		panic(err)
	}

	// Go's standard HTTP Client won't honor the header unless we pull it out here and specify it on request directly
	// so doing that here for POC for fix.
	contentLength, err := strconv.Atoi(httpRequest.SignedHeader.Get("Content-Length"))
	if err != nil {
		panic(err)
	}

	response, err := http.DefaultClient.Do(&http.Request{
		Method:        httpRequest.Method,
		URL:           p,
		ContentLength: int64(contentLength),
		Header:        httpRequest.SignedHeader,
		Body:          io.NopCloser(bytes.NewReader(data)),
	})
	if err != nil {
		panic(err)
	}

	respBody, err := io.ReadAll(response.Body)
	if err != nil {
		panic(err)
	}

	fmt.Println(response.StatusCode)
	fmt.Printf("%s", respBody)
}

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants