@@ -16,6 +16,18 @@ import (
1616 "time"
1717)
1818
19+ const (
20+ // defaultMaxClockSkew is the default maximum allowed clock skew for request timestamps (AWS default)
21+ defaultMaxClockSkew = 15 * time .Minute
22+
23+ // maxBodyReadSize is the maximum body size for signature calculation (10 MiB)
24+ // Requests larger than this must use UNSIGNED-PAYLOAD
25+ maxBodyReadSize = 10 * 1024 * 1024
26+
27+ // emptyPayloadHash is the SHA256 hash of an empty payload
28+ emptyPayloadHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
29+ )
30+
1931var (
2032 // ErrMissingAuthHeader is returned when Authorization header is missing
2133 ErrMissingAuthHeader = errors .New ("missing Authorization header" )
@@ -88,7 +100,7 @@ func NewSignatureV4(store CredentialStore, region, service string) *SignatureV4
88100 credentialStore : store ,
89101 region : region ,
90102 service : service ,
91- maxClockSkew : 15 * time . Minute , // AWS default
103+ maxClockSkew : defaultMaxClockSkew ,
92104 }
93105}
94106
@@ -217,8 +229,6 @@ func (s *SignatureV4) Verify(r *http.Request) error {
217229 } else if payloadHash != "" {
218230 // Client provided a specific payload hash - verify it matches the body
219231 // Protect against memory exhaustion by limiting body read size
220- const maxBodyReadSize = 10 * 1024 * 1024 // 10 MiB max for signature calculation
221-
222232 if r .ContentLength > maxBodyReadSize {
223233 // Request is too large to buffer for hash verification
224234 // Client should use X-Amz-Content-Sha256: UNSIGNED-PAYLOAD header
@@ -246,16 +256,13 @@ func (s *SignatureV4) Verify(r *http.Request) error {
246256 }
247257 } else {
248258 // Empty body - verify client sent empty payload hash
249- emptyHash := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
250- if subtle .ConstantTimeCompare ([]byte (emptyHash ), []byte (payloadHash )) != 1 {
259+ if subtle .ConstantTimeCompare ([]byte (emptyPayloadHash ), []byte (payloadHash )) != 1 {
251260 return ErrPayloadHashMismatch
252261 }
253262 }
254263 } else {
255264 // No payload hash header provided - we need to read body to calculate hash
256265 // Protect against memory exhaustion by limiting body read size
257- const maxBodyReadSize = 10 * 1024 * 1024 // 10 MiB max for signature calculation
258-
259266 if r .ContentLength > maxBodyReadSize {
260267 // Request is too large to buffer for signature verification
261268 // Client should use X-Amz-Content-Sha256: UNSIGNED-PAYLOAD header
@@ -276,7 +283,7 @@ func (s *SignatureV4) Verify(r *http.Request) error {
276283 r .Body = io .NopCloser (bytes .NewReader (payload ))
277284 payloadHash = hex .EncodeToString (sha256Hash (payload ))
278285 } else {
279- payloadHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" // empty payload hash
286+ payloadHash = emptyPayloadHash
280287 }
281288 }
282289
0 commit comments