Skip to content

ownCloud-6.0.0alpha2 and files without file extension #5516

Closed

Description

@steffen-moser

ownCloud-6.0.0alpha1 and ownCloud-6.0.0alpha2 show a defective behavior under the following conditions:

  • ownCloud is hosted on a Unix system (e.g. Oracle Solaris) which has got a "/usr/bin/file" command that behaves differently than the GNU file command (i.e. as no possibility to analyze the MIME type)
  • and a file without an file extension containing random binary data is uploaded.

If both conditions occur, I get the following error message from ownCloud:

An exception occurred while executing 'INSERT INTO "oc_mimetypes"("mimetype") VALUES(?)': SQLSTATE[23000]: Integrity constraint violation: 19 column mimetype is not unique

The folder which contains the file isn't accessible anymore.

The problem is most probably caused in the function detect() in "/lib/private/files/type/detection.php". There the following command is called:

 $fp = popen("file -b --mime-type $path 2>/dev/null", "r");

Solaris' /usr/bin/file command doesn't understand these command line options. As far as I know, it doesn't even offer the possibility to report a file's mime type.

Steps to reproduce:

  1. Install ownCloud-6.0.0beta1 on a Oracle Solaris 11.1 host.
  2. Upload a testfile via web frontend to owncloud which contains, for example, 128 MB of random data (created, for example, with the following command: dd if=/dev/urandom of=/dev/Testfile_128_MB bs=1024 count=131072). It is important that the file has no file extension (e.g. "bin").

Note:
The problem does not occur with ownCloud-5.0.12 on the same Solaris 11.1 server. The problem does not occur when the file's name is changed to a name with an extension. The problem does not occur when a file is uploaded which contains plain ASCII text and has no file extension, either.

Activity

  1. VicDeo commented on Oct 24, 2013

    @VicDeo
    Contributor

    @steffen-moser Speaking frankly file utility is only a hackish fallback in case fileinfo PHP extension is missing.
    It is highly recommended to have this extension enabled http://www.php.net/manual/en/book.fileinfo.php

  2. VicDeo commented on Oct 24, 2013

    @VicDeo
    Contributor

    Might be already fixed in beta1 with 3d5e229

  3. steffen-moser commented on Oct 29, 2013

    @steffen-moser
    Author

    Might be already fixed in beta1 with 3d5e229

    The duplicate problem is indeed fixed in beta1, so I don't get the SQL Exception anymore.

    Nevertheless, the misbehaving Solaris file utility is a problem as it overwrites the mime type with an empty string. PHP fileinfo is included in PHP-5.4.20 as it has been compiled with "--enable-fileinfo" (and a "php -m" also confirms that it is there).

    Let's have a look at detection.php:

    public function detect($path) {
           $isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
    
           if (@is_dir($path)) {
                   // directories are easy
                   return "httpd/unix-directory";
           }
    
           $mimeType = $this->detectPath($path);
    
           if ($mimeType === 'application/octet-stream' and function_exists('finfo_open')
                            and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
           ) {
                   $info = @strtolower(finfo_file($finfo, $path));
                   if ($info) {
                           $mimeType = substr($info, 0, strpos($info, ';'));
                           \OC_Log::write('core', "finfo: " . $mimeType, \OC_Log::INFO);
                   }
                   finfo_close($finfo);
           }
           if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) {
                   // use mime magic extension if available
                   $mimeType = mime_content_type($path);
                   \OC_Log::write('core', "mimeType: " . $mimeType, \OC_Log::INFO);
           }
           if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {
                   // it looks like we have a 'file' command,
                   // lets see if it does have mime support
                   $path = escapeshellarg($path);
                   $fp = popen("file -b --mime-type $path 2>/dev/null", "r");
                   $reply = fgets($fp);
                   pclose($fp);
    
                   //trim the newline
                   $mimeType = trim($reply);
                   \OC_Log::write('core', "file: " . $mimeType, \OC_Log::INFO);
    
           }
           return $mimeType;
    }

    As you can see, I've added three OC_Log outputs. When uploading a binary file without an extension, I get the following outputs in the log (with alpha1, alpha2 as well as with beta1):

    {"app":"core","message":"finfo: application\/octet-stream","level":1,"time":"2013-10-28T21:48:54+00:00"}
    {"app":"core","message":"mimeType: application\/octet-stream","level":1,"time":"2013-10-28T21:48:54+00:00"}
    {"app":"core","message":"file: ","level":1,"time":"2013-10-28T21:48:54+00:00"}
    

    So the mime type "application/octet-stream" is given back by finfo_file() and by mime_content_type(). But in the last if clause, it gets overwritten with the empty string as Solaris' file utility doesn't behave as expected by ownCloud. When doing a "file -b --mime-type $path 2>/dev/null" in Solaris, I always get the empty string as a result because "file -b --mime-type $path" only produces an error:

    file: illegal option -- b
    file: illegal option -- mime-type
    usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...
           file [-dh] [-M mfile] [-m mfile] -f ffile
           file -i [-h] [-f ffile] file ...
           file -i [-h] -f ffile
           file -c [-d] [-M mfile] [-m mfile]
    

    As a result, ownCloud works with the empty string for the mime type.

    In alpha1 and alpha2 this leads to the SQL Exception which I mentioned. In beta1 this doesn't result in the SQL Exception anymore, but the mime type of a binary file without an extension will stay the empty string (instead of "application/octet-stream"). This doesn't seem to affect ownCloud at all, but in my opinion it is quite inelegant to see that "application/octet-stream" is actually overwritten by "". I'd prefer that the sanity of the file utility is checked before being executed. At least, it should not overwrite an existing mime type by "".

  4. DeepDiver1975 commented on Oct 29, 2013

    @DeepDiver1975
    Contributor

    I'd suggest to return the mimetype detected by finfo_file.

    Executing the file tool should be a fallback only.

    @icewind1991 @bantu suggestions? 

  5. bantu commented on Oct 29, 2013

    @bantu

    @DeepDiver1975 I agree with

    [...] in my opinion it is quite inelegant to see that "application/octet-stream" is actually overwritten by "". I'd prefer that the sanity of the file utility is checked before being executed. At least, it should not overwrite an existing mime type by "".

    and

    I'd suggest to return the mimetype detected by finfo_file

  6. ghost assigned on Oct 29, 2013
  7. DeepDiver1975 commented on Oct 29, 2013

    @DeepDiver1975
    Contributor

    @bantu can I ask you to submit a pull request for this? Thanks a lot!

    This will have a major performance improvement as well from my understanding as described in #5084

    @karlitschek FYI

  8. DeepDiver1975 commented on Oct 29, 2013

    @DeepDiver1975
    Contributor

    @bantu this might be a valid solution of oc7 - not for oc6 - let's just add that single line of code which returns the mime type 😉 THX

  9. bantu commented on Oct 29, 2013

    @bantu

    @steffen-moser Just out of curiousity, what is your database system? According to #5474 an attempt to insert an empty string as a mimetype should fail.

  10. DeepDiver1975 commented on Oct 29, 2013

    @DeepDiver1975
    Contributor

    According to #5474 an attempt to insert an empty string as a mimetype should fail.

    afaik only on Oracle due to different handling of null and empty string

  11. bantu commented on Oct 29, 2013

    @bantu

    afaik only on Oracle due to different handling of null and empty string

    Argh. Yes, I forgot to add "... on Oracle".

  12. locked as resolved and limited conversation to collaborators on Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions