webhook: keep the bearer token on the configured endpoint's host (#13103)

* webhook: send the bearer token only to the configured endpoint's host

A redirect to another host or port, or from https to http, now gets the
request without the Authorization header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: test the token scope across redirects

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: compare effective ports when scoping the token

An omitted default port and an explicit :443 or :80 are the same
endpoint; http on port 80 upgrading to https on 443 keeps the token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-28 22:17:34 -08:00
1 parent 4ff3147ee5
commit 7092edfb06
2 files changed
+114 -1

No files matched your search

+36 -1
View File
@@ -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
+78
View File
@@ -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)
}
}
}