1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Morphatic\AutoDeploy\Origins; |
4
|
|
|
|
5
|
|
|
class Github extends AbstractOrigin implements OriginInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The name of the origin. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
public $name = 'Github'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Determines whether or not the Request originated from Github. |
16
|
|
|
* |
17
|
|
|
* @param Illuminate\Http\Request $this->request The Request object |
|
|
|
|
18
|
|
|
* |
19
|
|
|
* @return bool Returns true if the request originated from Github. False otherwise. |
20
|
|
|
*/ |
21
|
18 |
|
public function originated() |
22
|
|
|
{ |
23
|
|
|
// Correct IP range for Github maintained here: |
24
|
|
|
// https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/ |
25
|
18 |
|
$has_github_header = false !== strpos($this->request->header('User-Agent'), 'GitHub-Hookshot'); |
|
|
|
|
26
|
18 |
|
$has_github_ip = $this->ipInRange($this->request->server('REMOTE_ADDR'), '192.30.252.0', 22); |
|
|
|
|
27
|
18 |
|
if ($has_github_header && $has_github_ip) { |
|
|
|
|
28
|
4 |
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
14 |
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Verifies the authenticity of a webhook request from Github. |
36
|
|
|
* |
37
|
|
|
* Follows the procedure described here: https://developer.github.com/webhooks/securing/ |
38
|
|
|
* |
39
|
|
|
* @param Illuminate\Http\Request $this->request The Request object |
|
|
|
|
40
|
|
|
* |
41
|
|
|
* @return bool Returns true if the request is authentic. False otherwise. |
|
|
|
|
42
|
|
|
*/ |
43
|
4 |
|
public function verify() |
44
|
|
|
{ |
45
|
|
|
// get the Github signature |
46
|
4 |
|
$xhub = $this->request->header('X-Hub-Signature') ?: 'nothing'; |
47
|
|
|
|
48
|
|
|
// reconstruct the hash on this side |
49
|
4 |
|
$hash = 'sha1='.hash_hmac('sha1', $this->request->getContent(), config('auto-deploy.secret')); |
50
|
|
|
|
51
|
|
|
// securely compare them |
52
|
4 |
|
return hash_equals($xhub, $hash); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets the event the triggered the webhook request. |
57
|
|
|
* |
58
|
|
|
* @param Illuminate\Http\Request $this->request The Request object |
|
|
|
|
59
|
|
|
* |
60
|
|
|
* @return string The name of the event, e.g. push, release, create, etc. |
61
|
|
|
*/ |
62
|
2 |
|
public function event() |
63
|
|
|
{ |
64
|
2 |
|
return $this->request->header('X-GitHub-Event'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the URL to be cloned from. |
69
|
|
|
* |
70
|
|
|
* @param Illuminate\Http\Request $request The Request object |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @return string The URL of the repo. |
73
|
|
|
*/ |
74
|
36 |
|
public function getRepoUrl() |
75
|
|
|
{ |
76
|
36 |
|
return $this->request->json('repository.clone_url'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the ID of the commit that is to be cloned. |
81
|
|
|
* |
82
|
|
|
* @param Illuminate\Http\Request $request The Request object |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @return string The commit ID. |
85
|
|
|
*/ |
86
|
36 |
|
public function getCommitId() |
87
|
|
|
{ |
88
|
36 |
|
return $this->request->json('after'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.