I am using the JavaScript FileReader to upload files in my web application. I want to restrict the accepted file types to only .pem
and .key
. However, I noticed that I am able to upload .crt
and .cer
files as well. Is there something wrong with my implementation? I am using the following code:
<input
type="file"
accept=".key, .pem"
/>
2
Answers
The
accept
attribute doesn’t work in the way it seems you expect it to. Here’s a relevant excerpt from MDN’s documentation:The other answer is right that you shouldn’t rely on the
accept
attribute to safely filter the file’s types, but one might note that this is actually a Chrome Bug (probably macOS specific).According to the specs, if the token in the list of comma separated tokens is
However it seems that Chrome does look at the OS registry instead, and thus if on your OS
.pem
files are registered along with.cer
files, they’ll both be accepted even if you did only setaccept=".pem"
.This is the case on my macOS system where an
lsregister -dump
givesAs you can see, all these extensions share the same
type id
, and thus in my Chrome they’re all equivalent. This is not the case e.g. in Firefox, which does respect the specs and really filter by extension.(Note: I don’t think I’ve got anything that affected these extensions.)
Unfortunately there isn’t much you can do about it, since using the MIME type (
application/x-x509-ca-cert
) would have the exact same result. The best would be to star the linked issue and hope they can fix it some day.