Newer
Older
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
package webhooks
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"go.uber.org/zap"
)
// 2Mi should be enough for everybody
const maxBodySize = 2 << 20
// Message describes a received web hook. The dynamic type of Payload depends
type Message struct {
Event string `json:"event"`
Timestamp int64 `json:"ts"`
Payload map[string]interface{} `json:"payload"`
}
// Receive returns an http.Handler that receives webhooks from BBBAtScale and
// sends their contents to the returned channel.
func Receive(opts ...ReceiverOption) (<-chan Message, http.Handler, error) {
r := new(receiver)
for _, opt := range opts {
opt(r)
}
if r.ch == nil {
r.ch = make(chan Message)
}
if r.log == nil {
r.log = zap.NewNop()
}
return r.ch, r, nil
}
// A ReceiverOption can be passed to Receive to customize behaviour of a
// webhook Receiver.
type ReceiverOption func(*receiver)
// Authenticate enables authentication of incoming webhooks using HMAC-SHA512
// and the provided key.
func Authenticate(key []byte) ReceiverOption {
return func(r *receiver) {
r.macKey = key
}
}
// WithLogger instructs the receiver to log messages to l.
func WithLogger(l *zap.Logger) ReceiverOption {
return func(r *receiver) {
r.log = l
}
}
// WithChanBufferSize configures the channel returned from Receive with a
// buffer size equal to n.
func WithChanBufferSize(n int) ReceiverOption {
return func(r *receiver) {
r.ch = make(chan Message, n)
}
}
// The receiver is an http.Handler receiving web hooks from BBBatScale.
type receiver struct {
ch chan Message
log *zap.Logger
// Used for authenticating incoming requests. A SHA512-HMAC computed
// over the request body must match the tag sent in X-Hook-Signature
// header.
macKey []byte
}
func (wr *receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log := wr.log.Sugar().With(
"method", r.Method,
"url", r.URL.String(),
"remote", r.RemoteAddr,
"ua", r.UserAgent(),
"xff", r.Header.Get("X-Forwarded-For"),
)
switch r.Method {
case "POST":
default:
w.Header().Set("Allow", "POST")
if r.Method == "OPTIONS" {
return
}
code := http.StatusMethodNotAllowed
http.Error(w, http.StatusText(code), code)
log.Info("method not allowed")
return
}
p, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, maxBodySize))
if err != nil {
code := http.StatusBadRequest
http.Error(w, http.StatusText(code), code)
log.Warn("read", err)
return
}
sig := r.Header.Get("X-Hook-Signature")
ok := wr.verifyTag(sig, p)
if !ok {
code := http.StatusForbidden
http.Error(w, http.StatusText(code), code)
log.Warnw("invalid mac tag", "tag", sig)
return
}
var v Message
if err := json.Unmarshal(p, &v); err != nil {
code := http.StatusBadRequest
http.Error(w, http.StatusText(code), code)
log.Warn("unmarshal: ", err)
return
}
// Basic sanity check to avoid passing empty messages up the channel.
if v.Event == "" {
code := http.StatusBadRequest
http.Error(w, http.StatusText(code), code)
log.Warn("event missing from message")
return
}
log.Infow("incoming hook", "event", v.Event)
wr.ch <- v
}
func (wr *receiver) verifyTag(header string, body []byte) bool {
if len(wr.macKey) == 0 {
// request authentication disabled
return true
}
v1, t, err := disassembleHookSignature(header)
if err != nil {
return false
}
tag, err := hex.DecodeString(v1)
if err != nil {
return false
}
var b bytes.Buffer
fmt.Fprintf(&b, "%d.%s", t, body)
mac := hmac.New(sha512.New, wr.macKey)
mac.Write(b.Bytes())
expect := mac.Sum(nil)
return hmac.Equal(tag, expect)
}
func disassembleHookSignature(s string) (v1 string, t int64, err error) {
elems := strings.Split(s, ",")
if len(elems) < 2 {
return "", 0, fmt.Errorf(
"invalid argument: need at least v1 and t parts: %q", s)
}
for _, e := range elems {
kv := strings.SplitN(e, "=", 2)
if len(kv) != 2 {
return "", 0, fmt.Errorf(
"invalid argument: expect k=v, got %q", e)
}
switch kv[0] {
case "v1":
if kv[1] == "" {
return "", 0, errors.New(
"invalid argument: missing value for v1")
}
v1 = kv[1]
case "t":
d, err := strconv.ParseInt(kv[1], 10, 64)
if err != nil {
return "", 0, fmt.Errorf(
"invalid argument: t: %v", err)
}
if d <= 0 {
return "", 0, fmt.Errorf(
"invalid argument: t: %d", d)
}
t = d
default:
// skip keys we don't know how to handle
}
}
if v1 == "" {
return "", 0, errors.New("invalid argument: missing v1")
}
if t == 0 {
return "", 0, errors.New("invalid argument: missing t")
}
return v1, t, nil
}