JuliusBrussee/caveman · error

secretbox: production refuses plaintext

Error message

secretbox: production refuses plaintext %s; use %s

What it means

ResolveEnvironmentSecret enforces that in production (per runtimeenv.IsProduction) secrets are supplied only as encrypted values in the ciphertext env var; a non-empty plaintext env var is rejected outright to prevent secrets leaking through insecure channels. The error names both variables so the operator knows which pair to fix.

Solutions

  1. Remove the plaintext env var from the production environment and set only the ciphertext variable with a secretbox-encrypted, base64 value.
  2. If local testing is intended, run with the non-production runtime environment flag so plaintext is permitted.
  3. Audit your deployment manifest / IaC for legacy plaintext secret keys and delete them from production templates.
  4. Encrypt the value with secretbox.Encrypt, base64-encode it, and place it in the ciphertextEnv variable instead.

Example fix

// before (prod manifest)
env:
  APP_TOKEN: "sk-live-abc123"          # plaintext
  APP_TOKEN_CIPHERTEXT: "<base64>"

// after
env:
  # APP_TOKEN removed for production
  APP_TOKEN_CIPHERTEXT: "<base64 secretbox envelope>"
Defensive patterns

Strategy: validation

Validate before calling

if runtimeenv.IsProduction() && strings.TrimSpace(os.Getenv(plaintextEnv)) != "" {
    return fmt.Errorf("%s must not be set in production; provide %s instead", plaintextEnv, ciphertextEnv)
}

Try / catch

val, err := secretbox.ResolveEnvironmentSecret("APP_TOKEN", "APP_TOKEN_CIPHERTEXT")
if err != nil {
    if strings.Contains(err.Error(), "production refuses plaintext") {
        log.Fatalf("deployment misconfiguration: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv) while the process is in production mode and os.Getenv(plaintextEnv) is set to a non-empty, non-whitespace value — regardless of whether the ciphertext var is also set.

Common situations: Copying a docker-compose / dev .env file (which sets plain env vars) to a production deployment; a platform injecting legacy plaintext vars; someone setting the plaintext var as a 'fallback' during an incident.

Related errors


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

Appendix: source

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

		return plaintext, nil
	}
	return Decrypt(envelope)
}

func useKMS() bool {
	return strings.EqualFold(strings.TrimSpace(os.Getenv("CAVE_KMS_PROVIDER")), kms.ProviderScaleway)
}

// 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)