fix(accesslog): escape double quotes in quoted log fields
Some checks failed
CodeQL / Analyze (push) Has been cancelled
Build and Publish Documentation / Doc Process (push) Has been cancelled
Build experimental image on branch / build-webui (push) Has been cancelled
Build experimental image on branch / Build experimental image on branch (push) Has been cancelled

This commit is contained in:
Kaan 2026-05-18 17:02:06 +03:00 committed by GitHub
parent 32c0861937
commit f2b11cd50d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package accesslog
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
@ -108,7 +109,7 @@ func toLogEntry(s, defaultValue string, quote bool) string {
}
if quote {
return `"` + s + `"`
return `"` + strings.ReplaceAll(s, `"`, `\"`) + `"`
}
return s
}

View file

@ -79,6 +79,27 @@ func TestCommonLogFormatter_Format(t *testing.T) {
ServiceURL: "http://10.0.0.2/toto",
},
expectedLog: `10.0.0.1 - Client [10/Nov/2009:14:00:00 -0900] "GET /foo http" 123 132 "referer" "agent" - "foo" "http://10.0.0.2/toto" 123000ms
`,
},
{
name: "user-agent with double quote is escaped",
data: map[string]any{
StartUTC: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
Duration: 1 * time.Millisecond,
ClientHost: "10.0.0.1",
ClientUsername: "-",
RequestMethod: http.MethodGet,
RequestPath: "/",
RequestProtocol: "HTTP/1.1",
DownstreamStatus: 200,
DownstreamContentSize: 0,
RequestRefererHeader: "-",
RequestUserAgentHeader: `foo " bar`,
RequestCount: 1,
RouterName: "test@file",
ServiceURL: "http://127.0.0.1:8080",
},
expectedLog: `10.0.0.1 - - [10/Nov/2009:23:00:00 +0000] "GET / HTTP/1.1" 200 0 "-" "foo \" bar" 1 "test@file" "http://127.0.0.1:8080" 1ms
`,
},
}
@ -221,6 +242,16 @@ func Test_toLog(t *testing.T) {
quoted: true,
expectedLog: `"foo"`,
},
{
desc: "Should escape double quotes in quoted string",
fields: logrus.Fields{
"Powpow": `foo " bar`,
},
fieldName: "Powpow",
defaultValue: defaultValue,
quoted: true,
expectedLog: `"foo \" bar"`,
},
{
desc: "Should return defaultValue if fieldName does not exist",
fields: logrus.Fields{