From 5fae2fc7a13a155fec607dc0c985a73a82e26b97 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Mon, 5 Jul 2021 17:25:09 +0700 Subject: [PATCH] MDL-71922 file: Enhance endless recursion requests protection --- repository/url/lib.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/repository/url/lib.php b/repository/url/lib.php index b3664a193b0..7b548e6be63 100644 --- a/repository/url/lib.php +++ b/repository/url/lib.php @@ -36,7 +36,11 @@ require_once(__DIR__.'/locallib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class repository_url extends repository { + /** @var int Maximum time of recursion. */ + const MAX_RECURSION_TIME = 5; var $processedfiles = array(); + /** @var int Recursion counter. */ + var $recursioncounter = 0; /** * @param int $repositoryid @@ -127,7 +131,16 @@ EOD; $url = htmlspecialchars_decode(url_to_absolute($baseurl, $relativeurl)); } if (in_array($url, $this->processedfiles)) { - // avoid endless recursion + // Avoid endless recursion for the same URL with same parameters. + return; + } + // Remove the query string before check. + $recursioncheckurl = preg_replace('/\?.*/', '', $url); + if (in_array($recursioncheckurl, $this->processedfiles)) { + $this->recursioncounter++; + } + if ($this->recursioncounter >= self::MAX_RECURSION_TIME) { + // Avoid endless recursion for the same URL with different parameters. return; } $this->processedfiles[] = $url;