Fix code scanning alert: This log write receives unsanitized user input

This commit is contained in:
Piotr Tabor 2022-04-18 23:10:13 +02:00
parent 93c54b816d
commit fe35b5130e

View File

@ -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)
}
}