MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / escapeCsvValue

Function escapeCsvValue

apps/web/src/app/admin/code-reviews/utils/csvExport.ts:46–61  ·  view source on GitHub ↗

* Escapes a CSV value to prevent formula injection and handle special characters. * * Protection against CSV injection attacks: * - Values starting with =, +, -, @, tab, or carriage return are prefixed with single quote * - This prevents execution of formulas like =HYPERLINK() or =CMD|'/C calc'!

(value: string | number | null | undefined)

Source from the content-addressed store, hash-verified

44 * - Internal double quotes are escaped by doubling them
45 */
46function escapeCsvValue(value: string | number | null | undefined): string {
47 if (value === null || value === undefined) return '';
48 let str = String(value);
49
50 // Prevent CSV formula injection
51 // Characters that can trigger formula execution in Excel/Google Sheets
52 if (/^[=+\-@\t\r]/.test(str)) {
53 str = `'${str}`; // Prefix with single quote to treat as text
54 }
55
56 // Standard CSV escaping
57 if (str.includes(',') || str.includes('"') || str.includes('\n')) {
58 return `"${str.replace(/"/g, '""')}"`;
59 }
60 return str;
61}
62
63function toDownloadFilenameToken(value: string): string {
64 return value.replace(/[^a-zA-Z0-9_-]+/g, '-');

Callers 1

exportCodeReviewsToCSVFunction · 0.85

Calls 1

replaceMethod · 0.65

Tested by

no test coverage detected