From fe35b5130ec6c4d4bc84bfcccac90b8d4d0e8e2a Mon Sep 17 00:00:00 2001 From: Piotr Tabor Date: Mon, 18 Apr 2022 23:10:13 +0200 Subject: [PATCH] Fix code scanning alert: This log write receives unsanitized user input --- contrib/lock/storage/storage.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/contrib/lock/storage/storage.go b/contrib/lock/storage/storage.go index 495210adf..917b86f42 100644 --- a/contrib/lock/storage/storage.go +++ b/contrib/lock/storage/storage.go @@ -90,12 +90,22 @@ func handler(w http.ResponseWriter, r *http.Request) { writeResponse(response{req.Val, req.Version, ""}, w) } } else { - fmt.Printf("unknown op: %s\n", req.Op) + fmt.Printf("unknown op: %s\n", escape(req.Op)) return } } +func escape(s string) string { + escaped := strings.Replace(s, "\n", " ", -1) + escaped = strings.Replace(escaped, "\r", " ", -1) + return escaped +} + func main() { http.HandleFunc("/", handler) - http.ListenAndServe(":8080", nil) + err := http.ListenAndServe(":8080", nil) + if err != nil { + fmt.Printf("failed to listen and serve: %s\n", err) + os.Exit(1) + } }