xref: /aosp_15_r20/external/curl/docs/libcurl/libcurl-security.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: libcurl-security
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - libcurl-thread (3)
9*6236dae4SAndroid Build Coastguard WorkerProtocol:
10*6236dae4SAndroid Build Coastguard Worker  - All
11*6236dae4SAndroid Build Coastguard WorkerAdded-in: n/a
12*6236dae4SAndroid Build Coastguard Worker---
13*6236dae4SAndroid Build Coastguard Worker<!-- markdown-link-check-disable -->
14*6236dae4SAndroid Build Coastguard Worker# NAME
15*6236dae4SAndroid Build Coastguard Worker
16*6236dae4SAndroid Build Coastguard Workerlibcurl-security - security considerations when using libcurl
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard Worker# Security
19*6236dae4SAndroid Build Coastguard Worker
20*6236dae4SAndroid Build Coastguard WorkerThe libcurl project takes security seriously. The library is written with
21*6236dae4SAndroid Build Coastguard Workercaution and precautions are taken to mitigate many kinds of risks encountered
22*6236dae4SAndroid Build Coastguard Workerwhile operating with potentially malicious servers on the Internet. It is a
23*6236dae4SAndroid Build Coastguard Workerpowerful library, however, which allows application writers to make trade-offs
24*6236dae4SAndroid Build Coastguard Workerbetween ease of writing and exposure to potential risky operations. If used
25*6236dae4SAndroid Build Coastguard Workerthe right way, you can use libcurl to transfer data pretty safely.
26*6236dae4SAndroid Build Coastguard Worker
27*6236dae4SAndroid Build Coastguard WorkerMany applications are used in closed networks where users and servers can
28*6236dae4SAndroid Build Coastguard Worker(possibly) be trusted, but many others are used on arbitrary servers and are
29*6236dae4SAndroid Build Coastguard Workerfed input from potentially untrusted users. Following is a discussion about
30*6236dae4SAndroid Build Coastguard Workersome risks in the ways in which applications commonly use libcurl and
31*6236dae4SAndroid Build Coastguard Workerpotential mitigations of those risks. It is not comprehensive, but shows
32*6236dae4SAndroid Build Coastguard Workerclasses of attacks that robust applications should consider. The Common
33*6236dae4SAndroid Build Coastguard WorkerWeakness Enumeration project at https://cwe.mitre.org/ is a good reference for
34*6236dae4SAndroid Build Coastguard Workermany of these and similar types of weaknesses of which application writers
35*6236dae4SAndroid Build Coastguard Workershould be aware.
36*6236dae4SAndroid Build Coastguard Worker
37*6236dae4SAndroid Build Coastguard Worker# Command Lines
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard WorkerIf you use a command line tool (such as curl) that uses libcurl, and you give
40*6236dae4SAndroid Build Coastguard Workeroptions to the tool on the command line those options can get read by other
41*6236dae4SAndroid Build Coastguard Workerusers of your system when they use *ps* or other tools to list currently
42*6236dae4SAndroid Build Coastguard Workerrunning processes.
43*6236dae4SAndroid Build Coastguard Worker
44*6236dae4SAndroid Build Coastguard WorkerTo avoid these problems, never feed sensitive things to programs using command
45*6236dae4SAndroid Build Coastguard Workerline options. Write them to a protected file and use the -K option to avoid
46*6236dae4SAndroid Build Coastguard Workerthis.
47*6236dae4SAndroid Build Coastguard Worker
48*6236dae4SAndroid Build Coastguard Worker# .netrc
49*6236dae4SAndroid Build Coastguard Worker
50*6236dae4SAndroid Build Coastguard Worker.netrc is a pretty handy file/feature that allows you to login quickly and
51*6236dae4SAndroid Build Coastguard Workerautomatically to frequently visited sites. The file contains passwords in
52*6236dae4SAndroid Build Coastguard Workerclear text and is a real security risk. In some cases, your .netrc is also
53*6236dae4SAndroid Build Coastguard Workerstored in a home directory that is NFS mounted or used on another network
54*6236dae4SAndroid Build Coastguard Workerbased file system, so the clear text password flies through your network every
55*6236dae4SAndroid Build Coastguard Workertime anyone reads that file.
56*6236dae4SAndroid Build Coastguard Worker
57*6236dae4SAndroid Build Coastguard WorkerFor applications that enable .netrc use, a user who manage to set the right
58*6236dae4SAndroid Build Coastguard WorkerURL might then be possible to pass on passwords.
59*6236dae4SAndroid Build Coastguard Worker
60*6236dae4SAndroid Build Coastguard WorkerTo avoid these problems, do not use .netrc files and never store passwords in
61*6236dae4SAndroid Build Coastguard Workerplain text anywhere.
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard Worker# Clear Text Passwords
64*6236dae4SAndroid Build Coastguard Worker
65*6236dae4SAndroid Build Coastguard WorkerMany of the protocols libcurl supports send name and password unencrypted as
66*6236dae4SAndroid Build Coastguard Workerclear text (HTTP Basic authentication, FTP, TELNET etc). It is easy for anyone
67*6236dae4SAndroid Build Coastguard Workeron your network or a network nearby yours to just fire up a network analyzer
68*6236dae4SAndroid Build Coastguard Workertool and eavesdrop on your passwords. Do not let the fact that HTTP Basic uses
69*6236dae4SAndroid Build Coastguard Workerbase64 encoded passwords fool you. They may not look readable at a first
70*6236dae4SAndroid Build Coastguard Workerglance, but they are easily "deciphered" by anyone within seconds.
71*6236dae4SAndroid Build Coastguard Worker
72*6236dae4SAndroid Build Coastguard WorkerTo avoid this problem, use an authentication mechanism or other protocol that
73*6236dae4SAndroid Build Coastguard Workerdoes not let snoopers see your password: Digest, CRAM-MD5, Kerberos, SPNEGO or
74*6236dae4SAndroid Build Coastguard WorkerNTLM authentication. Or even better: use authenticated protocols that protect
75*6236dae4SAndroid Build Coastguard Workerthe entire connection and everything sent over it.
76*6236dae4SAndroid Build Coastguard Worker
77*6236dae4SAndroid Build Coastguard Worker# Unauthenticated Connections
78*6236dae4SAndroid Build Coastguard Worker
79*6236dae4SAndroid Build Coastguard WorkerProtocols that do not have any form of cryptographic authentication cannot
80*6236dae4SAndroid Build Coastguard Workerwith any certainty know that they communicate with the right remote server.
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard WorkerIf your application is using a fixed scheme or fixed hostname, it is not safe
83*6236dae4SAndroid Build Coastguard Workeras long as the connection is unauthenticated. There can be a man-in-the-middle
84*6236dae4SAndroid Build Coastguard Workeror in fact the whole server might have been replaced by an evil actor.
85*6236dae4SAndroid Build Coastguard Worker
86*6236dae4SAndroid Build Coastguard WorkerUnauthenticated protocols are unsafe. The data that comes back to curl may
87*6236dae4SAndroid Build Coastguard Workerhave been injected by an attacker. The data that curl sends might be modified
88*6236dae4SAndroid Build Coastguard Workerbefore it reaches the intended server. If it even reaches the intended server
89*6236dae4SAndroid Build Coastguard Workerat all.
90*6236dae4SAndroid Build Coastguard Worker
91*6236dae4SAndroid Build Coastguard WorkerRemedies:
92*6236dae4SAndroid Build Coastguard Worker
93*6236dae4SAndroid Build Coastguard Worker## Restrict operations to authenticated transfers
94*6236dae4SAndroid Build Coastguard Worker
95*6236dae4SAndroid Build Coastguard WorkerUse authenticated protocols protected with HTTPS or SSH.
96*6236dae4SAndroid Build Coastguard Worker
97*6236dae4SAndroid Build Coastguard Worker## Make sure the server's certificate etc is verified
98*6236dae4SAndroid Build Coastguard Worker
99*6236dae4SAndroid Build Coastguard WorkerNever ever switch off certificate verification.
100*6236dae4SAndroid Build Coastguard Worker
101*6236dae4SAndroid Build Coastguard Worker# Redirects
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard WorkerThe CURLOPT_FOLLOWLOCATION(3) option automatically follows HTTP
104*6236dae4SAndroid Build Coastguard Workerredirects sent by a remote server. These redirects can refer to any kind of
105*6236dae4SAndroid Build Coastguard WorkerURL, not just HTTP. libcurl restricts the protocols allowed to be used in
106*6236dae4SAndroid Build Coastguard Workerredirects for security reasons: only HTTP, HTTPS, FTP and FTPS are
107*6236dae4SAndroid Build Coastguard Workerenabled by default. Applications may opt to restrict that set further.
108*6236dae4SAndroid Build Coastguard Worker
109*6236dae4SAndroid Build Coastguard WorkerA redirect to a file: URL would cause the libcurl to read (or write) arbitrary
110*6236dae4SAndroid Build Coastguard Workerfiles from the local filesystem. If the application returns the data back to
111*6236dae4SAndroid Build Coastguard Workerthe user (as would happen in some kinds of CGI scripts), an attacker could
112*6236dae4SAndroid Build Coastguard Workerleverage this to read otherwise forbidden data (e.g.
113*6236dae4SAndroid Build Coastguard Worker**file://localhost/etc/passwd**).
114*6236dae4SAndroid Build Coastguard Worker
115*6236dae4SAndroid Build Coastguard WorkerIf authentication credentials are stored in the ~/.netrc file, or Kerberos is
116*6236dae4SAndroid Build Coastguard Workerin use, any other URL type (not just file:) that requires authentication is
117*6236dae4SAndroid Build Coastguard Workeralso at risk. A redirect such as **ftp://some-internal-server/private-file** would
118*6236dae4SAndroid Build Coastguard Workerthen return data even when the server is password protected.
119*6236dae4SAndroid Build Coastguard Worker
120*6236dae4SAndroid Build Coastguard WorkerIn the same way, if an unencrypted SSH private key has been configured for the
121*6236dae4SAndroid Build Coastguard Workeruser running the libcurl application, SCP: or SFTP: URLs could access password
122*6236dae4SAndroid Build Coastguard Workeror private-key protected resources,
123*6236dae4SAndroid Build Coastguard Workere.g. **sftp://user@some-internal-server/etc/passwd**
124*6236dae4SAndroid Build Coastguard Worker
125*6236dae4SAndroid Build Coastguard WorkerThe CURLOPT_REDIR_PROTOCOLS_STR(3) and CURLOPT_NETRC(3) options can be
126*6236dae4SAndroid Build Coastguard Workerused to mitigate against this kind of attack.
127*6236dae4SAndroid Build Coastguard Worker
128*6236dae4SAndroid Build Coastguard WorkerA redirect can also specify a location available only on the machine running
129*6236dae4SAndroid Build Coastguard Workerlibcurl, including servers hidden behind a firewall from the attacker.
130*6236dae4SAndroid Build Coastguard WorkerE.g. **http://127.0.0.1/** or **http://intranet/delete-stuff.cgi?delete=all** or
131*6236dae4SAndroid Build Coastguard Worker**tftp://bootp-server/pc-config-data**
132*6236dae4SAndroid Build Coastguard Worker
133*6236dae4SAndroid Build Coastguard WorkerApplications can mitigate against this by disabling
134*6236dae4SAndroid Build Coastguard WorkerCURLOPT_FOLLOWLOCATION(3) and handling redirects itself, sanitizing URLs
135*6236dae4SAndroid Build Coastguard Workeras necessary. Alternately, an app could leave CURLOPT_FOLLOWLOCATION(3)
136*6236dae4SAndroid Build Coastguard Workerenabled but set CURLOPT_REDIR_PROTOCOLS_STR(3) and install a
137*6236dae4SAndroid Build Coastguard WorkerCURLOPT_OPENSOCKETFUNCTION(3) or CURLOPT_PREREQFUNCTION(3) callback
138*6236dae4SAndroid Build Coastguard Workerfunction in which addresses are sanitized before use.
139*6236dae4SAndroid Build Coastguard Worker
140*6236dae4SAndroid Build Coastguard Worker# CRLF in Headers
141*6236dae4SAndroid Build Coastguard Worker
142*6236dae4SAndroid Build Coastguard WorkerFor all options in libcurl which specify headers, including but not limited to
143*6236dae4SAndroid Build Coastguard WorkerCURLOPT_HTTPHEADER(3), CURLOPT_PROXYHEADER(3),
144*6236dae4SAndroid Build Coastguard WorkerCURLOPT_COOKIE(3), CURLOPT_USERAGENT(3), CURLOPT_REFERER(3)
145*6236dae4SAndroid Build Coastguard Workerand CURLOPT_RANGE(3), libcurl sends the headers as-is and does not apply
146*6236dae4SAndroid Build Coastguard Workerany special sanitation or normalization to them.
147*6236dae4SAndroid Build Coastguard Worker
148*6236dae4SAndroid Build Coastguard WorkerIf you allow untrusted user input into these options without sanitizing CRLF
149*6236dae4SAndroid Build Coastguard Workersequences in them, someone malicious may be able to modify the request in a
150*6236dae4SAndroid Build Coastguard Workerway you did not intend such as injecting new headers.
151*6236dae4SAndroid Build Coastguard Worker
152*6236dae4SAndroid Build Coastguard Worker# Local Resources
153*6236dae4SAndroid Build Coastguard Worker
154*6236dae4SAndroid Build Coastguard WorkerA user who can control the DNS server of a domain being passed in within a URL
155*6236dae4SAndroid Build Coastguard Workercan change the address of the host to a local, private address which a
156*6236dae4SAndroid Build Coastguard Workerserver-side libcurl-using application could then use. E.g. the innocuous URL
157*6236dae4SAndroid Build Coastguard Worker**http://fuzzybunnies.example.com/** could actually resolve to the IP
158*6236dae4SAndroid Build Coastguard Workeraddress of a server behind a firewall, such as 127.0.0.1 or
159*6236dae4SAndroid Build Coastguard Worker10.1.2.3. Applications can mitigate against this by setting a
160*6236dae4SAndroid Build Coastguard WorkerCURLOPT_OPENSOCKETFUNCTION(3) or CURLOPT_PREREQFUNCTION(3) and
161*6236dae4SAndroid Build Coastguard Workerchecking the address before a connection.
162*6236dae4SAndroid Build Coastguard Worker
163*6236dae4SAndroid Build Coastguard WorkerAll the malicious scenarios regarding redirected URLs apply just as well to
164*6236dae4SAndroid Build Coastguard Workernon-redirected URLs, if the user is allowed to specify an arbitrary URL that
165*6236dae4SAndroid Build Coastguard Workercould point to a private resource. For example, a web app providing a
166*6236dae4SAndroid Build Coastguard Workertranslation service might happily translate **file://localhost/etc/passwd**
167*6236dae4SAndroid Build Coastguard Workerand display the result. Applications can mitigate against this with the
168*6236dae4SAndroid Build Coastguard WorkerCURLOPT_PROTOCOLS_STR(3) option as well as by similar mitigation techniques
169*6236dae4SAndroid Build Coastguard Workerfor redirections.
170*6236dae4SAndroid Build Coastguard Worker
171*6236dae4SAndroid Build Coastguard WorkerA malicious FTP server could in response to the PASV command return an IP
172*6236dae4SAndroid Build Coastguard Workeraddress and port number for a server local to the app running libcurl but
173*6236dae4SAndroid Build Coastguard Workerbehind a firewall. Applications can mitigate against this by using the
174*6236dae4SAndroid Build Coastguard WorkerCURLOPT_FTP_SKIP_PASV_IP(3) option or CURLOPT_FTPPORT(3).
175*6236dae4SAndroid Build Coastguard Worker
176*6236dae4SAndroid Build Coastguard WorkerLocal servers sometimes assume local access comes from friends and trusted
177*6236dae4SAndroid Build Coastguard Workerusers. An application that expects https://example.com/file_to_read that and
178*6236dae4SAndroid Build Coastguard Workerinstead gets http://192.168.0.1/my_router_config might print a file that would
179*6236dae4SAndroid Build Coastguard Workerotherwise be protected by the firewall.
180*6236dae4SAndroid Build Coastguard Worker
181*6236dae4SAndroid Build Coastguard WorkerAllowing your application to connect to local hosts, be it the same machine
182*6236dae4SAndroid Build Coastguard Workerthat runs the application or a machine on the same local network, might be
183*6236dae4SAndroid Build Coastguard Workerpossible to exploit by an attacker who then perhaps can "port-scan" the
184*6236dae4SAndroid Build Coastguard Workerparticular hosts - depending on how the application and servers acts.
185*6236dae4SAndroid Build Coastguard Worker
186*6236dae4SAndroid Build Coastguard Worker# IPv4 Addresses
187*6236dae4SAndroid Build Coastguard Worker
188*6236dae4SAndroid Build Coastguard WorkerSome users might be tempted to filter access to local resources or similar
189*6236dae4SAndroid Build Coastguard Workerbased on numerical IPv4 addresses used in URLs. This is a bad and error-prone
190*6236dae4SAndroid Build Coastguard Workeridea because of the many different ways a numerical IPv4 address can be
191*6236dae4SAndroid Build Coastguard Workerspecified and libcurl accepts: one to four dot-separated fields using one of
192*6236dae4SAndroid Build Coastguard Workeror a mix of decimal, octal or hexadecimal encoding.
193*6236dae4SAndroid Build Coastguard Worker
194*6236dae4SAndroid Build Coastguard Worker# IPv6 Addresses
195*6236dae4SAndroid Build Coastguard Worker
196*6236dae4SAndroid Build Coastguard Workerlibcurl handles IPv6 addresses transparently and just as easily as IPv4
197*6236dae4SAndroid Build Coastguard Workeraddresses. That means that a sanitizing function that filters out addresses
198*6236dae4SAndroid Build Coastguard Workerlike 127.0.0.1 is not sufficient - the equivalent IPv6 addresses **::1**,
199*6236dae4SAndroid Build Coastguard Worker**::**, **0:00::0:1**, **::127.0.0.1** and **::ffff:7f00:1** supplied
200*6236dae4SAndroid Build Coastguard Workersomehow by an attacker would all bypass a naive filter and could allow access
201*6236dae4SAndroid Build Coastguard Workerto undesired local resources. IPv6 also has special address blocks like
202*6236dae4SAndroid Build Coastguard Workerlink-local and site-local that generally should not be accessed by a
203*6236dae4SAndroid Build Coastguard Workerserver-side libcurl-using application. A poorly configured firewall installed
204*6236dae4SAndroid Build Coastguard Workerin a data center, organization or server may also be configured to limit IPv4
205*6236dae4SAndroid Build Coastguard Workerconnections but leave IPv6 connections wide open. In some cases, setting
206*6236dae4SAndroid Build Coastguard WorkerCURLOPT_IPRESOLVE(3) to CURL_IPRESOLVE_V4 can be used to limit resolved
207*6236dae4SAndroid Build Coastguard Workeraddresses to IPv4 only and bypass these issues.
208*6236dae4SAndroid Build Coastguard Worker
209*6236dae4SAndroid Build Coastguard Worker# Uploads
210*6236dae4SAndroid Build Coastguard Worker
211*6236dae4SAndroid Build Coastguard WorkerWhen uploading, a redirect can cause a local (or remote) file to be
212*6236dae4SAndroid Build Coastguard Workeroverwritten. Applications must not allow any unsanitized URL to be passed in
213*6236dae4SAndroid Build Coastguard Workerfor uploads. Also, CURLOPT_FOLLOWLOCATION(3) should not be used on
214*6236dae4SAndroid Build Coastguard Workeruploads. Instead, the applications should consider handling redirects itself,
215*6236dae4SAndroid Build Coastguard Workersanitizing each URL first.
216*6236dae4SAndroid Build Coastguard Worker
217*6236dae4SAndroid Build Coastguard Worker# Authentication
218*6236dae4SAndroid Build Coastguard Worker
219*6236dae4SAndroid Build Coastguard WorkerUse of CURLOPT_UNRESTRICTED_AUTH(3) could cause authentication
220*6236dae4SAndroid Build Coastguard Workerinformation to be sent to an unknown second server. Applications can mitigate
221*6236dae4SAndroid Build Coastguard Workeragainst this by disabling CURLOPT_FOLLOWLOCATION(3) and handling
222*6236dae4SAndroid Build Coastguard Workerredirects itself, sanitizing where necessary.
223*6236dae4SAndroid Build Coastguard Worker
224*6236dae4SAndroid Build Coastguard WorkerUse of the CURLAUTH_ANY option to CURLOPT_HTTPAUTH(3) could result in username
225*6236dae4SAndroid Build Coastguard Workerand password being sent in clear text to an HTTP server. Instead, use
226*6236dae4SAndroid Build Coastguard WorkerCURLAUTH_ANYSAFE which ensures that the password is encrypted over the
227*6236dae4SAndroid Build Coastguard Workernetwork, or else fail the request.
228*6236dae4SAndroid Build Coastguard Worker
229*6236dae4SAndroid Build Coastguard WorkerUse of the CURLUSESSL_TRY option to CURLOPT_USE_SSL(3) could result in
230*6236dae4SAndroid Build Coastguard Workerusername and password being sent in clear text to an FTP server. Instead, use
231*6236dae4SAndroid Build Coastguard WorkerCURLUSESSL_CONTROL to ensure that an encrypted connection is used or else fail
232*6236dae4SAndroid Build Coastguard Workerthe request.
233*6236dae4SAndroid Build Coastguard Worker
234*6236dae4SAndroid Build Coastguard Worker# Cookies
235*6236dae4SAndroid Build Coastguard Worker
236*6236dae4SAndroid Build Coastguard WorkerIf cookies are enabled and cached, then a user could craft a URL which
237*6236dae4SAndroid Build Coastguard Workerperforms some malicious action to a site whose authentication is already
238*6236dae4SAndroid Build Coastguard Workerstored in a cookie. E.g.
239*6236dae4SAndroid Build Coastguard Worker**http://mail.example.com/delete-stuff.cgi?delete=all** Applications can
240*6236dae4SAndroid Build Coastguard Workermitigate against this by disabling cookies or clearing them between requests.
241*6236dae4SAndroid Build Coastguard Worker
242*6236dae4SAndroid Build Coastguard Worker# Dangerous SCP URLs
243*6236dae4SAndroid Build Coastguard Worker
244*6236dae4SAndroid Build Coastguard WorkerSCP URLs can contain raw commands within the scp: URL, which is a side effect
245*6236dae4SAndroid Build Coastguard Workerof how the SCP protocol is designed. E.g.
246*6236dae4SAndroid Build Coastguard Worker~~~
247*6236dae4SAndroid Build Coastguard Worker  scp://user:pass@host/a;date >/tmp/test;
248*6236dae4SAndroid Build Coastguard Worker~~~
249*6236dae4SAndroid Build Coastguard WorkerApplications must not allow unsanitized SCP: URLs to be passed in for
250*6236dae4SAndroid Build Coastguard Workerdownloads.
251*6236dae4SAndroid Build Coastguard Worker
252*6236dae4SAndroid Build Coastguard Worker# file://
253*6236dae4SAndroid Build Coastguard Worker
254*6236dae4SAndroid Build Coastguard WorkerBy default curl and libcurl support file:// URLs. Such a URL is always an
255*6236dae4SAndroid Build Coastguard Workeraccess, or attempted access, to a local resource. If your application wants to
256*6236dae4SAndroid Build Coastguard Workeravoid that, keep control of what URLs to use and/or prevent curl/libcurl from
257*6236dae4SAndroid Build Coastguard Workerusing the protocol.
258*6236dae4SAndroid Build Coastguard Worker
259*6236dae4SAndroid Build Coastguard WorkerBy default, libcurl prohibits redirects to file:// URLs.
260*6236dae4SAndroid Build Coastguard Worker
261*6236dae4SAndroid Build Coastguard Worker# Warning: file:// on Windows
262*6236dae4SAndroid Build Coastguard Worker
263*6236dae4SAndroid Build Coastguard WorkerThe Windows operating system tries automatically, and without any way for
264*6236dae4SAndroid Build Coastguard Workerapplications to disable it, to establish a connection to another host over the
265*6236dae4SAndroid Build Coastguard Workernetwork and access it (over SMB or other protocols), if only the correct file
266*6236dae4SAndroid Build Coastguard Workerpath is accessed.
267*6236dae4SAndroid Build Coastguard Worker
268*6236dae4SAndroid Build Coastguard WorkerWhen first realizing this, the curl team tried to filter out such attempts in
269*6236dae4SAndroid Build Coastguard Workerorder to protect applications for inadvertent probes of for example internal
270*6236dae4SAndroid Build Coastguard Workernetworks etc. This resulted in CVE-2019-15601 and the associated security fix.
271*6236dae4SAndroid Build Coastguard Worker
272*6236dae4SAndroid Build Coastguard WorkerHowever, we have since been made aware of the fact that the previous fix was far
273*6236dae4SAndroid Build Coastguard Workerfrom adequate as there are several other ways to accomplish more or less the
274*6236dae4SAndroid Build Coastguard Workersame thing: accessing a remote host over the network instead of the local file
275*6236dae4SAndroid Build Coastguard Workersystem.
276*6236dae4SAndroid Build Coastguard Worker
277*6236dae4SAndroid Build Coastguard WorkerThe conclusion we have come to is that this is a weakness or feature in the
278*6236dae4SAndroid Build Coastguard WorkerWindows operating system itself, that we as an application cannot safely
279*6236dae4SAndroid Build Coastguard Workerprotect users against. It would just be a whack-a-mole race we do not want to
280*6236dae4SAndroid Build Coastguard Workerparticipate in. There are too many ways to do it and there is no knob we can
281*6236dae4SAndroid Build Coastguard Workeruse to turn off the practice.
282*6236dae4SAndroid Build Coastguard Worker
283*6236dae4SAndroid Build Coastguard WorkerIf you use curl or libcurl on Windows (any version), disable the use of the
284*6236dae4SAndroid Build Coastguard WorkerFILE protocol in curl or be prepared that accesses to a range of "magic paths"
285*6236dae4SAndroid Build Coastguard Workerpotentially make your system access other hosts on your network. curl cannot
286*6236dae4SAndroid Build Coastguard Workerprotect you against this.
287*6236dae4SAndroid Build Coastguard Worker
288*6236dae4SAndroid Build Coastguard Worker# What if the user can set the URL
289*6236dae4SAndroid Build Coastguard Worker
290*6236dae4SAndroid Build Coastguard WorkerApplications may find it tempting to let users set the URL that it can work
291*6236dae4SAndroid Build Coastguard Workeron. That is probably fine, but opens up for mischief and trickery that you as
292*6236dae4SAndroid Build Coastguard Workeran application author may want to address or take precautions against.
293*6236dae4SAndroid Build Coastguard Worker
294*6236dae4SAndroid Build Coastguard WorkerIf your curl-using script allow a custom URL do you also, perhaps
295*6236dae4SAndroid Build Coastguard Workerunintentionally, allow the user to pass other options to the curl command line
296*6236dae4SAndroid Build Coastguard Workerif creative use of special characters are applied?
297*6236dae4SAndroid Build Coastguard Worker
298*6236dae4SAndroid Build Coastguard WorkerIf the user can set the URL, the user can also specify the scheme part to
299*6236dae4SAndroid Build Coastguard Workerother protocols that you did not intend for users to use and perhaps did not
300*6236dae4SAndroid Build Coastguard Workerconsider. curl supports over 20 different URL schemes. "http://" might be what
301*6236dae4SAndroid Build Coastguard Workeryou thought, "ftp://" or "imap://" might be what the user gives your
302*6236dae4SAndroid Build Coastguard Workerapplication. Also, cross-protocol operations might be done by using a
303*6236dae4SAndroid Build Coastguard Workerparticular scheme in the URL but point to a server doing a different protocol
304*6236dae4SAndroid Build Coastguard Workeron a non-standard port.
305*6236dae4SAndroid Build Coastguard Worker
306*6236dae4SAndroid Build Coastguard WorkerRemedies:
307*6236dae4SAndroid Build Coastguard Worker
308*6236dae4SAndroid Build Coastguard Worker## Use --proto
309*6236dae4SAndroid Build Coastguard Worker
310*6236dae4SAndroid Build Coastguard Workercurl command lines can use *--proto* to limit what URL schemes it accepts
311*6236dae4SAndroid Build Coastguard Worker
312*6236dae4SAndroid Build Coastguard Worker## Use CURLOPT_PROTOCOLS_STR
313*6236dae4SAndroid Build Coastguard Worker
314*6236dae4SAndroid Build Coastguard Workerlibcurl programs can use CURLOPT_PROTOCOLS_STR(3) to limit what URL schemes it accepts
315*6236dae4SAndroid Build Coastguard Worker
316*6236dae4SAndroid Build Coastguard Worker## consider not allowing the user to set the full URL
317*6236dae4SAndroid Build Coastguard Worker
318*6236dae4SAndroid Build Coastguard WorkerMaybe just let the user provide data for parts of it? Or maybe filter input to
319*6236dae4SAndroid Build Coastguard Workeronly allow specific choices?
320*6236dae4SAndroid Build Coastguard Worker
321*6236dae4SAndroid Build Coastguard Worker# RFC 3986 vs WHATWG URL
322*6236dae4SAndroid Build Coastguard Worker
323*6236dae4SAndroid Build Coastguard Workercurl supports URLs mostly according to how they are defined in RFC 3986, and
324*6236dae4SAndroid Build Coastguard Workerhas done so since the beginning.
325*6236dae4SAndroid Build Coastguard Worker
326*6236dae4SAndroid Build Coastguard WorkerWeb browsers mostly adhere to the WHATWG URL Specification.
327*6236dae4SAndroid Build Coastguard Worker
328*6236dae4SAndroid Build Coastguard WorkerThis deviance makes some URLs copied between browsers (or returned over HTTP
329*6236dae4SAndroid Build Coastguard Workerfor redirection) and curl not work the same way. It can also cause problems if
330*6236dae4SAndroid Build Coastguard Workeran application parses URLs differently from libcurl and makes different
331*6236dae4SAndroid Build Coastguard Workerassumptions about a link. This can mislead users into getting the wrong thing,
332*6236dae4SAndroid Build Coastguard Workerconnecting to the wrong host or otherwise not working identically.
333*6236dae4SAndroid Build Coastguard Worker
334*6236dae4SAndroid Build Coastguard WorkerWithin an application, this can be mitigated by always using the
335*6236dae4SAndroid Build Coastguard Workercurl_url(3) API to parse URLs, ensuring that they are parsed the same way
336*6236dae4SAndroid Build Coastguard Workeras within libcurl itself.
337*6236dae4SAndroid Build Coastguard Worker
338*6236dae4SAndroid Build Coastguard Worker# FTP uses two connections
339*6236dae4SAndroid Build Coastguard Worker
340*6236dae4SAndroid Build Coastguard WorkerWhen performing an FTP transfer, two TCP connections are used: one for setting
341*6236dae4SAndroid Build Coastguard Workerup the transfer and one for the actual data.
342*6236dae4SAndroid Build Coastguard Worker
343*6236dae4SAndroid Build Coastguard WorkerFTP is not only unauthenticated, but the setting up of the second transfer is
344*6236dae4SAndroid Build Coastguard Workeralso a weak spot. The second connection to use for data, is either setup with
345*6236dae4SAndroid Build Coastguard Workerthe PORT/EPRT command that makes the server connect back to the client on the
346*6236dae4SAndroid Build Coastguard Workergiven IP+PORT, or with PASV/EPSV that makes the server setup a port to listen
347*6236dae4SAndroid Build Coastguard Workerto and tells the client to connect to a given IP+PORT.
348*6236dae4SAndroid Build Coastguard Worker
349*6236dae4SAndroid Build Coastguard WorkerAgain, unauthenticated means that the connection might be meddled with by a
350*6236dae4SAndroid Build Coastguard Workerman-in-the-middle or that there is a malicious server pretending to be the
351*6236dae4SAndroid Build Coastguard Workerright one.
352*6236dae4SAndroid Build Coastguard Worker
353*6236dae4SAndroid Build Coastguard WorkerA malicious FTP server can respond to PASV commands with the IP+PORT of a
354*6236dae4SAndroid Build Coastguard Workertotally different machine. Perhaps even a third party host, and when there are
355*6236dae4SAndroid Build Coastguard Workermany clients trying to connect to that third party, it could create a
356*6236dae4SAndroid Build Coastguard WorkerDistributed Denial-Of-Service attack out of it. If the client makes an upload
357*6236dae4SAndroid Build Coastguard Workeroperation, it can make the client send the data to another site. If the
358*6236dae4SAndroid Build Coastguard Workerattacker can affect what data the client uploads, it can be made to work as a
359*6236dae4SAndroid Build Coastguard WorkerHTTP request and then the client could be made to issue HTTP requests to third
360*6236dae4SAndroid Build Coastguard Workerparty hosts.
361*6236dae4SAndroid Build Coastguard Worker
362*6236dae4SAndroid Build Coastguard WorkerAn attacker that manages to control curl's command line options can tell curl
363*6236dae4SAndroid Build Coastguard Workerto send an FTP PORT command to ask the server to connect to a third party host
364*6236dae4SAndroid Build Coastguard Workerinstead of back to curl.
365*6236dae4SAndroid Build Coastguard Worker
366*6236dae4SAndroid Build Coastguard WorkerThe fact that FTP uses two connections makes it vulnerable in a way that is
367*6236dae4SAndroid Build Coastguard Workerhard to avoid.
368*6236dae4SAndroid Build Coastguard Worker
369*6236dae4SAndroid Build Coastguard Worker# Active FTP passes on the local IP address
370*6236dae4SAndroid Build Coastguard Worker
371*6236dae4SAndroid Build Coastguard WorkerIf you use curl/libcurl to do *active* FTP transfers, curl passes on the
372*6236dae4SAndroid Build Coastguard Workeraddress of your local IP to the remote server - even when for example using a
373*6236dae4SAndroid Build Coastguard WorkerSOCKS or HTTP proxy in between curl and the target server.
374*6236dae4SAndroid Build Coastguard Worker
375*6236dae4SAndroid Build Coastguard Worker# Denial of Service
376*6236dae4SAndroid Build Coastguard Worker
377*6236dae4SAndroid Build Coastguard WorkerA malicious server could cause libcurl to effectively hang by sending data
378*6236dae4SAndroid Build Coastguard Workerslowly, or even no data at all but just keeping the TCP connection open. This
379*6236dae4SAndroid Build Coastguard Workercould effectively result in a denial-of-service attack. The
380*6236dae4SAndroid Build Coastguard WorkerCURLOPT_TIMEOUT(3) and/or CURLOPT_LOW_SPEED_LIMIT(3) options can
381*6236dae4SAndroid Build Coastguard Workerbe used to mitigate against this.
382*6236dae4SAndroid Build Coastguard Worker
383*6236dae4SAndroid Build Coastguard WorkerA malicious server could cause libcurl to download an infinite amount of data,
384*6236dae4SAndroid Build Coastguard Workerpotentially causing system resources to be exhausted resulting in a system or
385*6236dae4SAndroid Build Coastguard Workerapplication crash. Setting the CURLOPT_MAXFILESIZE_LARGE(3) option is not
386*6236dae4SAndroid Build Coastguard Workersufficient to guard against this. Instead, applications should monitor the
387*6236dae4SAndroid Build Coastguard Workeramount of data received within the write or progress callback and abort once
388*6236dae4SAndroid Build Coastguard Workerthe limit is reached.
389*6236dae4SAndroid Build Coastguard Worker
390*6236dae4SAndroid Build Coastguard WorkerA malicious HTTP server could cause an infinite redirection loop, causing a
391*6236dae4SAndroid Build Coastguard Workerdenial-of-service. This can be mitigated by using the
392*6236dae4SAndroid Build Coastguard WorkerCURLOPT_MAXREDIRS(3) option.
393*6236dae4SAndroid Build Coastguard Worker
394*6236dae4SAndroid Build Coastguard Worker# Arbitrary Headers
395*6236dae4SAndroid Build Coastguard Worker
396*6236dae4SAndroid Build Coastguard WorkerUser-supplied data must be sanitized when used in options like
397*6236dae4SAndroid Build Coastguard WorkerCURLOPT_USERAGENT(3), CURLOPT_HTTPHEADER(3),
398*6236dae4SAndroid Build Coastguard WorkerCURLOPT_POSTFIELDS(3) and others that are used to generate structured
399*6236dae4SAndroid Build Coastguard Workerdata. Characters like embedded carriage returns or ampersands could allow the
400*6236dae4SAndroid Build Coastguard Workeruser to create additional headers or fields that could cause malicious
401*6236dae4SAndroid Build Coastguard Workertransactions.
402*6236dae4SAndroid Build Coastguard Worker
403*6236dae4SAndroid Build Coastguard Worker# Server-supplied Names
404*6236dae4SAndroid Build Coastguard Worker
405*6236dae4SAndroid Build Coastguard WorkerA server can supply data which the application may, in some cases, use as a
406*6236dae4SAndroid Build Coastguard Workerfilename. The curl command-line tool does this with *--remote-header-name*,
407*6236dae4SAndroid Build Coastguard Workerusing the Content-disposition: header to generate a filename. An application
408*6236dae4SAndroid Build Coastguard Workercould also use CURLINFO_EFFECTIVE_URL(3) to generate a filename from a
409*6236dae4SAndroid Build Coastguard Workerserver-supplied redirect URL. Special care must be taken to sanitize such
410*6236dae4SAndroid Build Coastguard Workernames to avoid the possibility of a malicious server supplying one like
411*6236dae4SAndroid Build Coastguard Worker**"/etc/passwd"**, **"autoexec.bat"**, **"prn:"** or even **".bashrc"**.
412*6236dae4SAndroid Build Coastguard Worker
413*6236dae4SAndroid Build Coastguard Worker# Server Certificates
414*6236dae4SAndroid Build Coastguard Worker
415*6236dae4SAndroid Build Coastguard WorkerA secure application should never use the CURLOPT_SSL_VERIFYPEER(3)
416*6236dae4SAndroid Build Coastguard Workeroption to disable certificate validation. There are numerous attacks that are
417*6236dae4SAndroid Build Coastguard Workerenabled by applications that fail to properly validate server TLS/SSL
418*6236dae4SAndroid Build Coastguard Workercertificates, thus enabling a malicious server to spoof a legitimate
419*6236dae4SAndroid Build Coastguard Workerone. HTTPS without validated certificates is potentially as insecure as a
420*6236dae4SAndroid Build Coastguard Workerplain HTTP connection.
421*6236dae4SAndroid Build Coastguard Worker
422*6236dae4SAndroid Build Coastguard Worker# Showing What You Do
423*6236dae4SAndroid Build Coastguard Worker
424*6236dae4SAndroid Build Coastguard WorkerRelatedly, be aware that in situations when you have problems with libcurl and
425*6236dae4SAndroid Build Coastguard Workerask someone for help, everything you reveal in order to get best possible help
426*6236dae4SAndroid Build Coastguard Workermight also impose certain security related risks. Hostnames, usernames, paths,
427*6236dae4SAndroid Build Coastguard Workeroperating system specifics, etc. (not to mention passwords of course) may in
428*6236dae4SAndroid Build Coastguard Workerfact be used by intruders to gain additional information of a potential
429*6236dae4SAndroid Build Coastguard Workertarget.
430*6236dae4SAndroid Build Coastguard Worker
431*6236dae4SAndroid Build Coastguard WorkerBe sure to limit access to application logs if they could hold private or
432*6236dae4SAndroid Build Coastguard Workersecurity-related data. Besides the obvious candidates like usernames and
433*6236dae4SAndroid Build Coastguard Workerpasswords, things like URLs, cookies or even filenames could also hold
434*6236dae4SAndroid Build Coastguard Workersensitive data.
435*6236dae4SAndroid Build Coastguard Worker
436*6236dae4SAndroid Build Coastguard WorkerTo avoid this problem, you must of course use your common sense. Often, you
437*6236dae4SAndroid Build Coastguard Workercan just edit out the sensitive data or just search/replace your true
438*6236dae4SAndroid Build Coastguard Workerinformation with faked data.
439*6236dae4SAndroid Build Coastguard Worker
440*6236dae4SAndroid Build Coastguard Worker# setuid applications using libcurl
441*6236dae4SAndroid Build Coastguard Worker
442*6236dae4SAndroid Build Coastguard Workerlibcurl-using applications that set the 'setuid' bit to run with elevated or
443*6236dae4SAndroid Build Coastguard Workermodified rights also implicitly give that extra power to libcurl and this
444*6236dae4SAndroid Build Coastguard Workershould only be done after careful considerations.
445*6236dae4SAndroid Build Coastguard Worker
446*6236dae4SAndroid Build Coastguard WorkerGiving setuid powers to the application means that libcurl can save files using
447*6236dae4SAndroid Build Coastguard Workerthose new rights (if for example the `SSLKEYLOGFILE` environment variable is
448*6236dae4SAndroid Build Coastguard Workerset). Also: if the application wants these powers to read or manage secrets
449*6236dae4SAndroid Build Coastguard Workerthat the user is otherwise not able to view (like credentials for a login
450*6236dae4SAndroid Build Coastguard Workeretc), it should be noted that libcurl still might understand proxy environment
451*6236dae4SAndroid Build Coastguard Workervariables that allow the user to redirect libcurl operations to use a proxy
452*6236dae4SAndroid Build Coastguard Workercontrolled by the user.
453*6236dae4SAndroid Build Coastguard Worker
454*6236dae4SAndroid Build Coastguard Worker# File descriptors, fork and NTLM
455*6236dae4SAndroid Build Coastguard Worker
456*6236dae4SAndroid Build Coastguard WorkerAn application that uses libcurl and invokes *fork()* gets all file
457*6236dae4SAndroid Build Coastguard Workerdescriptors duplicated in the child process, including the ones libcurl
458*6236dae4SAndroid Build Coastguard Workercreated.
459*6236dae4SAndroid Build Coastguard Worker
460*6236dae4SAndroid Build Coastguard Workerlibcurl itself uses *fork()* and *execl()* if told to use the
461*6236dae4SAndroid Build Coastguard Worker**CURLAUTH_NTLM_WB** authentication method which then invokes the helper
462*6236dae4SAndroid Build Coastguard Workercommand in a child process with file descriptors duplicated. Make sure that
463*6236dae4SAndroid Build Coastguard Workeronly the trusted and reliable helper program is invoked.
464*6236dae4SAndroid Build Coastguard Worker
465*6236dae4SAndroid Build Coastguard WorkerThis feature was removed from curl in 8.8.0.
466*6236dae4SAndroid Build Coastguard Worker
467*6236dae4SAndroid Build Coastguard Worker# Secrets in memory
468*6236dae4SAndroid Build Coastguard Worker
469*6236dae4SAndroid Build Coastguard WorkerWhen applications pass usernames, passwords or other sensitive data to
470*6236dae4SAndroid Build Coastguard Workerlibcurl to be used for upcoming transfers, those secrets are kept around as-is
471*6236dae4SAndroid Build Coastguard Workerin memory. In many cases they are stored in the heap for as long as the handle
472*6236dae4SAndroid Build Coastguard Workeritself for which the options are set.
473*6236dae4SAndroid Build Coastguard Worker
474*6236dae4SAndroid Build Coastguard WorkerIf an attacker can access the heap, like maybe by reading swap space or via a
475*6236dae4SAndroid Build Coastguard Workercore dump file, such data might be accessible.
476*6236dae4SAndroid Build Coastguard Worker
477*6236dae4SAndroid Build Coastguard WorkerFurther, when eventually closing a handle and the secrets are no longer
478*6236dae4SAndroid Build Coastguard Workerneeded, libcurl does not explicitly clear memory before freeing it, so
479*6236dae4SAndroid Build Coastguard Workercredentials may be left in freed data.
480*6236dae4SAndroid Build Coastguard Worker
481*6236dae4SAndroid Build Coastguard Worker# Saving files
482*6236dae4SAndroid Build Coastguard Worker
483*6236dae4SAndroid Build Coastguard Workerlibcurl cannot protect against attacks where an attacker has write access to
484*6236dae4SAndroid Build Coastguard Workerthe same directory where libcurl is directed to save files.
485*6236dae4SAndroid Build Coastguard Worker
486*6236dae4SAndroid Build Coastguard Worker# Cookies
487*6236dae4SAndroid Build Coastguard Worker
488*6236dae4SAndroid Build Coastguard WorkerIf libcurl is built with PSL (**Public Suffix List**) support, it detects and
489*6236dae4SAndroid Build Coastguard Workerdiscards cookies that are specified for such suffix domains that should not be
490*6236dae4SAndroid Build Coastguard Workerallowed to have cookies.
491*6236dae4SAndroid Build Coastguard Worker
492*6236dae4SAndroid Build Coastguard Workerif libcurl is *not* built with PSL support, it has no ability to stop super
493*6236dae4SAndroid Build Coastguard Workercookies.
494*6236dae4SAndroid Build Coastguard Worker
495*6236dae4SAndroid Build Coastguard Worker# Report Security Problems
496*6236dae4SAndroid Build Coastguard Worker
497*6236dae4SAndroid Build Coastguard WorkerShould you detect or just suspect a security problem in libcurl or curl,
498*6236dae4SAndroid Build Coastguard Workercontact the project curl security team immediately. See
499*6236dae4SAndroid Build Coastguard Workerhttps://curl.se/dev/secprocess.html for details.
500