Is HttpPostedFile.ContentType a flawless way to validate an uploaded file?

前端 未结 3 1708
太阳男子
太阳男子 2020-12-19 05:42

I want to validate the file type to make sure the user is uploading an image of type JPEG, GIF, or PNG. Instead of checking the file extension, I figured using HttpPostedFil

相关标签:
3条回答
  • 2020-12-19 06:14

    To reliably know the content type, you might want to look into Content Type sniffing, for instance:

    http://suika.fam.cx/www/markup/html/whatpm/Whatpm/ContentType.html

    This tries to determine the content type of the file by examining the first few bytes.

    0 讨论(0)
  • 2020-12-19 06:17

    Both using the extension and the HTTP headers are equally unreliable, as they both can be faked with ease, either by a malicious attacker using raw HTTP requests, or by an innocent browser user picking an incorrectly named file. If you want to be certain, you must crack the file open and analyze the content, there is no other way.

    0 讨论(0)
  • 2020-12-19 06:24

    Using the extension is probably safer. The ContentType is sent in the http request from the client. If you test for the extension, the user can change the extension of an exe to jpg, but it won't run as an exe.

    0 讨论(0)
提交回复
热议问题