diff --git a/cmd/example-dlna-server/logutil.go b/cmd/example-dlna-server/logutil.go new file mode 100644 index 00000000..bd484699 --- /dev/null +++ b/cmd/example-dlna-server/logutil.go @@ -0,0 +1,13 @@ +package main + +import "strings" + +// sanitizeLog strips newline characters from s to prevent log-injection +// (CodeQL go/log-injection). Values from HTTP requests may contain +// attacker-controlled newlines. +func sanitizeLog(s string) string { + s = strings.ReplaceAll(s, "\n", `\n`) + s = strings.ReplaceAll(s, "\r", `\r`) + + return s +} diff --git a/cmd/example-dlna-server/main.go b/cmd/example-dlna-server/main.go index 22a3e4b9..d1bd47c2 100644 --- a/cmd/example-dlna-server/main.go +++ b/cmd/example-dlna-server/main.go @@ -654,8 +654,8 @@ func withAccessLog(logger *slog.Logger, next http.Handler) http.Handler { r.Body = io.NopCloser(bytes.NewReader(body)) browseAttrs = []any{ - "objectID", between(string(body), "", ""), - "browseFlag", between(string(body), "", ""), + "objectID", sanitizeLog(between(string(body), "", "")), + "browseFlag", sanitizeLog(between(string(body), "", "")), } } @@ -664,7 +664,7 @@ func withAccessLog(logger *slog.Logger, next http.Handler) http.Handler { attrs := []any{ "method", r.Method, - "path", r.URL.Path, + "path", sanitizeLog(r.URL.Path), "status", rec.status, "bytes", rec.bytes, "from", r.RemoteAddr,