router-for-me/CLIProxyAPI · error

invalid auth file for auth_index %s: %w

Error message

invalid auth file for auth_index %s: %w

What it means

Wrapped error from Host.authPhysicalJSONByIndex when json.Unmarshal of the auth file into map[string]any fails. The file exists and is non-empty but is not valid JSON, so it cannot be interpreted as an auth record.

Source

Thrown at internal/pluginhost/auth_callbacks.go:257

		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 {
		return "", nil, fmt.Errorf("json is required")
	}
	var metadata map[string]any
	if errUnmarshal := json.Unmarshal(rawJSON, &metadata); errUnmarshal != nil {

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Validate the file with a JSON linter (jq . auths/file.json) to locate the syntax error
  2. Fix or regenerate the file; re-run the OAuth flow if regeneration is easier
  3. Ensure only the proxy writes auth files, with atomic write-then-rename
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(path)
if err == nil && !json.Valid(data) {
    // reject/repair before passing to the host
}

Try / catch

var synErr *json.SyntaxError
if errors.As(err, &synErr) {
    // use synErr.Offset to locate the malformed JSON in the auth file
}

Prevention

When it happens

Trigger: Auth file containing JSON5/YAML/HTML (e.g. an error page saved by a fetch), truncated JSON, BOM-prefixed or doubly-encoded content; a log line accidentally written into the auth file.

Common situations: Manual editing of auths/*.json introducing a syntax error; scripts writing pretty-printed or commented JSON; partial writes from non-atomic producers.

Related errors


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