Skip to content

Commit 85d2800

Browse files
vaadin-botugur-vaadinvursen
authored
fix: make extension patterns match the end of the file name (#5652) (#5660)
* fix: make extension patterns match the end of the file name * fix: make the regex safari-compatible * refactor: remove extra lines * refactor: update naming for clarification * refactor: rename file name and extension in test * Update packages/upload/test/adding-files.test.js * test: add a test for rejecting files that have partial extension match * refactor: extract regex logic in a loop and make it a function * Update packages/upload/src/vaadin-upload.js * refactor: change the call in order to align with the naming change * fix: return null instead of undefined --------- Co-authored-by: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com> Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
1 parent 97f47a8 commit 85d2800

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ class Upload extends ElementMixin(ThemableMixin(ControllerMixin(PolymerElement))
492492
];
493493
}
494494

495+
/** @private */
496+
get __acceptRegexp() {
497+
if (!this.accept) {
498+
return null;
499+
}
500+
const processedTokens = this.accept.split(',').map((token) => {
501+
let processedToken = token.trim();
502+
// Escape regex operators common to mime types
503+
processedToken = processedToken.replace(/[+.]/gu, '\\$&');
504+
// Make extension patterns match the end of the file name
505+
if (processedToken.startsWith('\\.')) {
506+
processedToken = `.*${processedToken}$`;
507+
}
508+
// Handle star (*) wildcards
509+
return processedToken.replace(/\/\*/gu, '/.*');
510+
});
511+
// Create accept regex
512+
return new RegExp(`^(${processedTokens.join('|')})$`, 'iu');
513+
}
514+
495515
/** @protected */
496516
ready() {
497517
super.ready();
@@ -879,12 +899,8 @@ class Upload extends ElementMixin(ThemableMixin(ControllerMixin(PolymerElement))
879899
);
880900
return;
881901
}
882-
const fileExt = file.name.match(/\.[^.]*$|$/u)[0];
883-
// Escape regex operators common to mime types
884-
const escapedAccept = this.accept.replace(/[+.]/gu, '\\$&');
885-
// Create accept regex that can match comma separated patterns, star (*) wildcards
886-
const re = new RegExp(`^(${escapedAccept.replace(/[, ]+/gu, '|').replace(/\/\*/gu, '/.*')})$`, 'iu');
887-
if (this.accept && !(re.test(file.type) || re.test(fileExt))) {
902+
const re = this.__acceptRegexp;
903+
if (re && !(re.test(file.type) || re.test(file.name))) {
888904
this.dispatchEvent(
889905
new CustomEvent('file-reject', {
890906
detail: { file, error: this.i18n.error.incorrectFileType },

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ describe('adding files', () => {
190190
expect(upload.files.length).to.equal(1);
191191
});
192192

193+
it('should allow files with extensions containing multiple dots', () => {
194+
upload.accept = 'image/*,.bar.baz,video/*';
195+
file.name = 'foo.bar.baz';
196+
upload._addFiles([file]);
197+
expect(upload.files).to.have.lengthOf(1);
198+
});
199+
200+
it('should reject files that have partial extension match', () => {
201+
upload.accept = 'image/*,.bar.baz,video/*';
202+
file.name = 'foo.baz';
203+
upload._addFiles([file]);
204+
expect(upload.files).to.have.lengthOf(0);
205+
});
206+
193207
it('should allow files with correct mime type', () => {
194208
upload.accept = 'application/x-octet-stream';
195209
upload._addFiles([file]);

0 commit comments

Comments
 (0)