Skip to content

Commit 9360140

Browse files
committed
add e2e test for presigned requests
Signed-off-by: Artem Torubarov <torubarov.a.a@gmail.com>
1 parent a2be6e4 commit 9360140

8 files changed

Lines changed: 800 additions & 30 deletions

File tree

‎pkg/s3client/util.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ func processHeaders(origin http.Header) (toSign http.Header, notToSign http.Head
281281
continue
282282
}
283283

284+
if strings.EqualFold(name, s3.AmzContentSha256) {
285+
// the proxy re-signs the forwarded request: the payload hash must be
286+
// part of the new signature whether the client signed it or not,
287+
// otherwise it is re-added unsigned below and the storage rejects
288+
// the signature.
289+
toSign[name] = vals
290+
continue
291+
}
292+
284293
if len(origSigned) == 0 {
285294
// sign all if there no signedHeaders in origin
286295
toSign[name] = vals

‎service/proxy/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ When using Helm, specify Swift storage separately in `proxy.config.storage` and
3333

3434
### Auth
3535

36-
For S3, Proxy supports S3 signature v4 (v2 can be enabled). See `auth` section in [config.yaml](./config.yaml):
36+
For S3, Proxy supports S3 signature v4, presigned (query string) v4 URLs, and v2 (v2 can be enabled). See `auth` section in [config.yaml](./config.yaml):
3737
- `auth.useStorage` - use credentials from a configured storage
3838
- `auth.custom` - use custom credentials for proxy endpoint
3939

‎service/proxy/auth/middleware.go‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ type middleware struct {
6363

6464
func (m *middleware) Wrap(next http.Handler) http.Handler {
6565
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66-
user, err := m.isReqAuthenticated(r)
66+
user, payloadHash, err := m.isReqAuthenticated(r)
6767
if err != nil {
6868
util.WriteError(r.Context(), w, err)
6969
return
7070
}
71+
// The proxy re-signs the request for the storage in pkg/s3client, so the
72+
// payload hash the signature was verified against must be declared as a
73+
// header: it is part of the new signature, and the storage would
74+
// otherwise fall back to sha256("") and reject it.
75+
r.Header.Set(s3.AmzContentSha256, payloadHash)
7176
if !isRequestSignatureV4(r) && isRequestPresignedSignatureV4(r) {
7277
// the presigned query-string auth params were validated:
7378
// strip them so that the request forwarded to the backend
@@ -108,17 +113,23 @@ func (m *middleware) getCred(accessKey string) (credMeta, error) {
108113
return res, nil
109114
}
110115

111-
func (m *middleware) isReqAuthenticated(r *http.Request) (string, error) {
116+
// isReqAuthenticated verifies the request signature and returns the
117+
// authenticated user together with the payload hash the signature was
118+
// verified against.
119+
func (m *middleware) isReqAuthenticated(r *http.Request) (string, string, error) {
112120
switch {
113121
case isRequestSignatureV4(r):
114122
sha256sum := getContentSha256Cksum(r)
115-
return m.doesSignatureV4Match(sha256sum, r)
123+
user, err := m.doesSignatureV4Match(sha256sum, r)
124+
return user, sha256sum, err
116125
case isRequestPresignedSignatureV4(r):
117126
return m.doesPresignedSignatureV4Match(r)
118127
case m.allowV2 && isRequestSignatureV2(r):
119-
return m.doesSignatureV2Match(r)
128+
// a V2 signature does not cover the payload
129+
user, err := m.doesSignatureV2Match(r)
130+
return user, unsignedPayload, err
120131
}
121-
return "", mclient.ErrorResponse{
132+
return "", unsignedPayload, mclient.ErrorResponse{
122133
XMLName: xml.Name{},
123134
Code: "CredentialsNotSupported",
124135
Message: "This request does not support given credentials type.",

‎service/proxy/auth/signature_v4_presign.go‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ func isRequestPresignedSignatureV4(r *http.Request) bool {
4747
}
4848

4949
// doesPresignedSignatureV4Match verifies the signature of a presigned
50-
// (query-string authenticated) AWS Signature Version '4' request and
51-
// returns the authenticated user.
52-
func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, error) {
50+
// (query-string authenticated) AWS Signature Version '4' request and returns
51+
// the authenticated user together with the payload hash it verified against.
52+
func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, string, error) {
5353
query := r.URL.Query()
5454

5555
psv, err := s3.ParsePreSignV4(query)
5656
if err != nil {
57-
return "", err
57+
return "", unsignedPayload, err
5858
}
5959

6060
credInfo, err := m.getCred(psv.Credential.AccessKey)
6161
if err != nil {
62-
return "", err
62+
return "", unsignedPayload, err
6363
}
6464
cred := credInfo.cred
6565

6666
if psv.Expires < presignedMinExpires || psv.Expires > presignedMaxExpires {
67-
return "", mclient.ErrorResponse{
67+
return "", unsignedPayload, mclient.ErrorResponse{
6868
XMLName: xml.Name{},
6969
Code: "AuthorizationQueryParametersError",
7070
Message: "X-Amz-Expires must be from 1 second to 604800 seconds (7 days).",
@@ -75,7 +75,7 @@ func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, err
7575
}
7676
now := time.Now().UTC()
7777
if psv.Date.After(now.Add(presignedMaxClockSkew)) {
78-
return "", mclient.ErrorResponse{
78+
return "", unsignedPayload, mclient.ErrorResponse{
7979
XMLName: xml.Name{},
8080
Code: "AccessDenied",
8181
Message: "Request is not valid yet",
@@ -85,7 +85,7 @@ func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, err
8585
}
8686
}
8787
if now.Sub(psv.Date) > psv.Expires {
88-
return "", mclient.ErrorResponse{
88+
return "", unsignedPayload, mclient.ErrorResponse{
8989
XMLName: xml.Name{},
9090
Code: "AccessDenied",
9191
Message: "Request has expired",
@@ -97,13 +97,17 @@ func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, err
9797

9898
extractedSignedHeaders, err := s3.ExtractSignedHeaders(psv.SignedHeaders, r)
9999
if err != nil {
100-
return "", err
100+
return "", unsignedPayload, err
101101
}
102102

103-
// The payload of a presigned request is not signed: the recommended
104-
// value UNSIGNED-PAYLOAD is used unless the client explicitly signed
105-
// a payload checksum through the X-Amz-Content-Sha256 query parameter.
103+
// The payload of a presigned request is not signed: the recommended value
104+
// UNSIGNED-PAYLOAD is used unless the client signed a payload checksum,
105+
// through the X-Amz-Content-Sha256 query parameter or, as MinIO also
106+
// accepts, through the header of the same name.
106107
hashedPayload := query.Get(s3.AmzContentSha256)
108+
if hashedPayload == "" {
109+
hashedPayload = r.Header.Get(s3.AmzContentSha256)
110+
}
107111
if hashedPayload == "" {
108112
hashedPayload = unsignedPayload
109113
}
@@ -118,7 +122,7 @@ func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, err
118122
newSignature := getV4Signature(signingKey, stringToSign)
119123

120124
if !compareSignatureV4(newSignature, psv.Signature) {
121-
return "", mclient.ErrorResponse{
125+
return "", unsignedPayload, mclient.ErrorResponse{
122126
XMLName: xml.Name{},
123127
Code: "SignatureDoesNotMatch",
124128
Message: "The request signature that the server calculated does not match the signature that you provided. Check your AWS secret access key and signing method. For more information, see REST Authentication and SOAP Authentication.",
@@ -128,7 +132,7 @@ func (m *middleware) doesPresignedSignatureV4Match(r *http.Request) (string, err
128132
}
129133
}
130134

131-
return credInfo.user, nil
135+
return credInfo.user, hashedPayload, nil
132136
}
133137

134138
// removePresignParams strips the AWS Signature Version '4' query-string

0 commit comments

Comments
 (0)