Skip to content

Commit 3da3feb

Browse files
committed
vue: Add improved file type checks before file extraction.
1 parent 1c56c3a commit 3da3feb

File tree

5 files changed

+92
-46
lines changed

5 files changed

+92
-46
lines changed

ddm/core/static/ddm_core/vue/css/vue_uploader.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ddm/core/static/ddm_core/vue/js/vue_uploader.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ddm/core/static/ddm_core/vue/js/vue_uploader.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/UploaderApp/src/components/FileUploader.vue

+87-41
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,26 @@
4646

4747
<div class="col ul-status-message">
4848
<label class="select-file-btn">
49-
<input :name="'ul-' + componentId" type="file" @change="processFile" class="d-none">
49+
50+
<input v-if="expectsZip"
51+
:name="'ul-' + componentId"
52+
accept=".zip,application/zip,application/x-zip-compressed,multipart/x-zip"
53+
type="file"
54+
@change="processFile"
55+
class="d-none">
56+
<input v-else-if="blueprints[0].format === 'json'"
57+
:name="'ul-' + componentId"
58+
accept=".json,application/json"
59+
type="file"
60+
@change="processFile"
61+
class="d-none">
62+
<input v-else-if="blueprints[0].format === 'csv'"
63+
:name="'ul-' + componentId"
64+
accept=".csv,text/csv"
65+
type="file"
66+
@change="processFile"
67+
class="d-none">
68+
5069
{{ $t('choose-file') }}
5170
</label>
5271
</div>
@@ -130,7 +149,8 @@
130149
<div class="row">
131150
<div class="col extraction-information-container">
132151
<template v-for="bp in blueprints" :key="bp">
133-
<div class="ul-status row align-items-start pt-2 pb-2" :class="{ 'ul-success': blueprintData[bp.id.toString()].status === 'success', 'ul-failed': blueprintData[bp.id.toString()].status === 'failed'}">
152+
<div class="ul-status row align-items-start pt-2 pb-2"
153+
:class="{ 'ul-success': blueprintData[bp.id.toString()].status === 'success', 'ul-failed': blueprintData[bp.id.toString()].status === 'failed'}">
134154

135155
<!-- Pending -->
136156
<template v-if="blueprintData[bp.id.toString()].status === 'pending'">
@@ -408,6 +428,23 @@ export default {
408428
this.emitToParent();
409429
},
410430
methods: {
431+
/**
432+
* Validate that a file is a ZIP file.
433+
* @param file
434+
*/
435+
checkIsZip(file) {
436+
const extensionIsValid = file.name.toLowerCase().endsWith('.zip');
437+
const mimeIsValid = [
438+
'application/zip',
439+
'application/x-zip-compressed',
440+
'multipart/x-zip'
441+
].includes(file.type);
442+
if(extensionIsValid && mimeIsValid) {
443+
return true;
444+
} else {
445+
return false;
446+
}
447+
},
411448
/**
412449
* Processes the file uploaded by the participant.
413450
* @param event
@@ -421,48 +458,57 @@ export default {
421458
422459
// Procedure if supplied file is expected to be a zip-folder.
423460
if (uploader.expectsZip && files.length === 1) {
424-
JSZip
425-
.loadAsync(files[0])
426-
.then(z => {
427-
uploader.blueprints.forEach(blueprint => {
428-
let re = new RegExp(blueprint.regex_path);
429-
let reHasMatched = false;
430-
z.file(re).forEach(f => {
431-
reHasMatched = true;
432-
f
433-
.async("string")
434-
.then(c => uploader.processContent(c, blueprint))
435-
.catch(e => {
436-
uploader.postError(4199, e.message);
437-
uploader.recordError(uploader.$t('error-generic') + e.message, blueprint.id.toString());
438-
})
461+
462+
// Check that file is a ZIP file.
463+
const uploadedFile = files[0]
464+
if (uploader.checkIsZip(uploadedFile) === false) {
465+
uploader.postError(4101, uploader.$t('error-not-zip'));
466+
uploader.recordError(uploader.$t('error-not-zip'), 'general');
467+
} else {
468+
469+
JSZip
470+
.loadAsync(uploadedFile)
471+
.then(z => {
472+
uploader.blueprints.forEach(blueprint => {
473+
let re = new RegExp(blueprint.regex_path);
474+
let reHasMatched = false;
475+
z.file(re).forEach(f => {
476+
reHasMatched = true;
477+
f
478+
.async("string")
479+
.then(c => uploader.processContent(c, blueprint))
480+
.catch(e => {
481+
uploader.postError(4199, e.message);
482+
uploader.recordError(uploader.$t('error-generic') + e.message, blueprint.id.toString());
483+
})
484+
})
485+
if (!reHasMatched) {
486+
uploader.postError(4180, uploader.$t('error-regex-not-matched'), blueprint.id);
487+
uploader.postError(4181, `Files in uploaded folder: ${Object.keys(z.files)}`, blueprint.id);
488+
uploader.recordError(uploader.$t('error-regex-not-matched'), blueprint.id.toString());
489+
}
439490
})
440-
if (!reHasMatched) {
441-
uploader.postError(4180, uploader.$t('error-regex-not-matched'), blueprint.id);
442-
uploader.postError(4181, `Files in uploaded folder: ${Object.keys(z.files)}`, blueprint.id);
443-
uploader.recordError(uploader.$t('error-regex-not-matched'), blueprint.id.toString());
491+
})
492+
.catch(e => {
493+
let myMess = '';
494+
let statusCode = 0;
495+
if (e.message.includes('zip') && e.message.includes('central')) {
496+
myMess = uploader.$t('error-not-zip');
497+
statusCode = 4101;
498+
} else if (e.message.includes('Corrupted zip')) {
499+
myMess = uploader.$t('error-zip-corrupted');
500+
statusCode = 4102;
501+
} else if (e.message.includes('Encrypted zip')) {
502+
myMess = uploader.$t('error-zip-encrypted');
503+
statusCode = 4103;
504+
} else {
505+
myMess = uploader.$t('error-generic') + e.message;
506+
statusCode = 4198;
444507
}
508+
uploader.postError(statusCode, e.message);
509+
uploader.recordError(myMess, 'general');
445510
})
446-
})
447-
.catch(e => {
448-
let myMess = '';
449-
let statusCode = 0;
450-
if (e.message.includes('zip') && e.message.includes('central')) {
451-
myMess = uploader.$t('error-not-zip');
452-
statusCode = 4101;
453-
} else if (e.message.includes('Corrupted zip')) {
454-
myMess = uploader.$t('error-zip-corrupted');
455-
statusCode = 4102;
456-
} else if (e.message.includes('Encrypted zip')) {
457-
myMess = uploader.$t('error-zip-encrypted');
458-
statusCode = 4103;
459-
} else {
460-
myMess = uploader.$t('error-generic') + e.message;
461-
statusCode = 4198;
462-
}
463-
uploader.postError(statusCode, e.message);
464-
uploader.recordError(myMess, 'general');
465-
})
511+
}
466512
}
467513
468514
// Procedure if supplied file is expected to be a single file.

0 commit comments

Comments
 (0)