{"record":{"id":"bb8e046b8083c651","repo":"JuliusBrussee/caveman","slug":"s-is-not-valid-base64-w","errorCode":null,"errorMessage":"%s is not valid base64: %w","messagePattern":"(.+?) is not valid base64: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":43,"sourceCode":"\t\"time\"\n\n\t\"github.com/JuliusBrussee/caveman/shared/platform/kms\"\n\t\"github.com/JuliusBrussee/caveman/shared/platform/runtimeenv\"\n)\n\n// envKey is the name of the environment variable holding the base64-encoded\n// 32-byte master key.\nconst envKey = \"CAVE_LOCAL_ENCRYPTION_KEY\"\n\n// loadKey reads and validates the 32-byte AES key from the environment.\nfunc loadKey() ([]byte, error) {\n\tkeyB64 := os.Getenv(envKey)\n\tif keyB64 == \"\" {\n\t\treturn nil, fmt.Errorf(\"%s is not set; cannot encrypt/decrypt secrets\", envKey)\n\t}\n\tkeyBytes, err := base64.StdEncoding.DecodeString(keyB64)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%s is not valid base64: %w\", envKey, err)\n\t}\n\tif len(keyBytes) != 32 {\n\t\treturn nil, fmt.Errorf(\"%s must decode to exactly 32 bytes, got %d\", envKey, len(keyBytes))\n\t}\n\treturn keyBytes, nil\n}\n\n// Encrypt seals plaintext with AES-256-GCM and a fresh random nonce, returning\n// nonce(12) || ciphertext+tag as raw bytes.\nfunc Encrypt(plaintext []byte) ([]byte, error) {\n\tif useKMS() {\n\t\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\t\tdefer cancel()\n\t\twrapped, err := kms.Encrypt(ctx, plaintext)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"secretbox: KMS encrypt: %w\", err)\n\t\t}\n\t\treturn wrapped, nil","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L25-L61","documentation":"The CAVE_LOCAL_ENCRYPTION_KEY value must be valid standard base64; loadKey wraps the base64 decode failure and returns this error. This catches keys that were corrupted, truncated, or quoted/padded incorrectly.","triggerScenarios":"Setting CAVE_LOCAL_ENCRYPTION_KEY to raw random bytes instead of base64 text, adding whitespace/newlines or surrounding quotes from a .env file, or using base64.URL encoding instead of StdEncoding.","commonSituations":"Pasting a key with shell-escaped characters; .env parsers keeping quotes; generating a key with a URL-safe base64 tool and feeding it to a StdEncoding decoder.","solutions":["Regenerate cleanly: openssl rand -base64 32 and set the output verbatim, without quotes.","Strip whitespace/quotes from the value before assignment; verify with echo \"$CAVE_LOCAL_ENCRYPTION_KEY\" | base64 -d | wc -c (expect 32).","If your source emits URL-safe base64, convert: strings.NewReplacer(\"-\",\"+\",\"_\",\"/\").Replace(v) or use RawStdEncoding at the source."],"exampleFix":"// before\nexport CAVE_LOCAL_ENCRYPTION_KEY=\"abc123\" # not valid base64 of 32 bytes / arbitrary text\n// after\nexport CAVE_LOCAL_ENCRYPTION_KEY=$(openssl rand -base64 32)","handlingStrategy":"validation","validationCode":"keyB64 := os.Getenv(\"CAVE_LOCAL_ENCRYPTION_KEY\")\nif _, err := base64.StdEncoding.DecodeString(strings.TrimSpace(keyB64)); err != nil {\n    return fmt.Errorf(\"CAVE_LOCAL_ENCRYPTION_KEY is not valid base64: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := secretbox.Encrypt(pt); err != nil && strings.Contains(err.Error(), \"not valid base64\") {\n    // regenerate the key and fix the env value\n}","preventionTips":["Set keys programmatically from a secret manager, not hand-pasted","Never wrap the value in quotes inside .env files","Verify with: echo \"$KEY\" | base64 -d >/dev/null"],"tags":["encryption","base64","environment","configuration"],"backgroundTag":"invalid-env-var-value","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"}