router-for-me/CLIProxyAPI · error

auth file is empty for auth_index %s

Error message

auth file is empty for auth_index %s

What it means

Thrown by Host.authPhysicalJSONByIndex when the auth file exists and is readable but contains only whitespace bytes (len(bytesTrimSpace(data)) == 0). An auth file must be a non-empty JSON object; an empty file is treated as corruption, not a valid empty state.

Source

Thrown at internal/pluginhost/auth_callbacks.go:253

func (h *Host) authPhysicalJSONByIndex(authIndex string) (*coreauth.Auth, []byte, error) {
	auth, errGet := h.authByIndex(authIndex)
	if errGet != nil {
		return nil, nil, errGet
	}
	path := strings.TrimSpace(authAttribute(auth, "path"))
	if path == "" {
		return nil, nil, fmt.Errorf("auth file path not found for auth_index %s", authIndex)
	}
	data, errRead := os.ReadFile(path)
	if errRead != nil {
		if os.IsNotExist(errRead) {
			return nil, nil, fmt.Errorf("auth file not found for auth_index %s", authIndex)
		}
		return nil, nil, fmt.Errorf("failed to read auth file: %w", errRead)
	}
	if len(bytesTrimSpace(data)) == 0 {
		return nil, nil, fmt.Errorf("auth file is empty for auth_index %s", authIndex)
	}
	var metadata map[string]any
	if errUnmarshal := json.Unmarshal(data, &metadata); errUnmarshal != nil {
		return nil, nil, fmt.Errorf("invalid auth file for auth_index %s: %w", authIndex, errUnmarshal)
	}
	return auth, data, nil
}

func validateHostAuthSaveRequest(req pluginapi.HostAuthSaveRequest) (string, []byte, error) {
	name := strings.TrimSpace(req.Name)
	if isUnsafeAuthFileName(name) {
		return "", nil, fmt.Errorf("invalid auth file name")
	}
	if !strings.HasSuffix(strings.ToLower(name), ".json") {
		return "", nil, fmt.Errorf("auth file name must end with .json")
	}
	rawJSON := bytesTrimSpace(req.JSON)
	if len(rawJSON) == 0 {

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Delete the empty file and re-run the provider's auth flow to regenerate it
  2. If the content is known, restore it from backup
  3. Use atomic write tooling (write temp + rename) when generating auth files to avoid truncation
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat(path); err == nil && info.Size() == 0 {
    // treat as corrupt; skip or regenerate before reading
}

Try / catch

if err != nil && strings.Contains(err.Error(), "auth file is empty") {
    // delete the file and re-run provider auth
}

Prevention

When it happens

Trigger: Auth file truncated to zero bytes by a crash during write, a failed atomic replace, or manual editing; a file created by touch as a placeholder in the auths directory.

Common situations: Power loss / kill -9 during credential refresh leaving a zero-length file; deployment tooling creating empty placeholder files; editor saving an emptied file.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/5ab03b568e2cb265. Report an issue: GitHub.