@@ -42,6 +42,10 @@ const (
4242 EventTypeHeader = "X-Github-Event"
4343 // DeliveryIDHeader is the GitHub header key used to pass the unique ID for the webhook event.
4444 DeliveryIDHeader = "X-Github-Delivery"
45+
46+ // maxPayloadSize is the maximum size of a GitHub webhook payload.
47+ // GitHub documents a 25 MB limit for webhook payloads.
48+ maxPayloadSize = 25 * 1024 * 1024
4549)
4650
4751var (
@@ -146,8 +150,19 @@ func checkMAC(message, messageMAC, key []byte, hashFunc func() hash.Hash) bool {
146150 return hmac .Equal (messageMAC , expectedMAC )
147151}
148152
149- // messageMAC returns the hex-decoded HMAC tag from the signature and its
150- // corresponding hash function.
153+ // readPayloadBody reads the body from readable, enforcing maxPayloadSize.
154+ func readPayloadBody (readable io.Reader ) ([]byte , error ) {
155+ body , err := io .ReadAll (io .LimitReader (readable , maxPayloadSize + 1 ))
156+ if err != nil {
157+ return nil , err
158+ }
159+ if len (body ) > maxPayloadSize {
160+ return nil , errors .New ("webhook payload exceeds maximum allowed size" )
161+ }
162+ return body , nil
163+ }
164+
165+ // messageMAC returns the MAC method and the corresponding hash function.
151166func messageMAC (signature string ) ([]byte , func () hash.Hash , error ) {
152167 if signature == "" {
153168 return nil , nil , errors .New ("missing signature" )
@@ -199,7 +214,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s
199214 switch contentType {
200215 case "application/json" :
201216 var err error
202- if body , err = io . ReadAll (readable ); err != nil {
217+ if body , err = readPayloadBody (readable ); err != nil {
203218 return nil , err
204219 }
205220
@@ -213,7 +228,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s
213228 const payloadFormParam = "payload"
214229
215230 var err error
216- if body , err = io . ReadAll (readable ); err != nil {
231+ if body , err = readPayloadBody (readable ); err != nil {
217232 return nil , err
218233 }
219234
0 commit comments