Skip to content

Commit

Permalink
bug #28278 [HttpFoundation] Fix unprepared BinaryFileResponse sends e…
Browse files Browse the repository at this point in the history
…mpty file (wackymole)

This PR was merged into the 2.8 branch.

Discussion
----------

[HttpFoundation] Fix unprepared BinaryFileResponse sends empty file

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes, with the exception of preexisting, unrelated failures
| Fixed tickets | #28237
| License       | MIT
| Doc PR        |

When you call `BinaryFileResponse#sendContent()` without first calling `prepare()` the response is sent but the contents are empty. `prepare()` properly initializes the `$maxlen` and `$offset` properties. However, `sendContent()` doesn't do any sanity checking, and so, uses the uninitialized properties. This causes `stream_copy_to_stream()` to copy empty contents and the file that is sent, to contain nothing.

This change initializes the properties at definition instead of in `prepare()`.

> Additionally:
> - Bug fixes must be submitted against the lowest branch where they apply

~I'm not sure how early this bug exists, or how far back to go. I'll check to see if 2.7 and 2.8 are affected and report back.~

Commits
-------

dba8687 Instantiate $offset and $maxlen at definition
  • Loading branch information
fabpot committed Aug 27, 2018
2 parents 2554ad0 + dba8687 commit b547855
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Expand Up @@ -31,8 +31,8 @@ class BinaryFileResponse extends Response
* @var File
*/
protected $file;
protected $offset;
protected $maxlen;
protected $offset = 0;
protected $maxlen = -1;
protected $deleteFileAfterSend = false;

/**
Expand Down
Expand Up @@ -206,6 +206,19 @@ public function provideFullFileRanges()
);
}

public function testUnpreparedResponseSendsFullFile()
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);

$data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');

$this->expectOutputString($data);
$response = clone $response;
$response->sendContent();

$this->assertEquals(200, $response->getStatusCode());
}

/**
* @dataProvider provideInvalidRanges
*/
Expand Down

0 comments on commit b547855

Please sign in to comment.