Issue #3016814 by jhedstrom, peximo, alexpott, jungle, scuba_fly, mr.baileys, FrancescoQ, cgoffin, Petr Illek, fabio84: Don't trigger hook_file_download when no file is requested

merge-requests/2419/head
Alex Pott 2020-04-05 13:02:24 +01:00
parent 81c6dc6218
commit 24e1692a9f
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 14 additions and 1 deletions

View File

@ -72,11 +72,14 @@ class DownloadTest extends FileManagedTestBase {
$url = file_create_url($file->getFileUri());
// Set file_test access header to allow the download.
file_test_reset();
file_test_set_return('download', ['x-foo' => 'Bar']);
$this->drupalGet($url);
$this->assertEqual($this->drupalGetHeader('x-foo'), 'Bar', 'Found header set by file_test module on private download.');
$this->assertNull($this->drupalGetHeader('x-drupal-cache'), 'Page cache is disabled on private file download.');
$this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.');
// Ensure hook_file_download is fired correctly.
$this->assertEquals($file->getFileUri(), \Drupal::state()->get('file_test.results')['download'][0][0]);
// Test that the file transferred correctly.
$this->assertSame($contents, $this->getSession()->getPage()->getContent(), 'Contents of the file are correct.');
@ -88,9 +91,19 @@ class DownloadTest extends FileManagedTestBase {
$this->assertSame(403, $response->getStatusCode(), 'Correctly denied access to a file when file_test sets the header to -1.');
// Try non-existent file.
file_test_reset();
$url = file_create_url('private://' . $this->randomMachineName());
$response = $http_client->head($url, ['http_errors' => FALSE]);
$this->assertSame(404, $response->getStatusCode(), 'Correctly returned 404 response for a non-existent file.');
// Assert that hook_file_download is not called.
$this->assertEquals([], \Drupal::state()->get('file_test.results')['download']);
// Try requesting the private file url without a file specified.
file_test_reset();
$this->drupalGet('/system/files');
$this->assertSession()->statusCodeEquals(404);
// Assert that hook_file_download is not called.
$this->assertEquals([], \Drupal::state()->get('file_test.results')['download']);
}
/**

View File

@ -71,7 +71,7 @@ class FileDownloadController extends ControllerBase {
// Merge remaining path arguments into relative file path.
$uri = $scheme . '://' . $target;
if ($this->streamWrapperManager->isValidScheme($scheme) && file_exists($uri)) {
if ($this->streamWrapperManager->isValidScheme($scheme) && is_file($uri)) {
// Let other modules provide headers and controls access to the file.
$headers = $this->moduleHandler()->invokeAll('file_download', [$uri]);