-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [HIGH] Fix CSV Formula Injection in Export #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: developmental
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, it, expect } from 'vitest' | ||
| import { csvField } from './export' | ||
|
|
||
| describe('csvField', () => { | ||
| it('returns empty string for null or undefined', () => { | ||
| expect(csvField(null)).toBe('') | ||
| expect(csvField(undefined)).toBe('') | ||
| }) | ||
|
|
||
| it('formats normal strings correctly', () => { | ||
| expect(csvField('hello')).toBe('hello') | ||
| expect(csvField('hello world')).toBe('hello world') | ||
| }) | ||
|
|
||
| it('formats numbers correctly', () => { | ||
| expect(csvField(123)).toBe('123') | ||
| expect(csvField(0)).toBe('0') | ||
| expect(csvField(-123)).toBe('-123') | ||
| }) | ||
|
|
||
| it('escapes strings with quotes, commas, or newlines', () => { | ||
| expect(csvField('hello,world')).toBe('"hello,world"') | ||
| expect(csvField('hello\nworld')).toBe('"hello\nworld"') | ||
| expect(csvField('hello\rworld')).toBe('"hello\rworld"') | ||
| expect(csvField('hello"world')).toBe('"hello""world"') | ||
| }) | ||
|
|
||
| it('prepends single quote for strings vulnerable to CSV Formula Injection', () => { | ||
| expect(csvField('=1+1')).toBe('"\'=1+1"') | ||
| expect(csvField('=SUM(A1:A2)')).toBe('"\'=SUM(A1:A2)"') | ||
| expect(csvField('+1+1')).toBe('"\'+1+1"') | ||
| expect(csvField('-1+1')).toBe('"\'-1+1"') | ||
| expect(csvField('@SUM(A1)')).toBe('"\'@SUM(A1)"') | ||
| expect(csvField('\t1+1')).toBe('"\'\t1+1"') | ||
| expect(csvField('\r1+1')).toBe('"\'\r1+1"') | ||
| }) | ||
|
|
||
| it('escapes quotes in malicious inputs properly', () => { | ||
| expect(csvField('="hello"')).toBe('"\'=""hello"""') | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export function csvField(value: string | number | null | undefined) { | ||
| if (value === null || value === undefined) return '' | ||
|
|
||
| if (typeof value !== 'number') { | ||
| const text = String(value) | ||
| if (/^[=+\-@\t\r]/.test(text)) { | ||
| return `"'${text.replaceAll('"', '""')}"` | ||
| } | ||
|
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π Info: Prompts starting with a dash get an apostrophe prefix The formula guard ( Was this helpful? React with π or π to provide feedback. |
||
| } | ||
|
|
||
| const text = String(value) | ||
| return /[",\r\n]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Unrelated deepmerge-ts override added
package.json pins
deepmerge-tsto8.0.0in pnpm overrides, unrelated to the CSV fix. Confirm the pin is intentional and version-compatible across transitive paths.Was this helpful? React with π or π to provide feedback.