Skip to content

Commit

Permalink
Fix method sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Feb 14, 2024
1 parent 5e7e8fc commit d6407be
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions multiepoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func newMultiEpochHandler(handler *MultiEpoch, lsConf *ListenerConfig) func(ctx
{
// handle the /metrics endpoint
if string(reqCtx.Path()) == "/metrics" {
method = "/metrics"
handler := fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())
handler(reqCtx)
return
Expand Down Expand Up @@ -381,7 +382,7 @@ func newMultiEpochHandler(handler *MultiEpoch, lsConf *ListenerConfig) func(ctx
// errorResp is the error response to be sent to the client.
errorResp, err := handler.handleRequest(setRequestIDToContext(reqCtx, reqID), rqCtx, &rpcRequest)
if err != nil {
klog.Errorf("[%s] failed to handle %s: %v", reqID, sanitizeMethod(method), err)
klog.Errorf("[%s] failed to handle %q: %v", reqID, sanitizeMethod(method), err)
}
if errorResp != nil {
metrics_methodToSuccessOrFailure.WithLabelValues(sanitizeMethod(method), "failure").Inc()
Expand Down Expand Up @@ -465,7 +466,17 @@ func sanitizeMethod(method string) string {
if isValidLocalMethod(method) {
return method
}
return "<unknown>"
return allowOnlyAsciiPrintable(method)
}

func allowOnlyAsciiPrintable(s string) string {
return strings.Map(func(r rune) rune {
// allow only printable ASCII characters
if r >= 32 && r <= 126 {
return r
}
return -1
}, s)
}

func isValidLocalMethod(method string) bool {
Expand Down

0 comments on commit d6407be

Please sign in to comment.