Skip to content

Commit 89a5fff

Browse files
Merge branch 'master' into 6.0
2 parents f3eaea5 + 48a46d2 commit 89a5fff

File tree

8 files changed

+189
-6
lines changed

8 files changed

+189
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ If you are behind php 7.0 you can still use phpbu version [4.0.10](https://phar.
6363
+ Text file
6464
+ Json file
6565
+ Mail
66+
+ Prometheus
6667
+ Telegram
6768
+ Webhook
6869

src/Backup/Crypter/OpenSSL.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use phpbu\App\Backup\Restore\Plan;
55
use phpbu\App\Backup\Target;
66
use phpbu\App\Cli\Executable;
7+
use phpbu\App\Result;
78
use phpbu\App\Util;
89

910
/**
@@ -54,6 +55,79 @@ class OpenSSL extends Abstraction implements Simulator, Restorable
5455
*/
5556
private $keepUncrypted;
5657

58+
private $weakAlgorithms = [
59+
'rc2' => true,
60+
'rc2-40' => true,
61+
'rc2-64' => true,
62+
'rc2-128' => true,
63+
'rc2-40-cbc' => true,
64+
'rc2-64-cbc' => true,
65+
'rc2-cbc' => true,
66+
'rc2-cfb' => true,
67+
'rc2-ecb' => true,
68+
'rc2-ofb' => true,
69+
'rc4' => true,
70+
'rc4-40' => true,
71+
'des' => true,
72+
'des-cbc' => true,
73+
'des-cfb' => true,
74+
'des-ecb' => true,
75+
'des-ede' => true,
76+
'des-ede-cbc' => true,
77+
'des-ede-cfb' => true,
78+
'des-ede-ofb' => true,
79+
'des-ede3' => true,
80+
'des-ede3-cbc' => true,
81+
'des-ede3-cfb' => true,
82+
'des-ede3-ofb' => true,
83+
'des-ofb' => true,
84+
'des3' => true,
85+
'desx' => true,
86+
'seed' => true,
87+
'seed-cbc' => true,
88+
'seed-cfb' => true,
89+
'seed-ecb' => true,
90+
'seed-ofb' => true,
91+
];
92+
93+
/**
94+
* @inheritDoc
95+
*/
96+
public function crypt(Target $target, Result $result)
97+
{
98+
if ($this->isUsingWeakAlgorithm()) {
99+
$name = strtolower(get_class($this));
100+
101+
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
102+
}
103+
104+
return parent::crypt($target, $result);
105+
}
106+
107+
108+
/**
109+
* @inheritDoc
110+
*/
111+
public function simulate(Target $target, Result $result)
112+
{
113+
if ($this->isUsingWeakAlgorithm()) {
114+
$name = strtolower(get_class($this));
115+
116+
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
117+
}
118+
119+
return parent::simulate($target, $result);
120+
}
121+
122+
public function isUsingWeakAlgorithm(): bool
123+
{
124+
if (null === $this->algorithm) {
125+
throw new Exception('algorithm is not set');
126+
}
127+
128+
return isset($this->weakAlgorithms[$this->algorithm]);
129+
}
130+
57131
/**
58132
* Setup
59133
*

src/Event/Warning.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace phpbu\App\Event;
3+
4+
/**
5+
* Debug Event
6+
*
7+
* @package phpbu
8+
* @subpackage Event
9+
* @author MoeBrowne <moebrowne@users.noreply.github.com>
10+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
11+
* @link http://phpbu.de/
12+
* @since Class available since Release 6.0.0
13+
*/
14+
class Warning
15+
{
16+
/**
17+
* Event name
18+
*/
19+
const NAME = 'phpbu.warning';
20+
21+
/**
22+
* Warning message
23+
*
24+
* @var string
25+
*/
26+
protected $message;
27+
28+
/**
29+
* Constructor.
30+
*
31+
* @param string $message
32+
*/
33+
public function __construct(string $message)
34+
{
35+
$this->message = $message;
36+
}
37+
38+
/**
39+
* Message getter.
40+
*
41+
* @return string
42+
*/
43+
public function getMessage() : string
44+
{
45+
return $this->message;
46+
}
47+
}

src/Log/Webhook.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ public function setup(array $options)
128128
if (empty($options['uri'])) {
129129
throw new Exception('no uri given');
130130
}
131+
132+
// PHP >7.2 deprecated the filter options and enabled them by default
133+
$filterOptions = version_compare(PHP_VERSION, '7.2.0', '<')
134+
? FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED
135+
: null;
136+
137+
if (!filter_var($options['uri'], FILTER_VALIDATE_URL, $filterOptions)) {
138+
throw new Exception('webhook URI is invalid');
139+
}
140+
131141
$this->uri = $options['uri'];
132142
$this->method = Arr::getValue($options, 'method', 'GET');
133143
$this->username = Arr::getValue($options, 'username', '');

src/Result.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,15 @@ public function debug($msg) : void
572572
$event = new Event\Debug($msg);
573573
$this->eventDispatcher->dispatch(Event\Debug::NAME, $event);
574574
}
575+
576+
/**
577+
* Warning
578+
*
579+
* @param string $msg
580+
*/
581+
public function warn($msg) : void
582+
{
583+
$event = new Event\Warning($msg);
584+
$this->eventDispatcher->dispatch(Event\Warning::NAME, $event);
585+
}
575586
}

