Skip to content

Commit 1a09415

Browse files
krisisdeekoder
authored andcommitted
Fix bucket name handling in signature V2 (#777)
1 parent 795cbb6 commit 1a09415

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

pkg/s3signer/request-signature-v2.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ const (
4242
func encodeURL2Path(u *url.URL) (path string) {
4343
// Encode URL path.
4444
if isS3, _ := filepath.Match("*.s3*.amazonaws.com", u.Host); isS3 {
45-
hostSplits := strings.SplitN(u.Host, ".", 4)
46-
// First element is the bucket name.
47-
bucketName := hostSplits[0]
45+
bucketName := u.Host[:strings.LastIndex(u.Host, ".s3")]
4846
path = "/" + bucketName
4947
path += u.Path
5048
path = s3utils.EncodePath(path)

pkg/s3signer/utils_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,35 @@ import (
2525
// Tests url encoding.
2626
func TestEncodeURL2Path(t *testing.T) {
2727
type urlStrings struct {
28+
bucketName string
2829
objName string
2930
encodedObjName string
3031
}
3132

3233
bucketName := "bucketName"
3334
want := []urlStrings{
3435
{
36+
bucketName: "bucketName",
3537
objName: "本語",
3638
encodedObjName: "%E6%9C%AC%E8%AA%9E",
3739
},
3840
{
41+
bucketName: "bucketName",
3942
objName: "本語.1",
4043
encodedObjName: "%E6%9C%AC%E8%AA%9E.1",
4144
},
4245
{
4346
objName: ">123>3123123",
47+
bucketName: "bucketName",
4448
encodedObjName: "%3E123%3E3123123",
4549
},
4650
{
51+
bucketName: "bucketName",
4752
objName: "test 1 2.txt",
4853
encodedObjName: "test%201%202.txt",
4954
},
5055
{
56+
bucketName: "test.bucketName",
5157
objName: "test++ 1.txt",
5258
encodedObjName: "test%2B%2B%201.txt",
5359
},
@@ -63,4 +69,5 @@ func TestEncodeURL2Path(t *testing.T) {
6369
t.Fatal("Error")
6470
}
6571
}
72+
6673
}

0 commit comments

Comments
 (0)