diff --git a/weed/notification/webhook/http.go b/weed/notification/webhook/http.go index 4a044ae128..ff6daca06d 100644 --- a/weed/notification/webhook/http.go +++ b/weed/notification/webhook/http.go @@ -8,6 +8,8 @@ import ( "fmt" "io" "net/http" + "net/url" + "strings" "sync" "time" @@ -84,7 +86,7 @@ func (h *httpClient) sendMessageWithRetry(message *webhookMessage, depth int) er } req.Header.Set("Content-Type", "application/json") - if h.token != "" { + if h.token != "" && sameWebhookHost(h.endpoint, targetURL) { req.Header.Set("Authorization", security.BearerPrefix+h.token) } @@ -164,6 +166,39 @@ func (h *httpClient) sendMessageWithRetry(message *webhookMessage, depth int) er return nil } +// sameWebhookHost reports whether target may receive the endpoint's token: +// same host and port, or the default-port upgrade from http to https. +func sameWebhookHost(endpoint, target string) bool { + if target == endpoint { + return true + } + e, err := url.Parse(endpoint) + if err != nil { + return false + } + t, err := url.Parse(target) + if err != nil { + return false + } + if !strings.EqualFold(e.Hostname(), t.Hostname()) { + return false + } + if e.Scheme == t.Scheme { + return webhookPort(e) == webhookPort(t) + } + return e.Scheme == "http" && t.Scheme == "https" && webhookPort(e) == "80" && webhookPort(t) == "443" +} + +func webhookPort(u *url.URL) string { + if p := u.Port(); p != "" { + return p + } + if u.Scheme == "https" { + return "443" + } + return "80" +} + func (h *httpClient) setFinalURL(url string) { h.endpointMu.Lock() h.finalURL = url diff --git a/weed/notification/webhook/http_test.go b/weed/notification/webhook/http_test.go index f64c01f2df..20d5d51ef1 100644 --- a/weed/notification/webhook/http_test.go +++ b/weed/notification/webhook/http_test.go @@ -520,3 +520,81 @@ func TestHttpClientInvalidatesCacheOnNetworkError(t *testing.T) { t.Errorf("Expected cache to be invalidated, but still has %s", clearedCache) } } + +func TestHttpClientRedirectTokenScope(t *testing.T) { + var otherHostAuth []string + otherHost := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + otherHostAuth = append(otherHostAuth, r.Header.Get("Authorization")) + w.WriteHeader(http.StatusOK) + })) + defer otherHost.Close() + + var sameHostAuth []string + endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/moved": + http.Redirect(w, r, "/new", http.StatusTemporaryRedirect) + case "/new": + sameHostAuth = append(sameHostAuth, r.Header.Get("Authorization")) + w.WriteHeader(http.StatusOK) + default: + http.Redirect(w, r, otherHost.URL+"/metadata", http.StatusFound) + } + })) + defer endpoint.Close() + + message := newWebhookMessage("/test/path", &filer_pb.EventNotification{NewEntry: &filer_pb.Entry{Name: "test.txt"}}) + + crossHost, err := newHTTPClient(&config{endpoint: endpoint.URL, authBearerToken: "test-token", timeoutSeconds: 5}) + if err != nil { + t.Fatalf("Failed to create HTTP client: %v", err) + } + for i := 0; i < 2; i++ { + if err := crossHost.sendMessage(message); err != nil { + t.Fatalf("send %d: %v", i, err) + } + } + if len(otherHostAuth) != 2 || otherHostAuth[0] != "" || otherHostAuth[1] != "" { + t.Errorf("token forwarded to the redirect host: %q", otherHostAuth) + } + + sameHost, err := newHTTPClient(&config{endpoint: endpoint.URL + "/moved", authBearerToken: "test-token", timeoutSeconds: 5}) + if err != nil { + t.Fatalf("Failed to create HTTP client: %v", err) + } + if err := sameHost.sendMessage(message); err != nil { + t.Fatalf("send: %v", err) + } + if len(sameHostAuth) != 1 || sameHostAuth[0] != "Bearer test-token" { + t.Errorf("same-host redirect lost the token: %q", sameHostAuth) + } +} + +func TestSameWebhookHost(t *testing.T) { + cases := []struct { + endpoint, target string + want bool + }{ + {"https://hooks.example.com/a", "https://hooks.example.com/a", true}, + {"https://hooks.example.com/a", "https://HOOKS.example.com/b", true}, + {"http://hooks.example.com/a", "https://hooks.example.com/a", true}, + {"https://hooks.example.com/a", "http://hooks.example.com/a", false}, + {"https://hooks.example.com/a", "https://hooks.example.com:8443/a", false}, + {"https://hooks.example.com/a", "https://evil.example.net/a", false}, + {"http://127.0.0.1:8080/a", "http://127.0.0.1:9090/a", false}, + {"https://hooks.example.com/a", "http://169.254.169.254/latest", false}, + {"https://hooks.example.com/a", "://bad", false}, + {"https://hooks.example.com/a", "https://hooks.example.com:443/b", true}, + {"http://hooks.example.com:80/a", "http://hooks.example.com/b", true}, + {"http://hooks.example.com:8080/a", "https://hooks.example.com/a", false}, + {"https://hooks.example.com:8443/a", "https://hooks.example.com:8443/b", true}, + {"http://[::1]:8080/a", "http://[::1]:8080/b", true}, + {"http://[::1]:8080/a", "http://[::1]:9090/a", false}, + {"http://hooks.example.com/a", "ftp://hooks.example.com/a", false}, + } + for _, c := range cases { + if got := sameWebhookHost(c.endpoint, c.target); got != c.want { + t.Errorf("sameWebhookHost(%q, %q) = %v, want %v", c.endpoint, c.target, got, c.want) + } + } +}