kepner_53726 (Discord, magpie 0.1.2926): a custom Anthropic provider at a
company intranet gateway failed 124 turns in 3 days mid-stream with "tls:
bad record MAC" / "received record with version eaf1", each a 200 with 0
output tokens, until a restart; a plain Go 1.26.8 client to the gateway held.
Cause: none found in magpie. The bytes are read by stdlib crypto/tls, and
every conn wrapper on the path only passes them through, so a record whose
MAC fails was mangled before it reached us: by the gateway or a box between
(the system proxy route, a TLS-inspecting middlebox). HTTP/2 meets it far
more than one-off clients do: one long-lived connection carries every
stream, plus magpie's health pings (netproxy.HTTP2Health), so a gateway
that interleaves its writes racily breaks it.
- Provider.HTTP1 ("HTTP version" row, "HTTP/1.1 only" tick, under the
editor's Advanced; shown for a provider of the user's own at an https://
address, the same set InsecureTLS takes). Via adds netproxy.WithHTTP1 for
its own base URLs' hosts; Dispatch sends those over its HTTP/1.1 copy.
Another host, and a request made for another provider (With), keep HTTP/2.
Save refuses it on a preset's; a save that leaves the key out keeps it.
- With no setting: a TLS record error read on an HTTP/2 reply (h2Watch) or
round trip marks the host:port, and Dispatch speaks HTTP/1.1 to it for 6
hours. Go's transport already drops the broken connection. Zero-config
default otherwise unchanged.
- Advanced opens by itself when the tick is set. No note under the row
(the core script budget, not raised).
Run (mote, at 1ccad582d plus this; budget and these tests again at a10ad0ec8 plus this): gofmt -l; go vet of netproxy,
provider, gui; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet
./...; go test -tags nogui -run 'HTTP1|RecordError|H2Record|InsecureTLS|
Dispatch|With|Budget|I18n' over netproxy, provider and gui, ok;
TestCoreScriptBudget ok after trimming. GUI: provider-http1 (new, 60 of 60,
chromium+webkit, 5 languages, 1100/420), provider-insecure-tls,
provider-advanced-fold, editor-advanced-align, provider-limit-rest,
i18n-en-no-cjk pass. i18n-keys "a plugin's words are its own" is red on
three plugin strings this change doesn't touch (library, routing, usage).
TestH2RecordErrorMovesHostToHTTP1 mangles a real TLS stream through a TCP
relay that flips a byte, and checks the next request goes over HTTP/1.1.
Not broken by hand yet; not tried against a real company gateway, or in
the real app window.
Reported-by: kepner_53726 (Discord)
128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
package netproxy
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type http1Key struct{}
|
|
|
|
// WithHTTP1 is ctx for requests made on behalf of a provider the user
|
|
// added and asked to be spoken HTTP/1.1 to (kepner_53726 on Discord: a
|
|
// company gateway whose HTTP/2 connections broke mid-reply with "tls: bad
|
|
// record MAC", while HTTP/1.1 to it held): a request through a transport
|
|
// Dispatch made to the host of one of bases goes over HTTP/1.1 only,
|
|
// through a transport kept for HTTP/1.1. Any other host is spoken to as
|
|
// before. With (a request made for another provider) drops it.
|
|
func WithHTTP1(ctx context.Context, bases ...string) context.Context {
|
|
hosts := map[string]bool{}
|
|
for _, b := range bases {
|
|
u, err := url.Parse(strings.TrimSpace(b))
|
|
if err != nil || u.Hostname() == "" {
|
|
continue
|
|
}
|
|
hosts[hostKey(u)] = true
|
|
}
|
|
if len(hosts) == 0 {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, http1Key{}, hosts)
|
|
}
|
|
|
|
func http1Of(ctx context.Context) map[string]bool {
|
|
if ctx == nil {
|
|
return nil
|
|
}
|
|
h, _ := ctx.Value(http1Key{}).(map[string]bool)
|
|
return h
|
|
}
|
|
|
|
// http1Asked says req's provider asked for HTTP/1.1 to its host
|
|
// (WithHTTP1).
|
|
func http1Asked(req *http.Request) bool {
|
|
hosts := http1Of(req.Context())
|
|
return len(hosts) > 0 && req.URL != nil && hosts[hostKey(req.URL)]
|
|
}
|
|
|
|
// h2Broken is how long a host:port whose HTTP/2 connection carried a TLS
|
|
// record that didn't decrypt (recordError) is spoken HTTP/1.1 to.
|
|
const h2Broken = 6 * time.Hour
|
|
|
|
var h1For struct {
|
|
sync.Mutex
|
|
at map[string]time.Time // host:port → when its HTTP/2 broke
|
|
}
|
|
|
|
// h2BrokenFor says the HTTP/2 connection to req's host:port broke so
|
|
// within h2Broken.
|
|
func h2BrokenFor(req *http.Request) bool {
|
|
if req.URL == nil || !strings.EqualFold(req.URL.Scheme, "https") {
|
|
return false
|
|
}
|
|
k := hostKey(req.URL)
|
|
h1For.Lock()
|
|
defer h1For.Unlock()
|
|
at, ok := h1For.at[k]
|
|
if ok && time.Since(at) > h2Broken {
|
|
delete(h1For.at, k)
|
|
return false
|
|
}
|
|
return ok
|
|
}
|
|
|
|
func markH2Broken(req *http.Request) {
|
|
if req.URL == nil {
|
|
return
|
|
}
|
|
h1For.Lock()
|
|
defer h1For.Unlock()
|
|
if h1For.at == nil {
|
|
h1For.at = map[string]time.Time{}
|
|
}
|
|
h1For.at[hostKey(req.URL)] = time.Now()
|
|
}
|
|
|
|
// recordError says err is TLS finding the bytes it read are no record of
|
|
// its connection: one that fails its MAC ("bad record MAC"), or a header
|
|
// that is no record's ("received record with version eaf1", an oversized
|
|
// length). The connection is dead after it, and Go's transport drops it;
|
|
// what is said here is that the stream under the TLS was mangled, by the
|
|
// far side or by a box between, which HTTP/2's one connection for every
|
|
// request to a host met far more often than HTTP/1.1's.
|
|
func recordError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
var rh tls.RecordHeaderError
|
|
if errors.As(err, &rh) {
|
|
return true
|
|
}
|
|
s := err.Error()
|
|
return strings.Contains(s, "bad record MAC") || strings.Contains(s, "received record with version") ||
|
|
strings.Contains(s, "oversized record received")
|
|
}
|
|
|
|
// h2Watch is the body of a reply that came over HTTP/2: a read that fails
|
|
// with a TLS record error (recordError) has its host spoken HTTP/1.1 to
|
|
// for h2Broken, so the requests after it don't go out on another HTTP/2
|
|
// connection to break the same way.
|
|
type h2Watch struct {
|
|
io.ReadCloser
|
|
req *http.Request
|
|
}
|
|
|
|
func (b h2Watch) Read(p []byte) (int, error) {
|
|
n, err := b.ReadCloser.Read(p)
|
|
if err != nil && err != io.EOF && recordError(err) {
|
|
markH2Broken(b.req)
|
|
}
|
|
return n, err
|
|
}
|