Skip to content

Commit

Permalink
fix(mproxy): use HasPrefix rather than full match for pathPrefix
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Apr 30, 2024
1 parent 8d54341 commit f3cf3d9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
13 changes: 0 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package mproxy

import (
"crypto/tls"
"strings"

mptls "github.com/absmach/mproxy/pkg/tls"
"github.com/caarlos0/env/v11"
Expand All @@ -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 {
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"

"github.com/absmach/mproxy"
"github.com/absmach/mproxy/pkg/session"
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/mqtt/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log/slog"
"net"
"net/http"
"strings"
"time"

"github.com/absmach/mproxy"
Expand All @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit f3cf3d9

Please sign in to comment.