Skip to content

sops: MAC mismatch – file changed outside of sops

SOPStested on sops v3.13.3

sops computes a MAC over every value in a file – including the unencrypted ones such as metadata.namespace. Any change outside of sops breaks it. That's intended: otherwise an attacker could silently redirect an encrypted Secret into another namespace.

Symptom

sed -i 's/namespace: notes-dev/namespace: notes-qa/' dev/notes-db.yaml
sops decrypt dev/notes-db.yaml 2>&1 | cut -c1-40
MAC mismatch. File has 66C57B21830010AFB

Typical trigger: someone "just quickly" changes the namespace or a name – with sed or in an editor, because metadata is readable after all.

Diagnosis

The message names the level: MAC means the file as a whole, AES_GCM a single value, "Failed to get the data key" the key. git diff shows the changed line.

sops decrypt raetsel.yaml >/dev/null 2>fehler.txt; echo "rc=$?"
grep -oE 'MAC mismatch|AES_GCM|metadata not found|yaml: .*|Failed to get the data key' \
  fehler.txt | head -1
rc=1
MAC mismatch

Causes

Plain textPlain-text field changed with sed or an editor

The MAC covers all values, including the unencrypted ones. Changing namespace without using sops breaks the checksum.

TamperingA file from someone else

If you don't know who changed the file, a MAC error is an alarm signal, not a nuisance – that's exactly what it's for.

AES_GCMNot this – an ENC[…] value is damaged

If an encrypted value was touched, sops reports AES_GCM, not MAC. The value can't be rescued, only restored from Git.

sops decrypt dev/notes-db.yaml 2>&1 | tail -1
Error decrypting tree: Error walking tree: Could not decrypt value: Could not decrypt
with AES_GCM: cipher: message authentication failed

Fix

If the change is intended, decrypt with --ignore-mac and encrypt again – that computes the MAC over the new state. sops set doesn't help here: it checks the MAC first, too. From now on, change files only through sops set or sops edit.
sops decrypt --ignore-mac dev/notes-db.yaml \
  | sops encrypt --filename-override dev/notes-db.yaml /dev/stdin \
  > ~/sops/tag14/fix.yaml
mv ~/sops/tag14/fix.yaml dev/notes-db.yaml
sops decrypt --extract '["metadata"]["namespace"]' dev/notes-db.yaml
notes-qa

Don't: Using --ignore-mac on a file whose change you can't account for. It means: "I know who changed the file."

▶
Break it yourself and find it – free
SOPS, day 14: recognise eight typical failures with sops and Sealed Secrets by their message and fix them without losing values.
Go to day 14 →

FAQ

What's the difference between MAC mismatch and "Could not decrypt with AES_GCM"?

MAC mismatch concerns the file as a whole – usually plain text was changed. AES_GCM concerns a single ENC[…] value that is damaged.

How do I catch broken MACs before the merge?

In CI, check every sops file with sops filestatus and make a decryption attempt with the CI key.

Related error patterns

More