JuliusBrussee/caveman · error

secretbox: decrypted to an empty secret

Error message

secretbox: %s decrypted to an empty secret

What it means

ResolveEnvironmentSecret decrypted the ciphertext env var successfully, but the resulting plaintext was empty (zero bytes). An empty secret is never meaningful, so the library rejects it to catch encryption mistakes early — most often someone encrypted an empty string or a whitespace-only value.

Solutions

  1. Set the real secret value before encrypting: verify len(secret) > 0 in the encryption script and regenerate the ciphertext.
  2. Check the rotation/CI pipeline for an empty source variable (e.g. $TOKEN unset → encrypted empty string).
  3. Re-run your secret provisioning tool and confirm the ciphertext env var now decrypts to the expected non-empty value.
  4. Add a startup assertion that every required secret resolves to a non-empty string before serving traffic.

Example fix

// before (script encrypted an unset var)
enc=$(printf '%s' "$API_KEY" | ./encrypt)   # API_KEY empty

// after
: "${API_KEY:?API_KEY must be set before encrypting}"
enc=$(printf '%s' "$API_KEY" | ./encrypt)
plain, err := secretbox.ResolveEnvironmentSecret("", "API_KEY") // ok, non-empty
Defensive patterns

Strategy: validation

Validate before calling

val, err := secretbox.ResolveEnvironmentSecret("", "API_KEY")
if err == nil && val == "" {
    return fmt.Errorf("API_KEY resolved to an empty value; re-provision the secret")
}

Try / catch

val, err := secretbox.ResolveEnvironmentSecret("", "API_KEY")
if err != nil {
    if strings.Contains(err.Error(), "decrypted to an empty secret") {
        return fmt.Errorf("stored ciphertext for API_KEY is empty — regenerate it from the real token")
    }
    return err
}

Prevention

When it happens

Trigger: Calling ResolveEnvironmentSecret where the ciphertextEnv decodes and decrypts cleanly but the stored plaintext was "" — e.g. secretbox.Encrypt([]byte("")) output, or a secret that was blanked during a rotation and never repopulated.

Common situations: A CI script encrypting an unset variable (empty expansion) and publishing the resulting envelope; an operator rotating a token by clearing it first; a placeholder left in the secrets store.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

	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)