Add log-injection test using strings.ReplaceAll

This commit is contained in:
Chris Smowton
2021-12-15 15:35:14 +00:00
committed by GitHub
parent f86510ee20
commit 9de1532735

View File

@@ -369,3 +369,11 @@ func handlerGood(req *http.Request) {
escapedUsername = strings.Replace(escapedUsername, "\r", "", -1)
log.Printf("user %s logged in.\n", escapedUsername)
}
// GOOD: The user-provided value is escaped before being written to the log.
func handlerGood2(req *http.Request) {
username := req.URL.Query()["username"][0]
escapedUsername := strings.ReplaceAll(username, "\n", "")
escapedUsername = strings.ReplaceAll(escapedUsername, "\r", "")
log.Printf("user %s logged in.\n", escapedUsername)
}