Skip to content

Commit 19f494d

Browse files
authored
Merge pull request #1789 from galaxian85/bugfix/invalid-filename-cause-crash
FIX: server crash when filename too long
2 parents 30fe18d + 1ab2a36 commit 19f494d

File tree

3 files changed

+89
-13
lines changed

3 files changed

+89
-13
lines changed

lib/imageRouter/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ const response = require('../response')
1616
const imageRouter = module.exports = Router()
1717

1818
function checkImageValid (filepath) {
19-
const buffer = readChunk.sync(filepath, 0, 12)
20-
/** @type {{ ext: string, mime: string } | null} */
21-
const mimetypeFromBuf = imageType(buffer)
22-
const mimeTypeFromExt = mime.lookup(path.extname(filepath))
19+
try {
20+
const buffer = readChunk.sync(filepath, 0, 12)
21+
/** @type {{ ext: string, mime: string } | null} */
22+
const mimetypeFromBuf = imageType(buffer)
23+
const mimeTypeFromExt = mime.lookup(path.extname(filepath))
2324

24-
return mimetypeFromBuf && config.allowedUploadMimeTypes.includes(mimetypeFromBuf.mime) &&
25-
mimeTypeFromExt && config.allowedUploadMimeTypes.includes(mimeTypeFromExt)
25+
return mimetypeFromBuf && config.allowedUploadMimeTypes.includes(mimetypeFromBuf.mime) &&
26+
mimeTypeFromExt && config.allowedUploadMimeTypes.includes(mimeTypeFromExt)
27+
} catch (err) {
28+
logger.error(err)
29+
return false
30+
}
2631
}
2732

2833
// upload image

package-lock.json

Lines changed: 77 additions & 6 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"ejs": "~2.6.2",
5555
"express": "~4.17.1",
5656
"express-session": "~1.16.2",
57-
"formidable": "~1.2.1",
57+
"formidable": "^2.1.1",
5858
"helmet": "~3.20.0",
5959
"https-proxy-agent": "^3.0.1",
6060
"i18n": "~0.8.3",

0 commit comments

Comments
 (0)