|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
7
|
|
|
use whm\Smoke\Http\Response; |
|
8
|
|
|
use whm\Smoke\Rules\Rule; |
|
9
|
|
|
use whm\Smoke\Rules\StandardRule; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This rule checks if a https request contains any insecure includes via http. |
|
13
|
|
|
*/ |
|
14
|
|
|
class PasswordSecureTransferRule extends StandardRule |
|
15
|
|
|
{ |
|
16
|
|
|
protected $contentTypes = array('text/html'); |
|
17
|
|
|
|
|
18
|
|
|
private $knownIdentifier = array(); |
|
19
|
|
|
|
|
20
|
|
|
protected function doValidation(ResponseInterface $response) |
|
21
|
|
|
{ |
|
22
|
|
|
$crawler = new Crawler((string)$response->getBody()); |
|
23
|
|
|
$actionNodes = $crawler->filterXPath('//form[//input[@type="password"]]'); |
|
24
|
|
|
|
|
25
|
|
|
$url = (string)$response->getUri(); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
foreach ($actionNodes as $node) { |
|
28
|
|
|
$action = $node->getAttribute('action'); |
|
29
|
|
|
|
|
30
|
|
|
if (strpos($action, 'https://') === 0) { |
|
31
|
|
|
continue; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$fullPath = $node->tagName; |
|
35
|
|
|
$parent = $node->parentNode; |
|
36
|
|
|
|
|
37
|
|
|
while ($parent = $parent->parentNode) { |
|
38
|
|
|
if (property_exists($parent, 'tagName')) { |
|
39
|
|
|
$fullPath = $parent->tagName . '/' . $fullPath; |
|
40
|
|
|
} else { |
|
41
|
|
|
break; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (in_array($fullPath, $this->knownIdentifier, true)) { |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->knownIdentifier[] = $fullPath; |
|
50
|
|
|
|
|
51
|
|
|
$this->assert(strpos($url, 'https://') !== false, 'Password is transferred insecure using HTTP.'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
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: