@@ -82,6 +82,12 @@ export const parseCSVHeader = async (i18n, file, signal = undefined) => {
8282 preview : 1
8383 } ) ;
8484 const columns = data . length !== 0 ? data [ 0 ] : [ ] ;
85+ // Make a simple try at detecting a binary file by searching for a null
86+ // character. We do that in order to avoid displaying unintelligible binary
87+ // data to the user. Also, Backend would probably reject a null character
88+ // that's sent to it.
89+ if ( columns . some ( column => column . includes ( '\0' ) ) )
90+ throw new Error ( i18n . t ( 'util.csv.invalidCSV' , { name : file . name } ) ) ;
8591 const unhandledErrors = errors . filter ( error =>
8692 error . code !== 'UndetectableDelimiter' ) ;
8793 if ( unhandledErrors . length === 0 ) {
@@ -200,6 +206,12 @@ export const parseCSV = async (i18n, file, columns, options = {}) => {
200206 throw new Error ( i18n . tc ( 'util.csv.dataWithoutHeader' , columns . length , counts ) ) ;
201207 }
202208
209+ if ( values . some ( value => value . includes ( '\0' ) ) ) {
210+ const error = new Error ( i18n . t ( 'util.csv.invalidCSV' , { name : file . name } ) ) ;
211+ error . row = NaN ;
212+ throw error ;
213+ }
214+
203215 data . push ( transformRow ( values , columns ) ) ;
204216 for ( const warning of warnings ) warning . pushRow ( values , index , columns ) ;
205217 } ;
@@ -235,6 +247,9 @@ export const parseCSV = async (i18n, file, columns, options = {}) => {
235247 worker : true
236248 } ) ;
237249 } catch ( error ) {
250+ // Mention the row number in the error message unless the `row` property of
251+ // the error has been set to NaN.
252+ if ( Number . isNaN ( error . row ) ) throw error ;
238253 throw new Error ( i18n . t ( 'util.csv.rowError' , {
239254 message : error . message ,
240255 row : i18n . n ( ( error . row ?? rowIndex ) + 1 , 'default' )
0 commit comments