@@ -4,12 +4,22 @@ const fs = require('fs')
44const Router = require ( 'express' ) . Router
55const formidable = require ( 'formidable' )
66
7+ const readChunk = require ( 'read-chunk' )
8+ const imageType = require ( 'image-type' )
9+
710const config = require ( '../config' )
811const logger = require ( '../logger' )
912const response = require ( '../response' )
1013
1114const imageRouter = module . exports = Router ( )
1215
16+ function checkImageValid ( filepath ) {
17+ const supported = [ 'png' , 'jpg' , 'jpeg' , 'bmp' , 'tif' , 'tiff' , 'gif' ]
18+ const buffer = readChunk . sync ( filepath , 0 , 12 )
19+ const type = imageType ( buffer )
20+ return type && supported . some ( e => e === type . ext )
21+ }
22+
1323// upload image
1424imageRouter . post ( '/uploadimage' , function ( req , res ) {
1525 var form = new formidable . IncomingForm ( )
@@ -24,6 +34,10 @@ imageRouter.post('/uploadimage', function (req, res) {
2434 logger . info ( 'SERVER received uploadimage: ' + JSON . stringify ( files . image ) )
2535 }
2636
37+ if ( ! checkImageValid ( files . image . path ) ) {
38+ return response . errorForbidden ( req , res )
39+ }
40+
2741 const uploadProvider = require ( './' + config . imageUploadType )
2842 uploadProvider . uploadImage ( files . image . path , function ( err , url ) {
2943 // remove temporary upload file, and ignore any error
0 commit comments