carlos / platform Public

billingstripe: the write transport offers only HTTP/1.1 by ALPN; Stripe was negotiating h2 and every billing POST failed as no response

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 8241475139a77cafd72bf6d9e261b20481605571 parent 20a8267
2 files changed, +122 −0
  • internal/billingstripe/client.go +11 −0
  • internal/billingstripe/write_transport_test.go +111 −0

CI cancelled — superseded by a newer push — run details

diff --git a/internal/billingstripe/client.go b/internal/billingstripe/client.go
index e60a8699d..78ed6299d 100644
--- a/internal/billingstripe/client.go
+++ b/internal/billingstripe/client.go
@@ -166,6 +166,17 @@ func newWriteTransport() *http.Transport {
t.DisableKeepAlives = true
t.ForceAttemptHTTP2 = false
t.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
+ // Clone copies DefaultTransport's TLS config, and once DefaultTransport
+ // has been used (the read client does) its NextProtos hold "h2". With
+ // that, the handshake offers h2 by ALPN, Stripe picks it, and this
+ // HTTP/1.x transport reads an HTTP/2 SETTINGS frame as a malformed
+ // response. Offer HTTP/1.1 only, on a config of our own.
+ if t.TLSClientConfig != nil {
+ t.TLSClientConfig = t.TLSClientConfig.Clone()
+ } else {
+ t.TLSClientConfig = &tls.Config{}
+ }
+ t.TLSClientConfig.NextProtos = []string{"http/1.1"}
return t
}
diff --git a/internal/billingstripe/write_transport_test.go b/internal/billingstripe/write_transport_test.go
new file mode 100644
index 000000000..88e437ca9
--- /dev/null
+++ b/internal/billingstripe/write_transport_test.go
@@ -0,0 +1,111 @@
+package billingstripe
+
+import (
+ "context"
+ "crypto/x509"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "testing"
+)
+
+// TestWriteTransport_NegotiatesHTTP11AgainstH2Server pins the production
+// failure: a TLS server that speaks h2 picked it by ALPN because the write
+// transport (cloned from http.DefaultTransport) still offered it, and the
+// HTTP/1.x transport then choked on the server's HTTP/2 SETTINGS frame.
+func TestWriteTransport_NegotiatesHTTP11AgainstH2Server(t *testing.T) {
+ // Make DefaultTransport look as it does in a running process, where
+ // reads have already used it: its TLS config offers h2.
+ dt := http.DefaultTransport.(*http.Transport)
+ if dt.TLSClientConfig == nil || !containsProto(dt.TLSClientConfig.NextProtos, "h2") {
+ warm := httptest.NewUnstartedServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
+ warm.EnableHTTP2 = true
+ warm.StartTLS()
+ defer warm.Close()
+ pool := x509.NewCertPool()
+ pool.AddCert(warm.Certificate())
+ tr := dt.Clone()
+ tr.ForceAttemptHTTP2 = true
+ if tr.TLSClientConfig == nil {
+ tr.TLSClientConfig = warm.Client().Transport.(*http.Transport).TLSClientConfig.Clone()
+ }
+ tr.TLSClientConfig.RootCAs = pool
+ resp, err := (&http.Client{Transport: tr}).Get(warm.URL)
+ if err != nil {
+ t.Fatalf("warm-up request: %v", err)
+ }
+ resp.Body.Close()
+ tr.CloseIdleConnections()
+ }
+
+ var proto string
+ srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ proto = r.Proto
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"id":"cus_1"}`))
+ }))
+ srv.EnableHTTP2 = true
+ srv.StartTLS()
+ defer srv.Close()
+
+ c := testClient(t, srv.URL)
+ wt := c.writeClient.Transport.(*http.Transport)
+ if wt.TLSClientConfig == nil {
+ wt.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone()
+ }
+ pool := x509.NewCertPool()
+ pool.AddCert(srv.Certificate())
+ wt.TLSClientConfig.RootCAs = pool
+
+ var out struct {
+ ID string `json:"id"`
+ }
+ if err := c.do(context.Background(), http.MethodPost, "/v1/customers", nil, "idem-1", &out); err != nil {
+ t.Fatalf("POST through the write path: %v", err)
+ }
+ if out.ID != "cus_1" {
+ t.Fatalf("decoded id = %q, want cus_1", out.ID)
+ }
+ if proto != "HTTP/1.1" {
+ t.Fatalf("server saw %q, want HTTP/1.1", proto)
+ }
+}
+
+func TestNewWriteTransport_OffersOnlyHTTP11(t *testing.T) {
+ dt := http.DefaultTransport.(*http.Transport)
+ // Ensure DefaultTransport's TLS config is populated, as it is once used.
+ _ = dt.Clone()
+ var before []string
+ var hadConfig bool
+ if dt.TLSClientConfig != nil {
+ hadConfig = true
+ before = append([]string(nil), dt.TLSClientConfig.NextProtos...)
+ }
+ cfgBefore := dt.TLSClientConfig
+
+ wt := newWriteTransport()
+ if wt.TLSClientConfig == nil {
+ t.Fatal("write transport has no TLS config")
+ }
+ if got, want := wt.TLSClientConfig.NextProtos, []string{"http/1.1"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("NextProtos = %v, want %v", got, want)
+ }
+ if dt.TLSClientConfig != cfgBefore {
+ t.Fatal("DefaultTransport's TLSClientConfig pointer was replaced")
+ }
+ if wt.TLSClientConfig == dt.TLSClientConfig {
+ t.Fatal("write transport shares DefaultTransport's TLSClientConfig")
+ }
+ if hadConfig && !reflect.DeepEqual(dt.TLSClientConfig.NextProtos, before) {
+ t.Fatalf("DefaultTransport NextProtos mutated: %v, was %v", dt.TLSClientConfig.NextProtos, before)
+ }
+}
+
+func containsProto(ps []string, p string) bool {
+ for _, s := range ps {
+ if s == p {
+ return true
+ }
+ }
+ return false
+}