Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 7c29598

Browse files
Merge pull request #608 from cagdas001/fix-269
fix(upload): add mime-type check at file uploads
2 parents df28334 + 43f8f8c commit 7c29598

File tree

4 files changed

+115
-9
lines changed

4 files changed

+115
-9
lines changed

client/src/components/Upload/Initial/index.jsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ export default function Initial({ onError, onUpload, templateId }) {
4040
};
4141

4242
const upload = (files) => {
43-
const allowedMineTypes = [
44-
"application/vnd.ms-excel",
45-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
46-
"text/csv",
47-
];
48-
if (files && files[0] && allowedMineTypes.indexOf(files[0].type) !== -1)
49-
onUpload(files[0]);
50-
else setInvalidFileExtension(true);
43+
const allowedExtensions = ["xls", "xlsx", "csv"];
44+
if (files && files[0]) {
45+
const ext = files[0].name.split(".").pop();
46+
if (allowedExtensions.includes(ext.toLowerCase())) {
47+
onUpload(files[0]);
48+
} else {
49+
setInvalidFileExtension(true);
50+
}
51+
}
5152
};
5253

5354
let contentStyle = style.content;
@@ -85,6 +86,7 @@ export default function Initial({ onError, onUpload, templateId }) {
8586
onChange={(e) => upload(e.target.files)}
8687
ref={fileInputRef}
8788
type="file"
89+
accept=".xls,.xlsx,.csv"
8890
/>
8991
<img src={spreadsheetIcon} alt="icon" />
9092
<div className={style.label1}>
@@ -99,7 +101,11 @@ export default function Initial({ onError, onUpload, templateId }) {
99101
</span>
100102
</div>
101103
<div className={style.label2}>Supports XLS, XLSX or CSV file</div>
102-
<div className={style.label3} onClick={downloadTemplate} disabled={isDisabledDownload}>
104+
<div
105+
className={style.label3}
106+
onClick={downloadTemplate}
107+
disabled={isDisabledDownload}
108+
>
103109
Download Import Template (.XLSX)
104110
</div>
105111
</>

package-lock.json

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dynamoose": "^1.8.0",
3131
"express": "^4.17.1",
3232
"express-interceptor": "^1.2.0",
33+
"file-type": "^14.6.2",
3334
"get-parameter-names": "^0.3.0",
3435
"http-status-codes": "^1.3.0",
3536
"js-yaml": "^3.14.0",

src/services/UploadService.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@ const _ = require('lodash')
55
const Joi = require('joi')
66
const config = require('config')
77
const { v4: uuid } = require('uuid')
8+
const FileType = require('file-type')
9+
const errors = require('../common/errors')
810
const helper = require('../common/helper')
911
const logger = require('../common/logger')
1012

13+
/**
14+
* Checks the type of uploaded file and ensures it's allowed.
15+
* @param {Object} upload The uploaded file
16+
*/
17+
async function ensureFileTypeIsValid(upload) {
18+
const allowedExtensions = ['xls', 'xlsx', 'csv']
19+
const allowedMimeTypes = [
20+
'application/vnd.ms-excel',
21+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
22+
'text/csv',
23+
]
24+
const fileType = await FileType.fromBuffer(upload.buffer)
25+
const fileExt = upload.originalname.split('.').pop().toLowerCase()
26+
27+
const isValidMimeType = fileType && _.includes(allowedMimeTypes, fileType.mime)
28+
const isValidExt = _.includes(allowedExtensions, fileExt)
29+
const isAllowed = fileType !== undefined ? isValidMimeType : isValidExt
30+
if (isAllowed === false) {
31+
throw new errors.ForbiddenError(`You are allowed to upload only ${_.join(allowedExtensions, ',')} types.`)
32+
}
33+
}
34+
1135
/**
1236
* Get upload entity by id.
1337
* @param {String} id the upload id
@@ -31,6 +55,7 @@ getEntity.schema = {
3155
* @returns {Object} the created upload
3256
*/
3357
async function create (authUser, upload, data) {
58+
await ensureFileTypeIsValid(upload)
3459
const id = uuid()
3560
// upload file to s3 under uploads folder
3661
const objectKey = await helper.uploadToS3(config.UPLOAD_S3_BUCKET, upload, `uploads/${id}`)

0 commit comments

Comments
 (0)