fix(api): remove formula triggers after each ';' in CSV export [T-2627]

Merged
EV
5d ago
+130-8

What was wrong

CSV export removed the formula triggers (=, +, -, @, tab, carriage return) only at the start of a term or translation:

  • api/src/formatters/csv.ts:42 defines formulaTriggers, and csv.ts:56 (while (start < str.length && formulaTriggers.includes(str.charAt(start)))) strips them only from the start of the value.
  • csv.ts:40 said that ; does not start a formula, so it stays.
  • csv.ts:75 (quoted_match: '\r') means csv-stringify quotes only values with ,, ", a newline or \r. A value with ; is written as it is.

So a translation such as x;=cmd|' /C calc'!A0 was exported as the line greeting,x;=cmd|' /C calc'!A0. Anyone who can save a term or translation (a project member, an API client, an import) could store such a value. When someone exports the locale as CSV and opens it in a spreadsheet whose list separator is ;, the default in many European locales, the line splits at the ;, the next cell starts with = and runs as a formula. +, - and @ after a ; work the same way.

What changed

  • csvInjectionProtector (api/src/formatters/csv.ts) now also removes the formula triggers right after each ;, every trigger in a row, as it already did at the start of the value. The ; and every other character stay: x;=1+1 becomes x;1+1, b;;-1 becomes b;;1, and a; =b (a space after the ;) is exported unchanged.
  • The comments above formulaTriggers and csvInjectionProtector now explain the ; rule.
  • fixtures/cleaned.csv and the existing risky-payload test are unchanged. That fixture has no ; followed by a trigger, so it still matches.
  • docs/changelog.md: a line under next.

Tests

New in api/src/formatters/csv.spec.ts:

  1. should remove the formula triggers after each semicolon, so a spreadsheet that splits on semicolons gets no formula: exports x;=1+1, a;@SUM(1+1), b;;-1 and c;+=1 in both columns, then parses the output with csv-parse using delimiter ;, as such a spreadsheet does. It checks that no cell starts with =, +, -, @, tab or carriage return, and checks the exact values when the file is read with ,.
  2. should export a value with a space between the semicolon and a formula trigger unchanged: a; =b comes out exactly as it went in.

Before the fix, test 1 fails: Expected pattern: not /^[=+\-@\t\r]/, Received string: "=1+1,x". The suite gives Tests: 1 failed, 8 passed, 9 total. After the fix, csv.spec.ts gives Tests: 9 passed, 9 total. Test 2 is a guard against removing too much, so it passes both before and after.

The spec asked for the csv-parse option relax_quotes. The api uses csv-parse 4.16.3, where that option is still named relax (it was renamed relax_quotes in v5), so the test uses relax: true. It also sets relax_column_count, because the rows split into different numbers of cells at ;.

Checks run (Windows, Node 24)

  • api jest, csv.spec.ts: fails before (1 failed, 8 passed) and passes after (9 passed).
  • yarn --cwd api test (full unit suite, --maxWorkers=2) on the merged head: 233 of 234 tests passed, 28 of 29 suites. The one failing suite is not about CSV export and fails only on this PC (see below).
  • tsc -p api/tsconfig.json --noEmit (the type check yarn build runs): passed.
  • tslint -p tsconfig.json -c tslint.json in api (yarn lint): passed.
  • prettier --check "**/*.ts" at the root (CircleCI check_lint): passed.

The suite that fails only on this PC: formatters/properties.spec.ts also fails on Windows on unchanged code, because properties.stringify writes CRLF there (a = 1\r\nb = 2). CI runs on Linux.

develop was merged into the branch to resolve a docs/changelog.md conflict (#612 added an entry at the same place). Both entries are kept. The changelog entry is wrapped at 80 columns for Codacy markdownlint MD013.

Fleet task T-2627; authored by the lead lane while the fleet is paused by its daily cost ceiling.

🤖 Generated with Claude Code