Documentation
¶
Overview ¶
Package logutil provides shared helpers for safely emitting user-controlled or network-derived strings through structured logging (slog).
slog's JSONHandler quotes string attribute values, so embedded control characters cannot break the wire format on its own. These helpers nevertheless strip control characters at the call site for two reasons:
- Defense in depth: when downstream pipelines (fluentd, journald, stdout-tail consumers) interpret the rendered log line as text, embedded ESC / BEL / NUL bytes can fool a parser even though slog itself escaped them.
- Operator UX: rendered logs read by humans should not be polluted by terminal control sequences from a hostile request.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SafeAddr ¶
SafeAddr normalizes a network address string for structured-log emission. On success (host:port parses) it returns net.JoinHostPort(host, port), which round-trips equivalently for IPv4 / IPv6 / hostname forms. On parse failure (e.g. unix-socket "@" prefix, malformed peer address) it falls back to Sanitize so any control characters are stripped before the value reaches the logger.
func Sanitize ¶
Sanitize returns s with ASCII control characters (codepoint < 0x20 or equal to 0x7f), Unicode C1 controls (U+0080 through U+009F), and Unicode line/paragraph separators (U+2028, U+2029) removed. Printable Unicode characters like letters and emoji are preserved.
Use this on every user-controlled string that flows into a slog attribute, e.g. r.Method, r.URL.Path, r.Header.Get(...), env values.
Types ¶
This section is empty.