fix(netxlite): do not mutate outgoing requests (#508)

I have recently seen a data race related our way of
mutating the outgoing request to set the host header.

Unfortunately, I've lost track of the race output,
because I rebooted my Linux box before saving it.

Though, after inspecting why and and where we're mutating
outgoing requets, I've found that:

1. we add the host header when logging to have it logged,
which is not a big deal since we already emit the URL
rather than just the URL path when logging a request, and
so we can safely zap this piece of code;

2. as a result, in measurements we may omit the host header
but again this is pretty much obvious from the URL itself
and so it should not be very important (nonetheless,
avoid surprises and keep the existing behavior);

3. when the User-Agent header is not set, we default to
a `miniooni/0.1.0-dev` user agent, which is probably not
very useful anyway, so we can actually remove it.

Part of https://github.com/ooni/probe/issues/1733 (this diff
has been extracted from https://github.com/ooni/probe-cli/pull/506).
This commit is contained in:
Simone Basso
2021-09-27 13:35:47 +02:00
committed by GitHub
parent 741a8bc4c2
commit deb1589bdb
9 changed files with 55 additions and 164 deletions
+3 -35
View File
@@ -31,16 +31,6 @@ type httpTransportLogger struct {
var _ HTTPTransport = &httpTransportLogger{}
func (txp *httpTransportLogger) RoundTrip(req *http.Request) (*http.Response, error) {
host := req.Host
if host == "" {
host = req.URL.Host
}
req.Header.Set("Host", host) // anticipate what Go would do
return txp.logTrip(req)
}
// logTrip is an HTTP round trip with logging.
func (txp *httpTransportLogger) logTrip(req *http.Request) (*http.Response, error) {
txp.Logger.Debugf("> %s %s", req.Method, req.URL.String())
for key, values := range req.Header {
for _, value := range values {
@@ -149,15 +139,10 @@ func NewOOHTTPBaseTransport(dialer Dialer, tlsDialer TLSDialer) HTTPTransport {
// WrapHTTPTransport creates a new HTTP transport using
// the given logger for logging.
//
// The returned transport will set a default user agent if the
// request has not already set a user agent.
func WrapHTTPTransport(logger Logger, txp HTTPTransport) HTTPTransport {
return &httpUserAgentTransport{
HTTPTransport: &httpTransportLogger{
HTTPTransport: txp,
Logger: logger,
},
return &httpTransportLogger{
HTTPTransport: txp,
Logger: logger,
}
}
@@ -248,23 +233,6 @@ func (c *httpTLSConnWithReadTimeout) Read(b []byte) (int, error) {
return c.TLSConn.Read(b)
}
// httpUserAgentTransport is a transport that ensures that we always
// set an OONI specific default User-Agent header.
type httpUserAgentTransport struct {
HTTPTransport
}
const defaultHTTPUserAgent = "miniooni/0.1.0-dev"
func (txp *httpUserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", defaultHTTPUserAgent)
}
return txp.HTTPTransport.RoundTrip(req)
}
var _ HTTPTransport = &httpUserAgentTransport{}
// NewHTTPTransportStdlib creates a new HTTPTransport that uses
// the Go standard library for all operations, including DNS
// resolutions and TLS handshakes.
+1 -92
View File
@@ -6,7 +6,6 @@ import (
"io"
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"
@@ -51,39 +50,6 @@ func TestHTTPTransportLogger(t *testing.T) {
}
})
t.Run("we add the host header", func(t *testing.T) {
foundHost := &atomicx.Int64{}
txp := &httpTransportLogger{
Logger: log.Log,
HTTPTransport: &mocks.HTTPTransport{
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
if req.Header.Get("Host") == "www.google.com" {
foundHost.Add(1)
}
return nil, io.EOF
},
},
}
req := &http.Request{
Header: http.Header{},
URL: &url.URL{
Scheme: "https",
Host: "www.google.com",
Path: "/",
},
}
resp, err := txp.RoundTrip(req)
if !errors.Is(err, io.EOF) {
t.Fatal("not the error we expected")
}
if resp != nil {
t.Fatal("expected nil response here")
}
if foundHost.Load() != 1 {
t.Fatal("host header was not added")
}
})
t.Run("with success", func(t *testing.T) {
var count int
lo := &mocks.Logger{
@@ -224,8 +190,7 @@ func TestNewHTTPTransport(t *testing.T) {
d := &mocks.Dialer{}
td := &mocks.TLSDialer{}
txp := NewHTTPTransport(log.Log, d, td)
ua := txp.(*httpUserAgentTransport)
logger := ua.HTTPTransport.(*httpTransportLogger)
logger := txp.(*httpTransportLogger)
if logger.Logger != log.Log {
t.Fatal("invalid logger")
}
@@ -425,62 +390,6 @@ func TestHTTPTLSDialerWithReadTimeout(t *testing.T) {
})
}
func TestHTTPUserAgentTransport(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) {
var called bool
txp := &httpUserAgentTransport{
HTTPTransport: &mocks.HTTPTransport{
MockCloseIdleConnections: func() {
called = true
},
},
}
txp.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
})
t.Run("RoundTrip", func(t *testing.T) {
t.Run("without an user-agent", func(t *testing.T) {
var ua string
txp := &httpUserAgentTransport{
HTTPTransport: &mocks.HTTPTransport{
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
ua = req.Header.Get("User-Agent")
return &http.Response{}, nil
},
},
}
txp.RoundTrip(&http.Request{Header: make(http.Header)})
if ua != defaultHTTPUserAgent {
t.Fatal("not the expected user-agent")
}
})
t.Run("with an user-agent", func(t *testing.T) {
var ua string
expected := "antani/1.0"
txp := &httpUserAgentTransport{
HTTPTransport: &mocks.HTTPTransport{
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
ua = req.Header.Get("User-Agent")
return &http.Response{}, nil
},
},
}
txp.RoundTrip(&http.Request{
Header: http.Header{
"User-Agent": {expected},
},
})
if ua != expected {
t.Fatal("not the expected user-agent")
}
})
})
}
func TestNewHTTPTransportStdlib(t *testing.T) {
// What to test about this factory?
txp := NewHTTPTransportStdlib(log.Log)
-1
View File
@@ -32,7 +32,6 @@ type (
TLSHandshakerLogger = tlsHandshakerLogger
DialerSystem = dialerSystem
TLSDialerLegacy = tlsDialer
UserAgentTransport = httpUserAgentTransport
AddressResolver = resolverShortCircuitIPAddr
)