Skip to content

Commit

Permalink
allow null types in batch encryption [VAULT-849] (#10386)
Browse files Browse the repository at this point in the history
* allow null types in batch encryption

* dont allow plaintext to be null
  • Loading branch information
Hridoy Roy authored Nov 23, 2020
1 parent 60f4413 commit 6fb85f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions builtin/logical/transit/path_encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"fmt"
"reflect"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
Expand Down Expand Up @@ -154,21 +155,24 @@ func decodeBatchRequestItems(src interface{}, dst *[]BatchRequestItem) error {
}

if v, has := item["context"]; has {
if casted, ok := v.(string); ok {
if !reflect.ValueOf(v).IsValid() {
} else if casted, ok := v.(string); ok {
(*dst)[i].Context = casted
} else {
errs.Errors = append(errs.Errors, fmt.Sprintf("'[%d].context' expected type 'string', got unconvertible type '%T'", i, item["context"]))
}
}

if v, has := item["ciphertext"]; has {
if casted, ok := v.(string); ok {
if !reflect.ValueOf(v).IsValid() {
} else if casted, ok := v.(string); ok {
(*dst)[i].Ciphertext = casted
} else {
errs.Errors = append(errs.Errors, fmt.Sprintf("'[%d].ciphertext' expected type 'string', got unconvertible type '%T'", i, item["ciphertext"]))
}
}

// don't allow "null" to be passed in for the plaintext value
if v, has := item["plaintext"]; has {
if casted, ok := v.(string); ok {
(*dst)[i].Plaintext = casted
Expand All @@ -178,15 +182,17 @@ func decodeBatchRequestItems(src interface{}, dst *[]BatchRequestItem) error {
}

if v, has := item["nonce"]; has {
if casted, ok := v.(string); ok {
if !reflect.ValueOf(v).IsValid() {
} else if casted, ok := v.(string); ok {
(*dst)[i].Nonce = casted
} else {
errs.Errors = append(errs.Errors, fmt.Sprintf("'[%d].nonce' expected type 'string', got unconvertible type '%T'", i, item["nonce"]))
}
}

if v, has := item["key_version"]; has {
if casted, ok := v.(int); ok {
if !reflect.ValueOf(v).IsValid() {
} else if casted, ok := v.(int); ok {
(*dst)[i].KeyVersion = casted
} else {
errs.Errors = append(errs.Errors, fmt.Sprintf("'[%d].key_version' expected type 'int', got unconvertible type '%T'", i, item["key_version"]))
Expand Down
5 changes: 5 additions & 0 deletions builtin/logical/transit/path_encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,11 @@ func TestTransit_decodeBatchRequestItems(t *testing.T) {
},
dest: []BatchRequestItem{},
},
{
name: "src_plaintext-nil-nonce",
src: []interface{}{map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "nonce": "null"}},
dest: []BatchRequestItem{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 6fb85f1

Please sign in to comment.