Skip to content

Commit ec2adf2

Browse files
committed
RELTEC-12290: implemented github-like webhook secret support
1 parent 53c88cc commit ec2adf2

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Caddy GitHub Webhook Payload Validation Module
22

3-
This Caddy handler module validates GitHub webhook payloads by using a shared secret. It ensures that the incoming webhooks are legitimate and come from GitHub, thereby enhancing security for your application.
3+
This Caddy handler module validates all GitHub-Like webhook payloads by using a shared secret. It ensures that the incoming webhooks are legitimate and come from GitHub or for example Spacelift, thereby enhancing security for your application.
44

55
## Directive
66

@@ -9,6 +9,7 @@ The directive for this module is `validate_github_webhook_payload`.
99
## Features
1010

1111
- Validates GitHub webhook payloads.
12+
- Validates Spacelift webhook payloads.
1213
- Uses a shared secret to ensure the request integrity.
1314
- Compatible with Caddy v2.
1415

@@ -22,15 +23,15 @@ To use this module, you will need to build Caddy with the module included. Here'
2223
$ go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
2324
```
2425

25-
2. Build Caddy with the `validate_github_webhook_payload` module:
26+
2. Build Caddy with the `validate_github_like_webhook_payload` module:
2627

2728
```bash
28-
$ xcaddy build --with github.com/Roshick/validate_github_webhook_payload
29+
$ xcaddy build --with github.com/Interhyp/validate_github_like_webhook_payload
2930
```
3031

3132
## Configuration
3233

33-
To configure the `validate_github_webhook_payload` directive in your Caddyfile, provide the secret that you will use to validate the webhook payload.
34+
To configure the `validate_github_like_webhook_payload` directive in your Caddyfile, provide the secret that you will use to validate the webhook payload.
3435

3536
### Caddyfile Example
3637

@@ -41,7 +42,7 @@ To configure the `validate_github_webhook_payload` directive in your Caddyfile,
4142
4243
:80
4344
44-
validate_github_webhook_payload <your_secret_here>
45+
validate_github_like_webhook_payload <your_secret_here> <signature_header_field_name_here>
4546
4647
route {
4748
# Your other directives
@@ -50,6 +51,7 @@ route {
5051
```
5152

5253
Replace `<your_secret_here>` with the actual secret that you have configured in your GitHub webhook settings.
54+
Replace `<signature_header_field_name_here>` with the actual name of header transporting signature of webhook payload. It's `X-Signature-256` for Spacelift or `X-Hub-Signature-256` for Github for example.
5355
5456
## Usage
5557
@@ -75,7 +77,7 @@ The Caddyfile would be:
7577
7678
:80
7779
78-
validate_github_webhook_payload my_super_secret
80+
validate_github_like_webhook_payload my_super_secret X-Hub-Signature-256
7981
8082
route {
8183
handle_path /webhook {
@@ -86,7 +88,7 @@ route {
8688
8789
```
8890
89-
In this example, Caddy will verify the incoming webhook payloads sent to `/webhook` using the secret `my_super_secret`.
91+
In this example, Caddy will verify the incoming webhook payloads sent to `/webhook` using the secret `my_super_secret` and containg signature inside of `X-Hub-Signature-256` header field.
9092
9193
## Contribution
9294

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/Roshick/caddy-module-github-webhook-validation-payload
1+
module github.com/Interhyp/caddy-module-github-like-webhook-validation-payload
22

33
go 1.22
44

main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,30 @@ import (
1717

1818
func init() {
1919
caddy.RegisterModule(Middleware{})
20-
httpcaddyfile.RegisterHandlerDirective("validate_github_webhook_payload", parseCaddyfile)
20+
httpcaddyfile.RegisterHandlerDirective("validate_github_like_webhook_payload", parseCaddyfile)
2121
}
2222

2323
// Middleware implements an HTTP handler.
2424
type Middleware struct {
25-
Secret string `json:"secret,omitempty"`
25+
Secret string `json:"secret,omitempty"`
26+
HeaderName string `json:"headerName,omitempty"`
2627
}
2728

2829
// CaddyModule returns the Caddy module information.
2930
func (Middleware) CaddyModule() caddy.ModuleInfo {
3031
return caddy.ModuleInfo{
31-
ID: "http.handlers.github_webhook_validation_payload",
32+
ID: "http.handlers.github_like_webhook_validation_payload",
3233
New: func() caddy.Module { return new(Middleware) },
3334
}
3435
}
3536

3637
// Validate implements caddy.Validator.
3738
func (m *Middleware) Validate() error {
3839
if m.Secret == "" {
39-
return fmt.Errorf("github webhook secret is empty")
40+
return fmt.Errorf("webhook secret is empty")
41+
}
42+
if m.HeaderName == "" {
43+
return fmt.Errorf("webhook headerName is empty")
4044
}
4145

4246
return nil
@@ -55,7 +59,7 @@ func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy
5559
}
5660
r.Body = io.NopCloser(&buffer)
5761

58-
actual := []byte(strings.TrimPrefix(r.Header.Get("X-Hub-Signature-256"), "sha256="))
62+
actual := []byte(strings.TrimPrefix(r.Header.Get(m.HeaderName), "sha256="))
5963

6064
mac := hmac.New(sha256.New, []byte(m.Secret))
6165
mac.Write(payloadBytes)
@@ -82,8 +86,17 @@ func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
8286
return d.ArgErr()
8387
}
8488

85-
// store the argument
89+
// store the secret argument
8690
m.Secret = d.Val()
91+
92+
// headerName is optional argument
93+
if d.NextArg() {
94+
// store the argument
95+
m.HeaderName = d.Val()
96+
} else {
97+
m.HeaderName = "X-Hub-Signature-256"
98+
}
99+
87100
return nil
88101
}
89102

0 commit comments

Comments
 (0)