From fd6e87cf2d55e602f94f768517af215598c56a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sat, 5 Jul 2025 12:27:35 -0700 Subject: [PATCH] fix(history): sanitize NULL bytes in CSV error output --- server/history.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/server/history.go b/server/history.go index 8237d4d..0852dab 100644 --- a/server/history.go +++ b/server/history.go @@ -22,6 +22,23 @@ import ( "go.ntppool.org/data-api/ntpdb" ) +// sanitizeForCSV removes or replaces problematic characters for CSV output +func sanitizeForCSV(s string) string { + // Replace NULL bytes and other control characters with a placeholder + var result strings.Builder + for _, r := range s { + switch { + case r == 0: // NULL byte + result.WriteString("") + case r < 32 && r != '\t' && r != '\n' && r != '\r': // Other control chars except tab/newline/carriage return + result.WriteString(fmt.Sprintf("<0x%02X>", r)) + default: + result.WriteRune(r) + } + } + return result.String() +} + type historyMode uint8 const ( @@ -402,7 +419,7 @@ func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logs monName, rtt, leap, - l.Attributes.Error, + sanitizeForCSV(l.Attributes.Error), }) if err != nil { log.Warn("csv encoding error", "ls_id", l.ID, "err", err)