-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghwebhook.go
337 lines (317 loc) · 8.24 KB
/
ghwebhook.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package ghwebhook
import (
"context"
"errors"
"io/ioutil"
"mime"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/google/go-github/github"
)
// Webhook is a receiver for github webhook.
type Webhook struct {
// Secret is Secret in Github Settings/Webhooks/Manage webhook
Secret string
// RestrictAddr enables restrict Service Hook IP Addresses
// https://help.github.com/articles/github-s-ip-addresses/
RestrictAddr bool
// TrustAddrs is the list of trusted IP address (e.g. reverse proxies)
TrustAddrs []string
CommitComment func(e *github.CommitCommentEvent)
Create func(e *github.CreateEvent)
Delete func(e *github.DeleteEvent)
Deployment func(e *github.DeploymentEvent)
DeploymentStatus func(e *github.DeploymentStatusEvent)
Fork func(e *github.ForkEvent)
Gollum func(e *github.GollumEvent)
Installation func(e *github.InstallationEvent)
InstallationRepositories func(e *github.InstallationRepositoriesEvent)
IssueComment func(e *github.IssueCommentEvent)
Issues func(e *github.IssuesEvent)
Label func(e *github.LabelEvent)
Member func(e *github.MemberEvent)
Membership func(e *github.MembershipEvent)
Milestone func(e *github.MilestoneEvent)
Organization func(e *github.OrganizationEvent)
OrgBlock func(e *github.OrgBlockEvent)
PageBuild func(e *github.PageBuildEvent)
Ping func(e *github.PingEvent)
Project func(e *github.ProjectEvent)
ProjectCard func(e *github.ProjectCardEvent)
ProjectColumn func(e *github.ProjectColumnEvent)
Public func(e *github.PublicEvent)
PullRequestReview func(e *github.PullRequestReviewEvent)
PullRequestReviewComment func(e *github.PullRequestReviewCommentEvent)
PullRequest func(e *github.PullRequestEvent)
Push func(e *github.PushEvent)
Repository func(e *github.RepositoryEvent)
Release func(e *github.ReleaseEvent)
Status func(e *github.StatusEvent)
Team func(e *github.TeamEvent)
TeamAdd func(e *github.TeamAddEvent)
Watch func(e *github.WatchEvent)
mu sync.RWMutex
client *github.Client
trustAddrs []*net.IPNet
expiresAt time.Time
}
func (h *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.RestrictAddr {
if err := h.updateTrustAddrs(r.Context()); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err := h.validateAddr(r); err != nil {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
}
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
t, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
var payload []byte
switch t {
case "application/x-www-form-urlencoded":
if h.Secret != "" {
payload, err = github.ValidatePayload(r, []byte(h.Secret))
if err != nil {
break
}
values, err := url.ParseQuery(string(payload))
if err != nil {
break
}
payload = []byte(values.Get("payload"))
} else {
payload = []byte(r.PostFormValue("payload"))
}
case "application/json":
if h.Secret != "" {
payload, err = github.ValidatePayload(r, []byte(h.Secret))
} else {
payload, err = ioutil.ReadAll(r.Body)
}
default:
err = errors.New("unsupported content type")
}
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
e, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
go h.handle(e)
w.WriteHeader(http.StatusOK)
}
func (h *Webhook) handle(e interface{}) {
switch e := e.(type) {
case *github.CommitCommentEvent:
if h.CommitComment != nil {
h.CommitComment(e)
}
case *github.CreateEvent:
if h.Create != nil {
h.Create(e)
}
case *github.DeleteEvent:
if h.Delete != nil {
h.Delete(e)
}
case *github.DeploymentEvent:
if h.Deployment != nil {
h.Deployment(e)
}
case *github.DeploymentStatusEvent:
if h.DeploymentStatus != nil {
h.DeploymentStatus(e)
}
case *github.ForkEvent:
if h.Fork != nil {
h.Fork(e)
}
case *github.GollumEvent:
if h.Gollum != nil {
h.Gollum(e)
}
case *github.InstallationEvent:
if h.Installation != nil {
h.Installation(e)
}
case *github.InstallationRepositoriesEvent:
if h.InstallationRepositories != nil {
h.InstallationRepositories(e)
}
case *github.IssueCommentEvent:
if h.IssueComment != nil {
h.IssueComment(e)
}
case *github.IssuesEvent:
if h.Issues != nil {
h.Issues(e)
}
case *github.LabelEvent:
if h.Label != nil {
h.Label(e)
}
case *github.MemberEvent:
if h.Member != nil {
h.Member(e)
}
case *github.MembershipEvent:
if h.Membership != nil {
h.Membership(e)
}
case *github.MilestoneEvent:
if h.Milestone != nil {
h.Milestone(e)
}
case *github.OrganizationEvent:
if h.Organization != nil {
h.Organization(e)
}
case *github.OrgBlockEvent:
if h.OrgBlock != nil {
h.OrgBlock(e)
}
case *github.PageBuildEvent:
if h.PageBuild != nil {
h.PageBuild(e)
}
case *github.PingEvent:
if h.Ping != nil {
h.Ping(e)
}
case *github.ProjectEvent:
if h.Project != nil {
h.Project(e)
}
case *github.ProjectCardEvent:
if h.ProjectCard != nil {
h.ProjectCard(e)
}
case *github.ProjectColumnEvent:
if h.ProjectColumn != nil {
h.ProjectColumn(e)
}
case *github.PublicEvent:
if h.Public != nil {
h.Public(e)
}
case *github.PullRequestReviewEvent:
if h.PullRequestReview != nil {
h.PullRequestReview(e)
}
case *github.PullRequestReviewCommentEvent:
if h.PullRequestReviewComment != nil {
h.PullRequestReviewComment(e)
}
case *github.PullRequestEvent:
if h.PullRequest != nil {
h.PullRequest(e)
}
case *github.PushEvent:
if h.Push != nil {
h.Push(e)
}
case *github.RepositoryEvent:
if h.Repository != nil {
h.Repository(e)
}
case *github.ReleaseEvent:
if h.Release != nil {
h.Release(e)
}
case *github.StatusEvent:
if h.Status != nil {
h.Status(e)
}
case *github.TeamEvent:
if h.Team != nil {
h.Team(e)
}
case *github.TeamAddEvent:
if h.TeamAdd != nil {
h.TeamAdd(e)
}
case *github.WatchEvent:
if h.Watch != nil {
h.Watch(e)
}
}
}
func (h *Webhook) validateAddr(r *http.Request) error {
h.mu.RLock()
defer h.mu.RUnlock()
// validate X-Forwarded-For Header
forwarded := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
for _, forwardedIP := range forwarded {
ip := net.ParseIP(strings.TrimSpace(forwardedIP))
if err := h.validateIP(ip); err != nil {
return err
}
}
// validate RemoteAddr
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return err
}
ip := net.ParseIP(host)
if err := h.validateIP(ip); err != nil {
return err
}
return nil
}
func (h *Webhook) validateIP(ip net.IP) error {
for _, addr := range h.trustAddrs {
if addr.Contains(ip) {
return nil
}
}
return errors.New("untrusted ip")
}
func (h *Webhook) updateTrustAddrs(ctx context.Context) error {
if h.trustAddrs != nil && h.expiresAt.Before(time.Now()) {
return nil
}
h.mu.Lock()
defer h.mu.Unlock()
if !h.expiresAt.Before(time.Now()) {
return nil // updated TrustAddrs by another groutine
}
trustAddrs := make([]*net.IPNet, 0, len(h.TrustAddrs)+2)
for _, addr := range h.TrustAddrs {
_, ipNet, err := net.ParseCIDR(addr)
if err != nil {
return err
}
trustAddrs = append(trustAddrs, ipNet)
}
if h.client == nil {
h.client = github.NewClient(nil)
}
meta, _, err := h.client.APIMeta(ctx)
if err != nil {
return err
}
for _, addr := range meta.Hooks {
_, ipNet, err := net.ParseCIDR(addr)
if err != nil {
return err
}
trustAddrs = append(trustAddrs, ipNet)
}
h.trustAddrs = trustAddrs
h.expiresAt = time.Now().Add(24 * time.Hour)
return nil
}