Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape regex operators in upload accept pattern #4641

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/upload/src/vaadin-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Upload extends ElementMixin(ThemableMixin(PolymerElement)) {
<slot name="drop-label-icon">
<div part="drop-label-icon"></div>
</slot>
<slot name="drop-label" id="dropLabel"> [[_i18nPlural(maxFiles, i18n.dropFiles, i18n.dropFiles.*)]] </slot>
<slot name="drop-label" id="dropLabel"> [[_i18nPlural(maxFiles, i18n.dropFiles, i18n.dropFiles.*)]]</slot>
</div>
</div>
<slot name="file-list">
Expand Down Expand Up @@ -780,7 +780,10 @@ class Upload extends ElementMixin(ThemableMixin(PolymerElement)) {
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 packages/upload/test/adding-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,19 @@ describe('file list', () => {
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);
});
});
});