Skip to content

[2.x] Fix isBinary check to correctly indicate certain textual formats as being not binary #15900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion core/model/modx/modfilehandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,24 @@ public function isBinary($file) {

if (filesize($file) > 0 && class_exists('\finfo')) {
$finfo = new \finfo(FILEINFO_MIME);
$mimeType = strtolower($finfo->file($file));

return substr($finfo->file($file), 0, 4) !== 'text';
// Some mimetypes include a character set, e.g. application/json; charset=utf-8
// so we filter out the last part to make comparison easier
if (strpos($mimeType, ';') > 0) {
$mimeType = substr($mimeType, 0, strpos($mimeType, ';'));
}

return substr($mimeType, 0, 4) !== 'text'
&& !in_array($mimeType, array(
'application/json',
'application/ld+json',
'application/x-httpd-php', // also restricted by default based on extension
'application/x-sh',
'image/svg+xml',
'application/xhtml+xml',
'application/xml',
), true);
}

$fh = @fopen($file, 'r');
Expand Down