The vulnerability lies in the processing of the Access-Control-Request-Headers (ACRH) header in preflight requests. When this header contains a large number of commas, the old code in github.com/rs/cors would lead to excessive heap allocations and CPU usage, causing a denial of service.
The analysis of the patch 4c32059b2756926619f6bf70281b91be7b5dddb2 reveals the vulnerable pathway:
(*Cors).handlePreflight in cors.go was the entry point that received the request and initiated the processing of the ACRH header.
- It called a sequence of functions, including
splitHeaderValues (from utils.go), which used strings.Split to break the header value by commas. This was the primary source of excessive heap allocations, as a large number of commas would create a correspondingly large number of string objects and a large slice to hold them.
- The result was then passed to
convertDidCopy (from utils.go), which could perform further large allocations if header canonicalization resulted in changes to the already large slice of headers.
- Finally,
(*Cors).areHeadersAllowed (from cors.go) would iterate over this potentially massive list of headers, consuming CPU resources.
All these functions were part of the call chain that processed the malicious input and would appear in a runtime profile during exploitation. The patch addresses this by removing splitHeaderValues and areHeadersAllowed, and by introducing a new internal.SortedSet type with a Subsumes method that processes the ACRH header in chunks to prevent excessive allocations and processing.