JuliusBrussee/caveman · error

secretbox: is not valid base64

Error message

secretbox: %s is not valid base64

What it means

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.

Solutions

  1. Re-encode the secret envelope with base64.StdEncoding and replace the env var value verbatim.
  2. Strip quotes, whitespace, and prefixes (e.g. 'base64:' or 'Bearer ') from the value in your config file.
  3. If the value came from a tool emitting URL-safe base64, convert it to standard encoding (replace -_ with +/, add padding).
  4. If the value is simply a plaintext secret, remove it from the ciphertext var — it must be encrypted first via secretbox.Encrypt.

Example fix

// before
APP_TOKEN_CIPHERTEXT: ""aGVsbG8gd29ybGQ""   # quotes copied from docs

// after (generate correctly)
enc, _ := secretbox.Encrypt([]byte(token))
// export APP_TOKEN_CIPHERTEXT=$(printf '%s' "$enc" | base64 -w0)
Defensive patterns

Strategy: validation

Validate before calling

if _, err := base64.StdEncoding.DecodeString(strings.TrimSpace(os.Getenv(ciphertextEnv))); err != nil {
    return fmt.Errorf("%s must be standard base64 of a secretbox envelope", ciphertextEnv)
}

Type guard

func isStdBase64(s string) bool {
    _, err := base64.StdEncoding.DecodeString(s)
    return err == nil
}

Try / catch

val, err := secretbox.ResolveEnvironmentSecret("", "APP_TOKEN_CIPHERTEXT")
if err != nil {
    if strings.Contains(err.Error(), "is not valid base64") {
        return fmt.Errorf("%s is malformed; re-encode with `base64 -w0` and remove quotes", "APP_TOKEN_CIPHERTEXT")
    }
    return err
}

Prevention

When it happens

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

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

Related errors


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

Appendix: source

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

}

// ResolveEnvironmentSecret loads a boot-time secret. In production plaintext
// environment variables are rejected: operators must provide a base64-encoded
// secretbox/KMS envelope in ciphertextEnv. Local development may continue using
// plaintextEnv. An entirely absent optional secret returns an empty string.
func ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv string) (string, error) {
	plain := strings.TrimSpace(os.Getenv(plaintextEnv))
	encoded := strings.TrimSpace(os.Getenv(ciphertextEnv))
	production := runtimeenv.IsProduction()
	if production && plain != "" {
		return "", fmt.Errorf("secretbox: production refuses plaintext %s; use %s", plaintextEnv, ciphertextEnv)
	}
	if encoded == "" {
		return plain, nil
	}
	wrapped, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		return "", fmt.Errorf("secretbox: %s is not valid base64", ciphertextEnv)
	}
	decrypted, err := Decrypt(wrapped)
	if err != nil {
		return "", fmt.Errorf("secretbox: decrypt %s: %w", ciphertextEnv, err)
	}
	if len(decrypted) == 0 {
		return "", fmt.Errorf("secretbox: %s decrypted to an empty secret", ciphertextEnv)
	}
	return string(decrypted), nil
}

View on GitHub (pinned to 3ee70a1026)