This repository was archived by the owner on Apr 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHookController.php
More file actions
135 lines (119 loc) · 4.77 KB
/
Copy pathWebHookController.php
File metadata and controls
135 lines (119 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Module\CoreManagerModule\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Zikula\Core\Controller\AbstractController;
use Zikula\Core\Response\PlainResponse;
use Zikula\Module\CoreManagerModule\Exception\ClientException;
use Zikula\Module\CoreManagerModule\Exception\ServerException;
use Zikula\Module\CoreManagerModule\Manager\ReleaseManager;
use Zikula\Module\CoreManagerModule\Manager\PayloadManager;
/**
* GitHub webhook access point.
*/
class WebHookController extends AbstractController
{
/**
* @Route("/webhook-core", methods = {"POST"}, options={"i18n"=false})
*/
public function core(Request $request)
{
try {
$payloadManager = new PayloadManager($request, true);
} catch (\Exception $e) {
return $this->handleException($e);
}
$securityToken = $this->getVar('github_webhook_token');
if (!empty($securityToken)) {
$signature = $request->headers->get('X-Hub-Signature');
if (empty($signature)) {
return new PlainResponse('Missing security token!', Response::HTTP_BAD_REQUEST);
}
$computedSignature = $this->computeSignature($payloadManager->getRawPayload(), $securityToken);
if (!$this->secure_equals($computedSignature, $signature)) {
return new PlainResponse('Signature did not match!', Response::HTTP_BAD_REQUEST);
}
}
$event = $request->headers->get('X-Github-Event');
if (empty($event)) {
return new PlainResponse('"X-Github-Event" header is missing!', Response::HTTP_BAD_REQUEST);
}
$useragent = $request->headers->get('User-Agent');
if (strpos($useragent, 'GitHub-Hookshot/') !== 0) {
// User agent does not match "GitHub-Hookshot/*"
return new PlainResponse('User-Agent not allowed!', Response::HTTP_BAD_REQUEST);
}
if ($event != 'release') {
// We do not listen to that event.
return new PlainResponse('Event ignored!', Response::HTTP_OK);
}
$jsonPayload = $payloadManager->getJsonPayload();
// See https://developer.github.com/v3/activity/events/types/#releaseevent
if ('published' !== $jsonPayload['action']) {
return new PlainResponse('Release event ignored (action != "published")!', Response::HTTP_OK);
}
$repo = $this->getVar('github_core_repo', 'zikula/core');
if ($jsonPayload['repository']['full_name'] != $repo) {
return new PlainResponse('Release event ignored (repository != "' . $repo . '")!', Response::HTTP_BAD_REQUEST);
}
/** @var ReleaseManager $releaseManager */
$releaseManager = $this->get('zikula_core_manager_module.releasemanager');
$releaseManager->updateGitHubRelease($jsonPayload['release']);
return new PlainResponse('Release list reloaded!', Response::HTTP_OK);
}
/**
* Compute signature from payload using the security token.
*
* @param $payload
* @param $securityToken
*
* @return string
*/
private function computeSignature($payload, $securityToken)
{
return 'sha1=' . hash_hmac('sha1', $payload, $securityToken);
}
/**
* Compares two strings $a and $b in length-constant time.
*
* @param $a
* @param $b
*
* @return bool
*
* https://crackstation.net/hashing-security.htm#slowequals
*/
private function secure_equals($a, $b)
{
$diff = strlen($a) ^ strlen($b);
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++) {
$diff |= ord($a[$i]) ^ ord($b[$i]);
}
return $diff === 0;
}
private function handleException(\Exception $e)
{
switch (true) {
case $e instanceof ClientException:
$text = $e->getMessage();
$code = $e->getCode();
break;
case $e instanceof ServerException:
$text = "Something went wrong at our server. Please report this issue.\n\n{$e->getMessage()}\n{$e->getTraceAsString()}";
$code = $e->getCode();
break;
default:
$text = "Something unexpected happend. Please report this issue.\n\n{$e->getMessage()}\n{$e->getTraceAsString()}";
$code = Response::HTTP_INTERNAL_SERVER_ERROR;
}
return new PlainResponse($text, $code);
}
}