MIME Type detection when sending attachments
Ideally I would like to be able to use sup to communicate with the Linux Kernel Mailing List. This requires being able to send plaintext attachments. I believe that sup currently uses the MIME::Types::type_for function to determine a encoding type from the file extension. Unfortunately this returns Nil for the filename "Makefile" which results in the attachment being base64 encoded. I'm able to work around the issue by modifying line 87 of /sup/lib/sup/util.rb to t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("txt").first however this isn't an appropriate long term solution. It would be nice if sup used a (subjectively) better approach to determining file type. One option may be the ruby-filemagic gem which seems to provide correct results for the Makefile scenario.
Agreed, would this be something you could implement? Is the filemagic gem activly developed?
Also, does the new mime-types gem (#182) handle this better?
Unfortunately my Ruby skills are nonexistant, so I wouldn't be able to implement it any time soon. Having said that I've found a suitable work around using the :r option in vim to insert the attachment inline, so I'll close the issue.
Cheers.
please keep the issue open, as it is, after all, still an issue that someone may be able to work on. and, if i understand it correctly, it may even be an easier one that a person new to sup could get started with.
I think I get the same bug here: https://github.com/sup-heliotrope/sup/issues/369
Did you get to send your patch with sup ?
Regards, Guillaume.
filemagic seems to be active, in fact there was a pre release yesterday. However I'm wary of depending on non-ruby stuff.
I tried with mime-types 2.4.3, it didn't recognize the .patch extension. So we need some other way to detect this.
Here's an idea:
- Try with
MIME::Types.type_forfirst - Otherwise scan the whole file. If it's text only, set the attachment to
text/plain - Otherwise set it to default,
application-x-msdos-program(I'm not even sure it's a sensible default, though)
Roughly, something like that, maybe:
mime_type = MIME::Types.type_for(bfn).first
begin
File.open(bfn) do |f|
f.each_codepoint{}
end
rescue ArgumentError # We couldn't iterate over codepoints, it's not a text
mime_type = MIME::Types.type_for("exe").first
end
Excerpts from Matthieu Rakotojaona's message of January 13, 2015 22:39:
filemagic seems to be active, in fact there was a pre release yesterday. However I'm wary of depending on non-ruby stuff.
I tried with mime-types 2.4.3, it didn't recognize the
.patchextension. So we need some other way to detect this.Here's an idea:
- Try with
MIME::Types.type_forfirst- Otherwise scan the whole file. If it's text only, set the attachment to
text/plain- Otherwise set it to default,
application-x-msdos-program(I'm not even sure it's a sensible default, though)
I used application/octet-stream as default for potentially mangeled mime-types, see earlier security issue: the mime-type is passed to the shell when opening, allowing an attacker to leave a command in the mime-type.