Artifactspublic-v0

curl

master
ply4 git calls541 ms296 KiB2 cachedprotocol v2
520 lines23 KiBRaw

NAME

libcurl-security - security considerations when using libcurl

Security

The libcurl project takes security seriously. The library is written with caution and precautions are taken to mitigate many kinds of risks encountered while operating with potentially malicious servers on the Internet. It is a powerful library that allows application writers to make trade-offs between ease of writing and exposure to potential risky operations. If used the right way, you can use libcurl to transfer data pretty safely.

Many applications are used in closed networks where users and servers can (possibly) be trusted, but many others are used on arbitrary servers and are fed input from potentially untrusted users. Following is a discussion about some risks in the ways in which applications commonly use libcurl and potential mitigations of those risks. It is not comprehensive, but shows classes of attacks that robust applications should consider. The Common Weakness Enumeration project at https://cwe.mitre.org/ is a good reference for many of these and similar types of weaknesses of which application writers should be aware.

Command Lines

If you use a command line tool (such as curl) that uses libcurl, and you give options to the tool on the command line those options can get read by other users of your system when they use ps or other tools to list currently running processes.

To avoid these problems, never feed sensitive things to programs using command line options. Write them to a protected file and use the -K option to avoid this.

.netrc

.netrc is a pretty handy file/feature that allows you to login quickly and automatically to frequently visited sites. The file contains passwords in clear text and is a real security risk. In some cases, your .netrc is also stored in a home directory that is NFS mounted or used on another network based file system, so the clear text password flies through your network every time anyone reads that file.

For applications that enable .netrc use, a user who manages to set the right URL might then make it possible to pass on passwords.

To avoid these problems, do not use .netrc files and never store passwords in plain text anywhere.

Clear Text Passwords

Many of the protocols libcurl supports send name and password unencrypted as clear text (HTTP Basic authentication, FTP, TELNET etc). It is easy for anyone on your network or a network nearby yours to fire up a network analyzer tool and eavesdrop on your passwords. Do not let the fact that HTTP Basic uses base64 encoded passwords fool you. They may not look readable at a first glance, but they are easily "deciphered" by anyone within seconds.

To avoid this problem, use an authentication mechanism or other protocol that does not let snoopers see your password: Digest, CRAM-MD5, Kerberos, SPNEGO or NTLM authentication. Or even better: use authenticated protocols that protect the entire connection and everything sent over it.

Unauthenticated Connections

Protocols that do not have any form of cryptographic authentication cannot with any certainty know that they communicate with the right remote server.

If your application is using a fixed scheme or fixed hostname, it is not safe as long as the connection is unauthenticated. There can be a man-in-the-middle or in fact the whole server might have been replaced by an evil actor.

Unauthenticated protocols are unsafe. The data that comes back to curl may have been injected by an attacker. The data that curl sends might be modified before it reaches the intended server. If it even reaches the intended server at all.

Remedies:

Restrict operations to authenticated transfers

Use authenticated protocols protected with HTTPS or SSH.

Make sure the server's certificate etc is verified

Never ever switch off certificate verification.

Certificates and Long-running Connections

Certificate validation of encrypted connections is performed immediately after a connection is established. That connection could be used for many subsequent transfers, even if the certificate used for validation expires or is revoked, the local certificate bundle is changed in a way that would have caused that certificate to fail validation, the server changes its certificate to one that would have failed validation, or even if a completely different server is brought up under the same hostname. This could continue for many hours (or even years) after such a change occurs, which may not be desired behavior for some applications.

Remedies:

Use the CURLOPT_MAXLIFETIME_CONN(3) option to limit the amount of time that connections are used after they have been successfully validated. Further transfers require a new connection with validation performed again.

Redirects

The CURLOPT_FOLLOWLOCATION(3) option automatically follows HTTP redirects sent by a remote server. These redirects can refer to any kind of URL, not only HTTP. libcurl restricts the protocols allowed to be used in redirects for security reasons: only HTTP, HTTPS, FTP and FTPS are enabled by default. Applications may opt to restrict that set further.

