From f3cf3d9df73360b272865a1cea33ca9ef7b38b84 Mon Sep 17 00:00:00 2001 From: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Date: Tue, 30 Apr 2024 10:27:46 +0300 Subject: [PATCH] fix(mproxy): use `HasPrefix` rather than full match for pathPrefix Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --- config.go | 13 ------------- pkg/http/http.go | 12 ++++++------ pkg/mqtt/websocket/websocket.go | 5 ++--- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/config.go b/config.go index 3ca841f..f028ec1 100644 --- a/config.go +++ b/config.go @@ -5,7 +5,6 @@ package mproxy import ( "crypto/tls" - "strings" mptls "github.com/absmach/mproxy/pkg/tls" "github.com/caarlos0/env/v11" @@ -23,7 +22,6 @@ func NewConfig(opts env.Options) (Config, error) { if err := env.ParseWithOptions(&c, opts); err != nil { return Config{}, err } - c.PathPrefix = CleanPathPrefix(c.PathPrefix) cfg, err := mptls.NewConfig(opts) if err != nil { @@ -36,14 +34,3 @@ func NewConfig(opts env.Options) (Config, error) { } return c, nil } - -func CleanPathPrefix(path string) string { - path = strings.TrimSpace(path) - if path == "" { - return "/" - } - if path[0] != '/' { - return "/" + path - } - return path -} diff --git a/pkg/http/http.go b/pkg/http/http.go index cd57cce..6b8d63d 100644 --- a/pkg/http/http.go +++ b/pkg/http/http.go @@ -16,6 +16,7 @@ import ( "net/http" "net/http/httputil" "net/url" + "strings" "github.com/absmach/mproxy" "github.com/absmach/mproxy/pkg/session" @@ -29,16 +30,17 @@ const contentType = "application/json" var ErrMissingAuthentication = errors.New("missing authorization") func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != p.config.PathPrefix { - http.NotFound(w, r) - return - } // Metrics and health endpoints are served directly. if r.URL.Path == "/metrics" || r.URL.Path == "/health" { p.target.ServeHTTP(w, r) return } + if !strings.HasPrefix(r.URL.Path, p.config.PathPrefix) { + http.NotFound(w, r) + return + } + username, password, ok := r.BasicAuth() switch { case ok: @@ -100,8 +102,6 @@ type Proxy struct { } func NewProxy(config mproxy.Config, handler session.Handler, logger *slog.Logger) (Proxy, error) { - config.PathPrefix = mproxy.CleanPathPrefix(config.PathPrefix) - target, err := url.Parse(config.Target) if err != nil { return Proxy{}, err diff --git a/pkg/mqtt/websocket/websocket.go b/pkg/mqtt/websocket/websocket.go index 76e0856..14150f2 100644 --- a/pkg/mqtt/websocket/websocket.go +++ b/pkg/mqtt/websocket/websocket.go @@ -10,6 +10,7 @@ import ( "log/slog" "net" "net/http" + "strings" "time" "github.com/absmach/mproxy" @@ -29,8 +30,6 @@ type Proxy struct { // New - creates new WS proxy. func New(config mproxy.Config, handler session.Handler, interceptor session.Interceptor, logger *slog.Logger) *Proxy { - config.PathPrefix = mproxy.CleanPathPrefix(config.PathPrefix) - return &Proxy{ config: config, handler: handler, @@ -51,7 +50,7 @@ var upgrader = websocket.Upgrader{ } func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != p.config.PathPrefix { + if !strings.HasPrefix(r.URL.Path, p.config.PathPrefix) { http.NotFound(w, r) return }