fix(crypto): return client errors for malformed KMS contexts

Signed-off-by: Feng Ruohang <rh@vonng.com>
This commit is contained in:
Feng Ruohang
2026-11-12 07:49:47 +08:00
parent c39605d4f8
commit 2ed720a7fb
3 changed files with 40 additions and 3 deletions
+10 -2
View File
@@ -76,9 +76,17 @@ func (ssekms) ParseHTTP(h http.Header) (string, kms.Context, error) {
var ctx kms.Context
if context, ok := h[xhttp.AmzServerSideEncryptionKmsContext]; ok {
invalidContext := kms.Error{
Code: http.StatusBadRequest,
APICode: "InvalidArgument",
Err: "KMS context must be valid base64-encoded JSON with string values",
}
if len(context) == 0 {
return "", nil, invalidContext
}
b, err := base64.StdEncoding.DecodeString(context[0])
if err != nil {
return "", nil, err
return "", nil, invalidContext
}
// Persisting invalid UTF-8 replaces bytes and can collapse distinct
// keys, changing the associated data needed to decrypt the object.
@@ -92,7 +100,7 @@ func (ssekms) ParseHTTP(h http.Header) (string, kms.Context, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.Unmarshal(b, &ctx); err != nil {
return "", nil, err
return "", nil, invalidContext
}
}