diff --git a/builtin/logical/transit/backend_test.go b/builtin/logical/transit/backend_test.go index a9c27bcef624..315200fdabcb 100644 --- a/builtin/logical/transit/backend_test.go +++ b/builtin/logical/transit/backend_test.go @@ -1091,3 +1091,38 @@ func testPolicyFuzzingCommon(t *testing.T, be *backend) { // Wait for them all to finish wg.Wait() } + +func TestBadInput(t *testing.T) { + var b *backend + sysView := logical.TestSystemView() + storage := &logical.InmemStorage{} + + b = Backend(&logical.BackendConfig{ + StorageView: storage, + System: sysView, + }) + + req := &logical.Request{ + Storage: storage, + Operation: logical.UpdateOperation, + Path: "keys/test", + } + + resp, err := b.HandleRequest(req) + if err != nil { + t.Fatal(err) + } + if resp != nil { + t.Fatal("expected nil response") + } + + req.Path = "decrypt/test" + req.Data = map[string]interface{}{ + "ciphertext": "vault:v1:abcd", + } + + _, err = b.HandleRequest(req) + if err == nil { + t.Fatal("expected error") + } +} diff --git a/helper/keysutil/policy.go b/helper/keysutil/policy.go index 5e14334f6a93..85591f8e44bd 100644 --- a/helper/keysutil/policy.go +++ b/helper/keysutil/policy.go @@ -675,6 +675,10 @@ func (p *Policy) Decrypt(context, nonce []byte, value string) (string, error) { return "", errutil.InternalError{Err: err.Error()} } + if len(decoded) < gcm.NonceSize() { + return "", errutil.UserError{Err: "invalid ciphertext length"} + } + // Extract the nonce and ciphertext var ciphertext []byte if p.ConvergentEncryption && p.ConvergentVersion < 2 {