ethereum/go-ethereum · error

nil auth

Error message

nil auth

What it means

WithHTTPAuth panics when given a nil HTTPAuth provider. The option stores the authentication function that is invoked on every HTTP request; a nil function would crash later inside the request path, so the library fails fast at client construction time instead.

Source

Thrown at rpc/client_opt.go:128

	}
	return optionFunc(func(cfg *clientConfig) {
		cfg.tmprop = tmp
	})
}

// WithHTTPClient configures the http.Client used by the RPC client.
func WithHTTPClient(c *http.Client) ClientOption {
	return optionFunc(func(cfg *clientConfig) {
		cfg.httpClient = c
	})
}

// WithHTTPAuth configures HTTP request authentication. The given provider will be called
// whenever a request is made. Note that only one authentication provider can be active at
// any time.
func WithHTTPAuth(a HTTPAuth) ClientOption {
	if a == nil {
		panic("nil auth")
	}
	return optionFunc(func(cfg *clientConfig) {
		cfg.httpAuth = a
	})
}

// A HTTPAuth function is called by the client whenever a HTTP request is sent.
// The function must be safe for concurrent use.
//
// Usually, HTTPAuth functions will call h.Set("authorization", "...") to add
// auth information to the request.
type HTTPAuth func(h http.Header) error

// WithBatchItemLimit changes the maximum number of items allowed in batch requests.
//
// Note: this option applies when processing incoming batch requests. It does not affect
// batch requests sent by the client.
func WithBatchItemLimit(limit int) ClientOption {

View on GitHub (pinned to 6bb0588ad8)

Solutions

  1. Pass a real HTTPAuth, e.g. rpc.WithHTTPAuth(func(h http.Header) error { h.Set("authorization", "Bearer "+tok); return nil }).
  2. If auth is optional, only include the option when the provider is non-nil.
  3. Check for accidental double configuration: only one authentication provider can be active at a time.

Example fix

// before
var auth rpc.HTTPAuth // nil when no auth configured
c, err := rpc.DialOptions(ctx, url, rpc.WithHTTPAuth(auth))

// after
var opts []rpc.ClientOption
if auth != nil {
    opts = append(opts, rpc.WithHTTPAuth(auth))
}
c, err := rpc.DialOptions(ctx, url, opts...)
Defensive patterns

Strategy: validation

Validate before calling

var opts []rpc.ClientOption
if auth != nil {
    opts = append(opts, rpc.WithHTTPAuth(auth))
}
c, err := rpc.DialOptions(ctx, url, opts...)

Prevention

When it happens

Trigger: Calling rpc.DialOptions(ctx, "http://...", rpc.WithHTTPAuth(nil)); forwarding an auth function from a config struct whose field was never set; conditionally building options and unconditionally appending a nil auth.

Common situations: Optional bearer-token authentication behind a config flag; switching from URL-embedded credentials to header-based auth; passing a function variable declared but not assigned (var auth rpc.HTTPAuth).

Related errors


AI-assisted analysis of ethereum/go-ethereum@6bb0588ad8 (2026-08-15). Data as JSON: /api/errors/ffb48687ee45c4c0. Report an issue: GitHub.