Skip to content

bug: CSV export does not quote a field containing a lone carriage return #757

Closed

Description

@gkhngyk

Description

escape() in web/src/lib/csv.ts decides whether a field needs quoting with /[",\n]/. That covers the quote, the comma and the line feed, but not a lone carriage return.

RFC 4180 requires a field containing CR or LF to be quoted. A parser reading an unquoted CR treats it as the end of a record, so one such value silently splits a row in two and shifts every column after it.

CRLF happens to be safe — the \n in it matches, so the field gets quoted. Only a bare \r slips through, which makes this narrow, but it is reachable: exported values include prompt text, brand names and citation titles, and scraped or pasted content can carry a bare CR.

The same function is behind every CSV in the product — Insights, Citations, Topics, Prompts, Traffic and Shopping all call toCsv.

Steps to reproduce

  1. Call toCsv([{ text: 'a\rb' }], ['text']).
  2. Look at the field it produces.

Or, in the product: get a value containing a bare CR into an exported column and open the file in a spreadsheet.

Expected behavior

The field is quoted, the same way a value containing \n already is:

text
"a\rb"

Actual behavior

The field is emitted raw, with the CR still in it:

text
a\rb

A parser reads that as two records. Every column after the break is shifted.

Deployment mode

Cloud (ansvisor.com)

Environment

Not environment-specific — toCsv is a pure function.

Notes for whoever picks this up

The fix is in the character class. Make sure \r alone triggers quoting and that CRLF keeps behaving as it does now.

web/src/lib/csv.test.ts already covers the comma, quote and newline cases in the style to follow. Add cases for a bare \r and for \r\n, and check that the new test fails before the fix and passes after — a test that passes either way proves nothing.

Out of scope: values beginning with =, +, - or @ are also passed through unchanged and a spreadsheet will evaluate them as formulas. That is a real concern but a separate one, and mitigating it changes the exported value, so it needs its own decision. Please don't fold it in here.

Activity

  1. added
    bug
    good first issue
    help wanted
    on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions