Skip to content

Commit

Permalink
feat: Accept URL escape for device command name and resource name
Browse files Browse the repository at this point in the history
- Use router.UseEncodedPath() to match the encoded original path to the routes
- Decode the Url path variable before passing to the controller

Close #4371

Signed-off-by: bruce <weichou1229@gmail.com>
  • Loading branch information
weichou1229 committed Feb 22, 2023
1 parent 76c26b6 commit 44004a9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
5 changes: 4 additions & 1 deletion internal/core/command/router.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2021 IOTech Ltd
// Copyright (C) 2021-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -19,6 +19,8 @@ import (
)

func LoadRestRoutes(r *mux.Router, dic *di.Container, serviceName string) {
// r.UseEncodedPath() tells the router to match the encoded original path to the routes
r.UseEncodedPath()
// Common
cc := commonController.NewCommonController(dic, serviceName)
r.HandleFunc(common.ApiPingRoute, cc.Ping).Methods(http.MethodGet)
Expand All @@ -34,4 +36,5 @@ func LoadRestRoutes(r *mux.Router, dic *di.Container, serviceName string) {

r.Use(correlation.ManageHeader)
r.Use(correlation.LoggingMiddleware(container.LoggingClientFrom(dic.Get)))
r.Use(correlation.UrlDecodeMiddleware(container.LoggingClientFrom(dic.Get)))
}
8 changes: 6 additions & 2 deletions internal/core/data/controller/messaging/subscriber.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2021-2022 IOTech Ltd
// Copyright (C) 2021-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"strings"

cbor "github.com/fxamacker/cbor/v2"
Expand Down Expand Up @@ -119,7 +120,10 @@ func validateEvent(messageTopic string, e dtos.Event) errors.EdgeX {
len := len(fields)
profileName := fields[len-3]
deviceName := fields[len-2]
sourceName := fields[len-1]
sourceName, err := url.QueryUnescape(fields[len-1])
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}

// Check whether the event fields match the message topic
if e.ProfileName != profileName {
Expand Down
3 changes: 3 additions & 0 deletions internal/core/data/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
)

func LoadRestRoutes(r *mux.Router, dic *di.Container, serviceName string) {
// r.UseEncodedPath() tells the router to match the encoded original path to the routes
r.UseEncodedPath()
// Common
cc := commonController.NewCommonController(dic, serviceName)
r.HandleFunc(common.ApiPingRoute, cc.Ping).Methods(http.MethodGet)
Expand Down Expand Up @@ -54,4 +56,5 @@ func LoadRestRoutes(r *mux.Router, dic *di.Container, serviceName string) {

r.Use(correlation.ManageHeader)
r.Use(correlation.LoggingMiddleware(container.LoggingClientFrom(dic.Get)))
r.Use(correlation.UrlDecodeMiddleware(container.LoggingClientFrom(dic.Get)))
}
5 changes: 4 additions & 1 deletion internal/core/metadata/router.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2021-2022 IOTech Ltd
// Copyright (C) 2021-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -20,6 +20,8 @@ import (
)

func LoadRestRoutes(r *mux.Router, dic *di.Container, serviceName string) {
// r.UseEncodedPath() tells the router to match the encoded original path to the routes
r.UseEncodedPath()
// Common
cc := commonController.NewCommonController(dic, serviceName)
r.HandleFunc(common.ApiPingRoute, cc.Ping).Methods(http.MethodGet)
Expand Down Expand Up @@ -88,4 +90,5 @@ func LoadRestRoutes(r *mux.Router, dic *di.Container, serviceName string) {

r.Use(correlation.ManageHeader)
r.Use(correlation.LoggingMiddleware(container.LoggingClientFrom(dic.Get)))
r.Use(correlation.UrlDecodeMiddleware(container.LoggingClientFrom(dic.Get)))
}
21 changes: 21 additions & 0 deletions internal/pkg/correlation/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package correlation

import (
"context"
"github.com/gorilla/mux"
"net/http"
"net/url"
"time"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
Expand Down Expand Up @@ -47,3 +49,22 @@ func LoggingMiddleware(lc logger.LoggingClient) func(http.Handler) http.Handler
})
}
}

// UrlDecodeMiddleware decode the path variables
// After invoking the router.UseEncodedPath() func, the path variables needs to decode before passing to the controller
func UrlDecodeMiddleware(lc logger.LoggingClient) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
for k, v := range vars {
unescape, err := url.QueryUnescape(v)
if err != nil {
lc.Debugf("failed to decode the %s from the value %s", k, v)
return
}
vars[k] = unescape
}
next.ServeHTTP(w, r)
})
}
}

0 comments on commit 44004a9

Please sign in to comment.