Skip to content

Commit

Permalink
Allow localhost-only routes to be used from host (#5867)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zensey authored Sep 1, 2023
1 parent e233f37 commit 4a25636
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tequilapi/http_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func NewServer(
g.Use(middlewares.ApplyMiddlewareTokenAuth(authenticator))
}

// Set to protect localhost-only endpoints due to use of nodeUI proxy
// With this set, context.ClientIP() will return only IP set by trusted proxy, not by a client!
g.SetTrustedProxies([]string{"127.0.0.1"})

for _, h := range handlers {
err := h(g)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tequilapi/middlewares/http_middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func NewHostFilter() func(*gin.Context) {

// NewLocalhostOnlyFilter returns instance of middleware allowing only requests
// with local client IP.
// Don't forget to Engine.SetTrustedProxies() if reverse proxy is used.
func NewLocalhostOnlyFilter() func(*gin.Context) {
return func(c *gin.Context) {

// ClientIP() parses the headers defined in Engine.RemoteIPHeaders if there is
clientIP := c.ClientIP()
if net.ParseIP(clientIP).IsLoopback() {
// so it handles clients behind proxy
isLocal := net.ParseIP(c.ClientIP()).IsLoopback()
if isLocal {
return
}

Expand Down

0 comments on commit 4a25636

Please sign in to comment.