Log injection

Log injection

Description

A newline in a user-supplied field can split a single log event into several lines.

Potential impact

Extra lines can mislead alerts and incident investigations. An HTML viewer can also execute unsafe markup if it renders log data as HTML.

Remediation

For line-oriented logs, remove or escape both \r and \n before writing user input, or log a trusted identifier. In a web viewer, display the value as text or encode it for its HTML context.

Examples

Before

go
user := r.FormValue("user")
log.Printf("user=%s", user)

After

go
user := strings.ReplaceAll(r.FormValue("user"), "\n", "")
user = strings.ReplaceAll(user, "\r", "")
log.Printf("user=%s", user)

The two strings.ReplaceAll calls remove both line separators from user before log.Printf writes it.

References