Skip to content

Commit

Permalink
fix(upload-core): upload filename check
Browse files Browse the repository at this point in the history
  • Loading branch information
Christie Baker authored and Christie Baker committed May 31, 2018
1 parent 2e414a3 commit cb98b56
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/upload-core/src/tests/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,42 @@ describe('upload.core', () => {
upload.setError('decrypting', 'Decrypting file');
expect(upload.status).toBe('decrypting');
});

it('should validate file name', () => {
const file = Buffer.from('hello world'.split(''));
file.name = 'good file name.pdf';
const upload = new Upload(
file,
Object.assign(options, { allowedFileNameCharacters: 'a-zA-Z0-9_ ' })
);
upload.start();
expect(upload.isValidFile()).toBeTruthy();

const file2 = Buffer.from('hello world'.split(''));
file2.name = 'Bad-file-name.pdf';
const upload2 = new Upload(
file2,
Object.assign(options, { allowedFileNameCharacters: 'a-zA-Z0-9 _' })
);
upload2.start();
expect(upload2.isValidFile()).toBeFalsy();

const file3 = Buffer.from('hello world'.split(''));
file3.name = '123File(1).xlsx';
const upload3 = new Upload(
file3,
Object.assign(options, { allowedFileNameCharacters: '_a-zA-Z0-9 ' })
);
upload3.start();
expect(upload3.isValidFile()).toBeFalsy();

const file4 = Buffer.from('hello world'.split(''));
file4.name = 'fileName';
const upload4 = new Upload(
file4,
Object.assign(options, { allowedFileNameCharacters: '_a-zA-Z0-9 ' })
);
upload4.start();
expect(upload4.isValidFile()).toBeTruthy();
});
});
14 changes: 14 additions & 0 deletions packages/upload-core/src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ class Upload {
return false;
}
}
if (this.options.allowedFileNameCharacters) {
const fileName = this.file.name.substring(
0,
this.file.name.lastIndexOf('.')
);
const regExp = new RegExp(
`([^${this.options.allowedFileNameCharacters}])`,
'g'
);
if (fileName && fileName.match(regExp) !== null) {
this.setError('rejected', 'File name contains characters not allowed');
return false;
}
}
return true;
}

Expand Down

0 comments on commit cb98b56

Please sign in to comment.