fixed unchecked integer conversion, fixed 'invalid host' bug, corrected error message

This commit is contained in:
yusing 2024-09-28 01:20:18 +08:00
parent 6f3a5ebe6e
commit 4120fd8d1c
3 changed files with 23 additions and 19 deletions

View file

@ -142,6 +142,6 @@ jobs:
format: "sarif" format: "sarif"
output: "trivy-results.sarif" output: "trivy-results.sarif"
- name: Upload Trivy SARIF Report - name: Upload Trivy SARIF Report
uses: github/codeql-action/upload-sarif@v2 uses: github/codeql-action/upload-sarif@v3
with: with:
sarif_file: "trivy-results.sarif" sarif_file: "trivy-results.sarif"

View file

@ -39,8 +39,8 @@ func FromJson(json types.ContainerJSON, dockerHost string) Container {
ports := make([]types.Port, 0) ports := make([]types.Port, 0)
for k, bindings := range json.NetworkSettings.Ports { for k, bindings := range json.NetworkSettings.Ports {
for _, v := range bindings { for _, v := range bindings {
pubPort, _ := strconv.Atoi(v.HostPort) pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16)
privPort, _ := strconv.Atoi(k.Port()) privPort, _ := strconv.ParseUint(k.Port(), 10, 16)
ports = append(ports, types.Port{ ports = append(ports, types.Port{
IP: v.HostIP, IP: v.HostIP,
PublicPort: uint16(pubPort), PublicPort: uint16(pubPort),

View file

@ -1,6 +1,7 @@
package route package route
import ( import (
"fmt"
"sync" "sync"
"net/http" "net/http"
@ -48,7 +49,7 @@ func SetFindMuxDomains(domains []string) {
if len(domains) == 0 { if len(domains) == 0 {
findMuxFunc = findMuxAnyDomain findMuxFunc = findMuxAnyDomain
} else { } 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) { func ProxyHandler(w http.ResponseWriter, r *http.Request) {
mux, err := findMuxFunc(r.Host) mux, err := findMuxFunc(r.Host)
if err != nil { if err != nil {
err = E.Failure("request"). http.Error(w, err.Error(), http.StatusNotFound)
Subjectf("%s %s%s", r.Method, r.Host, r.URL.Path). logrus.Error(E.Failure("request").
With(err) Subjectf("%s %s", r.Method, r.URL.String()).
http.Error(w, err.String(), http.StatusNotFound) With(err))
logrus.Error(err)
return return
} }
mux.ServeHTTP(w, r) mux.ServeHTTP(w, r)
} }
func findMuxAnyDomain(host string) (*http.ServeMux, E.NestedError) { func findMuxAnyDomain(host string) (*http.ServeMux, error) {
hostSplit := strings.Split(host, ".") hostSplit := strings.Split(host, ".")
n := len(hostSplit) n := len(hostSplit)
if n <= 2 { if n <= 2 {
return nil, E.Missing("subdomain") return nil, fmt.Errorf("missing subdomain in url")
} }
sd := strings.Join(hostSplit[:n-2], ".") sd := strings.Join(hostSplit[:n-2], ".")
if r, ok := httpRoutes.Load(PT.Alias(sd)); ok { if r, ok := httpRoutes.Load(PT.Alias(sd)); ok {
return r.mux, nil 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) { func findMuxByDomains(domains []string) func(host string) (*http.ServeMux, error) {
return func(host string) (*http.ServeMux, E.NestedError) { return func(host string) (*http.ServeMux, error) {
var subdomain string var subdomain string
for _, domain := range domains { for _, domain := range domains {
subdomain = strings.TrimSuffix(subdomain, domain) if !strings.HasPrefix(domain, ".") {
if subdomain != domain { domain = "." + domain
}
subdomain = strings.TrimSuffix(host, domain)
if len(subdomain) < len(host) {
break break
} }
} }
if subdomain == "" { // not matched if len(subdomain) == len(host) { // not matched
return nil, E.Invalid("host", host) return nil, fmt.Errorf("%s does not match any base domain", host)
} }
if r, ok := httpRoutes.Load(PT.Alias(subdomain)); ok { if r, ok := httpRoutes.Load(PT.Alias(subdomain)); ok {
return r.mux, nil return r.mux, nil
} }
return nil, E.NotExist("route", subdomain) return nil, fmt.Errorf("no such route: %s", subdomain)
} }
} }