fix: include decrypted sequence comments in the MAC (fixes #2243)#2245
Merged
felixfontein merged 2 commits intoJul 11, 2026
Merged
Conversation
felixfontein
reviewed
Jul 8, 2026
felixfontein
left a comment
Contributor
There was a problem hiding this comment.
Thanks for reporting this and creating a fix! There are a few issues with the test and the comment for the fix, though.
Contributor
Author
|
Thanks for the review @felixfontein! Pushed 1aa8912 addressing all four points:
Verified the test fails on the pre-fix code and passes with the fix. |
ec3fcb9 to
e9782f9
Compare
felixfontein
reviewed
Jul 9, 2026
502d079 to
9bdc594
Compare
A comment inside a YAML sequence is encrypted as an ENC[...,type:comment] element and is restored as a sops.Comment node when the file is loaded again. Since getsops#2227, Tree.Decrypt performed the MAC-inclusion test on the input node (isComment) instead of on the decrypted result. A comment that decrypts successfully becomes a non-Comment value, so testing the input excluded it from the MAC. Tree.Encrypt still folds the comment in (at encrypt time it is a non-Comment node), so the encrypt-side and decrypt-side MACs disagreed and every file containing a comment inside a sequence failed to decrypt with "MAC mismatch" -- including files that 3.13.2 encrypted itself. Restore the pre-getsops#2227 behaviour of testing the decrypted value's type, while keeping the non-string panic guard added in getsops#2227. Add a YAML store round-trip regression test that fails before this change. Fixes getsops#2243 Signed-off-by: Chris Coutinho <chris@coutinho.io> fix: address review feedback on MAC sequence-comment fix - Correct the explanatory comment in Tree.Decrypt: an encrypted non-Comment slice element whose metadata marks it a comment decrypts back to a Comment, so the MAC decision must use the decrypted result's type, not the input's. - Move the regression test into sops_test.go, building the tree from SOPS objects directly and encrypting/decrypting the same tree instead of round-tripping through the YAML store. Signed-off-by: Chris Coutinho <chris@coutinho.io>
…ment Address review feedback on the sequence-comment MAC fix: - sops.go: replace the MAC-inclusion comment with the reviewer's wording explaining that non-Comment values can decrypt to Comment values, so isComment on the input cannot be used here. - Move TestMACWithCommentInSequence into a new external test file (package sops_test), letting it import and use the real aes cipher. The previous stub cipher existed only because an internal (package sops) test cannot import aes without an import cycle (aes imports the root sops package). Drop the stub commentCipher. - Use assert.False for the post-encrypt Comment check, and additionally assert the decrypted element is restored to a Comment with its content. Signed-off-by: Chris Coutinho <chris@coutinho.io>
9bdc594 to
7295afe
Compare
felixfontein
approved these changes
Jul 11, 2026
Contributor
|
@cbcoutinho thanks for fixing this! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes a regression in 3.13.2 where sops fails to decrypt any YAML/JSON file
containing a comment inside a sequence, reporting
MAC mismatch. Thisaffects files written by ≤3.13.1 and files 3.13.2 encrypts itself.
Fixes #2243.
Root cause
A comment in a sequence is encrypted as an
ENC[...,type:comment]element andrestored as a
sops.Commentnode when the file is loaded again.#2227 changed the MAC-inclusion test in
Tree.Decryptfrom inspecting thedecrypted result to inspecting the input node:
A comment that decrypts successfully becomes a non-
Commentvalue, so the oldcheck included it in the MAC.
Tree.Encryptstill folds the comment in (atencrypt time it is a non-
Commentnode), so the encrypt-side and decrypt-sideMACs disagree for any sequence containing a comment.
Fix
Restore the pre-#2227 behaviour (test the decrypted value's type) while keeping
the non-string panic guard #2227 introduced. One-line semantic change.
Reproduction
A comment on a mapping, or a sequence with no comment, is unaffected.
Test
Adds
TestMACWithCommentInSequence(YAML store round-trip: load → encrypt →emit → reload → decrypt, asserting the MACs match). It fails on
mainandpasses with this change.
go test ./...for the root, stores, decrypt and cmdpackages passes;
gofmt/go vetclean.This PR was generated with the help of AI, and reviewed by a Human