test: add concurrency test for digestAuthTransport to ensure thread safety

- Introduced a new test, TestDigestAuthTransportConcurrency, to validate concurrent access to the digestAuthTransport.
- Implemented checks to ensure the nonce count (nc) is correctly incremented and protected from race conditions using a mutex.
- Enhanced the digestAuthTransport struct to include a mutex for safe concurrent operations.
This commit is contained in:
0x524a
2025-12-01 23:35:15 -05:00
parent 1f68023dbe
commit 08d55b4cb9
2 changed files with 106 additions and 2 deletions
+7 -1
View File
@@ -401,6 +401,7 @@ type digestAuthTransport struct {
username string
password string
nc int
ncMu sync.Mutex // Protects nc field from concurrent access
}
// RoundTrip implements http.RoundTripper with digest auth support
@@ -452,8 +453,13 @@ func (d *digestAuthTransport) createDigestAuthHeader(req *http.Request, authHead
method := req.Method
ha2 := md5Hash(method + ":" + uri)
// Increment nonce count atomically to prevent race conditions
// HTTP transports must be safe for concurrent use
d.ncMu.Lock()
d.nc++
ncStr := fmt.Sprintf("%08x", d.nc)
nc := d.nc
d.ncMu.Unlock()
ncStr := fmt.Sprintf("%08x", nc)
cnonce := generateNonce()
var responseStr string