1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\SecurityChecker\Tests; |
4
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\SecurityChecker\Models\SecurityAlert; |
6
|
|
|
use BringYourOwnIdeas\SecurityChecker\Tasks\SecurityAlertCheckTask; |
7
|
|
|
use SensioLabs\Security\Result; |
8
|
|
|
use SensioLabs\Security\SecurityChecker; |
9
|
|
|
use SilverStripe\Control\HTTPRequest; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
12
|
|
|
|
13
|
|
|
class SecurityAlertCheckTaskTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected $usesDatabase = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var SecurityAlertCheckTask |
19
|
|
|
*/ |
20
|
|
|
private $checkTask; |
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
QueuedJobService::config()->set('use_shutdown_function', false); |
27
|
|
|
|
28
|
|
|
$securityCheckerMock = $this->getSecurityCheckerMock(); |
29
|
|
|
$checkTask = new SecurityAlertCheckTask; |
30
|
|
|
$checkTask->setSecurityChecker($securityCheckerMock); |
31
|
|
|
$this->checkTask = $checkTask; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Run task buffering the output as so that it does not interfere with the test harness output. |
36
|
|
|
* |
37
|
|
|
* @param null|HTTPRequest $request |
38
|
|
|
* |
39
|
|
|
* @return string buffered output |
40
|
|
|
*/ |
41
|
|
|
private function runTask($request = null) |
42
|
|
|
{ |
43
|
|
|
ob_start(); |
44
|
|
|
$this->checkTask->run($request); |
45
|
|
|
return ob_get_clean(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* provide a mock to remove dependency on external service |
50
|
|
|
*/ |
51
|
|
|
protected function getSecurityCheckerMock($empty = false) |
52
|
|
|
{ |
53
|
|
|
// Mock info comes from SensioLabs API docs example output, |
54
|
|
|
// and a real (test) silverstripe/installer 3.2.0 installation |
55
|
|
|
// (using the aforementioned API) |
56
|
|
|
$mockOutput = <<<CVENOTICE |
57
|
|
|
{ |
58
|
|
|
"symfony\/symfony": { |
59
|
|
|
"version": "2.1.x-dev", |
60
|
|
|
"advisories": { |
61
|
|
|
"symfony\/symfony\/CVE-2013-1397.yaml": { |
62
|
|
|
"title": "Ability to enable\/disable object support in YAML parsing and dumping", |
63
|
|
|
"link": "http:\/\/symfony.com\/blog\/security-release-symfony-2-0-22-and-2-1-7-released", |
64
|
|
|
"cve": "CVE-2013-1397" |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
}, |
68
|
|
|
"silverstripe\/framework": { |
69
|
|
|
"version": "3.2.0", |
70
|
|
|
"advisories": { |
71
|
|
|
"silverstripe\/framework\/SS-2016-002-1.yaml": { |
72
|
|
|
"title": "SS-2016-002: CSRF vulnerability in GridFieldAddExistingAutocompleter", |
73
|
|
|
"link": "https:\/\/www.silverstripe.org\/download\/security-releases\/ss-2016-002\/", |
74
|
|
|
"cve": "" |
75
|
|
|
}, |
76
|
|
|
"silverstripe\/framework\/SS-2016-003-1.yaml": { |
77
|
|
|
"title": "SS-2016-003: Hostname, IP and Protocol Spoofing through HTTP Headers", |
78
|
|
|
"link": "https:\/\/www.silverstripe.org\/download\/security-releases\/ss-2016-003\/", |
79
|
|
|
"cve": "" |
80
|
|
|
}, |
81
|
|
|
"silverstripe\/framework\/SS-2015-028-1.yaml": { |
82
|
|
|
"title": "SS-2015-028: Missing security check on dev\/build\/defaults", |
83
|
|
|
"link": "https:\/\/www.silverstripe.org\/download\/security-releases\/ss-2015-028\/", |
84
|
|
|
"cve": "" |
85
|
|
|
}, |
86
|
|
|
"silverstripe\/framework\/SS-2015-027-1.yaml": { |
87
|
|
|
"title": "SS-2015-027: HtmlEditor embed url sanitisation", |
88
|
|
|
"link": "https:\/\/www.silverstripe.org\/download\/security-releases\/ss-2015-027\/", |
89
|
|
|
"cve": "" |
90
|
|
|
}, |
91
|
|
|
"silverstripe\/framework\/SS-2015-026-1.yaml": { |
92
|
|
|
"title": "SS-2015-026: Form field validation message XSS vulnerability", |
93
|
|
|
"link": "https:\/\/www.silverstripe.org\/download\/security-releases\/ss-2015-026\/", |
94
|
|
|
"cve": "" |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
CVENOTICE; |
100
|
|
|
|
101
|
|
|
$securityCheckerMock = $this->getMockBuilder(SecurityChecker::class)->setMethods(['check'])->getMock(); |
102
|
|
|
$securityCheckerMock->expects($this->any())->method('check')->will($this->returnValue( |
103
|
|
|
$empty ? new Result(0, '{}', 'json') : new Result(6, $mockOutput, 'json') |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
return $securityCheckerMock; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testUpdatesAreSaved() |
110
|
|
|
{ |
111
|
|
|
$preCheck = SecurityAlert::get(); |
112
|
|
|
$this->assertCount(0, $preCheck, 'database is empty to begin with'); |
113
|
|
|
|
114
|
|
|
$this->runTask(); |
115
|
|
|
|
116
|
|
|
$postCheck = SecurityAlert::get(); |
117
|
|
|
$this->assertCount(6, $postCheck, 'SecurityAlert has been stored'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testNoDuplicates() |
121
|
|
|
{ |
122
|
|
|
$this->runTask(); |
123
|
|
|
|
124
|
|
|
$postCheck = SecurityAlert::get(); |
125
|
|
|
$this->assertCount(6, $postCheck, 'SecurityAlert has been stored'); |
126
|
|
|
|
127
|
|
|
$this->runTask(); |
128
|
|
|
|
129
|
|
|
$postCheck = SecurityAlert::get(); |
130
|
|
|
$this->assertCount(6, $postCheck, 'The SecurityAlert isn\'t stored twice.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testSecurityAlertRemovals() |
134
|
|
|
{ |
135
|
|
|
$this->runTask(); |
136
|
|
|
|
137
|
|
|
$preCheck = SecurityAlert::get(); |
138
|
|
|
$this->assertCount(6, $preCheck, 'database has stored SecurityAlerts'); |
139
|
|
|
|
140
|
|
|
$securityCheckerMock = $this->getSecurityCheckerMock(true); |
141
|
|
|
$this->checkTask->setSecurityChecker($securityCheckerMock); |
142
|
|
|
|
143
|
|
|
$this->runTask(); |
144
|
|
|
|
145
|
|
|
$postCheck = SecurityAlert::get(); |
146
|
|
|
$this->assertCount(0, $postCheck, 'database is empty to finish with'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testIdentifierSetsFromTitleIfCVEIsNotSet() |
150
|
|
|
{ |
151
|
|
|
$this->runTask(); |
152
|
|
|
$frameworkAlert = SecurityAlert::get() |
153
|
|
|
->filter('PackageName', 'silverstripe/framework') |
154
|
|
|
->first(); |
155
|
|
|
$this->assertNotEmpty($frameworkAlert->Identifier); |
156
|
|
|
$this->assertRegExp('/^SS-201[56]-\d{3}$/', $frameworkAlert->Identifier); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|