@@ -378,8 +378,28 @@ func handlerGood2(req *http.Request) {
378378 log .Printf ("user %s logged in.\n " , escapedUsername )
379379}
380380
381+ // GOOD: The user-provided value is escaped before being written to the log.
382+ func handlerGood3 (req * http.Request ) {
383+ username := req .URL .Query ()["username" ][0 ]
384+ replacer := strings .NewReplacer ("\n " , "" , "\r " , "" )
385+ log .Printf ("user %s logged in.\n " , replacer .Replace (username ))
386+ log .Printf ("user %s logged in.\n " , replacerLocal1 (username ))
387+ log .Printf ("user %s logged in.\n " , replacerGlobal1 (username ))
388+ }
389+
390+ func replacerLocal1 (s string ) string {
391+ replacer := strings .NewReplacer ("\n " , "" , "\r " , "" )
392+ return replacer .Replace (s )
393+ }
394+
395+ var globalReplacer = strings .NewReplacer ("\n " , "" , "\r " , "" )
396+
397+ func replacerGlobal1 (s string ) string {
398+ return globalReplacer .Replace (s )
399+ }
400+
381401// GOOD: User-provided values formatted using a %q directive, which escapes newlines
382- func handlerGood3 (req * http.Request , ctx * goproxy.ProxyCtx ) {
402+ func handlerGood4 (req * http.Request , ctx * goproxy.ProxyCtx ) {
383403 username := req .URL .Query ()["username" ][0 ]
384404 testFlag := req .URL .Query ()["testFlag" ][0 ]
385405 log .Printf ("user %q logged in.\n " , username )
0 commit comments