1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Core;
use PhpMyAdmin\Header;
use ReflectionProperty;
use function gmdate;
use const DATE_RFC1123;
/**
* @covers \PhpMyAdmin\Header
* @group medium
*/
class HeaderTest extends AbstractTestCase
{
/**
* Configures global environment.
*/
protected function setUp(): void
{
parent::setUp();
parent::setTheme();
parent::setLanguage();
$GLOBALS['server'] = 0;
$GLOBALS['message'] = 'phpmyadminmessage';
$GLOBALS['PMA_PHP_SELF'] = Core::getenv('PHP_SELF');
$GLOBALS['server'] = 'server';
$GLOBALS['db'] = 'db';
$GLOBALS['table'] = '';
parent::setGlobalConfig();
$GLOBALS['cfg']['Servers'] = [];
$GLOBALS['cfg']['Server']['DisableIS'] = false;
$GLOBALS['cfg']['Server']['verbose'] = 'verbose host';
$GLOBALS['cfg']['Server']['pmadb'] = '';
$GLOBALS['cfg']['Server']['user'] = '';
$GLOBALS['cfg']['Server']['auth_type'] = 'cookie';
}
/**
* Test for disable
*/
public function testDisable(): void
{
$header = new Header();
$header->disable();
$this->assertEquals(
'',
$header->getDisplay()
);
}
/**
* Test for enable
*/
public function testEnable(): void
{
$GLOBALS['server'] = 0;
$header = new Header();
$this->assertStringContainsString(
'<title>phpMyAdmin</title>',
$header->getDisplay()
);
}
/**
* Test for Set BodyId
*/
public function testSetBodyId(): void
{
$header = new Header();
$header->setBodyId('PMA_header_id');
$this->assertStringContainsString(
'PMA_header_id',
$header->getDisplay()
);
}
/**
* Test for Get JsParams
*/
public function testGetJsParams(): void
{
$header = new Header();
$this->assertArrayHasKey(
'common_query',
$header->getJsParams()
);
}
/**
* Test for Get JsParamsCode
*/
public function testGetJsParamsCode(): void
{
$header = new Header();
$this->assertStringContainsString(
'CommonParams.setAll',
$header->getJsParamsCode()
);
}
/**
* Test for Get Message
*/
public function testGetMessage(): void
{
$header = new Header();
$this->assertStringContainsString(
'phpmyadminmessage',
$header->getMessage()
);
}
/**
* Test for Disable Warnings
*/
public function testDisableWarnings(): void
{
$reflection = new ReflectionProperty(Header::class, 'warningsEnabled');
$reflection->setAccessible(true);
$header = new Header();
$header->disableWarnings();
$this->assertFalse($reflection->getValue($header));
}
/**
* @param string|bool $frameOptions
*
* @covers \PhpMyAdmin\Core::getNoCacheHeaders
* @dataProvider providerForTestGetHttpHeaders
*/
public function testGetHttpHeaders(
$frameOptions,
string $cspAllow,
string $privateKey,
string $publicKey,
string $captchaCsp,
?string $expectedFrameOptions,
string $expectedCsp,
string $expectedXCsp,
string $expectedWebKitCsp
): void {
global $cfg;
$header = new Header();
$date = (string) gmdate(DATE_RFC1123);
$cfg['AllowThirdPartyFraming'] = $frameOptions;
$cfg['CSPAllow'] = $cspAllow;
$cfg['CaptchaLoginPrivateKey'] = $privateKey;
$cfg['CaptchaLoginPublicKey'] = $publicKey;
$cfg['CaptchaCsp'] = $captchaCsp;
$expected = [
'X-Frame-Options' => $expectedFrameOptions,
'Referrer-Policy' => 'no-referrer',
'Content-Security-Policy' => $expectedCsp,
'X-Content-Security-Policy' => $expectedXCsp,
'X-WebKit-CSP' => $expectedWebKitCsp,
'X-XSS-Protection' => '1; mode=block',
'X-Content-Type-Options' => 'nosniff',
'X-Permitted-Cross-Domain-Policies' => 'none',
'X-Robots-Tag' => 'noindex, nofollow',
'Expires' => $date,
'Cache-Control' => 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0',
'Pragma' => 'no-cache',
'Last-Modified' => $date,
'Content-Type' => 'text/html; charset=utf-8',
];
if ($expectedFrameOptions === null) {
unset($expected['X-Frame-Options']);
}
$headers = $this->callFunction($header, Header::class, 'getHttpHeaders', []);
$this->assertSame($expected, $headers);
}
public function providerForTestGetHttpHeaders(): array
{
return [
[
'1',
'',
'',
'',
'',
'DENY',
'default-src \'self\' ;script-src \'self\' \'unsafe-inline\' \'unsafe-eval\' ;'
. 'style-src \'self\' \'unsafe-inline\' ;img-src \'self\' data: *.tile.openstreetmap.org;'
. 'object-src \'none\';',
'default-src \'self\' ;options inline-script eval-script;referrer no-referrer;'
. 'img-src \'self\' data: *.tile.openstreetmap.org;object-src \'none\';',
'default-src \'self\' ;script-src \'self\' \'unsafe-inline\' \'unsafe-eval\';'
. 'referrer no-referrer;style-src \'self\' \'unsafe-inline\' ;'
. 'img-src \'self\' data: *.tile.openstreetmap.org;object-src \'none\';',
],
[
'SameOrigin',
'example.com example.net',
'PrivateKey',
'PublicKey',
'captcha.tld csp.tld',
'SAMEORIGIN',
'default-src \'self\' captcha.tld csp.tld example.com example.net;'
. 'script-src \'self\' \'unsafe-inline\' \'unsafe-eval\' '
. 'captcha.tld csp.tld example.com example.net;'
. 'style-src \'self\' \'unsafe-inline\' captcha.tld csp.tld example.com example.net;'
. 'img-src \'self\' data: example.com example.net *.tile.openstreetmap.org captcha.tld csp.tld ;'
. 'object-src \'none\';',
'default-src \'self\' captcha.tld csp.tld example.com example.net;'
. 'options inline-script eval-script;referrer no-referrer;img-src \'self\' data: example.com '
. 'example.net *.tile.openstreetmap.org captcha.tld csp.tld ;object-src \'none\';',
'default-src \'self\' captcha.tld csp.tld example.com example.net;script-src \'self\' '
. 'captcha.tld csp.tld example.com example.net \'unsafe-inline\' \'unsafe-eval\';'
. 'referrer no-referrer;style-src \'self\' \'unsafe-inline\' captcha.tld csp.tld ;'
. 'img-src \'self\' data: example.com example.net *.tile.openstreetmap.org captcha.tld csp.tld ;'
. 'object-src \'none\';',
],
[
true,
'',
'PrivateKey',
'PublicKey',
'captcha.tld csp.tld',
null,
'default-src \'self\' captcha.tld csp.tld ;'
. 'script-src \'self\' \'unsafe-inline\' \'unsafe-eval\' captcha.tld csp.tld ;'
. 'style-src \'self\' \'unsafe-inline\' captcha.tld csp.tld ;'
. 'img-src \'self\' data: *.tile.openstreetmap.org captcha.tld csp.tld ;object-src \'none\';',
'default-src \'self\' captcha.tld csp.tld ;'
. 'options inline-script eval-script;referrer no-referrer;'
. 'img-src \'self\' data: *.tile.openstreetmap.org captcha.tld csp.tld ;object-src \'none\';',
'default-src \'self\' captcha.tld csp.tld ;'
. 'script-src \'self\' captcha.tld csp.tld \'unsafe-inline\' \'unsafe-eval\';'
. 'referrer no-referrer;style-src \'self\' \'unsafe-inline\' captcha.tld csp.tld ;'
. 'img-src \'self\' data: *.tile.openstreetmap.org captcha.tld csp.tld ;object-src \'none\';',
],
];
}
}
|