Skip to content

Commit 347c54d

Browse files
committed
Correctly handle malformed signature
1 parent cd089e8 commit 347c54d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

handler.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ func (s *SubHandler) handlePost(w http.ResponseWriter, r *http.Request) {
109109
}
110110

111111
if s.doSignatureVerification {
112-
if valid, err := VerifyRequestSignature(r, bodyBytes, s.signatureSecret); err != nil {
113-
http.Error(w, "Internal Server Error", http.StatusBadRequest)
114-
return
115-
} else if !valid {
112+
if valid, err := VerifyRequestSignature(r, bodyBytes, s.signatureSecret); err != nil || !valid {
116113
http.Error(w, "Invalid request signature", http.StatusForbidden)
117114
return
118115
}

handler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ func TestSubHandler_ServeHTTP_VerificationBasic(t *testing.T) {
2626

2727
func TestSubHandler_ServeHTTP_VerificationInvalidSignature(t *testing.T) {
2828
handler := NewSubHandler(true, []byte(secret))
29+
30+
// Test invalid signature
2931
res := handleRequest(handler, newBadVerificationRequest)
3032
_ = res.Body.Close()
33+
assert.Equal(t, res.StatusCode, http.StatusForbidden)
3134

35+
// Test request with malformed signature
36+
res = handleRequest(handler, newInvalidVerificationRequest)
37+
_ = res.Body.Close()
3238
assert.Equal(t, res.StatusCode, http.StatusForbidden)
3339
}
3440

@@ -111,6 +117,13 @@ func newVerificationRequest() *http.Request {
111117
return req
112118
}
113119

120+
func newInvalidVerificationRequest() *http.Request {
121+
req := newVerificationRequest()
122+
// overwrite header with invalid signature
123+
req.Header.Set("Twitch-Eventsub-Message-Signature", "hey this is not valid")
124+
return req
125+
}
126+
114127
func newBadVerificationRequest() *http.Request {
115128
req := newVerificationRequest()
116129
// overwrite header with invalid signature

0 commit comments

Comments
 (0)