1
1
package webhook
2
2
3
3
import (
4
- "fmt"
5
4
"log/slog"
6
5
"net/http"
7
6
@@ -45,9 +44,9 @@ type Opt func(*Handler) error
45
44
// If not set, the webhook will not verify the signature of the request.
46
45
//
47
46
// For more information, see: https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries
48
- func WithSecretToken (secretToken [] byte ) Opt {
47
+ func WithSecretToken (secretToken string ) Opt {
49
48
return func (p * Handler ) error {
50
- p .secretToken = secretToken
49
+ p .secretToken = [] byte ( secretToken )
51
50
return nil
52
51
}
53
52
}
@@ -101,6 +100,7 @@ type Headers struct {
101
100
102
101
// ServeHTTP handles a webhook request.
103
102
func (h * Handler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
103
+ defer r .Body .Close ()
104
104
// Parse headers for debugging and audit purposes.
105
105
var head Headers
106
106
head .GithubHookID = r .Header .Get ("X-GitHub-Hook-ID" )
@@ -110,9 +110,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
110
110
head .GitHubHookInstallationTargetID = r .Header .Get ("X-GitHub-Hook-Installation-Target-ID" )
111
111
head .HubSignature256 = r .Header .Get ("X-Hub-Signature-256" )
112
112
113
+ if h .secretToken == nil && head .HubSignature256 != "" {
114
+ h .log .Warn ("received signature but no secret token is set" , "github_headers" , head )
115
+ http .Error (w , "invalid request" , http .StatusInternalServerError )
116
+ return
117
+ }
118
+
113
119
payload , err := github .ValidatePayload (r , h .secretToken ) // If secretToken is empty, the signature will not be verified.
114
120
if err != nil {
115
- h .log .Warn ("webhook validation failed" , "headers " , head )
121
+ h .log .Warn ("webhook validation failed" , "github_headers " , head )
116
122
http .Error (w , "invalid request" , http .StatusBadRequest )
117
123
return
118
124
}
@@ -134,8 +140,13 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
134
140
w .WriteHeader (http .StatusOK )
135
141
}
136
142
137
- // String returns a string representation of the Headers.
138
- func (h * Headers ) String () string {
139
- return fmt .Sprintf ("GithubHookID: %s\n GithubEvent: %s\n GithubDelivery: %s\n GitHubHookInstallationTargetType: %s\n GitHubHookInstallationTargetID: %s\n HubSignature256: %s\n " ,
140
- h .GithubHookID , h .GithubEvent , h .GithubDelivery , h .GitHubHookInstallationTargetType , h .GitHubHookInstallationTargetID , h .HubSignature256 )
143
+ func (h * Headers ) LogValue () slog.Value {
144
+ return slog .GroupValue (
145
+ slog .String ("github_hook_id" , h .GithubHookID ),
146
+ slog .String ("github_event" , h .GithubEvent ),
147
+ slog .String ("github_delivery" , h .GithubDelivery ),
148
+ slog .String ("github_hook_installation_target_type" , h .GitHubHookInstallationTargetType ),
149
+ slog .String ("github_hook_installation_target_id" , h .GitHubHookInstallationTargetID ),
150
+ slog .String ("hub_signature_256" , h .HubSignature256 ),
151
+ )
141
152
}
0 commit comments