From 4120fd8d1ce5e4b2fc7f0bbfa045cd81e8c2c7d2 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 28 Sep 2024 01:20:18 +0800 Subject: [PATCH] fixed unchecked integer conversion, fixed 'invalid host' bug, corrected error message --- .github/workflows/docker-image.yml | 2 +- src/docker/container.go | 4 ++-- src/route/http.go | 36 +++++++++++++++++------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index d35cc0b9..a6370b22 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -142,6 +142,6 @@ jobs: format: "sarif" output: "trivy-results.sarif" - name: Upload Trivy SARIF Report - uses: github/codeql-action/upload-sarif@v2 + uses: github/codeql-action/upload-sarif@v3 with: sarif_file: "trivy-results.sarif" diff --git a/src/docker/container.go b/src/docker/container.go index b7fcc026..956223e9 100644 --- a/src/docker/container.go +++ b/src/docker/container.go @@ -39,8 +39,8 @@ func FromJson(json types.ContainerJSON, dockerHost string) Container { ports := make([]types.Port, 0) for k, bindings := range json.NetworkSettings.Ports { for _, v := range bindings { - pubPort, _ := strconv.Atoi(v.HostPort) - privPort, _ := strconv.Atoi(k.Port()) + pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16) + privPort, _ := strconv.ParseUint(k.Port(), 10, 16) ports = append(ports, types.Port{ IP: v.HostIP, PublicPort: uint16(pubPort), diff --git a/src/route/http.go b/src/route/http.go index 5c38d694..8fa20ca8 100755 --- a/src/route/http.go +++ b/src/route/http.go @@ -1,6 +1,7 @@ package route import ( + "fmt" "sync" "net/http" @@ -48,7 +49,7 @@ func SetFindMuxDomains(domains []string) { if len(domains) == 0 { findMuxFunc = findMuxAnyDomain } else { - findMuxFunc = findMuxByDomain(domains) + findMuxFunc = findMuxByDomains(domains) } } @@ -169,44 +170,47 @@ func (u *URL) MarshalText() (text []byte, err error) { func ProxyHandler(w http.ResponseWriter, r *http.Request) { mux, err := findMuxFunc(r.Host) if err != nil { - err = E.Failure("request"). - Subjectf("%s %s%s", r.Method, r.Host, r.URL.Path). - With(err) - http.Error(w, err.String(), http.StatusNotFound) - logrus.Error(err) + http.Error(w, err.Error(), http.StatusNotFound) + logrus.Error(E.Failure("request"). + Subjectf("%s %s", r.Method, r.URL.String()). + With(err)) return } mux.ServeHTTP(w, r) } -func findMuxAnyDomain(host string) (*http.ServeMux, E.NestedError) { +func findMuxAnyDomain(host string) (*http.ServeMux, error) { hostSplit := strings.Split(host, ".") n := len(hostSplit) if n <= 2 { - return nil, E.Missing("subdomain") + return nil, fmt.Errorf("missing subdomain in url") } sd := strings.Join(hostSplit[:n-2], ".") if r, ok := httpRoutes.Load(PT.Alias(sd)); ok { return r.mux, nil } - return nil, E.NotExist("route", sd) + return nil, fmt.Errorf("no such route: %s", sd) } -func findMuxByDomain(domains []string) func(host string) (*http.ServeMux, E.NestedError) { - return func(host string) (*http.ServeMux, E.NestedError) { +func findMuxByDomains(domains []string) func(host string) (*http.ServeMux, error) { + return func(host string) (*http.ServeMux, error) { var subdomain string + for _, domain := range domains { - subdomain = strings.TrimSuffix(subdomain, domain) - if subdomain != domain { + if !strings.HasPrefix(domain, ".") { + domain = "." + domain + } + subdomain = strings.TrimSuffix(host, domain) + if len(subdomain) < len(host) { break } } - if subdomain == "" { // not matched - return nil, E.Invalid("host", host) + if len(subdomain) == len(host) { // not matched + return nil, fmt.Errorf("%s does not match any base domain", host) } if r, ok := httpRoutes.Load(PT.Alias(subdomain)); ok { return r.mux, nil } - return nil, E.NotExist("route", subdomain) + return nil, fmt.Errorf("no such route: %s", subdomain) } }