|
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 Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use whm\Html\Document; |
|
14
|
|
|
use whm\Smoke\Rules\CheckResult; |
|
15
|
|
|
use whm\Smoke\Rules\Rule; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This rule checks if a https document uses http (insecure) ressources. |
|
19
|
|
|
*/ |
|
20
|
|
|
class InsecureContentRule implements Rule |
|
21
|
|
|
{ |
|
22
|
|
|
private $excludedFiles = []; |
|
23
|
|
|
|
|
24
|
|
|
public function init($excludedFiles = []) |
|
25
|
|
|
{ |
|
26
|
|
|
foreach ($excludedFiles as $excludedFile) { |
|
27
|
|
|
$this->excludedFiles[] = $excludedFile['file']; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function validate(ResponseInterface $response) |
|
32
|
|
|
{ |
|
33
|
|
|
$uri = $response->getUri(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
if ('https' !== $uri->getScheme()) { |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$htmlDocument = new Document((string)$response->getBody()); |
|
40
|
|
|
|
|
41
|
|
|
$resources = $htmlDocument->getDependencies($uri, false); |
|
42
|
|
|
|
|
43
|
|
|
$unsecures = array(); |
|
44
|
|
|
|
|
45
|
|
|
foreach ($resources as $resource) { |
|
46
|
|
|
if ($resource->getScheme() && 'https' !== $resource->getScheme()) { |
|
47
|
|
|
$excluded = false; |
|
48
|
|
|
foreach ($this->excludedFiles as $excludedFile) { |
|
49
|
|
|
if (preg_match('*' . $excludedFile . '*', (string)$resource)) { |
|
50
|
|
|
$excluded = true; |
|
51
|
|
|
break; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
if (!$excluded) { |
|
55
|
|
|
$unsecures[] = $resource; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (count($unsecures) > 0) { |
|
61
|
|
|
$message = 'At least one dependency was found on a secure url, that was transfered insecure.<ul>'; |
|
62
|
|
|
foreach ($unsecures as $unsecure) { |
|
63
|
|
|
$message .= '<li>' . (string)$unsecure . '</li>'; |
|
64
|
|
|
} |
|
65
|
|
|
$message .= '</ul>'; |
|
66
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, $message, count($unsecures)); |
|
67
|
|
|
} else { |
|
68
|
|
|
return new CheckResult(CheckResult::STATUS_SUCCESS, 'No http element on that https url found.', 0); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: