Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(csv): sanitize leading formula characters to prevent formula injection (#934)

Mubarak Rajic5551a976b16
diff --git a/web/src/lib/csv.test.ts b/web/src/lib/csv.test.ts
index 9f331363d..be3e564b4 100644
--- a/web/src/lib/csv.test.ts
+++ b/web/src/lib/csv.test.ts
@@ -1,4 +1,4 @@
-import { expect, test } from 'vitest';
+import { expect, test } from 'vitest';
import { toCsv } from './csv.js';
test('plain values pass through unquoted', () => {
@@ -61,3 +61,23 @@ test('multiple rows are joined by newline', () => {
);
expect(result).toBe('a,b\n1,2\n3,4');
});
+
+test('string values starting with formula injection triggers get prefixed with single quote', () => {
+ const result = toCsv(
+ [{ formula: '=1+1', plus: '+cmd', minus: '-calc', at: '@user', tab: '\ttab', cr: '\rline' }],
+ ['formula', 'plus', 'minus', 'at', 'tab', 'cr'],
+ );
+ expect(result).toBe(
+ 'formula,plus,minus,at,tab,cr\n' + "'=1+1,'+cmd,'-calc,'@user,'\ttab,\"'\rline\"",
+ );
+});
+
+test('string with leading formula trigger and comma is prefixed and quoted', () => {
+ const result = toCsv([{ val: '=a,b' }], ['val']);
+ expect(result).toBe('val\n"\'=a,b"');
+});
+
+test('negative numbers remain untouched as numeric values', () => {
+ const result = toCsv([{ score: -3.2, rank: -5 }], ['score', 'rank']);
+ expect(result).toBe('score,rank\n-3.2,-5');
+});
diff --git a/web/src/lib/csv.ts b/web/src/lib/csv.ts
index 5f91226b3..81979956c 100644
--- a/web/src/lib/csv.ts
+++ b/web/src/lib/csv.ts
@@ -1,7 +1,13 @@
+const FORMULA_PREFIX_REGEX = /^[=+\-@\t\r]/;
+
function escape(value: unknown): string {
if (value === null || value === undefined) return '';
- const s = String(value);
+ let s = String(value);
+ if (typeof value === 'string' && FORMULA_PREFIX_REGEX.test(s)) {
+ s = `'${s}`;
+ }
+
if (/[",\r\n]/.test(s)) return `"${s.replace(/"/g, '""')}"`;
return s;
}