Menu

Stop trusting file extensions: validating uploads by content (with ffmpeg)
📰

Stop trusting file extensions: validating uploads by content (with ffmpeg)

DEV Community: node·Usman Farooqi·about 13 hours ago
#yr2puBM5
#devtonodejs#devto#dev#extm3u#file
Reading 0:00
15s threshold

A user of my free tool uploaded a recording named voice.m4a and got "This doesn't look like a real .m4a file." The file was fine. It was a WebM/Opus recording that had been saved with an .m4a extension. My upload check was wrong, not the user.

This is a short write-up of what I changed, and why file extensions are the wrong thing to trust.

The bug

My converter (Node + ffmpeg) checked the first bytes of an upload against the file's extension:

// before: decide by extension, then check one signature case "m4a" : return head . slice ( 4 , 8 ) . toString ( ) === "ftyp" ;

Enter fullscreen mode

Exit fullscreen mode

Real files are renamed all the time: WebM saved as .m4a , an MP3 named .wav , a screen recording shared as .mp4 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.