Skip to content

Commit 4932782

Browse files
authored
Merge pull request #850 from nyaruka/more-secret-comp
Use constant time comparison to check Meta webhook secret
2 parents 0b91e43 + fd930c6 commit 4932782

File tree

6 files changed

+10
-7
lines changed

6 files changed

+10
-7
lines changed

handlers/facebook_legacy/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (h *handler) receiveVerify(ctx context.Context, channel courier.Channel, w
8787

8888
// verify the token against our secret, if the same return the challenge FB sent us
8989
secret := r.URL.Query().Get("hub.verify_token")
90-
if secret != channel.StringConfigForKey(courier.ConfigSecret, "") {
90+
if !utils.SecretEqual(secret, channel.StringConfigForKey(courier.ConfigSecret, "")) {
9191
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("token does not match secret"))
9292
}
9393

handlers/jiochat/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gomodule/redigo/redis"
2020
"github.com/nyaruka/courier"
2121
"github.com/nyaruka/courier/handlers"
22+
"github.com/nyaruka/courier/utils"
2223
"github.com/nyaruka/gocommon/jsonx"
2324
"github.com/nyaruka/gocommon/urns"
2425
)
@@ -87,7 +88,7 @@ func (h *handler) VerifyURL(ctx context.Context, channel courier.Channel, w http
8788
ResponseText := "unknown request"
8889
StatusCode := 400
8990

90-
if encoded == form.Signature {
91+
if utils.SecretEqual(encoded, form.Signature) {
9192
ResponseText = form.EchoStr
9293
StatusCode = 200
9394
}

handlers/meta/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (h *handler) receiveVerify(ctx context.Context, channel courier.Channel, w
183183

184184
// verify the token against our server facebook webhook secret, if the same return the challenge FB sent us
185185
secret := r.URL.Query().Get("hub.verify_token")
186-
if secret != h.Server().Config().FacebookWebhookSecret {
186+
if !utils.SecretEqual(secret, h.Server().Config().FacebookWebhookSecret) {
187187
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("token does not match secret"))
188188
}
189189
// and respond with the challenge token

handlers/slack/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func (h *handler) Initialize(s courier.Server) error {
5252
}
5353

5454
func handleURLVerification(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, payload *moPayload) ([]courier.Event, error) {
55-
validationToken := channel.ConfigForKey(configValidationToken, "")
56-
if validationToken != payload.Token {
55+
validationToken := channel.StringConfigForKey(configValidationToken, "")
56+
if !utils.SecretEqual(payload.Token, validationToken) {
5757
w.WriteHeader(http.StatusForbidden)
5858
return nil, fmt.Errorf("wrong validation token for channel: %s", channel.UUID())
5959
}

handlers/vk/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/buger/jsonparser"
1818
"github.com/nyaruka/courier"
1919
"github.com/nyaruka/courier/handlers"
20+
"github.com/nyaruka/courier/utils"
2021
"github.com/nyaruka/gocommon/httpx"
2122
"github.com/nyaruka/gocommon/jsonx"
2223
"github.com/nyaruka/gocommon/urns"
@@ -196,7 +197,7 @@ func (h *handler) receiveEvent(ctx context.Context, channel courier.Channel, w h
196197
// check shared secret key before proceeding
197198
secret := channel.StringConfigForKey(courier.ConfigSecret, "")
198199

199-
if payload.SecretKey != secret {
200+
if !utils.SecretEqual(payload.SecretKey, secret) {
200201
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, errors.New("wrong secret key"))
201202
}
202203
// check event type and decode body to correspondent struct

handlers/wechat/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gomodule/redigo/redis"
2020
"github.com/nyaruka/courier"
2121
"github.com/nyaruka/courier/handlers"
22+
"github.com/nyaruka/courier/utils"
2223
"github.com/nyaruka/gocommon/urns"
2324
)
2425

@@ -84,7 +85,7 @@ func (h *handler) VerifyURL(ctx context.Context, channel courier.Channel, w http
8485
ResponseText := "unknown request"
8586
StatusCode := 400
8687

87-
if encoded == form.Signature {
88+
if utils.SecretEqual(encoded, form.Signature) {
8889
ResponseText = form.EchoStr
8990
StatusCode = 200
9091
}

0 commit comments

Comments
 (0)