-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abuse Protection for webhooks #491
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e1b1923
First pass at supporting receiver based webhook abuse protection
52b675e
Adding client test for new status codes.
05c3444
adding options to set the GET and OPTIONS function handlers.
bc04c59
adding docs for abuse protection
18e0cf8
don't drop encoding checkgi
9639549
fix unit tests for unknown encoding
5def1bd
Merge branch 'master' into abuse-protection
8e86388
allow nil event to be delivered if receiver wants
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
p, err := cloudevents.NewHTTP( | ||
cloudevents.WithDefaultOptionsHandlerFunc([]string{"POST", "OPTIONS"}, 100, []string{"http://localhost:8181"}, true), | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to create protocol: %s", err.Error()) | ||
} | ||
|
||
c, err := cloudevents.NewClient(p) | ||
if err != nil { | ||
log.Fatalf("failed to create client, %v", err) | ||
} | ||
|
||
log.Printf("will listen on :8080\n") | ||
log.Fatalf("failed to start receiver: %s", c.StartReceiver(ctx, receive)) | ||
} | ||
|
||
func receive(ctx context.Context, event cloudevents.Event) { | ||
fmt.Printf("%s", event) | ||
} | ||
|
||
// | ||
// Testing with: | ||
// | ||
// PORT=8181 go run ./cmd/tools/http/raw/ | ||
// | ||
// curl http://localhost:8080 -v -X OPTIONS -H "Origin: http://example.com" -H "WebHook-Request-Origin: http://example.com" -H "WebHook-Request-Callback: http://localhost:8181/do-this?now=true" | ||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
) | ||
|
||
type RawHTTP struct { | ||
Port int `envconfig:"PORT" default:"8080"` | ||
} | ||
|
||
func (raw *RawHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
if reqBytes, err := httputil.DumpRequest(r, true); err == nil { | ||
log.Printf("Raw HTTP Request:\n%+v", string(reqBytes)) | ||
_, _ = w.Write(reqBytes) | ||
} else { | ||
log.Printf("Failed to call DumpRequest: %s", err) | ||
} | ||
fmt.Println("------------------------------") | ||
} | ||
|
||
func main() { | ||
var env RawHTTP | ||
if err := envconfig.Process("", &env); err != nil { | ||
log.Fatalf("Failed to process env var: %s", err) | ||
} | ||
log.Printf("Starting listening on :%d\n", env.Port) | ||
log.Println(http.ListenAndServe(fmt.Sprintf(":%d", env.Port), &env)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
cecontext "github.com/cloudevents/sdk-go/v2/context" | ||
"go.uber.org/zap" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type WebhookConfig struct { | ||
AllowedMethods []string // defaults to POST | ||
AllowedRate *int | ||
AutoACKCallback bool | ||
AllowedOrigins []string | ||
} | ||
|
||
const ( | ||
DefaultAllowedRate = 1000 | ||
) | ||
|
||
// TODO: implement rate limiting. | ||
// Throttling is indicated by requests being rejected using HTTP status code 429 Too Many Requests. | ||
// TODO: use this if Webhook Request Origin has been turned on. | ||
// Inbound requests should be rejected if Allowed Origins is required by SDK. | ||
|
||
func (p *Protocol) OptionsHandler(rw http.ResponseWriter, req *http.Request) { | ||
if req.Method != http.MethodOptions || p.WebhookConfig == nil { | ||
rw.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
headers := make(http.Header) | ||
|
||
// The spec does not say we need to validate the origin, just the request origin. | ||
// After the handshake, we will validate the origin. | ||
if origin, ok := p.ValidateRequestOrigin(req); !ok { | ||
rw.WriteHeader(http.StatusBadRequest) | ||
return | ||
} else { | ||
headers.Set("WebHook-Allowed-Origin", origin) | ||
} | ||
|
||
allowedRateRequired := false | ||
if _, ok := req.Header[http.CanonicalHeaderKey("WebHook-Request-Rate")]; ok { | ||
// must send WebHook-Allowed-Rate | ||
allowedRateRequired = true | ||
} | ||
|
||
if p.WebhookConfig.AllowedRate != nil { | ||
headers.Set("WebHook-Allowed-Rate", strconv.Itoa(*p.WebhookConfig.AllowedRate)) | ||
} else if allowedRateRequired { | ||
headers.Set("WebHook-Allowed-Rate", strconv.Itoa(DefaultAllowedRate)) | ||
} | ||
|
||
if len(p.WebhookConfig.AllowedMethods) > 0 { | ||
headers.Set("Allow", strings.Join(p.WebhookConfig.AllowedMethods, ", ")) | ||
} else { | ||
headers.Set("Allow", http.MethodPost) | ||
} | ||
|
||
cb := req.Header.Get("WebHook-Request-Callback") | ||
if cb != "" { | ||
if p.WebhookConfig.AutoACKCallback { | ||
go func() { | ||
reqAck, err := http.NewRequest(http.MethodPost, cb, nil) | ||
if err != nil { | ||
cecontext.LoggerFrom(req.Context()).Errorw("OPTIONS handler failed to create http request attempting to ack callback.", zap.Error(err), zap.String("callback", cb)) | ||
return | ||
} | ||
|
||
// Write out the headers. | ||
for k := range headers { | ||
reqAck.Header.Set(k, headers.Get(k)) | ||
} | ||
|
||
_, err = http.DefaultClient.Do(reqAck) | ||
if err != nil { | ||
cecontext.LoggerFrom(req.Context()).Errorw("OPTIONS handler failed to ack callback.", zap.Error(err), zap.String("callback", cb)) | ||
return | ||
} | ||
}() | ||
return | ||
} else { | ||
cecontext.LoggerFrom(req.Context()).Infof("ACTION REQUIRED: Please validate web hook request callback: %q", cb) | ||
// TODO: what to do pending https://github.com/cloudevents/spec/issues/617 | ||
return | ||
} | ||
} | ||
|
||
// Write out the headers. | ||
for k := range headers { | ||
rw.Header().Set(k, headers.Get(k)) | ||
} | ||
} | ||
|
||
func (p *Protocol) ValidateRequestOrigin(req *http.Request) (string, bool) { | ||
return p.validateOrigin(req.Header.Get("WebHook-Request-Origin")) | ||
} | ||
|
||
func (p *Protocol) ValidateOrigin(req *http.Request) (string, bool) { | ||
return p.validateOrigin(req.Header.Get("Origin")) | ||
} | ||
|
||
func (p *Protocol) validateOrigin(ro string) (string, bool) { | ||
cecontext.LoggerFrom(context.TODO()).Infow("Validating origin.", zap.String("origin", ro)) | ||
|
||
for _, ao := range p.WebhookConfig.AllowedOrigins { | ||
if ao == "*" { | ||
return ao, true | ||
} | ||
// TODO: it is not clear what the rules for allowed hosts are. | ||
// Need to find docs for this. For now, test for prefix. | ||
if strings.HasPrefix(ro, ao) { | ||
return ao, true | ||
} | ||
} | ||
|
||
return ro, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could use this https://github.com/rs/cors/blob/master/cors.go or something similar to validate the origins, let's do it in a follow up.