1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Hook\Diff\Action; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Console\IO\NullIO; |
16
|
|
|
use CaptainHook\App\Hook\Debug; |
17
|
|
|
use CaptainHook\App\Mockery as AppMockery; |
18
|
|
|
use CaptainHook\App\Console\IO\Mockery as IOMockery; |
19
|
|
|
use CaptainHook\Secrets\Regex\Supplier\Aws; |
20
|
|
|
use CaptainHook\Secrets\Regex\Supplier\GitHub; |
21
|
|
|
use CaptainHook\Secrets\Regex\Supplier\Google; |
22
|
|
|
use CaptainHook\Secrets\Regex\Supplier\Password; |
23
|
|
|
use CaptainHook\Secrets\Regex\Supplier\Stripe; |
24
|
|
|
use Exception; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
use SebastianFeldmann\Git\Diff\Change; |
27
|
|
|
use SebastianFeldmann\Git\Diff\File; |
28
|
|
|
use SebastianFeldmann\Git\Diff\Line; |
29
|
|
|
|
30
|
|
|
class BlockSecretsTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
use AppMockery; |
33
|
|
|
use IOMockery; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tests BlockSecrets::getRestriction |
37
|
|
|
*/ |
38
|
|
|
public function testConstraint(): void |
39
|
|
|
{ |
40
|
|
|
$this->assertTrue(BlockSecrets::getRestriction()->isApplicableFor('pre-commit')); |
41
|
|
|
$this->assertTrue(BlockSecrets::getRestriction()->isApplicableFor('pre-push')); |
42
|
|
|
$this->assertFalse(BlockSecrets::getRestriction()->isApplicableFor('post-merge')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tests BlockSecrets::execute |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function testExecuteSuccess(): void |
51
|
|
|
{ |
52
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
53
|
|
|
$diffOperator->method('compareIndexTo')->willReturn( |
54
|
|
|
$this->createChanges('fail.txt', ['foo', 'bar', 'baz']) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$io = new NullIO(); |
58
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
59
|
|
|
$action = new Config\Action(BlockSecrets::class, ['entropyThreshold' => 10.0]); |
60
|
|
|
$repo = $this->createRepositoryMock(); |
61
|
|
|
$repo->method('getDiffOperator')->willReturn($diffOperator); |
62
|
|
|
|
63
|
|
|
$standard = new BlockSecrets(); |
64
|
|
|
$standard->execute($config, $io, $repo, $action); |
65
|
|
|
|
66
|
|
|
$this->assertTrue(true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tests BlockSecrets::execute |
71
|
|
|
* |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
public function testExecuteSuccessOnPush(): void |
75
|
|
|
{ |
76
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
77
|
|
|
$diffOperator->method('compareIndexTo')->willReturn( |
78
|
|
|
$this->createChanges('fail.txt', ['foo', 'bar', 'baz']) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$io = $this->createIOMock(); |
82
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
83
|
|
|
$io->method('getStandardInput')->willReturn(['main 12345 main 98765']); |
84
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
85
|
|
|
$action = new Config\Action(BlockSecrets::class, ['entropyThreshold' => 10.0]); |
86
|
|
|
$repo = $this->createRepositoryMock(); |
87
|
|
|
$repo->method('getDiffOperator')->willReturn($diffOperator); |
88
|
|
|
|
89
|
|
|
$standard = new BlockSecrets(); |
90
|
|
|
$standard->execute($config, $io, $repo, $action); |
91
|
|
|
|
92
|
|
|
$this->assertTrue(true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests BlockSecrets::execute |
98
|
|
|
* |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
|
public function testExecuteSuccessWithEntropyCheck(): void |
102
|
|
|
{ |
103
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
104
|
|
|
$diffOperator->method('compareIndexTo')->willReturn( |
105
|
|
|
$this->createChanges('fail.php', ['foo', 'bar', 'baz']) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$io = new NullIO(); |
109
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
110
|
|
|
$action = new Config\Action(BlockSecrets::class, ['entropyThreshold' => 10.0]); |
111
|
|
|
$repo = $this->createRepositoryMock(); |
112
|
|
|
$repo->method('getDiffOperator')->willReturn($diffOperator); |
113
|
|
|
|
114
|
|
|
$standard = new BlockSecrets(); |
115
|
|
|
$standard->execute($config, $io, $repo, $action); |
116
|
|
|
|
117
|
|
|
$this->assertTrue(true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Tests BlockSecrets::execute |
122
|
|
|
* |
123
|
|
|
* @throws \Exception |
124
|
|
|
*/ |
125
|
|
|
public function testExecuteFailure(): void |
126
|
|
|
{ |
127
|
|
|
$this->expectException(Exception::class); |
128
|
|
|
|
129
|
|
|
$options = [ |
130
|
|
|
'suppliers' => [ |
131
|
|
|
Aws::class, |
132
|
|
|
Password::class, |
133
|
|
|
Google::class, |
134
|
|
|
GitHub::class, |
135
|
|
|
Stripe::class |
136
|
|
|
] |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
140
|
|
|
$diffOperator->method('compareIndexTo')->willReturn( |
141
|
|
|
$this->createChanges('fail.txt', ['foo', 'AKIAIOSFODNN7EXAMPLE', 'bar']) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$io = new NullIO(); |
145
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
146
|
|
|
$action = new Config\Action(BlockSecrets::class, $options); |
147
|
|
|
$repo = $this->createRepositoryMock(); |
148
|
|
|
$repo->method('getDiffOperator')->willReturn($diffOperator); |
149
|
|
|
|
150
|
|
|
$standard = new BlockSecrets(); |
151
|
|
|
$standard->execute($config, $io, $repo, $action); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Tests BlockSecrets::execute |
156
|
|
|
* |
157
|
|
|
* @throws \Exception |
158
|
|
|
*/ |
159
|
|
|
public function testExecuteFailureByEntropy(): void |
160
|
|
|
{ |
161
|
|
|
$this->expectException(Exception::class); |
162
|
|
|
|
163
|
|
|
$options = ['entropyThreshold' => 1]; |
164
|
|
|
|
165
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
166
|
|
|
$diffOperator->method('compareIndexTo')->willReturn( |
167
|
|
|
$this->createChanges('fail.php', ['foo', '$password = "5ad7$-9Op0-x2§d"', 'bar']) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$io = new NullIO(); |
171
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
172
|
|
|
$action = new Config\Action(BlockSecrets::class, $options); |
173
|
|
|
$repo = $this->createRepositoryMock(); |
174
|
|
|
$repo->method('getDiffOperator')->willReturn($diffOperator); |
175
|
|
|
|
176
|
|
|
$standard = new BlockSecrets(); |
177
|
|
|
$standard->execute($config, $io, $repo, $action); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Tests BlockSecrets::execute |
182
|
|
|
* |
183
|
|
|
* @throws \Exception |
184
|
|
|
*/ |
185
|
|
|
public function testExecuteFailedByEntropyButAllowed(): void |
186
|
|
|
{ |
187
|
|
|
$options = ['entropyThreshold' => 1, 'allowed' => ['#5ad7\\$\\-9Op0\\-x2§d#']]; |
188
|
|
|
|
189
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
190
|
|
|
$diffOperator->expects($this->once())->method('compareIndexTo')->willReturn( |
191
|
|
|
$this->createChanges('fail.php', ['foo', '$password = "5ad7$-9Op0-x2§d"', 'bar']) |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$io = new NullIO(); |
195
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
196
|
|
|
$action = new Config\Action(BlockSecrets::class, $options); |
197
|
|
|
$repo = $this->createRepositoryMock(); |
198
|
|
|
$repo->expects($this->once())->method('getDiffOperator')->willReturn($diffOperator); |
199
|
|
|
|
200
|
|
|
$standard = new BlockSecrets(); |
201
|
|
|
$standard->execute($config, $io, $repo, $action); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Tests BlockSecrets::execute |
206
|
|
|
* |
207
|
|
|
* @throws \Exception |
208
|
|
|
*/ |
209
|
|
|
public function testExecuteProviderNotFound(): void |
210
|
|
|
{ |
211
|
|
|
$this->expectException(Exception::class); |
212
|
|
|
|
213
|
|
|
$options = [ |
214
|
|
|
'suppliers' => [ |
215
|
|
|
'Fooooooooooooo' |
216
|
|
|
] |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
$io = new NullIO(); |
220
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
221
|
|
|
$action = new Config\Action(BlockSecrets::class, $options); |
222
|
|
|
$repo = $this->createRepositoryMock(); |
223
|
|
|
$repo->method('getDiffOperator')->willReturn($this->createGitDiffOperator()); |
224
|
|
|
|
225
|
|
|
$standard = new BlockSecrets(); |
226
|
|
|
$standard->execute($config, $io, $repo, $action); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Tests BlockSecrets::execute |
231
|
|
|
* |
232
|
|
|
* @throws \Exception |
233
|
|
|
*/ |
234
|
|
|
public function testExecuteInvalidProvider(): void |
235
|
|
|
{ |
236
|
|
|
$this->expectException(Exception::class); |
237
|
|
|
|
238
|
|
|
$options = [ |
239
|
|
|
'suppliers' => [ |
240
|
|
|
Debug::class |
241
|
|
|
] |
242
|
|
|
]; |
243
|
|
|
|
244
|
|
|
$io = new NullIO(); |
245
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
246
|
|
|
$action = new Config\Action(BlockSecrets::class, $options); |
247
|
|
|
$repo = $this->createRepositoryMock(); |
248
|
|
|
$repo->method('getDiffOperator')->willReturn($this->createGitDiffOperator()); |
249
|
|
|
|
250
|
|
|
$standard = new BlockSecrets(); |
251
|
|
|
$standard->execute($config, $io, $repo, $action); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Tests BlockSecrets::execute |
256
|
|
|
*/ |
257
|
|
|
public function testExecuteAllow(): void |
258
|
|
|
{ |
259
|
|
|
$diffOperator = $this->createGitDiffOperator(); |
260
|
|
|
$diffOperator->method('compareIndexTo')->willReturn( |
261
|
|
|
$this->createChanges('fail.txt', ['foo', 'bar']) |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
$io = new NullIO(); |
265
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
266
|
|
|
$action = new Config\Action(BlockSecrets::class, [ |
267
|
|
|
'blocked' => ['#f[a-z]+#'], |
268
|
|
|
'allowed' => ['#foo#'] |
269
|
|
|
]); |
270
|
|
|
$repo = $this->createRepositoryMock(); |
271
|
|
|
$repo->expects($this->atLeast(1))->method('getDiffOperator')->willReturn($diffOperator); |
272
|
|
|
|
273
|
|
|
$standard = new BlockSecrets(); |
274
|
|
|
$standard->execute($config, $io, $repo, $action); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $fileName |
279
|
|
|
* @param array<string> $lines |
280
|
|
|
* @return array<File> |
281
|
|
|
*/ |
282
|
|
|
private function createChanges(string $fileName, array $lines): array |
283
|
|
|
{ |
284
|
|
|
$diffChange = new Change('+123,456 -789,012', ''); |
285
|
|
|
foreach ($lines as $line) { |
286
|
|
|
$diffChange->addLine(new Line('added', $line)); |
287
|
|
|
} |
288
|
|
|
$diffFile = new File($fileName, 'new'); |
289
|
|
|
$diffFile->addChange($diffChange); |
290
|
|
|
|
291
|
|
|
return [$diffFile]; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|