JuliusBrussee/caveman · error

production refuses low-diversity

Error message

production refuses low-diversity %s

What it means

validateProductionTextSecrets counts distinct bytes in the secret and rejects values with fewer than 8 unique bytes ('production refuses low-diversity %s'). A long but repetitive string (e.g. 'aaaa...' or repeated words) passes the length check but has effectively low entropy, so the library refuses it.

Solutions

  1. Replace the secret with high-entropy random data (`openssl rand -base64 48` or equivalent).
  2. Avoid padding tricks — use genuinely random bytes rather than repeating characters.
  3. Verify with a quick check that the value has at least 8 distinct bytes.
  4. If this is not production, switch to local mode which skips the diversity check.

Example fix

// before
export CAVE_BOOTSTRAP_TOKEN="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // 1 distinct byte
// error: production refuses low-diversity CAVE_BOOTSTRAP_TOKEN

// after
export CAVE_BOOTSTRAP_TOKEN="$(openssl rand -base64 48)"
Defensive patterns

Strategy: validation

Validate before calling

func distinctBytes(b []byte) int {
    seen := map[byte]struct{}{}
    for _, c := range b { seen[c] = struct{}{} }
    return len(seen)
}
func checkSecretDiversity(name string) error {
    if distinctBytes([]byte(strings.TrimSpace(os.Getenv(name)))) < 8 {
        return fmt.Errorf("%s has too little entropy; use random data", name)
    }
    return nil
}

Try / catch

if err := env.RefuseProductionDefaults(); err != nil {
    if strings.Contains(err.Error(), "low-diversity") {
        log.Fatalf("regenerate secret with random bytes: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: RefuseProductionDefaults / RefuseGatewayProductionDefaults sees a secret >=32 chars whose byte content has <8 distinct byte values — e.g. a repeated single character or pattern.

Common situations: Secrets padded with '0000...' or 'xxxx' to satisfy length checks; programmatically generated filler strings; misconfigured templating that repeats one character.

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/04726b9d9e260c48. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/env/env.go:107

	if Bool("CAVE_REPLAY_ENABLED", false) {
		if err := validateProductionTextSecrets([]string{"CAVE_ROUTER_REPLAY_TOKEN"}); err != nil {
			return err
		}
	}
	return validateProductionPublicURL()
}

func validateProductionTextSecrets(names []string) error {
	for _, name := range names {
		v := strings.TrimSpace(os.Getenv(name))
		if v == "" || v == "generated" || strings.Contains(strings.ToLower(v), "changeme") {
			return fmt.Errorf("production refuses default or empty %s", name)
		}
		if len(v) < 32 {
			return fmt.Errorf("production requires %s to contain at least 32 characters", name)
		}
		if distinctBytes([]byte(v)) < 8 {
			return fmt.Errorf("production refuses low-diversity %s", name)
		}
	}
	return nil
}

func validateProductionPublicURL() error {
	publicURL := strings.TrimSpace(os.Getenv("CAVE_PUBLIC_URL"))
	u, err := url.Parse(publicURL)
	if err != nil || u.Scheme != "https" || u.Hostname() == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Path != "" && u.Path != "/") {
		return fmt.Errorf("production requires CAVE_PUBLIC_URL to be an HTTPS origin without credentials, path, query, or fragment")
	}
	return nil
}

func distinctBytes(value []byte) int {
	seen := map[byte]struct{}{}
	for _, b := range value {
		seen[b] = struct{}{}

View on GitHub (pinned to 3ee70a1026)