Skip to content

Commit 21a9879

Browse files
Skip signature check for webhook optionally (#51)
1 parent 90f125d commit 21a9879

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

config.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

pkg/webhook/handlers/optimizely.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,18 @@ func (h *OptlyWebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Reque
112112
return
113113
}
114114

115-
// Check signature
116-
requestSignature := r.Header.Get(signatureHeader)
117-
isValid := h.validateSignature(requestSignature, body, webhookMsg.ProjectID)
118-
if !isValid {
119-
log.Error().Msg("Computed signature does not match signature in request. Ignoring message.")
120-
render.Status(r, http.StatusBadRequest)
121-
render.JSON(w, r, render.M{
122-
"error": "Computed signature does not match signature in request. Ignoring message.",
123-
})
124-
return
115+
// Check signature if check is not skipped
116+
if !webhookConfig.SkipSignatureCheck {
117+
requestSignature := r.Header.Get(signatureHeader)
118+
isValid := h.validateSignature(requestSignature, body, webhookMsg.ProjectID)
119+
if !isValid {
120+
log.Error().Msg("Computed signature does not match signature in request. Ignoring message.")
121+
render.Status(r, http.StatusBadRequest)
122+
render.JSON(w, r, render.M{
123+
"error": "Computed signature does not match signature in request. Ignoring message.",
124+
})
125+
return
126+
}
125127
}
126128

127129
// Iterate through all SDK keys and update config

pkg/webhook/handlers/optimizely_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,42 @@ func TestHandleWebhookValidMessageInvalidSignature(t *testing.T) {
9999
assert.Regexp(t, "Computed signature does not match signature in request. Ignoring message.", rec.Body.String())
100100
}
101101

102+
func TestHandleWebhookSkippedCheckInvalidSignature(t *testing.T) {
103+
testCache := optlytest.NewCache()
104+
var testWebhookConfigs = []models.OptlyWebhookConfig {
105+
{
106+
ProjectID: 42,
107+
SDKKeys: []string{"myDatafile"},
108+
Secret: "I am secret",
109+
SkipSignatureCheck: true,
110+
},
111+
}
112+
optlyHandler := NewWebhookHandler(testCache, testWebhookConfigs)
113+
webhookMsg := models.OptlyMessage{
114+
ProjectID: 42,
115+
Timestamp: 42424242,
116+
Event: "project.datafile_updated",
117+
Data: models.DatafileUpdateData{
118+
Revision: 101,
119+
OriginURL: "origin.optimizely.com/datafiles/myDatafile",
120+
CDNUrl: "cdn.optimizely.com/datafiles/myDatafile",
121+
Environment: "Production",
122+
},
123+
}
124+
125+
validWebhookMessage, _ := json.Marshal(webhookMsg)
126+
127+
req := httptest.NewRequest("POST", "/webhooks/optimizely", bytes.NewBuffer(validWebhookMessage))
128+
req.Header.Set(signatureHeader, "sha1=some_random_signature_in_header")
129+
130+
rec := httptest.NewRecorder()
131+
handler := http.HandlerFunc(optlyHandler.HandleWebhook)
132+
handler.ServeHTTP(rec, req)
133+
134+
// Message is processed as usual with invalid signature as check is skipped
135+
assert.Equal(t, http.StatusNoContent, rec.Code)
136+
}
137+
102138
func TestHandleWebhookValidMessage(t *testing.T) {
103139
testCache := optlytest.NewCache()
104140
var testWebhookConfigs = []models.OptlyWebhookConfig{

pkg/webhook/models/optimizely.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ type OptlyMessage struct {
3535

3636
// OptlyWebhookConfig represents configuration of a single Optimizely webhook
3737
type OptlyWebhookConfig struct {
38-
ProjectID int64 `yaml:"projectId"`
39-
SDKKeys []string `yaml:"sdkKeys"`
40-
Secret string `yaml:"secret"`
38+
ProjectID int64 `yaml:"projectId"`
39+
SDKKeys []string `yaml:"sdkKeys"`
40+
Secret string `yaml:"secret"`
41+
SkipSignatureCheck bool `yaml:"skipSignatureCheck" default:"false"`
4142
}

0 commit comments

Comments
 (0)