JuliusBrussee/caveman · error

is not valid base64

Error message

%s is not valid base64: %w

What it means

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.

Solutions

  1. Regenerate cleanly: openssl rand -base64 32 and set the output verbatim, without quotes.
  2. Strip whitespace/quotes from the value before assignment; verify with echo "$CAVE_LOCAL_ENCRYPTION_KEY" | base64 -d | wc -c (expect 32).
  3. If your source emits URL-safe base64, convert: strings.NewReplacer("-","+","_","/").Replace(v) or use RawStdEncoding at the source.

Example fix

// before
export CAVE_LOCAL_ENCRYPTION_KEY="abc123" # not valid base64 of 32 bytes / arbitrary text
// after
export CAVE_LOCAL_ENCRYPTION_KEY=$(openssl rand -base64 32)
Defensive patterns

Strategy: validation

Validate before calling

keyB64 := os.Getenv("CAVE_LOCAL_ENCRYPTION_KEY")
if _, err := base64.StdEncoding.DecodeString(strings.TrimSpace(keyB64)); err != nil {
    return fmt.Errorf("CAVE_LOCAL_ENCRYPTION_KEY is not valid base64: %w", err)
}

Try / catch

if err := secretbox.Encrypt(pt); err != nil && strings.Contains(err.Error(), "not valid base64") {
    // regenerate the key and fix the env value
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/bb8e046b8083c651. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/secretbox/secretbox.go:43

	"time"

	"github.com/JuliusBrussee/caveman/shared/platform/kms"
	"github.com/JuliusBrussee/caveman/shared/platform/runtimeenv"
)

// envKey is the name of the environment variable holding the base64-encoded
// 32-byte master key.
const envKey = "CAVE_LOCAL_ENCRYPTION_KEY"

// loadKey reads and validates the 32-byte AES key from the environment.
func loadKey() ([]byte, error) {
	keyB64 := os.Getenv(envKey)
	if keyB64 == "" {
		return nil, fmt.Errorf("%s is not set; cannot encrypt/decrypt secrets", envKey)
	}
	keyBytes, err := base64.StdEncoding.DecodeString(keyB64)
	if err != nil {
		return nil, fmt.Errorf("%s is not valid base64: %w", envKey, err)
	}
	if len(keyBytes) != 32 {
		return nil, fmt.Errorf("%s must decode to exactly 32 bytes, got %d", envKey, len(keyBytes))
	}
	return keyBytes, nil
}

// Encrypt seals plaintext with AES-256-GCM and a fresh random nonce, returning
// nonce(12) || ciphertext+tag as raw bytes.
func Encrypt(plaintext []byte) ([]byte, error) {
	if useKMS() {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		wrapped, err := kms.Encrypt(ctx, plaintext)
		if err != nil {
			return nil, fmt.Errorf("secretbox: KMS encrypt: %w", err)
		}
		return wrapped, nil

View on GitHub (pinned to 3ee70a1026)