Skip to content

Commit cffb82e

Browse files
Merge pull request #246 from moebrowne
Webhook URI Validation
2 parents 5e04f05 + 779afad commit cffb82e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Log/Webhook.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ 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+
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
134+
$filterOptions = FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED;
135+
} else {
136+
$filterOptions = null;
137+
}
138+
139+
if (!filter_var($options['uri'], FILTER_VALIDATE_URL, $filterOptions)) {
140+
throw new Exception('webhook URI is invalid');
141+
}
142+
131143
$this->uri = $options['uri'];
132144
$this->method = Arr::getValue($options, 'method', 'GET');
133145
$this->username = Arr::getValue($options, 'username', '');

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)