Skip to content

[Bug]: Getting large file using Range: header leads to 500 Internal error due zero length parametr #40556

@vpecinka

Description

@vpecinka

⚠️ This issue respects the following points: ⚠️

Bug description

While investigating different behaviour between Chrome (failing) and Safari/Firefox (ok) when previewing shared video movie I found this bug which was resolved when I apply the following patch:

--- nextcloud/lib/private/Files/View.php.orig	2023-09-21 13:35:48.541745141 +0200
+++ nextcloud/lib/private/Files/View.php	2023-09-21 13:43:40.655166758 +0200
@@ -430,7 +430,7 @@
 				// forward file handle via chunked fread because fseek seem to have failed
 
 				$end = $from + 1;
-				while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) {
+				while (!feof($handle) && ftell($handle) < $end && ftell($handle) != $from) {
 					$len = $from - ftell($handle);
 					if ($len > $chunkSize) {
 						$len = $chunkSize;

Without this patch (thus with ftell($handle) !== $from and comparing types as well) I'm getting the following error. The error occured when using range: xxxx- header, in my case:

#  curl -i  -H "range: bytes=7143424-" http://localhost/s/9sYxySRojQkAbH9/download 
HTTP/1.1 500 Internal Server Error
:
:

The error:

{
  "reqId": "5JhSOjTTJG3wSIrW0zR9",
  "level": 3,
  "time": "2023-09-21T11:01:18+00:00",
  "remoteAddr": "127.0.0.1",
  "user": "--",
  "app": "index",
  "method": "GET",
  "url": "/s/J9wkisGiGMSioQr/download?path=&files=",
  "message": "fread(): Argument #2 ($length) must be greater than 0 in file '/www/nextcloud/lib/private/Files/View.php' line 438",
  "userAgent": "curl/7.88.1",
  "version": "27.0.2.1",
  "exception": {
    "Exception": "Exception",
    "Message": "fread(): Argument #2 ($length) must be greater than 0 in file '/www/nextcloud/lib/private/Files/View.php' line 438",
    "Code": 0,
    "Trace": [
      {
        "file": "/www/nextcloud/lib/private/AppFramework/App.php",
        "line": 183,
        "function": "dispatch",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->"
      },
      {
        "file": "/www/nextcloud/lib/private/Route/Router.php",
        "line": 315,
        "function": "main",
        "class": "OC\\AppFramework\\App",
        "type": "::"
      },
      {
        "file": "/www/nextcloud/lib/base.php",
        "line": 1071,
        "function": "match",
        "class": "OC\\Route\\Router",
        "type": "->"
      },
      {
        "file": "/www/nextcloud/index.php",
        "line": 36,
        "function": "handleRequest",
        "class": "OC",
        "type": "::"
      }
    ],
    "File": "/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
    "Line": 169,
    "Previous": {
      "Exception": "ValueError",
      "Message": "fread(): Argument #2 ($length) must be greater than 0",
      "Code": 0,
      "Trace": [
        {
          "file": "/www/nextcloud/lib/private/Files/View.php",
          "line": 438,
          "function": "fread"
        },
        {
          "file": "/www/nextcloud/lib/private/legacy/OC_Files.php",
          "line": 358,
          "function": "readfilePart",
          "class": "OC\\Files\\View",
          "type": "->"
        },
        {
          "file": "/www/nextcloud/lib/private/legacy/OC_Files.php",
          "line": 123,
          "function": "getSingleFile",
          "class": "OC_Files",
          "type": "::"
        },
        {
          "file": "/www/nextcloud/apps/files_sharing/lib/Controller/ShareController.php",
          "line": 525,
          "function": "get",
          "class": "OC_Files",
          "type": "::"
        },
        {
          "file": "/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
          "line": 230,
          "function": "downloadShare",
          "class": "OCA\\Files_Sharing\\Controller\\ShareController",
          "type": "->"
        },
        {
          "file": "/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
          "line": 137,
          "function": "executeController",
          "class": "OC\\AppFramework\\Http\\Dispatcher",
          "type": "->"
        },
        {
          "file": "/www/nextcloud/lib/private/AppFramework/App.php",
          "line": 183,
          "function": "dispatch",
          "class": "OC\\AppFramework\\Http\\Dispatcher",
          "type": "->"
        },
        {
          "file": "/www/nextcloud/lib/private/Route/Router.php",
          "line": 315,
          "function": "main",
          "class": "OC\\AppFramework\\App",
          "type": "::"
        },
        {
          "file": "/www/nextcloud/lib/base.php",
          "line": 1071,
          "function": "match",
          "class": "OC\\Route\\Router",
          "type": "->"
        },
        {
          "file": "/www/nextcloud/index.php",
          "line": 36,
          "function": "handleRequest",
          "class": "OC",
          "type": "::"
        }
      ],
      "File": "/www/nextcloud/lib/private/Files/View.php",
      "Line": 438
    },
    "CustomMessage": "--"
  }
}

Need to say, that I'm using Swift object store and maybe ftell() returns different type or $from is passed as different type. Didn't investigate further and didn't try with local filesystem storage.

Steps to reproduce

  1. have installation with Swift object store
  2. make public link to a file (better large file) - let's say XYZ
  3. call curl -i -H 'range: bytes=something_before_end-' 'http://localhost/s/XYZ/download'

Expected behavior

With the patched View.php as above it works smoothly and expected file part (bytes range) is downloaded.

Installation method

Community Manual installation with Archive

Nextcloud Server version

27

Operating system

Debian/Ubuntu

PHP engine version

PHP 8.2

Web server

Nginx

Database engine version

MySQL

Is this bug present after an update or on a fresh install?

None

Are you using the Nextcloud Server Encryption module?

Encryption is Disabled

What user-backends are you using?

  • Default user-backend (database)
  • LDAP/ Active Directory
  • SSO - SAML
  • Other

Configuration report

No response

List of activated Apps

No response

Nextcloud Signing status

No response

Nextcloud Logs

No response

Additional info

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions