Skip to content

Proxy middleware can panic when the balancer has no targets #2976

@shblue21

Description

@shblue21

Issue Description

ProxyWithConfig can panic when the balancer has no targets.

The built-in balancers return nil when their target list is empty. The proxy middleware then passes that nil target to proxyHTTP / proxyRaw, which can panic.

One way to hit this is to remove the last target with RemoveTarget.

I expected the middleware to return a proxy error, such as 502 Bad Gateway, instead of panicking.

Working code to debug

targetURL, _ := url.Parse("http://127.0.0.1:8080")

balancer := middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{
	{Name: "target", URL: targetURL},
})

e := echo.New()
e.Use(middleware.Proxy(balancer))

// Later, from code that updates proxy targets at runtime:
balancer.RemoveTarget("target")

// The next proxied request can panic because the balancer returns nil.

### Version/commit
v4.15.x and v5.1.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions