Simplify checkPageValidity()

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2023-12-06 00:06:47 +01:00
parent 4fb335e960
commit 2ab75067f8
3 changed files with 17 additions and 38 deletions

View File

@ -8245,11 +8245,6 @@ parameters:
count: 1
path: src/Core.php
-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
count: 3
path: src/Core.php
-
message: "#^Cannot access offset string on mixed\\.$#"
count: 8

View File

@ -157,37 +157,25 @@ class Core
}
/**
* Checks given $page against given $allowList and returns true if valid
* it optionally ignores query parameters in $page (script.php?ignored)
*
* @param string $page page to check
* @param mixed[] $allowList allow list to check page against
* @param bool $include whether the page is going to be included
* Checks if the given $page is index.php and returns true if valid.
* It ignores query parameters in $page (script.php?ignored)
*/
public static function checkPageValidity(string $page, array $allowList = [], bool $include = false): bool
public static function checkPageValidity(string $page): bool
{
if ($allowList === []) {
$allowList = ['index.php'];
}
if ($page === '') {
return false;
}
if (in_array($page, $allowList)) {
if ($page === 'index.php') {
return true;
}
if ($include) {
return false;
}
$newPage = mb_substr(
$page,
0,
(int) mb_strpos($page . '?', '?'),
);
if (in_array($newPage, $allowList)) {
if ($newPage === 'index.php') {
return true;
}
@ -198,7 +186,7 @@ class Core
(int) mb_strpos($newPage . '?', '?'),
);
return in_array($newPage, $allowList);
return $newPage === 'index.php';
}
/**

View File

@ -237,33 +237,29 @@ class CoreTest extends AbstractTestCase
/**
* Test for Core::checkPageValidity
*
* @param string $page Page
* @param string[] $allowList Allow list
* @param bool $include whether the page is going to be included
* @param bool $expected Expected value
* @param string $page Page
* @param bool $expected Expected value
*/
#[DataProvider('providerTestGotoNowhere')]
public function testGotoNowhere(string $page, array $allowList, bool $include, bool $expected): void
public function testGotoNowhere(string $page, bool $expected): void
{
$this->assertSame($expected, Core::checkPageValidity($page, $allowList, $include));
$this->assertSame($expected, Core::checkPageValidity($page));
}
/**
* Data provider for testGotoNowhere
*
* @return array<array{string, string[], bool, bool}>
* @return array<array{string, bool}>
*/
public static function providerTestGotoNowhere(): array
{
return [
['', [], false, false],
['', [], true, false],
['shell.php', ['index.php'], false, false],
['shell.php', ['index.php'], true, false],
['index.php?sql.php&test=true', ['index.php'], false, true],
['index.php?sql.php&test=true', ['index.php'], true, false],
['index.php%3Fsql.php%26test%3Dtrue', ['index.php'], false, true],
['index.php%3Fsql.php%26test%3Dtrue', ['index.php'], true, false],
['', false],
['shell.php', false],
['index.php?sql.php&test=true', true],
['shell.php?sql.php&test=true', false],
['index.php%3Fsql.php%26test%3Dtrue', true],
['shell.php%3Fsql.php%26test%3Dtrue', false],
];
}