Skip to content

Commit 298df99

Browse files
authored
set user agent on async abuse protection handshake (#78)
Signed-off-by: Kent Rancourt <[email protected]>
1 parent bb7585f commit 298df99

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

internal/cloudevents/http/abuse_protection.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package http
22

33
import (
4+
"fmt"
45
"log"
56
"net/http"
7+
"runtime"
68
"strconv"
79
"time"
810

11+
"github.com/brigadecore/brigade-foundations/version"
912
cloudHTTP "github.com/cloudevents/sdk-go/v2/protocol/http"
1013
)
1114

@@ -28,6 +31,7 @@ func ValidateEventSource(w http.ResponseWriter, r *http.Request) {
2831
// Complete the handshake asynchronously if a callback URL was provided...
2932
if callbackURL :=
3033
r.Header.Get("WebHook-Request-Callback"); callbackURL != "" {
34+
headers.Set("User-Agent", userAgentHeaderValue())
3135
// The spec is somewhat vague here. It says we can send either GET or POST,
3236
// but it doesn't explicitly state that the receiver (the event source we're
3337
// validating) has to accept both. To cover our bases and ensure
@@ -76,3 +80,12 @@ func executeSourceValidationCallback(method, url string, headers http.Header) {
7680
)
7781
}
7882
}
83+
84+
func userAgentHeaderValue() string {
85+
return fmt.Sprintf("Go/%s (%s-%s) brigade-cloudevents-gateway/%s",
86+
runtime.Version(),
87+
runtime.GOARCH,
88+
runtime.GOOS,
89+
version.Version(),
90+
)
91+
}

internal/cloudevents/http/abuse_protection_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ func TestValidateEventSource(t *testing.T) {
9393
}
9494
s.Server = httptest.NewServer(http.HandlerFunc(
9595
func(w http.ResponseWriter, r *http.Request) {
96+
require.Equal(t, "*", r.Header.Get("WebHook-Allowed-Origin"))
97+
require.Equal(
98+
t,
99+
strconv.Itoa(cloudHTTP.DefaultAllowedRate),
100+
r.Header.Get("WebHook-Allowed-Rate"),
101+
)
102+
require.Equal(t, http.MethodPost, r.Header.Get("Allow"))
103+
require.Equal(t, userAgentHeaderValue(), r.Header.Get("User-Agent"))
96104
switch r.Method {
97105
case http.MethodGet:
98106
// Indicate the GET callback was received

0 commit comments

Comments
 (0)