{"record":{"id":"be7901d14c8ab666","repo":"JuliusBrussee/caveman","slug":"secretbox-s-is-not-valid-base64","errorCode":null,"errorMessage":"secretbox: %s is not valid base64","messagePattern":"secretbox: (.+?) is not valid base64","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":178,"sourceCode":"}\n\n// ResolveEnvironmentSecret loads a boot-time secret. In production plaintext\n// environment variables are rejected: operators must provide a base64-encoded\n// 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":160,"sourceCodeEnd":189,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L160-L189","documentation":"ResolveEnvironmentSecret found a non-empty value in the ciphertext env var that is not valid standard base64. Encrypted secrets are stored base64-encoded, so the value cannot even be decoded into the binary envelope that Decrypt expects. This is a malformed-value check that fails before any crypto work happens.","triggerScenarios":"os.Getenv(ciphertextEnv) contains characters outside the standard base64 alphabet (spaces, quotes, 'Bearer ' prefixes, URL-safe '-_' chars when StdEncoding is required, or newlines from a YAML fold), or the value is truncated so padding is wrong.","commonSituations":"Pasting the encrypted value with surrounding whitespace/quotes into a YAML or .env file; using RawURLEncoding output from a different tool where StdEncoding is expected; a secret manager adding a prefix; shell exporting only part of the blob.","solutions":["Re-encode the secret envelope with base64.StdEncoding and replace the env var value verbatim.","Strip quotes, whitespace, and prefixes (e.g. 'base64:' or 'Bearer ') from the value in your config file.","If the value came from a tool emitting URL-safe base64, convert it to standard encoding (replace -_ with +/, add padding).","If the value is simply a plaintext secret, remove it from the ciphertext var — it must be encrypted first via secretbox.Encrypt."],"exampleFix":"// before\nAPP_TOKEN_CIPHERTEXT: \"\"aGVsbG8gd29ybGQ\"\"   # quotes copied from docs\n\n// after (generate correctly)\nenc, _ := secretbox.Encrypt([]byte(token))\n// export APP_TOKEN_CIPHERTEXT=$(printf '%s' \"$enc\" | base64 -w0)","handlingStrategy":"validation","validationCode":"if _, err := base64.StdEncoding.DecodeString(strings.TrimSpace(os.Getenv(ciphertextEnv))); err != nil {\n    return fmt.Errorf(\"%s must be standard base64 of a secretbox envelope\", ciphertextEnv)\n}","typeGuard":"func isStdBase64(s string) bool {\n    _, err := base64.StdEncoding.DecodeString(s)\n    return err == nil\n}","tryCatchPattern":"val, err := secretbox.ResolveEnvironmentSecret(\"\", \"APP_TOKEN_CIPHERTEXT\")\nif err != nil {\n    if strings.Contains(err.Error(), \"is not valid base64\") {\n        return fmt.Errorf(\"%s is malformed; re-encode with `base64 -w0` and remove quotes\", \"APP_TOKEN_CIPHERTEXT\")\n    }\n    return err\n}","preventionTips":["Always produce ciphertext vars via `base64 -w0` (no line wrapping).","Quote YAML/.env values so spaces and special characters survive.","Never prefix encrypted values with scheme markers like 'base64:'.","Validate all *_CIPHERTEXT vars at service startup, not lazily."],"tags":["go","base64","config","env-var"],"backgroundTag":"invalid-base64-encoding","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"}