1- import {
2- sanitizeCSVValue ,
3- sanitizeCSVObject ,
4- sanitizeCSVData ,
5- isCSVSafe
6- } from './csvSanitization' ;
1+ import { sanitizeCSVValue , sanitizeCSVObject , sanitizeCSVData , isCSVSafe } from './csvSanitization' ;
72
83describe ( 'CSV Sanitization' , ( ) => {
94 describe ( 'sanitizeCSVValue' , ( ) => {
@@ -43,11 +38,13 @@ describe('CSV Sanitization', () => {
4338 } ) ;
4439
4540 test ( 'should handle complex formula injection attempts' , ( ) => {
46- expect ( sanitizeCSVValue ( '=cmd|"/c calc"!A0' ) ) . toBe ( "'=cmd|\"/c calc\"!A0" ) ;
47- expect ( sanitizeCSVValue ( '=HYPERLINK("http://evil.com","Click me")' ) ) . toBe ( "'=HYPERLINK(\"http://evil.com\",\"Click me\")" ) ;
48- expect ( sanitizeCSVValue ( '+cmd|"/c calc"!A0' ) ) . toBe ( "'+cmd|\"/c calc\"!A0" ) ;
49- expect ( sanitizeCSVValue ( '-cmd|"/c calc"!A0' ) ) . toBe ( "'-cmd|\"/c calc\"!A0" ) ;
50- expect ( sanitizeCSVValue ( '@SUM(1+1)*cmd|"/c calc"!A0' ) ) . toBe ( "'@SUM(1+1)*cmd|\"/c calc\"!A0" ) ;
41+ expect ( sanitizeCSVValue ( '=cmd|"/c calc"!A0' ) ) . toBe ( '\'=cmd|"/c calc"!A0' ) ;
42+ expect ( sanitizeCSVValue ( '=HYPERLINK("http://evil.com","Click me")' ) ) . toBe (
43+ '\'=HYPERLINK("http://evil.com","Click me")' ,
44+ ) ;
45+ expect ( sanitizeCSVValue ( '+cmd|"/c calc"!A0' ) ) . toBe ( '\'+cmd|"/c calc"!A0' ) ;
46+ expect ( sanitizeCSVValue ( '-cmd|"/c calc"!A0' ) ) . toBe ( '\'-cmd|"/c calc"!A0' ) ;
47+ expect ( sanitizeCSVValue ( '@SUM(1+1)*cmd|"/c calc"!A0' ) ) . toBe ( '\'@SUM(1+1)*cmd|"/c calc"!A0' ) ;
5148 } ) ;
5249 } ) ;
5350
@@ -58,7 +55,7 @@ describe('CSV Sanitization', () => {
5855 formula : '=SUM(A1:A10)' ,
5956 email : 'user@example.com' ,
6057 dangerous : '+malicious' ,
61- safe : 'normal text'
58+ safe : 'normal text' ,
6259 } ;
6360
6461 const result = sanitizeCSVObject ( input ) ;
@@ -78,8 +75,8 @@ describe('CSV Sanitization', () => {
7875 } ,
7976 metadata : {
8077 count : 5 ,
81- formula : '+dangerous'
82- }
78+ formula : '+dangerous' ,
79+ } ,
8380 } ;
8481
8582 const result = sanitizeCSVObject ( input ) ;
@@ -96,12 +93,12 @@ describe('CSV Sanitization', () => {
9693 number : 42 ,
9794 boolean : true ,
9895 nullValue : null ,
99- undefinedValue : undefined
96+ undefinedValue : undefined ,
10097 } ;
10198
10299 const result = sanitizeCSVObject ( input ) ;
103100
104- expect ( result . list ) . toBe ( " =formula,safe" ) ; // Arrays get stringified
101+ expect ( result . list ) . toBe ( ' =formula,safe' ) ; // Arrays get stringified
105102 expect ( result . number ) . toBe ( '42' ) ;
106103 expect ( result . boolean ) . toBe ( 'true' ) ;
107104 expect ( result . nullValue ) . toBe ( '' ) ;
@@ -115,13 +112,13 @@ describe('CSV Sanitization', () => {
115112 {
116113 name : 'User 1' ,
117114 response : '=SUM(A1:A10)' ,
118- email : 'user1@example.com'
115+ email : 'user1@example.com' ,
119116 } ,
120117 {
121- name : 'User 2' ,
118+ name : 'User 2' ,
122119 response : '+malicious_formula' ,
123- email : 'user2@example.com'
124- }
120+ email : 'user2@example.com' ,
121+ } ,
125122 ] ;
126123
127124 const result = sanitizeCSVData ( input ) ;
@@ -170,25 +167,25 @@ describe('CSV Sanitization', () => {
170167 describe ( 'Real-world attack scenarios' , ( ) => {
171168 test ( 'should prevent DDE (Dynamic Data Exchange) attacks' , ( ) => {
172169 const ddeAttack = '=cmd|"/c calc"!A1' ;
173- expect ( sanitizeCSVValue ( ddeAttack ) ) . toBe ( "' =cmd|\ "/c calc\ "!A1" ) ;
170+ expect ( sanitizeCSVValue ( ddeAttack ) ) . toBe ( '\' =cmd|"/c calc"!A1' ) ;
174171 expect ( isCSVSafe ( sanitizeCSVValue ( ddeAttack ) ) ) . toBe ( true ) ;
175172 } ) ;
176173
177174 test ( 'should prevent hyperlink-based attacks' , ( ) => {
178175 const hyperlinkAttack = '=HYPERLINK("http://evil.com","Click me")' ;
179- expect ( sanitizeCSVValue ( hyperlinkAttack ) ) . toBe ( "' =HYPERLINK(\ "http://evil.com\",\ "Click me\")" ) ;
176+ expect ( sanitizeCSVValue ( hyperlinkAttack ) ) . toBe ( '\' =HYPERLINK("http://evil.com", "Click me")' ) ;
180177 expect ( isCSVSafe ( sanitizeCSVValue ( hyperlinkAttack ) ) ) . toBe ( true ) ;
181178 } ) ;
182179
183180 test ( 'should prevent command execution via various prefixes' , ( ) => {
184181 const attacks = [
185182 '=cmd|"/c calc"!A0' ,
186- '+cmd|"/c calc"!A0' ,
183+ '+cmd|"/c calc"!A0' ,
187184 '-cmd|"/c calc"!A0' ,
188- '@SUM(1+1)*cmd|"/c calc"!A0'
185+ '@SUM(1+1)*cmd|"/c calc"!A0' ,
189186 ] ;
190187
191- attacks . forEach ( attack => {
188+ attacks . forEach ( ( attack ) => {
192189 const sanitized = sanitizeCSVValue ( attack ) ;
193190 expect ( sanitized ) . toMatch ( / ^ ' / ) ;
194191 expect ( isCSVSafe ( sanitized ) ) . toBe ( true ) ;
@@ -200,7 +197,7 @@ describe('CSV Sanitization', () => {
200197 { name : 'John Doe' , nickname : '=EVIL()' } ,
201198 { name : 'Jane Smith' , tag : '+Administrator' } ,
202199 { name : 'Bob Wilson' , response : '@dangerous_command' } ,
203- { name : 'Alice Brown' , comment : '-rm -rf /' }
200+ { name : 'Alice Brown' , comment : '-rm -rf /' } ,
204201 ] ;
205202
206203 const sanitized = sanitizeCSVData ( userInputs ) ;
0 commit comments