Request Hijacking Vulnerability in RubyGems 2.6.13 and earlier
The api_endpoint function in RubyGems still returned the full DNS SRV response string after only checking its host part, so a malicious DNS response could carry URI syntax that changes how the value is read later. The reporter found no working exploit in the current code, but warned that future callers could be tricked into fetching a different path, possibly enabling a downgrade attack or swapping one gem for another.
- Disclosed after 14 months
We received this report via security@ from [email protected], I'm filing here for tracking and visibility purposes...
"I was looking at commit 8d91516fb7037ecfb27622f605dc40245e0f8d32, which was the fix for the DNS hijacking issue CVE-2017-0902. The function still handles the DNS response in a potentially unsafe way. I did not find any actual vulnerabilities in the current code; the code that uses the result of api_endpoint (perhaps coincidentally) discards the potentially malicious components of the URI that api_endpoint returns. But future code may be vulnerable. I'm sending this to the security list because my checks for vulnerability may be incomplete.
The problem is that api_endpoint allows the DNS SRV response to contain URI-like syntax (which was the cause of CVE-2017-0902). The fix was to parse the syntax as if it were a URI, extract the host component, and only do a comparison using the host component, rather than the entire string. However, the entire string is still pasted into the return value, assuming the comparison succeeds. It can contain URI syntax characters like '?' and '#' that change the interpretation of what follows them.
I'm attaching a patch that adds a new test and changes api_endpoint to discard everything but the host after parsing the DNS SRV response as a URI. It would probably be even better simply to disallow any syntax other than hostname literals.
The lines that I initially thought was vulnerable, but appear not to be, are in lib/rubygems/source.rb: bundler_api_uri = api_uri + './api/v1/dependencies' uri = api_uri + "#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}" spec_path = api_uri + "#{file_name}.gz" The reason they are not vulnerable is that api_url is a URI object rather than a string, so the + operator is actually the merge method rather than string concatenation. The merge operator replaces any existing path, query, and fragment components, it seems. (It would not help if the attacker-provided string changed the URI's host, pass, or port components, but I could not think of a realistic path to exploitation using only those components.) However if api_uri had been coerced into a string, then the code would be vulnerable. An attacker could cause the client to download some other path, which could possibly lead to a downgrade attack or replacing one gem with another. <0001-Have-api_endpoint-replace-only-host-not-port-user-pa.patch>"
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the fix for an earlier DNS hijacking bug validated only the host component of the SRV response but still returned the unmodified string, leaving URI syntax characters in the value.
Impact
The reporter found no actual vulnerability in the current code because the URI merge method discards the attacker-controlled path, query and fragment, but future code that coerces the value to a string could let an attacker redirect downloads, possibly causing a downgrade attack or replacing one gem with another.
What the attacker needed
Attacker controls DNS SRV response; client resolves gems via SRV record; future code coerces value to string
The fix
A patch was proposed that makes api_endpoint discard everything except the host after parsing the SRV response as a URI, and the reporter suggests simply disallowing any syntax other than hostname literals.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
RubyGems is the package manager for Ruby, and it can look up where to download gems by asking DNS, the internet's name system, for a special record. A DNS record can be controlled by whoever runs the domain, so its contents must be treated as untrusted. Here the code checked only the hostname part of that record but passed the whole text along, which could confuse later code that builds download addresses.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.