feat: add validation for localhost routes to prevent usage of godoxy port causing self recursion

This commit is contained in:
yusing 2025-03-29 02:53:41 +08:00
parent a9e4f82e30
commit c8935102c3
3 changed files with 19 additions and 3 deletions

View file

@ -95,7 +95,7 @@ func GetEnvInt(key string, defaultValue int) int {
return GetEnv(key, defaultValue, strconv.Atoi)
}
func GetAddrEnv(key, defaultValue, scheme string) (addr, host, port, fullURL string) {
func GetAddrEnv(key, defaultValue, scheme string) (addr, host string, portInt int, fullURL string) {
addr = GetEnvString(key, defaultValue)
if addr == "" {
return
@ -108,6 +108,10 @@ func GetAddrEnv(key, defaultValue, scheme string) (addr, host, port, fullURL str
host = "localhost"
}
fullURL = fmt.Sprintf("%s://%s:%s", scheme, host, port)
portInt, err = strconv.Atoi(port)
if err != nil {
log.Fatal().Msgf("env %s: invalid port: %s", key, port)
}
return
}

View file

@ -3,6 +3,7 @@ package middleware
import (
"net"
"net/http"
"strconv"
"strings"
"github.com/yusing/go-proxy/internal/common"
@ -38,8 +39,8 @@ func (m *redirectHTTP) before(w http.ResponseWriter, r *http.Request) (proceed b
host = r.Host
}
if common.ProxyHTTPSPort != "443" {
r.URL.Host = host + ":" + common.ProxyHTTPSPort
if common.ProxyHTTPSPort != 443 {
r.URL.Host = host + ":" + strconv.Itoa(common.ProxyHTTPSPort)
} else {
r.URL.Host = host
}

View file

@ -75,6 +75,17 @@ func (r *Route) Validate() (err gperr.Error) {
r.isValidated = true
r.Finalize()
// return error if route is localhost:<godoxy_port>
switch r.Host {
case "localhost", "127.0.0.1":
switch r.Port.Proxy {
case common.ProxyHTTPPort, common.ProxyHTTPSPort, common.APIHTTPPort:
if r.Scheme.IsReverseProxy() || r.Scheme == types.SchemeTCP {
return gperr.Errorf("localhost:%d is reserved for godoxy", r.Port.Proxy)
}
}
}
errs := gperr.NewBuilder("entry validation failed")
switch r.Scheme {