All checks were successful
check / check (push) Successful in 2m56s
Three findings from the review of the per-target request headers feature, plus the follow-up they raised about the inbound headers the same delivery path forwards. One rule now governs every header a delivery carries on someone else's behalf: a redirect hop that leaves the origin the target names carries none of them. That covers the operator's configured headers and the inbound event headers forwarded from the sender alike. net/http withholds only Authorization and Cookie across a host change, so an operator's X-Api-Key or a sender's X-Hub-Signature would otherwise follow a 302 to a host nobody configured. Redirects are still followed — refusing them would break every destination that legitimately redirects and would record the 3xx as the delivery's result — but a hop to another host, another port, or down from https to http drops the lot. The shared SSRF-safe transport is kept on that client, so each hop is still dialled through the private-IP guard. The set to strip is not a name list. applyRequestHeaders now returns the canonical names of everything it applied on the sender's or operator's behalf, and the redirect policy strips exactly that, so a header added to the forward set is covered without a second edit. Content-Type and User-Agent are the delivery path's own rather than anyone else's, and both are excluded from that set so they always travel: Content-Type is set from the event and a 307 preserves the body across hosts, so it has to stay typed, and User-Agent is overwritten with this delivery path's own after the forwarded headers are applied, so the sender's never reaches the wire and stripping it off-origin would only substitute net/http's default. The origin comparison no longer collapses two IPv6 origins into one. Hostname() unwraps a literal's brackets, so re-appending the port with a bare colon rendered https://[2001:db8::1]:8080 and https://[2001:db8::1:8080] identically — a different address on a different port passing as the same origin. The port is joined with net.JoinHostPort, and both spellings are in TestSameDeliveryOrigin. The ten-hop cap gains a regression test. Installing a CheckRedirect is precisely what discards net/http's own limit, so a self-redirecting destination is driven through the policy and asserted to stop after exactly ten requests with the sentinel surfacing to the caller. Trailer joins the reserved names. net/http strips it from the request it writes, so a configured one was accepted, stored, and provably never sent. The invalid-header-name error no longer quotes the text before the first colon. That text is only a name if it parses as one; when it does not, a pasted value whose own colon split the line put half a token into a 400 body. TestParseTargetHeaders_ErrorsNeverQuoteAValue asserted this invariant while only exercising the after-the-colon case, and now covers the before-the-colon one. README documents the http target's config keys, the 300-second timeout ceiling, the reserved-header list and the redirect behaviour as one rule over both header classes, including that the drop is per hop rather than permanent: net/http re-copies the initial request's headers each hop, so a chain returning to the configured origin carries them again, exactly as it treats Authorization. It also records what following a 301, 302 or 303 costs, since that is net/http's own behaviour and the decision to follow redirects is what buys it: the POST becomes a GET and the event body and its Content-Type are dropped, so the destination the chain ends at receives no event while the delivery is still recorded Delivered. The edit form's hint gains Trailer and the redirect note. Closes #243
151 lines
3.6 KiB
Go
151 lines
3.6 KiB
Go
package delivery_test
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"sneak.berlin/go/webhooker/internal/delivery"
|
|
)
|
|
|
|
// newSSRFTestEngine builds an Engine whose shared client
|
|
// carries the SSRF-safe transport, mirroring production.
|
|
func newSSRFTestEngine() *delivery.Engine {
|
|
log := slog.New(slog.DiscardHandler)
|
|
|
|
client := &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: delivery.NewTestGuard().
|
|
NewSSRFSafeTransport(),
|
|
}
|
|
|
|
return delivery.NewTestEngine(log, client, 1)
|
|
}
|
|
|
|
// TestClientForRequest_TimeoutKeepsSSRFGuard asserts that a
|
|
// client returned by clientForRequest for a config with a
|
|
// per-target timeout still refuses connections to
|
|
// private/reserved addresses (the timeout must not drop the
|
|
// SSRF-safe transport).
|
|
func TestClientForRequest_TimeoutKeepsSSRFGuard(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
engine := newSSRFTestEngine()
|
|
|
|
blocked := []string{
|
|
loopbackHookURL,
|
|
metadataURL,
|
|
"http://[fe80::1]/hook",
|
|
}
|
|
|
|
for _, target := range blocked {
|
|
t.Run(target, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cfg := &delivery.HTTPTargetConfig{
|
|
URL: target,
|
|
Timeout: 5,
|
|
}
|
|
|
|
client := engine.ExportClientForRequest(cfg, nil)
|
|
|
|
require.NotSame(t, engine.ExportClient(), client,
|
|
"a per-target timeout must yield a "+
|
|
"distinct client",
|
|
)
|
|
|
|
assert.Equal(t,
|
|
5*time.Second, client.Timeout,
|
|
"the per-target timeout must be applied",
|
|
)
|
|
|
|
assert.Same(t,
|
|
engine.ExportClient().Transport,
|
|
client.Transport,
|
|
"the SSRF-safe transport must be reused, "+
|
|
"not dropped",
|
|
)
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
context.Background(),
|
|
http.MethodPost, target, nil,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
resp, doErr := client.Do(req)
|
|
if resp != nil {
|
|
_ = resp.Body.Close()
|
|
}
|
|
|
|
require.Error(t, doErr,
|
|
"request to %s must be blocked", target,
|
|
)
|
|
|
|
assert.Contains(t, doErr.Error(), "blocked",
|
|
"error must come from the SSRF guard",
|
|
)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestClientForRequest_NoTimeoutUnchanged asserts that a
|
|
// request with neither a per-target timeout nor an origin-scoped
|
|
// header gets the shared SSRF-safe client unchanged: there is then
|
|
// nothing for a redirect policy to strip.
|
|
func TestClientForRequest_NoTimeoutUnchanged(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
engine := newSSRFTestEngine()
|
|
|
|
cfg := &delivery.HTTPTargetConfig{
|
|
URL: "https://example.com/hook",
|
|
}
|
|
|
|
client := engine.ExportClientForRequest(cfg, nil)
|
|
|
|
assert.Same(t, engine.ExportClient(), client,
|
|
"without a per-target timeout the shared client "+
|
|
"must be returned unchanged",
|
|
)
|
|
}
|
|
|
|
// TestClientForRequest_HeadersKeepSSRFGuard asserts that the
|
|
// redirect policy an origin-scoped header installs is added to a
|
|
// client that still carries the SSRF-safe transport. The guard is
|
|
// a dial hook, so keeping it is what makes each redirect hop pass
|
|
// the private-IP check too.
|
|
func TestClientForRequest_HeadersKeepSSRFGuard(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
engine := newSSRFTestEngine()
|
|
|
|
cfg := &delivery.HTTPTargetConfig{
|
|
URL: "https://example.com/with-headers",
|
|
Headers: map[string]string{
|
|
"X-Api-Key": "configured",
|
|
},
|
|
}
|
|
|
|
client := engine.ExportClientForRequest(
|
|
cfg, []string{"X-Api-Key"},
|
|
)
|
|
|
|
require.NotNil(t, client.CheckRedirect,
|
|
"an origin-scoped header must install a redirect policy",
|
|
)
|
|
|
|
assert.Same(t,
|
|
engine.ExportClient().Transport, client.Transport,
|
|
"the SSRF-safe transport must be reused, not dropped",
|
|
)
|
|
|
|
assert.Equal(t,
|
|
engine.ExportClient().Timeout, client.Timeout,
|
|
"the shared client's timeout must be inherited",
|
|
)
|
|
}
|