A Deep Dive of CVE-2022–33987 (Got allows a redirect to a UNIX socket)
The got library allowed redirects to unix:// socket URLs, so an attacker who could supply a URL to an application using got could cause the server to send requests to local Unix sockets such as the Docker socket. This could lead to server-side request forgery and potentially control of local services.
Published on itnext.io
Chaim Sanders
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
MediaWiki is the PHP application behind Wikipedia and many other wikis. Like most large projects it publishes security releases, and the author subscribes to those announcements for several PHP projects, on the reasoning that heavily reviewed code is interesting to read. When MediaWiki published a new security release, the author decided to look at the changes in depth.
The subject of this write-up is not a MediaWiki vulnerability in the usual sense. It is CVE-2022-33987, a flaw in got, a Node HTTP request library, which MediaWiki pulled in through its skins and extensions. The feature at the centre of it is got's support for making HTTP requests over Unix domain sockets, and specifically what happens when a redirect points at such a socket.
Setup
The work here is source review rather than live testing. The tools and preconditions are:
- Subscriptions to security release announcements for PHP projects.
- GitHub's branch and tag comparison feature, to diff releases.
- MediaWiki's Phabricator project information, in particular the task at https://phabricator.wikimedia.org/T311360.
- The 2.81.4 release, whose commits were reviewed.
- The got source and documentation, plus the deprecated request framework for comparison.
No accounts, roles or special access are involved.
Finding it
Going through the commits in the 2.81.4 release, the author sorted the changes into three rough groups:
- Preparing for PHP 9's deprecation of null types passed to built-in functions expecting strings.
- Dependency updates, some of them security updates.
- Fixing performance issues from a recent change.
In the second group, several commits carried this message:
Updating got to 11.8.5* GHSA-pfrx-2q88-qq97
So MediaWiki skins and extensions were running vulnerable versions of got. The author notes the irony of a PHP project's security release being driven by a Node issue, and lists the submodules whose got versions were bumped:
skins/MinervaNeueextensions/Mathextensions/AbuseFilter
The upstream fix had been published on 25 May 2022, well before this release.
Exploitation
The scheme handling
got added support for requests via Unix domain sockets back in September 2015, in version 4.1.1. The stated purpose was simple: users may want to make requests to sockets running on the local server. That is not unlike making requests to localhost, except that Unix sockets tend to expose more powerful IPC tooling; the use cases given in the original discussion were docker and fleet.
got ended up supporting two forms: the direct Unix scheme unix:/ and an HTTP Unix scheme http://unix:/. The latter formatting was borrowed from the now deprecated request framework. The direct unix:/ scheme did not actually work, at least in 11.8.3, because a dependency, http-timer, could not parse that URL format:
at origin.emit (node_modules/@szmarczak/http-timer/dist/source/index.js:43:20)code: 'ERR_INVALID_PROTOCOL'
The redirect problem
The core issue is not making Unix socket requests as such, but how redirects were handled. If a malicious user could supply a URL to a vulnerable application, they could supply a URL that resolved to a 30x redirect ending at a unix:// socket location. Requests to localhost or the local network are the definition of server-side request forgery (SSRF). Normally a developer would be expected to filter such requests, but here there was no way for a standard developer to prevent the scheme from being used, because even if they validated the initial URL, got handled the redirect automatically.
got suggests a separate library, got-ssrf, for SSRF protection. That would have prevented the issue, because the unix part of the URL becomes the URL's hostname, and got-ssrf tries and fails to resolve it, returning:
RequestError: getaddrinfo ENOTFOUND unix
What a Unix socket gives an attacker
The most obvious target is services like Docker, but the author also points to work on checking whether files exist through SSRF. Local users typically talk to the Docker socket directly with something like:
curl — silent -XGET — unix-socket /var/run/docker.sock http://localhost/version
The equivalent in got is:
got('http://unix:/var/run/docker.sock:/version')
The version endpoint is a contrived example. The Docker management API is far more powerful: depending on the request method it supports creating a container (POST), killing a container (POST), checking auth configuration (POST), executing commands in a running container (POST), getting container logs (GET), exporting a container or images (GET), and much more.
Method preservation through redirects
To make matters worse, got follows redirects for many HTTP status codes, so an attacker can use a 307 or 308 to pass a POST request and its content through to a Unix socket without rewriting it to a GET, provided the vulnerable application is crafting a POST request in the first place.
The same flaw in request
Because got borrowed the http://unix/ formatting from the deprecated request framework, the author checked whether request has the same vulnerability. It does. Strictly this should be a distinct CVE, but since request is deprecated the maintainers would be unlikely to respond. A known issue requiring a host header to be set prevents it from working in more cases. The proof of concept:
import request from 'request'// http://[redacted]:81 redirects with a 307 to http://unix:/var/run/docker.sock:/versionrequest({ uri: 'http://[redacted]:81', headers: { 'host' : "anything" }, }, function (error, response, body) { console.log('body:', body);});
Root cause
The mistake is in how got handled redirect targets. A redirect to a Unix socket URL was followed even when the originally requested URL was an ordinary HTTP URL, so a developer who validated the URL they passed in still had no control over where the request ultimately landed. The unix component of the URL becomes the hostname, which is why got-ssrf blocks it. 307 and 308 redirects preserve the method and body, so a POST and its content can reach the socket if the application crafts one.
Impact
An attacker who can supply a URL to an application using a vulnerable version of got can cause the server to issue requests to Unix domain sockets on the local host. Depending on which sockets are present and what the application's request looks like, this can reach powerful local IPC services such as the Docker management API, and can potentially be used to check whether files exist. For MediaWiki, the author found that got was seemingly used only as part of the test suites, so for the most part MediaWiki was unaffected, though updating the dependencies was still worthwhile. Some of the modules that were updated also rely on request, which is both deprecated and, as shown, vulnerable.
Fix
For most users the fix is to update got to version 11.8.4 or greater. In addition, developers who accept untrusted URLs for resolution need to be aware of SSRF and to perform URL sanitisation and validation so that they are not resolving sensitive URLs.
The change in got itself is straightforward: redirect URLs containing Unix sockets are blocked unless the initialised URL was also a Unix socket. The maintainers are additionally proposing to default an option, EnableUnixSockets, to false, disabling Unix domains entirely by default.
Variants to test
- Other HTTP client libraries that support Unix sockets. Send a URL that redirects with a 307 to
http://unix:/var/run/docker.sock:/version. A positive result is the socket's response body coming back through the client. - Libraries that borrowed the
http://unix/URL format from request. Test the same redirect chain; a positive result is the request reaching the socket, remembering that some cases require a host header to be set. - Applications that validate the initial URL but follow redirects automatically. Point the validated URL at a server you control that issues a 30x to a Unix socket URL. A positive result is the application following the redirect to the socket.
- POST-based flows. Use a 307 or 308 redirect so the method and body are preserved. A positive result is the POST body arriving at the socket rather than being rewritten to a GET.
- Hostname-based SSRF filters. Check whether the filter resolves the
unixhostname; a filter that fails resolution and blocks the request (as got-ssrf does) is working, while one that lets it through is not.
What happened, in plain words
A security researcher named Chaim Sanders reads the announcements that software projects publish when they fix security problems. He does this for several projects written in a programming language called PHP. His reasoning is simple: the code in these projects is looked at by a lot of people, so it is interesting to read. One week, the wiki software MediaWiki published such an announcement, and he decided to look closely at what had changed.
What he found was not really a MediaWiki problem. MediaWiki, the software that runs Wikipedia and many other wikis, uses small pieces of other software inside it. One of those pieces is called got, a library written in a different language, JavaScript (specifically for a runtime called Node). got is used to make web requests from one computer to another. The security flaw, tracked as CVE-2022-33987, was in got, and MediaWiki had picked it up through its skins and extensions.
The feature at the heart of it is got's ability to send requests over something called a Unix domain socket. A socket is a channel a program can use to talk to another program on the same machine. The problem was what happened when a web request was redirected to such a socket. A redirect is what a web server sends when it says "the thing you asked for is over there, go and fetch it from this other address instead". If an attacker could get an application to follow a redirect that pointed at a socket, the application would end up talking to a local service that the attacker should never have been able to reach.
This write-up walks through how he found it, why it matters, and how it was fixed. It is source review, meaning he read the code and the change history rather than attacking a live system.
Setting up the investigation
Before any of the technical detail, it helps to know what he was working with. None of this involved special accounts or access; it was all public information and public code.
- He was subscribed to the security release announcements for several PHP projects. Think of this like signing up for email alerts from a company whenever it recalls a product: you get told when something is fixed, and you can then go and look at what changed.
- He used GitHub's branch and tag comparison feature. GitHub is a website where source code is stored and tracked. Every time developers change the code they save a snapshot, and the comparison feature shows you exactly what changed between two snapshots. It is like a "track changes" view in a word processor, but for code.
- He used MediaWiki's Phabricator project information, in particular a task at https://phabricator.wikimedia.org/T311360. Phabricator is a website where a project tracks its work, bugs and discussions.
- He reviewed the commits in the 2.81.4 release of the relevant MediaWiki component. A commit is one saved change to the code, with a short message describing it.
- He read the source code and documentation of got, and also of an older library called request, for comparison.
Finding it
Going through the commits in the 2.81.4 release, he sorted the changes into three rough groups:
- Preparing for PHP 9's deprecation of null types passed to built-in functions expecting strings. In plain terms, a future version of PHP will stop accepting a certain kind of empty value in some places, and the project was getting ready for that.
- Dependency updates, some of them security updates. A dependency is a piece of software your project relies on, like a part you buy in rather than build yourself. When a dependency has a security fix, you update to the fixed version.
- Fixing performance issues from a recent change. Performance means how fast or efficiently the software runs.
The second group is the interesting one. Several commits carried this message:
Updating got to 11.8.5* GHSA-pfrx-2q88-qq97
Line by line: "Updating got to 11.8.5" says which library was updated and to which version number. The asterisk and the code "GHSA-pfrx-2q88-qq97" are an identifier for a published security advisory, a public notice that describes a known vulnerability. So this commit is saying: we are moving to a newer version of got because of a known security problem.
So MediaWiki skins and extensions were running vulnerable versions of got. A skin is the visual theme of a wiki; an extension adds features to it. The author notes the irony of a PHP project's security release being driven by an issue in a JavaScript library, and lists the submodules whose got versions were bumped:
skins/MinervaNeueextensions/Mathextensions/AbuseFilter
This is a list of the parts of MediaWiki that were updated: the MinervaNeue skin, the Math extension and the AbuseFilter extension.
The upstream fix, meaning the fix made by the people who maintain got itself, had been published on 25 May 2022, well before this MediaWiki release. That gap is worth noticing: the fix existed for a while before this project picked it up.
Exploitation
How the socket support worked
got added support for requests via Unix domain sockets back in September 2015, in version 4.1.1. The stated purpose was simple: users may want to make requests to sockets running on the local server. That is not unlike making requests to localhost, the name a computer uses for itself, except that Unix sockets tend to expose more powerful IPC tooling. IPC stands for inter-process communication: programs on the same machine talking to each other. The use cases given in the original discussion were docker and fleet.
got ended up supporting two forms: the direct Unix scheme unix:/ and an HTTP Unix scheme http://unix:/. A scheme is the first part of a web address, the bit before the colon, such as http or https; it tells the software what kind of address this is and how to handle it. The http://unix:/ formatting was borrowed from the now deprecated request framework. Deprecated means the maintainers have declared it out of date and no longer recommend it, though it may still be in use.
The direct unix:/ scheme did not actually work, at least in version 11.8.3, because a dependency, http-timer, could not parse that URL format. Parsing means reading the address and breaking it into its parts. The error looked like this:
at origin.emit (node_modules/@szmarczak/http-timer/dist/source/index.js:43:20)code: 'ERR_INVALID_PROTOCOL'
This is a fragment of an error message. The first part points at a file and line inside the http-timer library where the failure happened. The second part, ERR_INVALID_PROTOCOL, is the error code: the software did not recognise the protocol, meaning the scheme, of the address it was given. So the direct form was broken, but the http://unix:/ form was the one that mattered.
The redirect problem
The core issue is not making Unix socket requests as such, but how redirects were handled. If a malicious user could supply a URL to a vulnerable application, they could supply a URL that resolved to a 30x redirect ending at a unix:// socket location. A 30x redirect is any of the HTTP status codes in the 300s, which a web server uses to say "look somewhere else".
Requests to localhost or the local network are the definition of server-side request forgery, usually shortened to SSRF. The idea is this: an application takes a web address from a user and fetches it on the user's behalf. If the application does not check where that address points, the user can make the application fetch things the user could not reach directly, including services on the server's own internal network. It is like asking a colleague to post a letter for you and giving them an address inside the building you are not allowed into.
Normally a developer would be expected to filter such requests, meaning check the address and refuse the dangerous ones. But here there was no way for a standard developer to prevent the scheme from being used, because even if they validated the initial URL, got handled the redirect automatically. The developer checks the first address, the application fetches it, the server replies "go to this other address instead", and got follows that second address without asking anyone. The check on the first address is useless if the second one is never checked.
got suggests a separate library, got-ssrf, for SSRF protection. That would have prevented the issue, because the unix part of the URL becomes the URL's hostname, and got-ssrf tries and fails to resolve it. The hostname is the part of an address that names the machine, like the domain name in a web address. Resolving means looking that name up to find the machine's numeric address. Since there is no real machine called unix, the lookup fails, and the library returns:
RequestError: getaddrinfo ENOTFOUND unix
This is an error message. RequestError says the request failed. getaddrinfo is the name of the system function that looks up addresses. ENOTFOUND means "not found", and unix is the name it could not find. In other words, the protective library tried to look up the machine called unix, failed, and blocked the request. That is exactly the behaviour you want from a filter.
What a Unix socket gives an attacker
The most obvious target is services like Docker, but the author also points to work on checking whether files exist through SSRF. Docker is software for running applications in isolated boxes called containers; it listens on a socket so that tools can tell it what to do. If an attacker can send commands to that socket, they can control the containers.
Local users typically talk to the Docker socket directly with something like:
curl — silent -XGET — unix-socket /var/run/docker.sock http://localhost/version
Line by line: curl is a command-line tool for making web requests. — silent tells it to keep quiet and not print progress noise. -XGET says to use the GET method, which is the standard way of asking a web server for something. — unix-socket /var/run/docker.sock says to send the request through the socket file at that path instead of over the network. http://localhost/version is the address being requested, where localhost means this same machine and /version is the endpoint being asked for. An endpoint is a specific address on a service that does a specific job. So the whole command asks Docker, through its local socket, what version it is running.
The equivalent in got is:
got('http://unix:/var/run/docker.sock:/version')
This is a single call to the got library. The string inside the brackets is the address. The http://unix: part says "use the HTTP style but go through a Unix socket". The next part, /var/run/docker.sock, is the path of the socket file. The final :/version is the endpoint on that socket being requested. So this one line does the same job as the curl command above: ask Docker for its version, through the local socket.
The version endpoint is a contrived example, meaning deliberately simple and not the dangerous part. The Docker management API is far more powerful. An API, or application programming interface, is a set of addresses and rules a program offers so other programs can ask it to do things. Depending on the request method, the Docker API supports creating a container (POST), killing a container (POST), checking auth configuration (POST), executing commands in a running container (POST), getting container logs (GET), exporting a container or images (GET), and much more. The method is the verb of a web request: GET asks for something, POST sends something to be acted on. Auth configuration means the settings that control who is allowed in.
Keeping the method through redirects
To make matters worse, got follows redirects for many HTTP status codes, so an attacker can use a 307 or 308 to pass a POST request and its content through to a Unix socket without rewriting it to a GET, provided the vulnerable application is crafting a POST request in the first place. A 307 or 308 redirect is a special kind that says "repeat exactly the same request, method and all, at the new address". Ordinary redirects often turn a POST into a GET, which would lose the data being sent. With 307 or 308, the data survives the hop. So if the application was sending a POST with a body, the body arrives at the socket too.
The same flaw in request
Because got borrowed the http://unix/ formatting from the deprecated request framework, the author checked whether request has the same vulnerability. It does. Strictly this should be a distinct CVE, meaning its own separate security identifier, but since request is deprecated the maintainers would be unlikely to respond. A known issue requiring a host header to be set prevents it from working in more cases. A header is extra information sent alongside a web request, like the label on a parcel; the host header says which machine the request is for. The proof of concept is:
import request from 'request'// http://[redacted]:81 redirects with a 307 to http://unix:/var/run/docker.sock:/versionrequest({ uri: 'http://[redacted]:81', headers: { 'host' : "anything" }, }, function (error, response, body) { console.log('body:', body);});
Line by line: import request from 'request' loads the request library so it can be used. The comment after it explains the setup: the address http://[redacted]:81 is a server on the local machine that replies with a 307 redirect pointing to http://unix:/var/run/docker.sock:/version. request({ starts a request with an options object. uri: 'http://[redacted]:81' is the address to fetch first. headers: { 'host' : "anything" } sets the host header to the word anything, which is the workaround for the known issue mentioned above. The closing braces end the options. function (error, response, body) is the callback, the function that runs when the request finishes, receiving any error, the response and the body. console.log('body:', body) prints the body, which is the content returned. So the whole snippet fetches the first address, follows its redirect to the Docker socket, and prints whatever comes back.
Root cause
The mistake is in how got handled redirect targets. A redirect to a Unix socket URL was followed even when the originally requested URL was an ordinary HTTP URL, so a developer who validated the URL they passed in still had no control over where the request ultimately landed. The unix component of the URL becomes the hostname, which is why got-ssrf blocks it. 307 and 308 redirects preserve the method and body, so a POST and its content can reach the socket if the application crafts one.
Impact
An attacker who can supply a URL to an application using a vulnerable version of got can cause the server to issue requests to Unix domain sockets on the local host. Depending on which sockets are present and what the application's request looks like, this can reach powerful local IPC services such as the Docker management API, and can potentially be used to check whether files exist.
For MediaWiki, the author found that got was seemingly used only as part of the test suites, so for the most part MediaWiki was unaffected, though updating the dependencies was still worthwhile. A test suite is the set of automated checks developers run to confirm the software still works; it is not part of what runs for real users. Some of the modules that were updated also rely on request, which is both deprecated and, as shown, vulnerable.
Fix
For most users the fix is to update got to version 11.8.4 or greater. In addition, developers who accept untrusted URLs for resolution need to be aware of SSRF and to perform URL sanitisation and validation so that they are not resolving sensitive URLs. Sanitisation means cleaning up input, and validation means checking it against rules before using it.
The change in got itself is straightforward: redirect URLs containing Unix sockets are blocked unless the initialised URL was also a Unix socket. In other words, if you started with an ordinary web address, a redirect cannot take you to a socket; if you deliberately started with a socket address, that is still allowed. The maintainers are additionally proposing to default an option, EnableUnixSockets, to false, disabling Unix domains entirely by default. An option is a setting; defaulting it to false means the safe behaviour applies unless someone explicitly turns it on.
Variants to test
If you want to look for the same kind of problem elsewhere, these are the angles to try:
- Other HTTP client libraries that support Unix sockets. Send a URL that redirects with a 307 to
http://unix:/var/run/docker.sock:/version. A positive result is the socket's response body coming back through the client. - Libraries that borrowed the
http://unix/URL format from request. Test the same redirect chain; a positive result is the request reaching the socket, remembering that some cases require a host header to be set. - Applications that validate the initial URL but follow redirects automatically. Point the validated URL at a server you control that issues a 30x to a Unix socket URL. A positive result is the application following the redirect to the socket.
- POST-based flows. Use a 307 or 308 redirect so the method and body are preserved. A positive result is the POST body arriving at the socket rather than being rewritten to a GET.
- Hostname-based SSRF filters. Check whether the filter resolves the
unixhostname; a filter that fails resolution and blocks the request (as got-ssrf does) is working, while one that lets it through is not.
Glossary
- API (application programming interface): a set of addresses and rules a program offers so other programs can ask it to do things.
- Auth configuration: the settings that control who is allowed to use a service.
- Callback: a function that runs when an operation finishes, receiving the result.
- Commit: one saved change to source code, with a short message describing it.
- Container: an isolated box in which an application runs, managed by software such as Docker.
- CVE: a public identifier for a known security vulnerability.
- Dependency: a piece of software your project relies on rather than building itself.
- Deprecated: declared out of date and no longer recommended by its maintainers.
- Endpoint: a specific address on a service that performs a specific job.
- Extension: an add-on that gives a wiki extra features.
- GET: the standard web request method for asking a server for something.
- GHSA identifier: a code naming a published security advisory.
- Header: extra information sent alongside a web request, such as which machine it is for.
- Hostname: the part of an address that names a machine.
- HTTP: the protocol browsers and servers use to exchange web requests and responses.
- IPC (inter-process communication): programs on the same machine talking to each other.
- JavaScript: a programming language, here run by Node.
- localhost: the name a computer uses for itself.
- Method: the verb of a web request, such as GET or POST.
- Node: a runtime that lets JavaScript run outside a browser.
- Option: a setting that changes how a piece of software behaves.
- Parsing: reading an address or piece of text and breaking it into its parts.
- PHP: a programming language commonly used for websites, including MediaWiki.
- POST: a web request method that sends data to be acted on.
- Protocol: the scheme of an address, such as http, telling software how to handle it.
- Redirect: a server's reply telling the client to fetch a different address instead.
- Resolving: looking up a machine name to find its numeric address.
- Sanitisation: cleaning up input before using it.
- Scheme: the first part of a web address, before the colon, such as http.
- Security advisory: a public notice describing a known vulnerability and its fix.
- Skin: the visual theme of a wiki.
- Socket: a channel a program uses to talk to another program, often on the same machine.
- Source review: examining code and its history rather than attacking a live system.
- SSRF (server-side request forgery): tricking an application into fetching an address the attacker could not reach directly, such as an internal service.
- Submodule: a separate piece of code included inside a larger project.
- Test suite: the automated checks developers run to confirm the software still works.
- Unix domain socket: a socket that exists as a file on the local machine and is used for local communication.
- Upstream: the original project that maintains a piece of software, as opposed to projects that use it.
- URL: a web address.
- Validation: checking input against rules before using it.
- Vulnerability: a weakness in software that an attacker can exploit.
- 30x redirect: any HTTP status code in the 300s, meaning look somewhere else.
- 307 / 308 redirect: a redirect that repeats the same request, method and body included, at the new address.