Skip to content

Commit 4b5e778

Browse files
authored
Merge pull request #59 from utilitywarehouse/as-webhook-validation
add flag to skip webhook validation
2 parents 595b984 + 4519c91 commit 4b5e778

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

main.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ func usage() {
7171
fmt.Fprintf(os.Stderr, "\nUsage:\n")
7272
fmt.Fprintf(os.Stderr, "\tgit-mirror [global options]\n")
7373
fmt.Fprintf(os.Stderr, "\nGLOBAL OPTIONS:\n")
74-
fmt.Fprintf(os.Stderr, "\t-log-level value (default: 'info') Log level [$LOG_LEVEL]\n")
75-
fmt.Fprintf(os.Stderr, "\t-config value (default: '/etc/git-mirror/config.yaml') Absolute path to the config file. [$GIT_MIRROR_CONFIG]\n")
76-
fmt.Fprintf(os.Stderr, "\t-watch-config value (default: true) watch config for changes and reload when changes encountered. [$GIT_MIRROR_WATCH_CONFIG]\n")
77-
fmt.Fprintf(os.Stderr, "\t-http-bind-address value (default: ':9001') The address the web server binds to. [$GIT_MIRROR_HTTP_BIND]\n")
78-
fmt.Fprintf(os.Stderr, "\t-one-time (default: 'false') Exit after first mirror. [$GIT_MIRROR_ONE_TIME]\n")
79-
fmt.Fprintf(os.Stderr, "\t-github-webhook-secret (default: '') The Github webhook secret used to validate payload [$GITHUB_WEBHOOK_SECRET]\n")
80-
fmt.Fprintf(os.Stderr, "\t-github-webhook-path (default: '/github-webhook') The path on which webserver will receive github webhook events [$GITHUB_WEBHOOK_PATH]\n")
74+
fmt.Fprintf(os.Stderr, "\t-log-level value (default: 'info') Log level [$LOG_LEVEL]\n")
75+
fmt.Fprintf(os.Stderr, "\t-config value (default: '/etc/git-mirror/config.yaml') Absolute path to the config file. [$GIT_MIRROR_CONFIG]\n")
76+
fmt.Fprintf(os.Stderr, "\t-watch-config value (default: true) watch config for changes and reload when changes encountered. [$GIT_MIRROR_WATCH_CONFIG]\n")
77+
fmt.Fprintf(os.Stderr, "\t-http-bind-address value (default: ':9001') The address the web server binds to. [$GIT_MIRROR_HTTP_BIND]\n")
78+
fmt.Fprintf(os.Stderr, "\t-one-time (default: 'false') Exit after first mirror. [$GIT_MIRROR_ONE_TIME]\n")
79+
fmt.Fprintf(os.Stderr, "\t-github-webhook-secret (default: '') The Github webhook secret used to validate payload [$GITHUB_WEBHOOK_SECRET]\n")
80+
fmt.Fprintf(os.Stderr, "\t-github-skip-sig-validation (default: false) If set github webhook signature validation will be skipped [$GITHUB_SKIP_SIG_VALIDATION]\n")
81+
fmt.Fprintf(os.Stderr, "\t-github-webhook-path (default: '/github-webhook') The path on which webserver will receive github webhook events [$GITHUB_WEBHOOK_PATH]\n")
8182

8283
os.Exit(2)
8384
}
@@ -90,6 +91,7 @@ func main() {
9091
flagWatchConfig := flag.Bool("watch-config", envBool("GIT_MIRROR_WATCH_CONFIG", true), "watch config for changes and reload when changes encountered")
9192
flagHttpBind := flag.String("http-bind-address", envString("GIT_MIRROR_HTTP_BIND", ":9001"), "The address the web server binds to")
9293
flagGithubWhSecret := flag.String("github-webhook-secret", envString("GITHUB_WEBHOOK_SECRET", ""), "The Github webhook secret used to validate payload")
94+
flagGithubWhSkipValidation := flag.Bool("github-skip-sig-validation", envBool("GITHUB_SKIP_SIG_VALIDATION", false), "If set github webhook signature validation will be skipped")
9395
flagGithubWhPath := flag.String("github-webhook-path", envString("GITHUB_WEBHOOK_PATH", "/github-webhook"), "The path on which webserver will receive github webhook events")
9496

9597
flagOneTime := flag.Bool("one-time", envBool("GIT_MIRROR_ONE_TIME", false), "Exit after first mirror")
@@ -166,9 +168,10 @@ func main() {
166168

167169
// setup webhook and metrics server
168170
wh := &GithubWebhookHandler{
169-
repoPool: repoPool,
170-
log: logger.With("logger", "github-webhook"),
171-
secret: *flagGithubWhSecret,
171+
repoPool: repoPool,
172+
log: logger.With("logger", "github-webhook"),
173+
secret: *flagGithubWhSecret,
174+
skipSigValidation: *flagGithubWhSkipValidation,
172175
}
173176

174177
server := &http.Server{
@@ -187,8 +190,8 @@ func main() {
187190
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
188191
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
189192

190-
// register handler only if secret is set
191-
if flagGithubWhSecret != nil && *flagGithubWhSecret != "" {
193+
// register handler if skip validation flag is set or secret is set
194+
if *flagGithubWhSkipValidation || *flagGithubWhSecret != "" {
192195
mux.Handle(*flagGithubWhPath, wh)
193196
}
194197

webhook.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ type GitHubEvent struct {
3232
}
3333

3434
type GithubWebhookHandler struct {
35-
repoPool *repopool.RepoPool
36-
secret string
37-
log *slog.Logger
35+
repoPool *repopool.RepoPool
36+
secret string
37+
skipSigValidation bool
38+
log *slog.Logger
3839
}
3940

4041
func (wh *GithubWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -50,10 +51,12 @@ func (wh *GithubWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
5051
return
5152
}
5253

53-
if !wh.isValidSignature(body, r.Header.Get("X-Hub-Signature-256")) {
54-
wh.log.Error("invalid signature")
55-
w.WriteHeader(http.StatusBadRequest)
56-
return
54+
if !wh.skipSigValidation {
55+
if !wh.isValidSignature(body, r.Header.Get("X-Hub-Signature-256")) {
56+
wh.log.Error("invalid signature")
57+
w.WriteHeader(http.StatusBadRequest)
58+
return
59+
}
5760
}
5861

5962
event := r.Header.Get("X-GitHub-Event")
@@ -81,6 +84,9 @@ func (wh *GithubWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
8184
}
8285

8386
func (wh *GithubWebhookHandler) isValidSignature(message []byte, signature string) bool {
87+
if signature == "" {
88+
return false
89+
}
8490
return hmac.Equal([]byte(signature), []byte(wh.computeHMAC(message, wh.secret)))
8591
}
8692

0 commit comments

Comments
 (0)