Skip to content

Commit

Permalink
Merge pull request #410 from vaadin/cp-wc-4641
Browse files Browse the repository at this point in the history
fix: escape regex operators in upload accept pattern
  • Loading branch information
sissbruecker authored Jun 27, 2024
2 parents 4d01e6b + 5a5605f commit 564930a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/vaadin-upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,10 @@
return;
}
const fileExt = file.name.match(/\.[^\.]*$|$/)[0];
const re = new RegExp('^(' + this.accept.replace(/[, ]+/g, '|').replace(/\/\*/g, '/.*') + ')$', 'i');
// Escape regex operators common to mime types
const escapedAccept = this.accept.replace(/[+.]/g, '\\$&');
// Create accept regex that can match comma separated patterns, star (*) wildcards
const re = new RegExp(`^(${escapedAccept.replace(/[, ]+/g, '|').replace(/\/\*/g, '/.*')})$`, 'i');
if (this.accept && !(re.test(file.type) || re.test(fileExt))) {
this.dispatchEvent(
new CustomEvent('file-reject', {
Expand Down
14 changes: 14 additions & 0 deletions test/adding-files.html
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,20 @@
upload._addFiles([file]);
expect(upload.files.length).to.equal(1);
});

it('should allow files when using regex operators in accept string', () => {
file = createFile(testFileSize, 'image/svg+xml');
upload.accept = 'image/svg+xml';
upload._addFiles([file]);
expect(upload.files.length).to.equal(1);
});

it('should reject files when accept contains regex single character wildcard and file type is not an exact match', () => {
file = createFile(testFileSize, 'application/vndxms-excel');
upload.accept = 'application/vnd.ms-excel';
upload._addFiles([file]);
expect(upload.files.length).to.equal(0);
});
});

});
Expand Down

0 comments on commit 564930a

Please sign in to comment.