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
- Read the wrapped inner error: fix per its meaning (key mismatch → align keys; too short → re-export full value).
- Re-encrypt the secret with the key configured in the environment where it is consumed and redeploy the ciphertext var.
- Confirm the ciphertext value belongs to the env var you set (easy to swap APP_TOKEN/ADMIN_TOKEN values).
- 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
- Match SECRETBOX_KEY per environment; store ciphertexts per-environment, never shared.
- Re-encrypt all stored secrets as part of key rotation runbooks.
- Add a smoke test that decrypts every required secret at deploy time.
- Inspect the wrapped inner error — it tells you key-mismatch vs truncation.
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
- CAVEMAN_CCR_MAX_BYTES must be a positive integer
- secretbox: is not valid base64
- aes-gcm open
- aes-gcm
- CAVEMAN_AUTH_TOKEN must be at least
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)