A redirect to a file: URL would cause the libcurl to read (or write) arbitrary files from the local file system. If the application returns the data back to the user (as would happen in some kinds of CGI scripts), an attacker could leverage this to read otherwise forbidden data (e.g. file://localhost/etc/passwd).

If authentication credentials are stored in the ~/.netrc file, or Kerberos is in use, any other URL type (except file:) that requires authentication is also at risk. A redirect such as ftp://some-internal-server/private-file would then return data even when the server is password protected.

In the same way, if an unencrypted SSH private key has been configured for the user running the libcurl application, SCP: or SFTP: URLs could access password or private-key protected resources, e.g. sftp://user@some-internal-server/etc/passwd

The CURLOPT_REDIR_PROTOCOLS_STR(3) and CURLOPT_NETRC(3) options can be used to mitigate against this kind of attack.

A redirect can also specify a location available only on the machine running libcurl, including servers hidden behind a firewall from the attacker. E.g. https://127.0.0.1/ or https://intranet.example/delete-stuff.cgi?delete=all or tftp://bootp-server.example/pc-config-data

Applications can mitigate against this by disabling CURLOPT_FOLLOWLOCATION(3) and handling redirects itself, sanitizing URLs as necessary. Alternately, an app could leave CURLOPT_FOLLOWLOCATION(3) enabled but set CURLOPT_REDIR_PROTOCOLS_STR(3) and install a CURLOPT_OPENSOCKETFUNCTION(3) or CURLOPT_PREREQFUNCTION(3) callback function in which addresses are sanitized before use.

CRLF in Headers

For all options in libcurl which specify headers, including but not limited to CURLOPT_HTTPHEADER(3), CURLOPT_PROXYHEADER(3), CURLOPT_COOKIE(3), CURLOPT_USERAGENT(3), CURLOPT_REFERER(3) and CURLOPT_RANGE(3), libcurl sends the headers as-is and does not apply any special sanitation or normalization to them.

If you allow untrusted user input into these options without sanitizing CRLF sequences in them, someone malicious may be able to modify the request in a way you did not intend such as injecting new headers.

Local Resources

A user who can control the DNS server of a domain being passed in within a URL can change the address of the host to a local, private address which a server-side libcurl-using application could then use. E.g. the innocuous URL https://fuzzybunnies.example.com/ could actually resolve to the IP address of a server behind a firewall, such as 127.0.0.1 or 10.1.2.3. Applications can mitigate against this by setting a CURLOPT_OPENSOCKETFUNCTION(3) or CURLOPT_PREREQFUNCTION(3) and checking the address before a connection.

All the malicious scenarios regarding redirected URLs apply equally to non-redirected URLs, if the user is allowed to specify an arbitrary URL that could point to a private resource. For example, a web app providing a translation service might happily translate file://localhost/etc/passwd and display the result. Applications can mitigate against this with the CURLOPT_PROTOCOLS_STR(3) option as well as by similar mitigation techniques for redirections.

A malicious FTP server could in response to the PASV command return an IP address and port number for a server local to the app running libcurl but behind a firewall. Applications can mitigate against this by using the CURLOPT_FTP_SKIP_PASV_IP(3) option or CURLOPT_FTPPORT(3).

Local servers sometimes assume local access comes from friends and trusted users. An application that expects https://example.com/file_to_read and instead gets http://192.168.0.1/my_router_config might print a file that would otherwise be protected by the firewall.

Allowing your application to connect to local hosts, be it the same machine that runs the application or a machine on the same local network, might be possible to exploit by an attacker who then perhaps can "port-scan" the particular hosts - depending on how the application and servers acts.

IPv4 Addresses

Some users might be tempted to filter access to local resources or similar based on numerical IPv4 addresses used in URLs. This is a bad and error-prone idea because of the many different ways a numerical IPv4 address can be specified and libcurl accepts: one to four dot-separated fields using one of or a mix of decimal, octal or hexadecimal encoding.

IPv6 Addresses

libcurl handles IPv6 addresses transparently and as easily as IPv4 addresses. That means that a sanitizing function that filters out addresses like 127.0.0.1 is not sufficient - the equivalent IPv6 addresses ::1, ::, 0:00::0:1, ::127.0.0.1 and ::ffff:7f00:1 supplied somehow by an attacker would all bypass a naive filter and could allow access to undesired local resources. IPv6 also has special address blocks like link-local and site-local that generally should not be accessed by a server-side libcurl-using application. A poorly configured firewall installed in a data center, organization or server may also be configured to limit IPv4 connections but leave IPv6 connections wide open. In some cases, setting CURLOPT_IPRESOLVE(3) to CURL_IPRESOLVE_V4 can be used to limit resolved addresses to IPv4 only and bypass these issues.

Uploads

When uploading, a redirect can cause a local (or remote) file to be overwritten. Applications must not allow any unsanitized URL to be passed in for uploads. Also, CURLOPT_FOLLOWLOCATION(3) should not be used on uploads. Instead, the applications should consider handling redirects itself, sanitizing each URL first.

Authentication

Use of CURLOPT_UNRESTRICTED_AUTH(3) could cause authentication information to be sent to an unknown second server. Applications can mitigate against this by disabling CURLOPT_FOLLOWLOCATION(3) and handling redirects itself, sanitizing where necessary.

Use of the CURLAUTH_ANY option to CURLOPT_HTTPAUTH(3) could result in username and password being sent in clear text to an HTTP server. Instead, use CURLAUTH_ANYSAFE which ensures that the password is encrypted over the network, or else fail the request.

Use of the CURLUSESSL_TRY option to CURLOPT_USE_SSL(3) could result in username and password being sent in clear text to an FTP server. Instead, use CURLUSESSL_CONTROL to ensure that an encrypted connection is used or else fail the request.

Cookies

If cookies are enabled and cached, then a user could craft a URL which performs some malicious action to a site whose authentication is already stored in a cookie. E.g. https://mail.example.com/delete-stuff.cgi?delete=all Applications can mitigate against this by disabling cookies or clearing them between requests.

Dangerous SCP URLs

SCP URLs can contain raw commands within the scp: URL, which is a side effect of how the SCP protocol is designed. E.g.

  scp://user:pass@host/a;date >/tmp/test;

Applications must not allow unsanitized SCP: URLs to be passed in for downloads.

file://

By default curl and libcurl support file:// URLs. Such a URL is always an access, or attempted access, to a local resource. If your application wants to avoid that, keep control of what URLs to use and/or prevent curl/libcurl from using the protocol.

By default, libcurl prohibits redirects to file:// URLs.

Warning: file:// on Windows

The Windows operating system tries automatically, and without any way for applications to disable it, to establish a connection to another host over the network and access it (over SMB or other protocols), if only the correct file path is accessed.

When first realizing this, the curl team tried to filter out such attempts in order to protect applications for inadvertent probes of for example internal networks etc. This resulted in CVE-2019-15601 and the associated security fix.

We have since been made aware of the fact that the previous fix was far from adequate as there are several other ways to accomplish more or less the same thing: accessing a remote host over the network instead of the local file system.

The conclusion we have come to is that this is a weakness or feature in the Windows operating system itself, that we as an application cannot safely protect users against. It would make a whack-a-mole race we do not want to participate in. There are too many ways to do it and there is no knob we can use to turn off the practice.

If you use curl or libcurl on Windows (any version), disable the use of the FILE protocol in curl or be prepared that accesses to a set of special paths potentially make your system access other hosts on your network. curl cannot protect you against this.

What if the user can set the URL

Applications may find it tempting to let users set the URL that it can work on. That is probably fine, but opens up for mischief and trickery that you as an application author may want to address or take precautions against.

If your curl-using script allow a custom URL do you also, perhaps unintentionally, allow the user to pass other options to the curl command line if creative use of special characters are applied?

If the user can set the URL, the user can also specify the scheme part to other protocols that you did not intend for users to use and perhaps did not consider. curl supports over 27 different URL schemes. https:// might be what you expect, ftp:// or imap:// might be what the user gives your application. Also, cross-protocol operations might be done by using a particular scheme in the URL but point to a server doing a different protocol on a non-standard port.

Remedies:

Use --proto

curl command lines can use --proto to limit what URL schemes it accepts

Use CURLOPT_PROTOCOLS_STR

libcurl programs can use CURLOPT_PROTOCOLS_STR(3) to limit what URL schemes it accepts

consider not allowing the user to set the full URL

Maybe let the user provide data for parts of it? Or maybe filter input to only allow specific choices? Remember that the naive approach of appending a user-specified string to a base URL could still allow unexpected results through use of characters like ../ or ? or Unicode characters or hiding characters using various escaping means.

RFC 3986 vs WHATWG URL

curl supports URLs mostly according to how they are defined in RFC 3986, and has done so since the beginning.

Web browsers mostly adhere to the WHATWG URL Specification.

This deviance makes some URLs copied between browsers (or returned over HTTP for redirection) and curl not work the same way. It can also cause problems if an application parses URLs differently from libcurl and makes different assumptions about a link. This can mislead users into getting the wrong thing, connecting to the wrong host or otherwise not working identically.

Within an application, this can be mitigated by always using the curl_url(3) API to parse URLs, ensuring that they are parsed the same way as within libcurl itself.

FTP uses two connections

When performing an FTP transfer, two TCP connections are used: one for setting up the transfer and one for the actual data.

FTP is not only unauthenticated, but the setting up of the second transfer is also a weak spot. The second connection to use for data, is either setup with the PORT/EPRT command that makes the server connect back to the client on the given IP+PORT, or with PASV/EPSV that makes the server setup a port to listen to and tells the client to connect to a given IP+PORT.

Again, unauthenticated means that the connection might be meddled with by a man-in-the-middle or that there is a malicious server pretending to be the right one.

A malicious FTP server can respond to PASV commands with the IP+PORT of a totally different machine. Perhaps even a third party host, and when there are many clients trying to connect to that third party, it could create a Distributed Denial-Of-Service attack out of it. If the client makes an upload operation, it can make the client send the data to another site. If the attacker can affect what data the client uploads, it can be made to work as a HTTP request and then the client could be made to issue HTTP requests to third party hosts.

An attacker that manages to control curl's command line options can tell curl to send an FTP PORT command to ask the server to connect to a third party host instead of back to curl.

The fact that FTP uses two connections makes it vulnerable in a way that is hard to avoid.

Active FTP passes on the local IP address

If you use curl/libcurl to do active FTP transfers, curl passes on the address of your local IP interface to the remote server - even when for example using a SOCKS or HTTP proxy in between curl and the target server.

Denial of Service

A malicious server could cause libcurl to effectively hang by sending data slowly, or even no data at all but keeping the TCP connection open. This could effectively result in a denial-of-service attack. The CURLOPT_TIMEOUT(3) and/or CURLOPT_LOW_SPEED_LIMIT(3) options can be used to mitigate against this.

A malicious server could cause libcurl to download an infinite amount of data, potentially causing system resources to be exhausted resulting in a system or application crash. Setting the CURLOPT_MAXFILESIZE_LARGE(3) option is not sufficient to guard against this. Instead, applications should monitor the amount of data received within the write or progress callback and abort once the limit is reached.

A malicious HTTP server could cause an infinite redirection loop, causing a denial-of-service. This can be mitigated by using the CURLOPT_MAXREDIRS(3) option.

Arbitrary Headers

User-supplied data must be sanitized when used in options like CURLOPT_USERAGENT(3), CURLOPT_HTTPHEADER(3), CURLOPT_POSTFIELDS(3) and others that are used to generate structured data. Characters like embedded carriage returns or ampersands could allow the user to create additional headers or fields that could cause malicious transactions.

Server-supplied Names

A server can supply data which the application may, in some cases, use as a filename. The curl command-line tool does this with --remote-header-name, using the Content-disposition: header to generate a filename. An application could also use CURLINFO_EFFECTIVE_URL(3) to generate a filename from a server-supplied redirect URL. Special care must be taken to sanitize such names to avoid the possibility of a malicious server supplying one like "/etc/passwd", "autoexec.bat", "prn:" or even ".bashrc".

Server Certificates

A secure application should never use the CURLOPT_SSL_VERIFYPEER(3) option to disable certificate validation. There are numerous attacks that are enabled by applications that fail to properly validate server TLS/SSL certificates, thus enabling a malicious server to spoof a legitimate one. HTTPS without validated certificates is potentially as insecure as a plain HTTP connection.

Showing What You Do

Relatedly, be aware that in situations when you have problems with libcurl and ask someone for help, everything you reveal in order to get best possible help might also impose certain security related risks. Hostnames, usernames, paths, operating system specifics, etc. (not to mention passwords of course) may in fact be used by intruders to gain additional information of a potential target.

Be sure to limit access to application logs if they could hold private or security-related data. Besides the obvious candidates like usernames and passwords, things like URLs, cookies or even filenames could also hold sensitive data.

To avoid this problem, you must of course use your common sense. Often, you can edit out the sensitive data or search/replace your true information with faked data.

setuid applications using libcurl

libcurl-using applications that set the 'setuid' bit to run with elevated or modified rights also implicitly give that extra power to libcurl and this should only be done after careful considerations.

Giving setuid powers to the application means that libcurl can save files using those new rights (if for example the SSLKEYLOGFILE environment variable is set). Also: if the application wants these powers to read or manage secrets that the user is otherwise not able to view (like credentials for a login etc), it should be noted that libcurl still might understand proxy environment variables that allow the user to redirect libcurl operations to use a proxy controlled by the user.

File descriptors, fork and NTLM

An application that uses libcurl and invokes fork() gets all file descriptors duplicated in the child process, including the ones libcurl created.

libcurl itself uses fork() and execl() if told to use the CURLAUTH_NTLM_WB authentication method which then invokes the helper command in a child process with file descriptors duplicated. Make sure that only the trusted and reliable helper program is invoked.

This feature was removed from curl in 8.8.0.

Secrets in memory

When applications pass usernames, passwords or other sensitive data to libcurl to be used for upcoming transfers, those secrets are kept around as-is in memory. In many cases they are stored in the heap for as long as the handle itself for which the options are set.

If an attacker can access the heap, like maybe by reading swap space or via a core dump file, such data might be accessible.

Further, when eventually closing a handle and the secrets are no longer needed, libcurl does not explicitly clear memory before freeing it, so credentials may be left in freed data.

Saving files

libcurl cannot protect against attacks where an attacker has write access to the same directory where libcurl is directed to save files.

Cookies

If libcurl is built with PSL (Public Suffix List) support, it detects and discards cookies that are specified for such suffix domains that should not be allowed to have cookies.

if libcurl is not built with PSL support, it has no ability to stop super cookies.

Report Security Problems

Should you detect or suspect a security problem in libcurl or curl, contact the project curl security team immediately. See https://curl.se/dev/secprocess.html for details.

1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: libcurl-security
5Section: 3
6Source: libcurl
7See-also:
8 - libcurl-thread (3)
9Protocol:
10 - All
11Added-in: n/a
12---
13<!-- markdown-link-check-disable -->
14# NAME
16libcurl-security - security considerations when using libcurl
18# Security
20The libcurl project takes security seriously. The library is written with
21caution and precautions are taken to mitigate many kinds of risks encountered
22while operating with potentially malicious servers on the Internet. It is a
23powerful library that allows application writers to make trade-offs between
24ease of writing and exposure to potential risky operations. If used the right
25way, you can use libcurl to transfer data pretty safely.
27Many applications are used in closed networks where users and servers can
28(possibly) be trusted, but many others are used on arbitrary servers and are
29fed input from potentially untrusted users. Following is a discussion about
30some risks in the ways in which applications commonly use libcurl and
31potential mitigations of those risks. It is not comprehensive, but shows
32classes of attacks that robust applications should consider. The Common
33Weakness Enumeration project at https://cwe.mitre.org/ is a good reference for
34many of these and similar types of weaknesses of which application writers
35should be aware.
37# Command Lines
39If you use a command line tool (such as curl) that uses libcurl, and you give
40options to the tool on the command line those options can get read by other
41users of your system when they use *ps* or other tools to list currently
42running processes.
44To avoid these problems, never feed sensitive things to programs using command
45line options. Write them to a protected file and use the -K option to avoid
46this.
48# .netrc
50.netrc is a pretty handy file/feature that allows you to login quickly and
51automatically to frequently visited sites. The file contains passwords in
52clear text and is a real security risk. In some cases, your .netrc is also
53stored in a home directory that is NFS mounted or used on another network
54based file system, so the clear text password flies through your network every
55time anyone reads that file.
57For applications that enable .netrc use, a user who manages to set the right
58URL might then make it possible to pass on passwords.
60To avoid these problems, do not use .netrc files and never store passwords in
61plain text anywhere.
63# Clear Text Passwords
65Many of the protocols libcurl supports send name and password unencrypted as
66clear text (HTTP Basic authentication, FTP, TELNET etc). It is easy for anyone
67on your network or a network nearby yours to fire up a network analyzer tool
68and eavesdrop on your passwords. Do not let the fact that HTTP Basic uses
69base64 encoded passwords fool you. They may not look readable at a first
70glance, but they are easily "deciphered" by anyone within seconds.
72To avoid this problem, use an authentication mechanism or other protocol that
73does not let snoopers see your password: Digest, CRAM-MD5, Kerberos, SPNEGO or
74NTLM authentication. Or even better: use authenticated protocols that protect
75the entire connection and everything sent over it.
77# Unauthenticated Connections
79Protocols that do not have any form of cryptographic authentication cannot
80with any certainty know that they communicate with the right remote server.
82If your application is using a fixed scheme or fixed hostname, it is not safe
83as long as the connection is unauthenticated. There can be a man-in-the-middle
84or in fact the whole server might have been replaced by an evil actor.
86Unauthenticated protocols are unsafe. The data that comes back to curl may
87have been injected by an attacker. The data that curl sends might be modified
88before it reaches the intended server. If it even reaches the intended server
89at all.
91Remedies:
93## Restrict operations to authenticated transfers
95Use authenticated protocols protected with HTTPS or SSH.
97## Make sure the server's certificate etc is verified
99Never ever switch off certificate verification.
101# Certificates and Long-running Connections
103Certificate validation of encrypted connections is performed immediately after
104a connection is established. That connection could be used for many subsequent
105transfers, even if the certificate used for validation expires or is revoked,
106the local certificate bundle is changed in a way that would have caused that
107certificate to fail validation, the server changes its certificate to one
108that would have failed validation, or even if a completely different server is
109brought up under the same hostname. This could continue for many hours (or
110even years) after such a change occurs, which may not be desired behavior for
111some applications.
113Remedies:
115Use the CURLOPT_MAXLIFETIME_CONN(3) option to limit the amount of time that
116connections are used after they have been successfully validated. Further
117transfers require a new connection with validation performed again.
119# Redirects
121The CURLOPT_FOLLOWLOCATION(3) option automatically follows HTTP redirects sent
122by a remote server. These redirects can refer to any kind of URL, not only
123HTTP. libcurl restricts the protocols allowed to be used in redirects for
124security reasons: only HTTP, HTTPS, FTP and FTPS are enabled by default.
125Applications may opt to restrict that set further.
127A redirect to a file: URL would cause the libcurl to read (or write) arbitrary
128files from the local file system. If the application returns the data back to
129the user (as would happen in some kinds of CGI scripts), an attacker could
130leverage this to read otherwise forbidden data (e.g.
131**file://localhost/etc/passwd**).
133If authentication credentials are stored in the ~/.netrc file, or Kerberos is
134in use, any other URL type (except file:) that requires authentication is also
135at risk. A redirect such as **ftp://some-internal-server/private-file** would
136then return data even when the server is password protected.
138In the same way, if an unencrypted SSH private key has been configured for the
139user running the libcurl application, SCP: or SFTP: URLs could access password
140or private-key protected resources,
141e.g. **sftp://user@some-internal-server/etc/passwd**
143The CURLOPT_REDIR_PROTOCOLS_STR(3) and CURLOPT_NETRC(3) options can be
144used to mitigate against this kind of attack.
146A redirect can also specify a location available only on the machine running
147libcurl, including servers hidden behind a firewall from the attacker. E.g.
148**https://127.0.0.1/** or
149**https://intranet.example/delete-stuff.cgi?delete=all** or
150**tftp://bootp-server.example/pc-config-data**
152Applications can mitigate against this by disabling
153CURLOPT_FOLLOWLOCATION(3) and handling redirects itself, sanitizing URLs
154as necessary. Alternately, an app could leave CURLOPT_FOLLOWLOCATION(3)
155enabled but set CURLOPT_REDIR_PROTOCOLS_STR(3) and install a
156CURLOPT_OPENSOCKETFUNCTION(3) or CURLOPT_PREREQFUNCTION(3) callback
157function in which addresses are sanitized before use.
159# CRLF in Headers
161For all options in libcurl which specify headers, including but not limited to
162CURLOPT_HTTPHEADER(3), CURLOPT_PROXYHEADER(3),
163CURLOPT_COOKIE(3), CURLOPT_USERAGENT(3), CURLOPT_REFERER(3)
164and CURLOPT_RANGE(3), libcurl sends the headers as-is and does not apply
165any special sanitation or normalization to them.
167If you allow untrusted user input into these options without sanitizing CRLF
168sequences in them, someone malicious may be able to modify the request in a
169way you did not intend such as injecting new headers.
171# Local Resources
173A user who can control the DNS server of a domain being passed in within a URL
174can change the address of the host to a local, private address which a
175server-side libcurl-using application could then use. E.g. the innocuous URL
176**https://fuzzybunnies.example.com/** could actually resolve to the IP address
177of a server behind a firewall, such as 127.0.0.1 or 10.1.2.3. Applications can
178mitigate against this by setting a CURLOPT_OPENSOCKETFUNCTION(3) or
179CURLOPT_PREREQFUNCTION(3) and checking the address before a connection.
181All the malicious scenarios regarding redirected URLs apply equally to
182non-redirected URLs, if the user is allowed to specify an arbitrary URL that
183could point to a private resource. For example, a web app providing a
184translation service might happily translate **file://localhost/etc/passwd**
185and display the result. Applications can mitigate against this with the
186CURLOPT_PROTOCOLS_STR(3) option as well as by similar mitigation techniques
187for redirections.
189A malicious FTP server could in response to the PASV command return an IP
190address and port number for a server local to the app running libcurl but
191behind a firewall. Applications can mitigate against this by using the
192CURLOPT_FTP_SKIP_PASV_IP(3) option or CURLOPT_FTPPORT(3).
194Local servers sometimes assume local access comes from friends and trusted
195users. An application that expects https://example.com/file_to_read and
196instead gets http://192.168.0.1/my_router_config might print a file that would
197otherwise be protected by the firewall.
199Allowing your application to connect to local hosts, be it the same machine
200that runs the application or a machine on the same local network, might be
201possible to exploit by an attacker who then perhaps can "port-scan" the
202particular hosts - depending on how the application and servers acts.
204# IPv4 Addresses
206Some users might be tempted to filter access to local resources or similar
207based on numerical IPv4 addresses used in URLs. This is a bad and error-prone
208idea because of the many different ways a numerical IPv4 address can be
209specified and libcurl accepts: one to four dot-separated fields using one of
210or a mix of decimal, octal or hexadecimal encoding.
212# IPv6 Addresses
214libcurl handles IPv6 addresses transparently and as easily as IPv4 addresses.
215That means that a sanitizing function that filters out addresses like
216127.0.0.1 is not sufficient - the equivalent IPv6 addresses **::1**, **::**,
217**0:00::0:1**, **::127.0.0.1** and **::ffff:7f00:1** supplied somehow by an
218attacker would all bypass a naive filter and could allow access to undesired
219local resources. IPv6 also has special address blocks like link-local and
220site-local that generally should not be accessed by a server-side
221libcurl-using application. A poorly configured firewall installed in a data
222center, organization or server may also be configured to limit IPv4
223connections but leave IPv6 connections wide open. In some cases, setting
224CURLOPT_IPRESOLVE(3) to CURL_IPRESOLVE_V4 can be used to limit resolved
225addresses to IPv4 only and bypass these issues.
227# Uploads
229When uploading, a redirect can cause a local (or remote) file to be
230overwritten. Applications must not allow any unsanitized URL to be passed in
231for uploads. Also, CURLOPT_FOLLOWLOCATION(3) should not be used on
232uploads. Instead, the applications should consider handling redirects itself,
233sanitizing each URL first.
235# Authentication
237Use of CURLOPT_UNRESTRICTED_AUTH(3) could cause authentication
238information to be sent to an unknown second server. Applications can mitigate
239against this by disabling CURLOPT_FOLLOWLOCATION(3) and handling
240redirects itself, sanitizing where necessary.
242Use of the CURLAUTH_ANY option to CURLOPT_HTTPAUTH(3) could result in username
243and password being sent in clear text to an HTTP server. Instead, use
244CURLAUTH_ANYSAFE which ensures that the password is encrypted over the
245network, or else fail the request.
247Use of the CURLUSESSL_TRY option to CURLOPT_USE_SSL(3) could result in
248username and password being sent in clear text to an FTP server. Instead, use
249CURLUSESSL_CONTROL to ensure that an encrypted connection is used or else fail
250the request.
252# Cookies
254If cookies are enabled and cached, then a user could craft a URL which
255performs some malicious action to a site whose authentication is already
256stored in a cookie. E.g.
257**https://mail.example.com/delete-stuff.cgi?delete=all** Applications can
258mitigate against this by disabling cookies or clearing them between requests.
260# Dangerous SCP URLs
262SCP URLs can contain raw commands within the scp: URL, which is a side effect
263of how the SCP protocol is designed. E.g.
264~~~
265 scp://user:pass@host/a;date >/tmp/test;
266~~~
267Applications must not allow unsanitized SCP: URLs to be passed in for
268downloads.
270# `file://`
272By default curl and libcurl support `file://` URLs. Such a URL is always an
273access, or attempted access, to a local resource. If your application wants to
274avoid that, keep control of what URLs to use and/or prevent curl/libcurl from
275using the protocol.
277By default, libcurl prohibits redirects to `file://` URLs.
279# Warning: `file://` on Windows
281The Windows operating system tries automatically, and without any way for
282applications to disable it, to establish a connection to another host over the
283network and access it (over SMB or other protocols), if only the correct file
284path is accessed.
286When first realizing this, the curl team tried to filter out such attempts in
287order to protect applications for inadvertent probes of for example internal
288networks etc. This resulted in CVE-2019-15601 and the associated security fix.
290We have since been made aware of the fact that the previous fix was far from
291adequate as there are several other ways to accomplish more or less the same
292thing: accessing a remote host over the network instead of the local file
293system.
295The conclusion we have come to is that this is a weakness or feature in the
296Windows operating system itself, that we as an application cannot safely
297protect users against. It would make a whack-a-mole race we do not want to
298participate in. There are too many ways to do it and there is no knob we can
299use to turn off the practice.
301If you use curl or libcurl on Windows (any version), disable the use of the
302FILE protocol in curl or be prepared that accesses to a set of special paths
303potentially make your system access other hosts on your network. curl cannot
304protect you against this.
306# What if the user can set the URL
308Applications may find it tempting to let users set the URL that it can work
309on. That is probably fine, but opens up for mischief and trickery that you as
310an application author may want to address or take precautions against.
312If your curl-using script allow a custom URL do you also, perhaps
313unintentionally, allow the user to pass other options to the curl command line
314if creative use of special characters are applied?
316If the user can set the URL, the user can also specify the scheme part to
317other protocols that you did not intend for users to use and perhaps did not
318consider. curl supports over 27 different URL schemes. `https://` might be
319what you expect, `ftp://` or `imap://` might be what the user gives your
320application. Also, cross-protocol operations might be done by using a
321particular scheme in the URL but point to a server doing a different protocol
322on a non-standard port.
324Remedies:
326## Use --proto
328curl command lines can use *--proto* to limit what URL schemes it accepts
330## Use CURLOPT_PROTOCOLS_STR
332libcurl programs can use CURLOPT_PROTOCOLS_STR(3) to limit what URL schemes it accepts
334## consider not allowing the user to set the full URL
336Maybe let the user provide data for parts of it? Or maybe filter input to only
337allow specific choices? Remember that the naive approach of appending a
338user-specified string to a base URL could still allow unexpected results
339through use of characters like ../ or ? or Unicode characters or hiding
340characters using various escaping means.
342# RFC 3986 vs WHATWG URL
344curl supports URLs mostly according to how they are defined in RFC 3986, and
345has done so since the beginning.
347Web browsers mostly adhere to the WHATWG URL Specification.
349This deviance makes some URLs copied between browsers (or returned over HTTP
350for redirection) and curl not work the same way. It can also cause problems if
351an application parses URLs differently from libcurl and makes different
352assumptions about a link. This can mislead users into getting the wrong thing,
353connecting to the wrong host or otherwise not working identically.
355Within an application, this can be mitigated by always using the
356curl_url(3) API to parse URLs, ensuring that they are parsed the same way
357as within libcurl itself.
359# FTP uses two connections
361When performing an FTP transfer, two TCP connections are used: one for setting
362up the transfer and one for the actual data.
364FTP is not only unauthenticated, but the setting up of the second transfer is
365also a weak spot. The second connection to use for data, is either setup with
366the PORT/EPRT command that makes the server connect back to the client on the
367given IP+PORT, or with PASV/EPSV that makes the server setup a port to listen
368to and tells the client to connect to a given IP+PORT.
370Again, unauthenticated means that the connection might be meddled with by a
371man-in-the-middle or that there is a malicious server pretending to be the
372right one.
374A malicious FTP server can respond to PASV commands with the IP+PORT of a
375totally different machine. Perhaps even a third party host, and when there are
376many clients trying to connect to that third party, it could create a
377Distributed Denial-Of-Service attack out of it. If the client makes an upload
378operation, it can make the client send the data to another site. If the
379attacker can affect what data the client uploads, it can be made to work as a
380HTTP request and then the client could be made to issue HTTP requests to third
381party hosts.
383An attacker that manages to control curl's command line options can tell curl
384to send an FTP PORT command to ask the server to connect to a third party host
385instead of back to curl.
387The fact that FTP uses two connections makes it vulnerable in a way that is
388hard to avoid.
390# Active FTP passes on the local IP address
392If you use curl/libcurl to do *active* FTP transfers, curl passes on the
393address of your local IP interface to the remote server - even when for example
394using a SOCKS or HTTP proxy in between curl and the target server.
396# Denial of Service
398A malicious server could cause libcurl to effectively hang by sending data
399slowly, or even no data at all but keeping the TCP connection open. This could
400effectively result in a denial-of-service attack. The CURLOPT_TIMEOUT(3)
401and/or CURLOPT_LOW_SPEED_LIMIT(3) options can be used to mitigate against
402this.
404A malicious server could cause libcurl to download an infinite amount of data,
405potentially causing system resources to be exhausted resulting in a system or
406application crash. Setting the CURLOPT_MAXFILESIZE_LARGE(3) option is not
407sufficient to guard against this. Instead, applications should monitor the
408amount of data received within the write or progress callback and abort once
409the limit is reached.
411A malicious HTTP server could cause an infinite redirection loop, causing a
412denial-of-service. This can be mitigated by using the
413CURLOPT_MAXREDIRS(3) option.
415# Arbitrary Headers
417User-supplied data must be sanitized when used in options like
418CURLOPT_USERAGENT(3), CURLOPT_HTTPHEADER(3),
419CURLOPT_POSTFIELDS(3) and others that are used to generate structured
420data. Characters like embedded carriage returns or ampersands could allow the
421user to create additional headers or fields that could cause malicious
422transactions.
424# Server-supplied Names
426A server can supply data which the application may, in some cases, use as a
427filename. The curl command-line tool does this with *--remote-header-name*,
428using the Content-disposition: header to generate a filename. An application
429could also use CURLINFO_EFFECTIVE_URL(3) to generate a filename from a
430server-supplied redirect URL. Special care must be taken to sanitize such
431names to avoid the possibility of a malicious server supplying one like
432**"/etc/passwd"**, **"autoexec.bat"**, **"prn:"** or even **".bashrc"**.
434# Server Certificates
436A secure application should never use the CURLOPT_SSL_VERIFYPEER(3)
437option to disable certificate validation. There are numerous attacks that are
438enabled by applications that fail to properly validate server TLS/SSL
439certificates, thus enabling a malicious server to spoof a legitimate
440one. HTTPS without validated certificates is potentially as insecure as a
441plain HTTP connection.
443# Showing What You Do
445Relatedly, be aware that in situations when you have problems with libcurl and
446ask someone for help, everything you reveal in order to get best possible help
447might also impose certain security related risks. Hostnames, usernames, paths,
448operating system specifics, etc. (not to mention passwords of course) may in
449fact be used by intruders to gain additional information of a potential
450target.
452Be sure to limit access to application logs if they could hold private or
453security-related data. Besides the obvious candidates like usernames and
454passwords, things like URLs, cookies or even filenames could also hold
455sensitive data.
457To avoid this problem, you must of course use your common sense. Often, you
458can edit out the sensitive data or search/replace your true information with
459faked data.
461# setuid applications using libcurl
463libcurl-using applications that set the 'setuid' bit to run with elevated or
464modified rights also implicitly give that extra power to libcurl and this
465should only be done after careful considerations.
467Giving setuid powers to the application means that libcurl can save files using
468those new rights (if for example the `SSLKEYLOGFILE` environment variable is
469set). Also: if the application wants these powers to read or manage secrets
470that the user is otherwise not able to view (like credentials for a login
471etc), it should be noted that libcurl still might understand proxy environment
472variables that allow the user to redirect libcurl operations to use a proxy
473controlled by the user.
475# File descriptors, fork and NTLM
477An application that uses libcurl and invokes *fork()* gets all file
478descriptors duplicated in the child process, including the ones libcurl
479created.
481libcurl itself uses *fork()* and *execl()* if told to use the
482**CURLAUTH_NTLM_WB** authentication method which then invokes the helper
483command in a child process with file descriptors duplicated. Make sure that
484only the trusted and reliable helper program is invoked.
486This feature was removed from curl in 8.8.0.
488# Secrets in memory
490When applications pass usernames, passwords or other sensitive data to
491libcurl to be used for upcoming transfers, those secrets are kept around as-is
492in memory. In many cases they are stored in the heap for as long as the handle
493itself for which the options are set.
495If an attacker can access the heap, like maybe by reading swap space or via a
496core dump file, such data might be accessible.
498Further, when eventually closing a handle and the secrets are no longer
499needed, libcurl does not explicitly clear memory before freeing it, so
500credentials may be left in freed data.
502# Saving files
504libcurl cannot protect against attacks where an attacker has write access to
505the same directory where libcurl is directed to save files.
507# Cookies
509If libcurl is built with PSL (**Public Suffix List**) support, it detects and
510discards cookies that are specified for such suffix domains that should not be
511allowed to have cookies.
513if libcurl is *not* built with PSL support, it has no ability to stop super
514cookies.
516# Report Security Problems
518Should you detect or suspect a security problem in libcurl or curl, contact
519the project curl security team immediately. See
520https://curl.se/dev/secprocess.html for details.
Diagnostics4 git calls · 541 ms
CallStatusSizeTime0541 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=536
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2009.6 KiB537 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=9818
git.response.cache=MISS
git.response.duration_ms=537
git.fetch.wants=cefb386380b2b18ab99f7d684caeacede02917e4
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=9818
git.pkt.payload.max=9797
git.sideband.band1.bytes=9796
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e326ac0b0c98f1
Request
414f95d9…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format