nextcloud / nextcloud/server

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

Open
#40,556 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

0. Needs triage 27-feedback bug feature: files feature: object storage feature: sharing hotspot: file transfer performance
Dominant language
PHP
Stars
36.9k
Forks
5.2k
Avg merge
2d 3h
Merged PRs (30d)
713

Description

⚠️ 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/private/Files/View.php around readfilePart(), especially the fread() call at line 438, and trace the public-share download path shown in the stack trace. Reproduce with curl using a trailing byte range against a large file on Swift object storage; done means the request returns the expected file range instead of a 500 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, cloud
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.