|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This rule will find external ressources on a https transfered page that are insecure (http). |
|
5
|
|
|
* |
|
6
|
|
|
* @author Nils Langner <[email protected]> |
|
7
|
|
|
* @inspiredBy Christian Haller |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace whm\Smoke\Rules\Html; |
|
11
|
|
|
|
|
12
|
|
|
use phm\HttpWebdriverClient\Http\Response\UriAwareResponse; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use whm\Html\Document; |
|
15
|
|
|
use whm\Smoke\Rules\CheckResult; |
|
16
|
|
|
use whm\Smoke\Rules\Rule; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* This rule checks if a https document uses http (insecure) resources. |
|
20
|
|
|
*/ |
|
21
|
|
|
class InsecureContentRule implements Rule |
|
22
|
|
|
{ |
|
23
|
|
|
private $excludedFiles = []; |
|
24
|
|
|
|
|
25
|
|
|
private $nonStrictFiles = ['\.png', '\.jpg', '\.jpeg', '\.ico', '\.bmp', '\.gif']; |
|
26
|
|
|
|
|
27
|
|
|
public function init($excludedFiles = [], $nonStrictMode = false) |
|
28
|
|
|
{ |
|
29
|
|
|
foreach ($excludedFiles as $excludedFile) { |
|
30
|
|
|
$this->excludedFiles[] = $excludedFile['file']; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($nonStrictMode == 'on' || $nonStrictMode == true) { |
|
|
|
|
|
|
34
|
|
|
$this->excludedFiles = array_merge($this->excludedFiles, $this->nonStrictFiles); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function validate(ResponseInterface $response) |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var UriAwareResponse $response */ |
|
41
|
|
|
$uri = $response->getUri(); |
|
42
|
|
|
|
|
43
|
|
|
if ('https' !== $uri->getScheme()) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$htmlDocument = new Document((string)$response->getBody()); |
|
48
|
|
|
|
|
49
|
|
|
$resources = $htmlDocument->getDependencies($uri, false); |
|
50
|
|
|
|
|
51
|
|
|
$unsecures = array(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($resources as $resource) { |
|
54
|
|
|
if ($resource->getScheme() && 'https' !== $resource->getScheme()) { |
|
55
|
|
|
$excluded = false; |
|
56
|
|
|
foreach ($this->excludedFiles as $excludedFile) { |
|
57
|
|
|
if (preg_match('~' . $excludedFile . '~', (string)$resource)) { |
|
58
|
|
|
$excluded = true; |
|
59
|
|
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
if (!$excluded) { |
|
63
|
|
|
$unsecures[] = $resource; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (count($unsecures) > 0) { |
|
69
|
|
|
$message = 'At least one dependency was found on a secure url, that was transfered insecure.<ul>'; |
|
70
|
|
|
foreach ($unsecures as $unsecure) { |
|
71
|
|
|
$message .= '<li>' . (string)$unsecure . '</li>'; |
|
72
|
|
|
} |
|
73
|
|
|
$message .= '</ul>'; |
|
74
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, $message, count($unsecures)); |
|
75
|
|
|
} else { |
|
76
|
|
|
return new CheckResult(CheckResult::STATUS_SUCCESS, 'No insecure http element found.', 0); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.