Skip to content

Commit 4762a7d

Browse files
authored
Merge pull request #2227 from felixfontein/casting
Fix panic when expecting an encrypted string, but a non-string is encountered
2 parents 0eea6d0 + 4ed1e46 commit 4762a7d

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

sops.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ func (branch TreeBranch) walkBranch(in TreeBranch, path []string, commentsStack
418418
}
419419
key, ok := item.Key.(string)
420420
if !ok {
421-
return nil, fmt.Errorf("Tree contains a non-string key (type %T): %s. Only string keys are"+
422-
"supported", item.Key, item.Key)
421+
return nil, fmt.Errorf(
422+
"Tree contains a non-string key (type %T): %s. Only string keys are supported", item.Key, item.Key)
423423
}
424424
newV, err := branch.walkValue(item.Value, append(path, key), commentsStack, onLeaves)
425425
if err != nil {
@@ -554,6 +554,8 @@ func (tree Tree) Encrypt(key []byte, cipher Cipher) (string, error) {
554554
if ok && tree.Metadata.UnencryptedCommentRegex != "" {
555555
// If an encrypted comment matches tree.Metadata.UnencryptedCommentRegex, decryption will fail
556556
// as the MAC does not match, and the commented value will not be decrypted.
557+
// Note that cipher.Encrypt() returns a string, but we stored the result in an interface{}
558+
// variable, so we have to cast it back.
557559
matched, _ := regexp.Match(tree.Metadata.UnencryptedCommentRegex, []byte(in.(string)))
558560
if matched {
559561
return nil, fmt.Errorf("Encrypted comment %q matches UnencryptedCommentRegex! Make sure that UnencryptedCommentRegex cannot match an encrypted comment.", in)
@@ -595,13 +597,13 @@ func (tree Tree) Decrypt(key []byte, cipher Cipher) (string, error) {
595597
}
596598
walk := func(branch TreeBranch) error {
597599
_, err := branch.walkBranch(branch, make([]string, 0), make([][]string, 0), func(in interface{}, path []string, commentsStack [][]string) (interface{}, error) {
598-
c, ok := in.(Comment)
599-
encrypted := tree.shouldBeEncrypted(path, commentsStack, ok)
600+
c, isComment := in.(Comment)
601+
encrypted := tree.shouldBeEncrypted(path, commentsStack, isComment)
600602
var v interface{}
601603
if encrypted {
602604
var err error
603605
pathString := strings.Join(path, ":") + ":"
604-
if ok {
606+
if isComment {
605607
v, err = cipher.Decrypt(c.Value, key, pathString)
606608
if err != nil {
607609
// Assume the comment was not encrypted in the first place
@@ -612,18 +614,20 @@ func (tree Tree) Decrypt(key []byte, cipher Cipher) (string, error) {
612614
"SOPS.")
613615
v = c
614616
}
615-
} else {
616-
v, err = cipher.Decrypt(in.(string), key, pathString)
617+
} else if inStr, inIsStr := in.(string); inIsStr {
618+
v, err = cipher.Decrypt(inStr, key, pathString)
617619
if err != nil {
618620
return nil, fmt.Errorf("Could not decrypt value: %s", err)
619621
}
622+
} else {
623+
return nil, fmt.Errorf("Expected encrypted value as string, but got %T", in)
620624
}
621625
} else {
622626
v = in
623627
}
624628
if !tree.Metadata.MACOnlyEncrypted || encrypted {
625629
// Only add to MAC if not a comment
626-
if _, ok := v.(Comment); !ok {
630+
if !isComment {
627631
bytes, err := ToBytes(v)
628632
if err != nil {
629633
return nil, fmt.Errorf("Could not convert %s to bytes: %s", in, err)

0 commit comments

Comments
 (0)