Skip to content

Commit

Permalink
Improve error handling for unescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
v3n committed Sep 10, 2021
1 parent 4c40cee commit f1ff145
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
19 changes: 14 additions & 5 deletions runtime/mux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"errors"
"context"
"fmt"
"net/http"
Expand All @@ -14,7 +15,7 @@ import (
"google.golang.org/protobuf/proto"
)

// UnescapingMode defines the behavior of grpc-gateway for URL escaping.
// UnescapingMode defines the behavior of ServeMux when unescaping path parameters.
type UnescapingMode int

const (
Expand Down Expand Up @@ -286,9 +287,13 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {

pathParams, err := h.pat.MatchAndEscape(components, verb, s.unescapingMode)
if err != nil {
if err == ErrMalformedSequence {
var mse MalformedSequenceError
if ok := errors.As(err, &mse); ok {
_, outboundMarshaler := MarshalerForRequest(s, r)
s.routingErrorHandler(ctx, s, outboundMarshaler, w, r, http.StatusBadRequest)
s.errorHandler(ctx, s, outboundMarshaler, w, r, &HTTPStatusError{
HTTPStatus: http.StatusBadRequest,
Err: mse,
})
}
continue
}
Expand All @@ -305,9 +310,13 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, h := range handlers {
pathParams, err := h.pat.MatchAndEscape(components, verb, s.unescapingMode)
if err != nil {
if err == ErrMalformedSequence {
var mse MalformedSequenceError
if ok := errors.As(err, &mse); ok {
_, outboundMarshaler := MarshalerForRequest(s, r)
s.routingErrorHandler(ctx, s, outboundMarshaler, w, r, http.StatusBadRequest)
s.errorHandler(ctx, s, outboundMarshaler, w, r, &HTTPStatusError{
HTTPStatus: http.StatusBadRequest,
Err: mse,
})
}
continue
}
Expand Down
13 changes: 10 additions & 3 deletions runtime/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
Expand All @@ -18,6 +19,12 @@ var (
ErrMalformedSequence = errors.New("malformed escape sequence")
)

type MalformedSequenceError string

func (e MalformedSequenceError) Error() string {
return "malformed path escape " + strconv.Quote(string(e))
}

type op struct {
code utilities.OpCode
operand int
Expand Down Expand Up @@ -180,7 +187,7 @@ func (p Pattern) MatchAndEscape(components []string, verb string, unescapingMode
}
} else if op.code == utilities.OpPush {
if c, err = unescape(c, unescapingMode, false); err != nil {
return nil, ErrMalformedSequence
return nil, err
}
}
stack = append(stack, c)
Expand All @@ -193,7 +200,7 @@ func (p Pattern) MatchAndEscape(components []string, verb string, unescapingMode
end -= p.tailLen
c := strings.Join(components[pos:end], "/")
if c, err = unescape(c, unescapingMode, true); err != nil {
return nil, ErrMalformedSequence
return nil, err
}
stack = append(stack, c)
pos = end
Expand Down Expand Up @@ -343,7 +350,7 @@ func unescape(s string, mode UnescapingMode, multisegment bool) (string, error)
s = s[:3]
}

return "", ErrMalformedSequence
return "", MalformedSequenceError(s)
}
i += 3
} else {
Expand Down

0 comments on commit f1ff145

Please sign in to comment.