{"record":{"id":"0e59cfe71dd91403","repo":"JuliusBrussee/caveman","slug":"s-must-decode-to-exactly-32-bytes-got-d","errorCode":null,"errorMessage":"%s must decode to exactly 32 bytes, got %d","messagePattern":"(.+?) must decode to exactly 32 bytes, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":46,"sourceCode":"\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\n\t}\n\tif runtimeenv.IsProduction() {\n\t\treturn nil, fmt.Errorf(\"secretbox: production requires CAVE_KMS_PROVIDER=scaleway\")","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L28-L64","documentation":"After base64 decoding, the key must be exactly 32 bytes for AES-256-GCM; loadKey rejects any other length with this error naming the actual decoded size. This enforces AES-256 key material at load time.","triggerScenarios":"Setting CAVE_LOCAL_ENCRYPTION_KEY to the base64 of a 16- or 24-byte key (AES-128/192 material), a hex string, a passphrase, or a truncated/padded base64 string that decodes to != 32 bytes.","commonSituations":"Reusing keys generated for a different algorithm (e.g. 16-byte keys from an older tool); passing a human password instead of derived key bytes; copy/paste truncating the base64 string.","solutions":["Generate a correctly sized key: openssl rand -base64 32 (decodes to exactly 32 bytes).","If you must derive from a passphrase, use a KDF (e.g. scrypt/argon2) to produce 32 bytes, then base64-encode it.","Pre-check before calling Encrypt/Decrypt: decode with base64.StdEncoding and assert len(keyBytes) == 32."],"exampleFix":"// before\nexport CAVE_LOCAL_ENCRYPTION_KEY=$(openssl rand -hex 32) # decodes to 32 bytes? no — hex text decodes to 32 bytes only if base64 of 32 raw bytes; hex string is 64 chars -> invalid length/bytes\n// after\nexport CAVE_LOCAL_ENCRYPTION_KEY=$(openssl rand -base64 32)","handlingStrategy":"validation","validationCode":"if kb, err := base64.StdEncoding.DecodeString(os.Getenv(\"CAVE_LOCAL_ENCRYPTION_KEY\")); err != nil || len(kb) != 32 {\n    return fmt.Errorf(\"CAVE_LOCAL_ENCRYPTION_KEY must be base64 of exactly 32 bytes (openssl rand -base64 32)\")\n}","typeGuard":null,"tryCatchPattern":"if err := secretbox.Decrypt(ct); err != nil && strings.Contains(err.Error(), \"exactly 32 bytes\") {\n    // key material has the wrong size; regenerate before proceeding\n}","preventionTips":["Always generate keys with openssl rand -base64 32","Never use hex strings, passphrases, or short keys as the env value","Add the 32-byte decode check to environment smoke tests"],"tags":["encryption","key-size","aes","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"}