rubentalstra / bak-counter

counter for keeping track of the 'BAK'
GNU General Public License v3.0
2 stars 1 forks source link

File upload staat ook extensies toe die alleen toegestaande extensies bevatten door RegEx #135

Closed JorianWoltjer closed 6 months ago

JorianWoltjer commented 6 months ago

Tijdens het uploaden van een nieuwe profielfoto op /profile/updatePicture wordt de volgende "multer" configuratie gebruikt:

const multerUpload = multer({
    storage: multer.memoryStorage(),
    fileFilter: (req, file, cb) => {
        const allowedTypes = /jpeg|jpg|png|gif/;
        const isSupportedFile =
            allowedTypes.test(path.extname(file.originalname).toLowerCase()) &&
            allowedTypes.test(file.mimetype);
        if (isSupportedFile) {
            cb(null, true);
        } else {
            cb(new Error("Only image files (JPEG, JPG, PNG, GIF) are allowed!"), false); //! alart is not showing in the profile page
        }
    },
    limits: { fileSize: config.uploadLimits.fileSize },
});

Hier zouden /jpeg|jpg|png|gif/ alle 4 toegestaande extensies moeten definieren, maar tijdens de check wordt alleen allowedTypes.test() gebruikt. Deze checkt of een RegEx voorkomt in de string, niet of dit een volle match is. Dat betekent dat het mogelijk is om een profielfoto te uploaden met een bestandsextensie zoals .STARTpngEND, en deze wordt toegestaan omdat het png bevat.

------WebKitFormBoundarycQiR3fV8PtrVoCfJ
Content-Disposition: form-data; name="profilePicture"; filename="example.STARTpngEND"
Content-Type: image/png

‰PNG
...

Hoewel dit niet direct een security kwetsbaarheid is, kan het nog steeds netjes zijn om dit op te lossen aangezien de error zegt dat alleen "JPEG, JPG, PNG, GIF" toegestaan zijn.

rubentalstra commented 6 months ago

https://github.com/rubentalstra/bak-counter/commit/0ce18a32debe8089c5549d678708fe064d533827