-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abuse Protection for webhooks (#491)
* First pass at supporting receiver based webhook abuse protection * Adding client test for new status codes. * adding options to set the GET and OPTIONS function handlers. * adding docs for abuse protection * don't drop encoding checking * fix unit tests for unknown encoding * allow nil event to be delivered if receiver wants Signed-off-by: Scott Nichols <snichols@vmware.com>
- Loading branch information
Scott Nichols
authored
May 19, 2020
1 parent
467aaaf
commit 97abfeb
Showing
12 changed files
with
435 additions
and
10 deletions.
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.