Skip to content

fix(traefik-logs): parse CLF fields with escaped quotes - #1978

Merged
sabban merged 2 commits into
crowdsecurity:masterfrom
stephdl:fix/traefik-clf-escaped-quotes
Nov 10, 2026
Merged

sabban merged 2 commits into
crowdsecurity:masterfrom
stephdl:fix/traefik-clf-escaped-quotes

Conversation

@stephdl

@stephdl stephdl commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #1977. Traefik writes a " inside the Referer or the User-Agent as \", but only since 3.6.18 and 3.7.2. Older versions, 2.11 included, write it raw. \ is never escaped. NOTDQUOTE refused the escaped quote and the whole CLF line was dropped.

I read the referer and the user agent with DATA. I anchored the end of the line with $, and I read the router with NOTDQUOTE. With that, a crafted user agent cannot shift the request counter, the router or the server URL, whether Traefik escapes the quote or not. I also unescape \" in both fields so CLF gives the same value as the JSON format.

I added lines to the traefik_clf test. They cover an escaped quote, a trailing backslash, a quote in the referer, user agents built to shift the fields, and raw quotes as older Traefik writes them.

Checklist

  • I have read the contributing guide
  • I have tested my changes locally
  • For new parsers or scenarios, tests have been added
  • I have run the hub linter and no issues were reported (see contributing guide)
  • Automated tests are passing
  • AI was used to generate any/all content of this PR

@sabban
sabban requested a balanced review from Copilot November 3, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@stephdl

stephdl commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@sabban sorry to ping you but the AI review did not start

@sabban

sabban commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Hi,

Thank you for your contribution.

From reading your PR I suspect that your change add the exact same issue would happen with "" at the end of the User-Agent. Can you test with a generated log line with curl -A 'x\' against your traefik installation. If so using %{DATA:http_referer} and %{DATA:http_user_agent} should do the trick.

Regards,

@sabban sabban added the waiting-for-user Waiting for submitter answer regarding specific points in the PR label Nov 4, 2026
Traefik escapes " only since 3.6.18 and 3.7.2, and never
escapes \. Read referer and user agent with lazy DATA,
anchor the end of the line and read the router with
NOTDQUOTE so a crafted user agent cannot shift the
trailing fields. Unescape \" to match the JSON format.
@stephdl

stephdl commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

You're right, thanks. I tested it on a real Traefik with the User-Agent and Referer headers kept. Traefik escapes " but not \, so curl -A 'x\' is logged as "x\" and my TRAEFIK_QUOTED pattern dropped the line.

I switched the referer and the user agent to DATA as you suggested. While testing I found two more things. The router field also used DATA, so a user agent like x" 1 could close the field early and shift the request counter and the router name. The current parser has the same issue. I now read the router with NOTDQUOTE. Also, Traefik only escapes " since 3.6.18 and 3.7.2, and 2.11 still writes it raw. To cover both, I anchored the end of the line with $, so the trailing fields are always the real ones.

I also unescape \" in the referer and the user agent, so CLF gives the same value as the JSON format.

I added test lines for both escaped and raw output. All hubtests pass locally, and I checked the escaped cases with cscli explain on lines from a real Traefik 3.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-for-user Waiting for submitter answer regarding specific points in the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

traefik-logs: CLF lines with an escaped quote in the User-Agent are dropped

3 participants