Skip to content

Commit 247920e

Browse files
authored
fix: escape regex operators in upload accept pattern (#4641)
1 parent a700eb3 commit 247920e

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎packages/upload/src/vaadin-upload.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Upload extends ElementMixin(ThemableMixin(PolymerElement)) {
9999
<slot name="drop-label-icon">
100100
<div part="drop-label-icon"></div>
101101
</slot>
102-
<slot name="drop-label" id="dropLabel"> [[_i18nPlural(maxFiles, i18n.dropFiles, i18n.dropFiles.*)]] </slot>
102+
<slot name="drop-label" id="dropLabel"> [[_i18nPlural(maxFiles, i18n.dropFiles, i18n.dropFiles.*)]]</slot>
103103
</div>
104104
</div>
105105
<slot name="file-list">
@@ -780,7 +780,10 @@ class Upload extends ElementMixin(ThemableMixin(PolymerElement)) {
780780
return;
781781
}
782782
const fileExt = file.name.match(/\.[^.]*$|$/)[0];
783-
const re = new RegExp(`^(${this.accept.replace(/[, ]+/g, '|').replace(/\/\*/g, '/.*')})$`, 'i');
783+
// Escape regex operators common to mime types
784+
const escapedAccept = this.accept.replace(/[+.]/g, '\\$&');
785+
// Create accept regex that can match comma separated patterns, star (*) wildcards
786+
const re = new RegExp(`^(${escapedAccept.replace(/[, ]+/g, '|').replace(/\/\*/g, '/.*')})$`, 'i');
784787
if (this.accept && !(re.test(file.type) || re.test(fileExt))) {
785788
this.dispatchEvent(
786789
new CustomEvent('file-reject', {

‎packages/upload/test/adding-files.test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,19 @@ describe('file list', () => {
268268
upload._addFiles([file]);
269269
expect(upload.files.length).to.equal(1);
270270
});
271+
272+
it('should allow files when using regex operators in accept string', () => {
273+
file = createFile(testFileSize, 'image/svg+xml');
274+
upload.accept = 'image/svg+xml';
275+
upload._addFiles([file]);
276+
expect(upload.files.length).to.equal(1);
277+
});
278+
279+
it('should reject files when accept contains regex single character wildcard and file type is not an exact match', () => {
280+
file = createFile(testFileSize, 'application/vndxms-excel');
281+
upload.accept = 'application/vnd.ms-excel';
282+
upload._addFiles([file]);
283+
expect(upload.files.length).to.equal(0);
284+
});
271285
});
272286
});

0 commit comments

Comments
 (0)