Repository navigation
Description
Describe the bug
I work on NethServer 8, an open source server distribution. It ships Traefik as reverse proxy and CrowdSec with the crowdsecurity/traefik collection. Traefik logs to the journal in CLF, which is the default format. A contributor proposed to keep the User-Agent in the access log, so scenarios like http-bad-user-agent can use it (NethServer/ns8-traefik#152). While testing it, I found that some lines never reach the scenarios.
Traefik escapes a double quote inside a quoted CLF field. In pkg/middlewares/accesslog/logger_formatters.go, toLogEntry() does strings.ReplaceAll(s, "\"", "\\\"").
crowdsecurity/traefik-logs reads the Referer and the User-Agent with NOTDQUOTE, which is [^"]*. The escaped quote breaks the grok match and the whole line is dropped.
The client controls the User-Agent. One " in it, like curl -A 'x"y', is enough to hide all its requests from every HTTP scenario, path based ones included.
This only applies when the User-Agent header is kept in the access log (accessLog.fields.headers). With the default config Traefik writes "-" there. The JSON format is not affected, it parses the same User-Agent fine.
To Reproduce
Tested with CrowdSec v1.8.1 and crowdsecurity/traefik-logs 1.5.
cscli explain --type syslog --log 'Nov 3 10:00:00 host traefik[1916]: 1.2.3.4 - - [03/Nov/2026:10:00:00 +0000] "GET /.env HTTP/1.1" 404 19 "-" "Mozilla/5.0 \"test\"" 221 "mattermost1-https@file" "http://127.0.0.1:20001" 1ms'
├ s00-raw
| └ 🟢 crowdsecurity/syslog-logs (+12 ~9)
├ s01-parse
| ...
| └ 🔴 crowdsecurity/traefik-logs
└-------- parser failure 🔴
The same line with "Mozilla/5.0 test" as User-Agent works:
├ s01-parse
| ...
| └ 🟢 crowdsecurity/traefik-logs (+27 ~2)
├ s02-enrich
| ...
| ├ 🟢 crowdsecurity/http-logs (+7)
├-------- parser success 🟢
├ Scenarios
├ 🟢 crowdsecurity/http-crawl-non_statics
├ 🟢 crowdsecurity/http-probing
└ 🟢 crowdsecurity/http-sensitive-files
Expected behavior
The line with the escaped quote is parsed like the line without it, and reaches the same scenarios.
Additional context
A pattern that accepts escaped characters should fix it, for both the Referer and the User-Agent:
TRAEFIK_QUOTED: '(?:[^"\\]|\\.)*'and in NGINXACCESS2:
"%{TRAEFIK_QUOTED:http_referer}" "%{TRAEFIK_QUOTED:http_user_agent}"
I checked the regex against a normal line, a line with \", and a User-Agent built to shift the fields (x\" 1 \"evil). All three give the right fields. Happy to open a PR with this change and a test case in .tests/traefik_clf if that helps.
We opened a PR to work around it on our side (NethServer/ns8-crowdsec#226). It adds a local copy of the CLF pattern that only takes lines with \". We would rather have a fix upstream than maintain that copy.
Activity