fix(controlplane): return error on short attestation state ciphertext

Merged
BE
8h ago
+37-0

Fixes #3600

decrypt in app/controlplane/pkg/biz/attestationstate.go checked the salt and IV lengths but not the length of the remaining data before comparing it with the magic prefix. A stored value of 32 to 44 bytes passed the earlier checks and then caused a slice-out-of-range panic.

This change adds a len(ciphertext) < len(magic) check before the comparison, so a truncated or corrupted value now returns an error.

This does not cover the second point in the issue (moving to an authenticated cipher such as AES-GCM). That needs a format change and a migration path for existing rows, so it is left for a separate change.

Testing

  • Added TestDecryptShortInput in attestationstate_test.go. It calls decrypt with inputs of salt + IV only, one byte after the IV, and one byte short of the magic prefix. It checks that there is no panic and that an error is returned.
  • Added TestEncryptDecryptRoundTrip to check that normal encrypt and decrypt still work.
  • The new tests and the project checks pass.

The decrypt function could panic on a truncated database value. I wanted it to return an error, so one bad row does not crash the request.