XF\Http\Response->contentType() check not correct

digitalpoint

Well-known member
Affected version
2.2.9
Ran into an issue where a proxy system would throw an invalid content type error. It turns out it's because the contentType() method is doing a regex on what it considers a valid content type like so:
PHP:
if (!preg_match('#^[a-zA-Z0-9]+/[a-zA-Z0-9-+]+$#', $contentType))
{
   throw new \InvalidArgumentException('Invalid content type');
}

A Content-Type header can in fact also have a semi-colon with additional info (which was causing the problem for me). To work around it, I ended up dropping the semi-colon and anything after, but it's probably not the "right" thing to do.


Syntax
Content-Type: text/html; charset=UTF-8
Content-Type: multipart/form-data; boundary=something


Directives
media-type
The MIME type of the resource or the data.

charset
The character encoding standard.

boundary
For multipart entities the boundary directive is required. The directive consists of 1 to 70 characters from a set of characters (and not ending with white space) known to be very robust through email gateways. It is used to encapsulate the boundaries of the multiple parts of the message. Often, the header boundary is prepended with two dashes and the final boundary has two dashes appended at the end.
 

PaulB

Well-known member
There are also perfectly-valid content types that don't match that format, some of which could conceivably be dealt with by XenForo or its add-ons. For example, search this page for vnd.: https://www.iana.org/assignments/media-types/media-types.xhtml While most of those are obscure, some still see widespread use on the web and could appear on a vanilla XF installation. For example:
  • .m3u8 -> application/vnd.apple.mpegurl
  • .xls -> application/vnd.ms-excel
  • .ppt -> application/vnd.ms-powerpoint
  • .docx -> application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • .xlsx -> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • .pptx -> application/vnd.openxmlformats-officedocument.presentationml.presentation
 
Top