Files
2026-09-27 15:00:50 -05:00

209 lines
5.9 KiB
Go

package retriable
import (
"bufio"
"bytes"
"context"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/cockroachdb/errors"
"github.com/effective-security/porto/xhttp/header"
)
const redactedHeaderValue = "[REDACTED]"
// DumpRequestOut returns the wire representation of an outgoing client
// request, like httputil.DumpRequestOut: it includes any headers that the
// standard http.Transport adds, such as User-Agent. When body is true the
// request body is read into memory and restored on req.
//
// Sensitive credential headers are redacted in the dump without changing req.
// A nil request or URL returns an error. This is adapted from the Go standard
// library implementation.
func DumpRequestOut(req *http.Request, body bool) ([]byte, error) {
if req == nil {
return nil, errors.New("request is nil")
}
if req.URL == nil {
return nil, errors.New("http: nil Request.URL")
}
dumpReq := req.Clone(req.Context())
for name := range dumpReq.Header {
if strings.EqualFold(name, header.Authorization) ||
strings.EqualFold(name, header.DPoP) ||
strings.EqualFold(name, header.Cookie) ||
strings.EqualFold(name, header.ProxyAuthorization) {
dumpReq.Header[name] = []string{redactedHeaderValue}
}
}
dumpReq.URL.User = nil
save := req.Body
dummyBody := false
if !body {
contentLength := outgoingLength(req)
if contentLength != 0 {
dumpReq.Body = io.NopCloser(io.LimitReader(neverEnding('x'), contentLength))
dummyBody = true
}
} else {
var err error
save, dumpReq.Body, err = drainBody(req.Body)
if err != nil {
return nil, err
}
defer func() { req.Body = save }()
}
// Since we're using the actual Transport code to write the request,
// switch to http so the Transport doesn't try to do an SSL
// negotiation with our dumpConn and its bytes.Buffer & pipe.
// The wire format for https and http are the same, anyway.
reqSend := dumpReq
if dumpReq.URL.Scheme == "https" {
reqSend = new(http.Request)
*reqSend = *dumpReq
reqSend.URL = new(url.URL)
*reqSend.URL = *dumpReq.URL
reqSend.URL.Scheme = "http"
}
// Use the actual Transport code to record what we would send
// on the wire, but not using TCP. Use a Transport with a
// custom dialer that returns a fake net.Conn that waits
// for the full input (and recording it), and then responds
// with a dummy response.
var buf bytes.Buffer // records the output
pr, pw := io.Pipe()
defer pr.Close()
defer pw.Close()
dr := &delegateReader{c: make(chan io.Reader)}
t := &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return &dumpConn{io.MultiWriter(&buf, pw), dr}, nil
},
}
defer t.CloseIdleConnections()
// We need this channel to ensure that the reader
// goroutine exits if t.RoundTrip returns an error.
// See golang.org/issue/32571.
quitReadCh := make(chan struct{})
// Wait for the request before replying with a dummy response:
go func() {
req, err := http.ReadRequest(bufio.NewReader(pr))
if err == nil {
// Ensure all the body is read; otherwise
// we'll get a partial dump.
_, _ = io.Copy(io.Discard, req.Body)
_ = req.Body.Close()
}
select {
case dr.c <- strings.NewReader("HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n"):
case <-quitReadCh:
// Ensure delegateReader.Read doesn't block forever if we get an error.
close(dr.c)
}
}()
_, err := t.RoundTrip(reqSend)
if err != nil {
pw.Close()
dr.err = err
close(quitReadCh)
return nil, err
}
dump := buf.Bytes()
// If we used a dummy body above, remove it now.
// TODO: if the req.ContentLength is large, we allocate memory
// unnecessarily just to slice it off here. But this is just
// a debug function, so this is acceptable for now. We could
// discard the body earlier if this matters.
if dummyBody {
if i := bytes.Index(dump, []byte("\r\n\r\n")); i >= 0 {
dump = dump[:i+4]
}
}
return dump, nil
}
// drainBody reads all of b to memory and then returns two equivalent
// ReadClosers yielding the same bytes.
//
// It returns an error if the initial slurp of all bytes fails. It does not attempt
// to make the returned ReadClosers have identical error-matching behavior.
func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
if b == nil || b == http.NoBody {
// No copying needed. Preserve the magic sentinel meaning of NoBody.
return http.NoBody, http.NoBody, nil
}
var buf bytes.Buffer
if _, err = buf.ReadFrom(b); err != nil {
return nil, b, err
}
if err = b.Close(); err != nil {
return nil, b, err
}
return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
}
type neverEnding byte
func (b neverEnding) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = byte(b)
}
return len(p), nil
}
// outgoingLength is a copy of the unexported
// (*http.Request).outgoingLength method.
func outgoingLength(req *http.Request) int64 {
if req.Body == nil || req.Body == http.NoBody {
return 0
}
if req.ContentLength != 0 {
return req.ContentLength
}
return -1
}
// delegateReader is a reader that delegates to another reader,
// once it arrives on a channel.
type delegateReader struct {
c chan io.Reader
err error // only used if r is nil and c is closed.
r io.Reader // nil until received from c
}
func (r *delegateReader) Read(p []byte) (int, error) {
if r.r == nil {
var ok bool
if r.r, ok = <-r.c; !ok {
return 0, r.err
}
}
return r.r.Read(p)
}
// dumpConn is a net.Conn which writes to Writer and reads from Reader
type dumpConn struct {
io.Writer
io.Reader
}
func (c *dumpConn) Close() error { return nil }
func (c *dumpConn) LocalAddr() net.Addr { return nil }
func (c *dumpConn) RemoteAddr() net.Addr { return nil }
func (c *dumpConn) SetDeadline(time.Time) error { return nil }
func (c *dumpConn) SetReadDeadline(time.Time) error { return nil }
func (c *dumpConn) SetWriteDeadline(time.Time) error { return nil }