bogdanfinn / tls-client

net/http.Client like HTTP Client with options to select specific client TLS Fingerprints to use for requests.
BSD 4-Clause "Original" or "Old" License
860 stars 164 forks source link

Fixed `connectHeaders` concurrent map read & write #127

Closed Dharmey747 closed 3 months ago

Dharmey747 commented 3 months ago

In the latest update the possibility to define CONNECT headers was implemented, I encountered a bug with this new feature when setting a proxy to the client instance very quickly, that causes a race condition.

When setting a new proxy, the connectDialer will write the proxy authentication credentials to the DefaultHeader map. This caused no issue in previous versions because a new header map was spawned every time (make(http.Header)) but in this version the same map is used, the connectHeaders one.

Since writing and reading a map concurrently in go isn't supported, when lots of proxy change operations are happening it will eventually panic because two threads will try to write to the same map at the same time.

To fix this, I've implemented the built-in Clone() method to clone the header map, so every instance will have its own header map and nothing else will be trying to read or write to it.

bogdanfinn commented 3 months ago

@Dharmey747 thank you.