sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/github/webhooks.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package github 18 19 import ( 20 "io" 21 "net/http" 22 23 "github.com/sirupsen/logrus" 24 ) 25 26 // ValidateWebhook ensures that the provided request conforms to the 27 // format of a GitHub webhook and the payload can be validated with 28 // the provided hmac secret. It returns the event type, the event guid, 29 // the payload of the request, whether the webhook is valid or not, 30 // and finally the resultant HTTP status code 31 func ValidateWebhook(w http.ResponseWriter, r *http.Request, tokenGenerator func() []byte) (string, string, []byte, bool, int) { 32 defer r.Body.Close() 33 34 // Header checks: It must be a POST with an event type and a signature. 35 if r.Method != http.MethodPost { 36 responseHTTPError(w, http.StatusMethodNotAllowed, "405 Method not allowed") 37 return "", "", nil, false, http.StatusMethodNotAllowed 38 } 39 eventType := r.Header.Get("X-GitHub-Event") 40 if eventType == "" { 41 responseHTTPError(w, http.StatusBadRequest, "400 Bad Request: Missing X-GitHub-Event Header") 42 return "", "", nil, false, http.StatusBadRequest 43 } 44 eventGUID := r.Header.Get("X-GitHub-Delivery") 45 if eventGUID == "" { 46 responseHTTPError(w, http.StatusBadRequest, "400 Bad Request: Missing X-GitHub-Delivery Header") 47 return "", "", nil, false, http.StatusBadRequest 48 } 49 sig := r.Header.Get("X-Hub-Signature") 50 if sig == "" { 51 responseHTTPError(w, http.StatusForbidden, "403 Forbidden: Missing X-Hub-Signature") 52 return "", "", nil, false, http.StatusForbidden 53 } 54 contentType := r.Header.Get("content-type") 55 if contentType != "application/json" { 56 responseHTTPError(w, http.StatusBadRequest, "400 Bad Request: Hook only accepts content-type: application/json - please reconfigure this hook on GitHub") 57 return "", "", nil, false, http.StatusBadRequest 58 } 59 payload, err := io.ReadAll(r.Body) 60 if err != nil { 61 responseHTTPError(w, http.StatusInternalServerError, "500 Internal Server Error: Failed to read request body") 62 return "", "", nil, false, http.StatusInternalServerError 63 } 64 // Validate the payload with our HMAC secret. 65 if !ValidatePayload(payload, sig, tokenGenerator) { 66 responseHTTPError(w, http.StatusForbidden, "403 Forbidden: Invalid X-Hub-Signature") 67 return "", "", nil, false, http.StatusForbidden 68 } 69 70 return eventType, eventGUID, payload, true, http.StatusOK 71 } 72 73 func responseHTTPError(w http.ResponseWriter, statusCode int, response string) { 74 logrus.WithFields(logrus.Fields{ 75 "response": response, 76 "status-code": statusCode, 77 }).Debug(response) 78 http.Error(w, response, statusCode) 79 }