File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -37,8 +37,16 @@ var _ io.Writer = (*checksumWriter)(nil)
3737// results in a single call to the wrapped io.Writer.Write. This means that we
3838// are computing writing exactly one checksum per call to slog.Logger.Handle.
3939func (w * checksumWriter ) Write (in []byte ) (int , error ) {
40+ // slog handlers terminate each record with a trailing newline. rsyslog
41+ // strips that newline before writing the line to disk, and the log-validator
42+ // computes its checksum over the stripped line. Strip it here too so the
43+ // checksum we emit covers exactly the bytes the validator will see.
44+ body := in
45+ if len (body ) > 0 && body [len (body )- 1 ] == '\n' {
46+ body = body [:len (body )- 1 ]
47+ }
4048 out := bytes.Buffer {}
41- out .WriteString (LogLineChecksum (string (in )))
49+ out .WriteString (LogLineChecksum (string (body )))
4250 out .WriteString (" " )
4351 out .Write (in )
4452 _ , err := out .WriteTo (w .inner )
Original file line number Diff line number Diff line change @@ -22,9 +22,15 @@ func TestLogLineChecksum(t *testing.T) {
2222 {
2323 name : "simple" ,
2424 line : "hello, world" ,
25- // Deterministic base64url(CRC32("hello, world"))
25+ // Deterministic base64url(CRC32("hello, world")).
2626 want : "OnKr_w" ,
2727 },
28+ {
29+ name : "newline" ,
30+ line : "hello, world\n " ,
31+ // LogLineChecksum hashes every byte, so the trailing newline changes it.
32+ want : "U3Qk9A" ,
33+ },
2834 }
2935
3036 for _ , tc := range testCases {
@@ -66,11 +72,12 @@ func TestChecksumWriter(t *testing.T) {
6672 want : "OnKr_w hello, world" ,
6773 },
6874 {
69- name : "complex" ,
70- in : `level=INFO msg="he said \"hi\"\nthen" trail=actual
71- ` ,
72- want : `R3t8pA level=INFO msg="he said \"hi\"\nthen" trail=actual
73- ` ,
75+ name : "newline" ,
76+ in : "hello, world\n " ,
77+ // The checksumWriter knows that trailing newlines are actually line
78+ // terminators, not part of the line itself, so it discards them before
79+ // computing the checksum. Therefore the checksum should be the same.
80+ want : "OnKr_w hello, world\n " ,
7481 },
7582 }
7683
Original file line number Diff line number Diff line change @@ -180,7 +180,11 @@ func TestLoggerChecksum(t *testing.T) {
180180 if len (parts ) != 2 {
181181 t .Fatalf ("expected log line to have a space-separated checksum prefix, got %q" , got [0 ])
182182 }
183- if want := LogLineChecksum (parts [1 ]); parts [0 ] != want {
183+ // The trailing newline is a line terminator, not part of the line, so
184+ // strip it before computing the expected checksum.
185+ body := strings .TrimSuffix (parts [1 ], "\n " )
186+ want := LogLineChecksum (body )
187+ if parts [0 ] != want {
184188 t .Errorf ("checksum prefix %q does not match LogLineChecksum of remainder %q" , parts [0 ], want )
185189 }
186190}
You can’t perform that action at this time.
0 commit comments