src/Result/PrinterCli.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static function getSubscribedEvents(): array
110110
{
111111
return [
112112
'phpbu.debug' => 'onDebug',
113+
'phpbu.warning' => 'onWarning',
113114
'phpbu.app_start' => 'onPhpbuStart',
114115
'phpbu.backup_start' => 'onBackupStart',
115116
'phpbu.backup_failed' => 'onBackupFailed',
@@ -406,6 +407,16 @@ public function onDebug(Event\Debug $event)
406407
}
407408
}
408409

410+
/**
411+
* Warnings.
412+
*
413+
* @param \phpbu\App\Event\Warning $event
414+
*/
415+
public function onWarning(Event\Warning $event)
416+
{
417+
$this->writeWithColor('fg-black, bg-yellow', $event->getMessage() . PHP_EOL);
418+
}
419+
409420
/**
410421
* phpbu end event.
411422
*

tests/phpbu/Backup/Crypter/OpenSSLTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,23 @@ public function testGetSuffix()
171171
$suffix = $openSSL->getSuffix();
172172
$this->assertEquals('enc', $suffix);
173173
}
174+
175+
/**
176+
* Tests that a warning is emitted for weak algorithms
177+
*/
178+
public function testWeakAlgorithmsCauseWarnings()
179+
{
180+
$runner = $this->getRunnerMock();
181+
$runner->expects($this->once())
182+
->method('run')
183+
->willReturn($this->getRunnerResultMock(0, 'openssl'));
184+
185+
$target = $this->createTargetMock(__FILE__);
186+
$appResult = $this->getAppResultMock();
187+
$appResult->expects($this->once())->method('warn');
188+
189+
$openSSL = new OpenSSL($runner);
190+
$openSSL->setup(['pathToOpenSSL' => PHPBU_TEST_BIN, 'password' => 'fooBarBaz', 'algorithm' => 'des']);
191+
$openSSL->crypt($target, $appResult);
192+
}
174193
}

tests/phpbu/Log/WebhookTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function testSetupNoTarget()
3333
$json->setup([]);
3434
}
3535

36+
/**
37+
* Tests Webhook::setup
38+
*/
39+
public function testUriMustBeValid()
40+
{
41+
$this->expectException('phpbu\App\Exception');
42+
$json = new Webhook();
43+
$json->setup(['uri' => 'not a URI']);
44+
}
45+
3646
/**
3747
* Tests Webhook::onPhpbuEnd
3848
*/
@@ -46,7 +56,7 @@ public function testGet()
4656
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
4757
$phpbuEndEvent->method('getResult')->willReturn($result);
4858

49-
$uri = PHPBU_TEST_FILES . '/misc/webhook.fail.uri';
59+
$uri = 'https://webhook.fail.uri/hook';
5060
$json = new Webhook();
5161
$json->setup(['uri' => $uri]);
5262

@@ -66,7 +76,7 @@ public function testBasicAuth()
6676
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
6777
$phpbuEndEvent->method('getResult')->willReturn($result);
6878

69-
$uri = PHPBU_TEST_FILES . '/misc/webhook.fail.uri';
79+
$uri = 'https://webhook.fail.uri/hook';
7080
$json = new Webhook();
7181
$json->setup(['uri' => $uri, 'username' => 'foo', 'password' => 'bar']);
7282

@@ -85,7 +95,7 @@ public function testPostDefaultJsonSuccess()
8595
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
8696
$phpbuEndEvent->method('getResult')->willReturn($result);
8797

88-
$uri = PHPBU_TEST_FILES . '/misc/webhook.fake.uri';
98+
$uri = 'file://' . PHPBU_TEST_FILES . '/misc/webhook.fake.uri';
8999
$json = new Webhook();
90100
$json->setup(['uri' => $uri, 'contentType' => 'application/json', 'method' => 'post']);
91101

@@ -106,7 +116,7 @@ public function testPostDefaultJson()
106116
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
107117
$phpbuEndEvent->method('getResult')->willReturn($result);
108118

109-
$uri = PHPBU_TEST_FILES . '/misc/webhook.fail.uri';
119+
$uri = 'https://webhook.fail.uri/hook';
110120
$json = new Webhook();
111121
$json->setup(['uri' => $uri, 'contentType' => 'application/json', 'method' => 'post']);
112122

@@ -127,7 +137,7 @@ public function testPostXmlTemplate()
127137
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
128138
$phpbuEndEvent->method('getResult')->willReturn($result);
129139

130-
$uri = PHPBU_TEST_FILES . '/misc/webhook.fail.uri';
140+
$uri = 'https://webhook.fail.uri/hook';
131141
$path = PHPBU_TEST_FILES . '/misc/webhook.tpl';
132142
$json = new Webhook();
133143
$json->setup(['uri' => $uri, 'contentType' => 'application/xml', 'method' => 'post', 'template' => $path]);
@@ -151,7 +161,7 @@ public function testPostNoFormatter()
151161
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
152162
$phpbuEndEvent->method('getResult')->willReturn($result);
153163

154-
$uri = PHPBU_TEST_FILES . '/misc/webhook.fail.uri';
164+
$uri = 'https://webhook.fail.uri/hook';
155165
$json = new Webhook();
156166
$json->setup(['uri' => $uri, 'contentType' => 'application/html', 'method' => 'post']);
157167

0 commit comments

Comments
 (0)