{"record":{"id":"6ca23ca086bdc11b","repo":"JuliusBrussee/caveman","slug":"secretbox-decrypt-s-w","errorCode":null,"errorMessage":"secretbox: decrypt %s: %w","messagePattern":"secretbox: decrypt (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":182,"sourceCode":"// secretbox/KMS envelope in ciphertextEnv. Local development may continue using\n// plaintextEnv. An entirely absent optional secret returns an empty string.\nfunc ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv string) (string, error) {\n\tplain := strings.TrimSpace(os.Getenv(plaintextEnv))\n\tencoded := strings.TrimSpace(os.Getenv(ciphertextEnv))\n\tproduction := runtimeenv.IsProduction()\n\tif production && plain != \"\" {\n\t\treturn \"\", fmt.Errorf(\"secretbox: production refuses plaintext %s; use %s\", plaintextEnv, ciphertextEnv)\n\t}\n\tif encoded == \"\" {\n\t\treturn plain, nil\n\t}\n\twrapped, err := base64.StdEncoding.DecodeString(encoded)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"secretbox: %s is not valid base64\", ciphertextEnv)\n\t}\n\tdecrypted, err := Decrypt(wrapped)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"secretbox: decrypt %s: %w\", ciphertextEnv, err)\n\t}\n\tif len(decrypted) == 0 {\n\t\treturn \"\", fmt.Errorf(\"secretbox: %s decrypted to an empty secret\", ciphertextEnv)\n\t}\n\treturn string(decrypted), nil\n}\n","sourceCodeStart":164,"sourceCodeEnd":189,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L164-L189","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nexport SECRETBOX_KEY=$(cat keys/dev.key)   # but ciphertext came from prod\nplain, err := secretbox.ResolveEnvironmentSecret(\"\", \"DB_PASSWORD\")\n// secretbox: decrypt DB_PASSWORD: aes-gcm open: cipher: message authentication failed\n\n// after\nexport SECRETBOX_KEY=$(cat keys/prod.key)\nplain, err := secretbox.ResolveEnvironmentSecret(\"\", \"DB_PASSWORD\")","handlingStrategy":"try-catch","validationCode":"if raw, err := base64.StdEncoding.DecodeString(enc); err == nil && len(raw) < 12 {\n    return fmt.Errorf(\"%s decodes to a truncated envelope (%d bytes)\", ciphertextEnv, len(raw))\n}","typeGuard":null,"tryCatchPattern":"val, err := secretbox.ResolveEnvironmentSecret(\"\", \"DB_PASSWORD\")\nif err != nil {\n    if strings.Contains(err.Error(), \"decrypt \") {\n        return fmt.Errorf(\"cannot decrypt %s with the configured key — check environment/key alignment: %w\", \"DB_PASSWORD\", err)\n    }\n    return err\n}","preventionTips":["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."],"tags":["go","crypto","config","env-var"],"backgroundTag":"decryption-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}