JuliusBrussee/caveman · error

secretbox: decrypt

Error message

secretbox: decrypt %s: %w

What it means

ResolveEnvironmentSecret successfully base64-decoded the ciphertext env var, but the underlying Decrypt call failed. The original Decrypt error (e.g. 'ciphertext too short' or 'aes-gcm open: ...') is wrapped with this prefix, so the root cause is always the wrapped message. It means the base64 was valid but did not decode to a decryptable secretbox envelope under the current key.

Solutions

  1. Read the wrapped inner error: fix per its meaning (key mismatch → align keys; too short → re-export full value).
  2. Re-encrypt the secret with the key configured in the environment where it is consumed and redeploy the ciphertext var.
  3. Confirm the ciphertext value belongs to the env var you set (easy to swap APP_TOKEN/ADMIN_TOKEN values).
  4. During key rotation, keep the previous key available until all stored ciphertexts are re-encrypted.

Example fix

// before
export SECRETBOX_KEY=$(cat keys/dev.key)   # but ciphertext came from prod
plain, err := secretbox.ResolveEnvironmentSecret("", "DB_PASSWORD")
// secretbox: decrypt DB_PASSWORD: aes-gcm open: cipher: message authentication failed

// after
export SECRETBOX_KEY=$(cat keys/prod.key)
plain, err := secretbox.ResolveEnvironmentSecret("", "DB_PASSWORD")
Defensive patterns

Strategy: try-catch

Validate before calling

if raw, err := base64.StdEncoding.DecodeString(enc); err == nil && len(raw) < 12 {
    return fmt.Errorf("%s decodes to a truncated envelope (%d bytes)", ciphertextEnv, len(raw))
}

Try / catch

val, err := secretbox.ResolveEnvironmentSecret("", "DB_PASSWORD")
if err != nil {
    if strings.Contains(err.Error(), "decrypt ") {
        return fmt.Errorf("cannot decrypt %s with the configured key — check environment/key alignment: %w", "DB_PASSWORD", err)
    }
    return err
}

Prevention

When it happens

Trigger: The decoded envelope is shorter than the GCM nonce, was encrypted with a different key than the one configured in this environment, or the ciphertext bytes were altered after encoding — surfaced when calling ResolveEnvironmentSecret with a populated ciphertextEnv.

Common situations: Decrypting a secret produced in another environment/cluster with a different SECRETBOX_KEY; secrets rotated on the encryption side only; base64 re-wrapping that corrupted the payload; copying a ciphertext from the wrong service.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/6ca23ca086bdc11b. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/secretbox/secretbox.go:182

// secretbox/KMS envelope in ciphertextEnv. Local development may continue using
// plaintextEnv. An entirely absent optional secret returns an empty string.
func ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv string) (string, error) {
	plain := strings.TrimSpace(os.Getenv(plaintextEnv))
	encoded := strings.TrimSpace(os.Getenv(ciphertextEnv))
	production := runtimeenv.IsProduction()
	if production && plain != "" {
		return "", fmt.Errorf("secretbox: production refuses plaintext %s; use %s", plaintextEnv, ciphertextEnv)
	}
	if encoded == "" {
		return plain, nil
	}
	wrapped, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		return "", fmt.Errorf("secretbox: %s is not valid base64", ciphertextEnv)
	}
	decrypted, err := Decrypt(wrapped)
	if err != nil {
		return "", fmt.Errorf("secretbox: decrypt %s: %w", ciphertextEnv, err)
	}
	if len(decrypted) == 0 {
		return "", fmt.Errorf("secretbox: %s decrypted to an empty secret", ciphertextEnv)
	}
	return string(decrypted), nil
}

View on GitHub (pinned to 3ee70a1026)