saucelabs/forwarder

Potential data loss in dialer via HTTP proxy

Closed this issue · 1 comments

Hello,

I think there is potential data loss possible in routine responsible for connection via upstream HTTP CONNECT proxy:

https://github.com/saucelabs/forwarder/blob/3bb09ce8707de386003389b0fc4e496cf11b61b0/dialvia/http.go#L110C1-L110C1

It wraps connection into a buffered reader and then it uses such buffered reader to read upstream proxy response. But buffered reader will read as much as it is available in the socket up to reader's buffer size. If some data happen to arrive right together with proxy response, reader will consume that chunk of data as well. Once response is read, that buffered reader and content in its buffer is discarded, so under certain rare conditions there is a possible data loss.

It's not an issue for protocols where client speaks first (HTTP, TLS and so on), server won't speak before client anyway. However, I think it might be an issue for protocols where servers speaks first right after client connection (SMTP, FTP, SSH, ...).

I see three potential solutions:

  • Read byte by byte until proxy response is read, but still return dialed connection not wrapped. This is how curl reads HTTP connect proxy response.
  • Retain wrapping and return conn wrapped with buffered reader.
  • Wrap connection into an object which feed leftovers in buffered reader first and then continue to reading from connection.

@Snawoot thanks for reporting this. FYI I sent a PR to address this issue.