Merge branch 'MDL-81306-main' of https://github.com/andrewnicols/moodle
This commit is contained in:
+2
-1
@@ -315,10 +315,11 @@ $CFG->admin = 'admin';
|
||||
// '/dataroot/' => $CFG->dataroot,
|
||||
// '/cachedir/' => '/var/www/moodle/cache', // for custom $CFG->cachedir locations
|
||||
// '/localcachedir/' => '/var/local/cache', // for custom $CFG->localcachedir locations
|
||||
// '/localrequestdir/' => '/tmp', // for custom $CFG->localrequestdir locations
|
||||
// '/tempdir/' => '/var/www/moodle/temp', // for custom $CFG->tempdir locations
|
||||
// '/filedir' => '/var/www/moodle/filedir', // for custom $CFG->filedir locations
|
||||
// );
|
||||
// Please note: It is *not* possible to use X-Sendfile with the per-request directory.
|
||||
// The directory is highly likely to have been deleted by the time the web server sends the file.
|
||||
//
|
||||
// YUI caching may be sometimes improved by slasharguments:
|
||||
// $CFG->yuislasharguments = 1;
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core;
|
||||
|
||||
/**
|
||||
* Tests for lib/xsendfilelib.php.
|
||||
*
|
||||
* Please note that the PHP CLI SAPI used by PHPUnit does not return headers so some tests would be pointless to run.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2024 Andrew Lyons <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers ::xsendfile
|
||||
*/
|
||||
final class xsendfilelib_test extends \advanced_testcase {
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/xsendfilelib.php');
|
||||
}
|
||||
|
||||
public function test_not_enabled(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure it is disabled.
|
||||
$CFG->xsendfile = '';
|
||||
|
||||
// Use a file that would otherwise pass.
|
||||
$this->assertFalse(xsendfile($CFG->dataroot . '/.htaccess'));
|
||||
}
|
||||
|
||||
public function test_file_not_found(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure it is disabled.
|
||||
$CFG->xsendfile = 'X-Accel-Redirect';
|
||||
|
||||
$this->assertFalse(xsendfile($CFG->dataroot . '/FILE_NOT_FOUND'));
|
||||
}
|
||||
|
||||
public function test_file_found_headers_sent(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure it is disabled.
|
||||
$CFG->xsendfile = 'X-Accel-Redirect';
|
||||
|
||||
// This is a weird ond - we can't explicitly send headers, but we know that phpunit does.
|
||||
$this->assertFalse(xsendfile($CFG->dataroot . '/.htaccess'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a file served from a request dir is not served.
|
||||
*
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function test_file_found_request_dir(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure it is disabled.
|
||||
$CFG->xsendfile = 'X-Accel-Redirect';
|
||||
$CFG->xsendfilealiases = [
|
||||
'/request/' => make_request_directory(),
|
||||
];
|
||||
|
||||
$dir = make_request_directory();
|
||||
$file = $dir . '/testfile.txt';
|
||||
file_put_contents($file, 'Hello, world!');
|
||||
|
||||
// Use a file that would otherwise pass.
|
||||
$this->assertFalse(xsendfile($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a file served from an aliased dir is served.
|
||||
*
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function test_nginx_accelerated(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure it is enabled.
|
||||
$CFG->xsendfile = 'X-Accel-Redirect';
|
||||
$CFG->xsendfilealiases = [
|
||||
'/my/moodle/alias/moodledata/' => $CFG->dataroot,
|
||||
];
|
||||
|
||||
$file = $CFG->dataroot . '/testfile.txt';
|
||||
file_put_contents($file, 'Hello, world!');
|
||||
|
||||
$this->assertTrue(xsendfile($file));
|
||||
|
||||
// Note: The `headers_list()` method does not work with the CLI SAPI.
|
||||
// We can use xdebug if it's enabled.
|
||||
// This is mostly to aid debugging as it is not common to have xdebug enabled during CI tests.
|
||||
if (extension_loaded('xdebug')) {
|
||||
$headers = xdebug_get_headers();
|
||||
$this->assertNotEmpty($headers);
|
||||
$this->assertContains('X-Accel-Redirect: /my/moodle/alias/moodledata/testfile.txt', $headers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a file served from an unknown alias is not served.
|
||||
*
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function test_nginx_no_alias(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure it is enabled.
|
||||
$CFG->xsendfile = 'X-Accel-Redirect';
|
||||
$CFG->xsendfilealiases = [
|
||||
'/my/moodle/alias/requestdir/' => make_request_directory(),
|
||||
];
|
||||
|
||||
$file = $CFG->dataroot . '/testfile.txt';
|
||||
file_put_contents($file, 'Hello, world!');
|
||||
|
||||
$this->assertFalse(xsendfile($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an alias dir which doesn't exist is ignored.
|
||||
*
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function test_nginx_alias_dir_not_found(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$filedir = "{$CFG->dataroot}/non/existent/directory";
|
||||
|
||||
// Ensure it is enabled.
|
||||
$CFG->xsendfile = 'X-Accel-Redirect';
|
||||
$CFG->xsendfilealiases = [
|
||||
'/my/moodle/alias/' => $filedir,
|
||||
];
|
||||
|
||||
$file = $CFG->dataroot . '/testfile.txt';
|
||||
file_put_contents($file, 'Hello, world!');
|
||||
|
||||
$this->assertFalse(xsendfile($file));
|
||||
}
|
||||
}
|
||||
+20
-14
@@ -17,17 +17,16 @@
|
||||
/**
|
||||
* X-Sendfile support
|
||||
*
|
||||
* @package core_files
|
||||
* @package core
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
//NOTE: do not verify MOODLE_INTERNAL here, this is used from themes too
|
||||
|
||||
/**
|
||||
* Serve file using X-Sendfile header, this needs special server module
|
||||
* or configuration. Please make sure that all headers are already sent
|
||||
* and the all access control checks passed.
|
||||
* Serve files using the X-Sendfile header.
|
||||
*
|
||||
* This needs special server module or configuration.
|
||||
* Please make sure that all headers are already sent and the all access control checks passed.
|
||||
*
|
||||
* @param string $filepath
|
||||
* @return bool success
|
||||
@@ -49,19 +48,26 @@ function xsendfile($filepath) {
|
||||
|
||||
$filepath = realpath($filepath);
|
||||
|
||||
$localrequestdir = realpath($CFG->localrequestdir);
|
||||
if (str_contains($filepath, $localrequestdir)) {
|
||||
// Do not serve files from local request directory using xsendfile.
|
||||
// They are likely to be removed before xsendfile can serve them.
|
||||
return false;
|
||||
}
|
||||
|
||||
$aliased = false;
|
||||
if (!empty($CFG->xsendfilealiases) and is_array($CFG->xsendfilealiases)) {
|
||||
foreach ($CFG->xsendfilealiases as $alias=>$dir) {
|
||||
if (!empty($CFG->xsendfilealiases) && is_array($CFG->xsendfilealiases)) {
|
||||
foreach ($CFG->xsendfilealiases as $alias => $dir) {
|
||||
$dir = realpath($dir);
|
||||
if ($dir === false) {
|
||||
continue;
|
||||
}
|
||||
if (substr($dir, -1) !== DIRECTORY_SEPARATOR) {
|
||||
// add trailing dir separator
|
||||
// Add trailing dir separator.
|
||||
$dir .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
if (strpos($filepath, $dir) === 0) {
|
||||
$filepath = $alias.substr($filepath, strlen($dir));
|
||||
if (str_starts_with($filepath, $dir)) {
|
||||
$filepath = $alias . substr($filepath, strlen($dir));
|
||||
$aliased = true;
|
||||
break;
|
||||
}
|
||||
@@ -69,12 +75,12 @@ function xsendfile($filepath) {
|
||||
}
|
||||
|
||||
if ($CFG->xsendfile === 'X-LIGHTTPD-send-file') {
|
||||
// http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file says 1.4 it does not support byteserving
|
||||
// Version 1.4.40 and earlier do not support byte serving.
|
||||
// See http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file for more information.
|
||||
header('Accept-Ranges: none');
|
||||
|
||||
} else if ($CFG->xsendfile === 'X-Accel-Redirect') {
|
||||
// http://wiki.nginx.org/XSendfile
|
||||
// Nginx requires paths relative to aliases, you need to specify them in config.php
|
||||
// See http://wiki.nginx.org/XSendfile for more information.
|
||||
if (!$aliased) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user