Coverage Report

Created: 2025-11-09 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/curl/lib/http_proxy.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#include "http_proxy.h"
28
29
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
30
31
#include <curl/curl.h>
32
#include "sendf.h"
33
#include "http.h"
34
#include "url.h"
35
#include "select.h"
36
#include "progress.h"
37
#include "cfilters.h"
38
#include "cf-h1-proxy.h"
39
#include "cf-h2-proxy.h"
40
#include "connect.h"
41
#include "vtls/vtls.h"
42
#include "transfer.h"
43
#include "multiif.h"
44
#include "vauth/vauth.h"
45
#include "curlx/strparse.h"
46
47
/* The last 2 #include files should be in this order */
48
#include "curl_memory.h"
49
#include "memdebug.h"
50
51
static CURLcode dynhds_add_custom(struct Curl_easy *data,
52
                                  bool is_connect, int httpversion,
53
                                  struct dynhds *hds)
54
0
{
55
0
  struct connectdata *conn = data->conn;
56
0
  struct curl_slist *h[2];
57
0
  struct curl_slist *headers;
58
0
  int numlists = 1; /* by default */
59
0
  int i;
60
61
0
  enum Curl_proxy_use proxy;
62
63
0
  if(is_connect)
64
0
    proxy = HEADER_CONNECT;
65
0
  else
66
0
    proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy ?
67
0
      HEADER_PROXY : HEADER_SERVER;
68
69
0
  switch(proxy) {
70
0
  case HEADER_SERVER:
71
0
    h[0] = data->set.headers;
72
0
    break;
73
0
  case HEADER_PROXY:
74
0
    h[0] = data->set.headers;
75
0
    if(data->set.sep_headers) {
76
0
      h[1] = data->set.proxyheaders;
77
0
      numlists++;
78
0
    }
79
0
    break;
80
0
  case HEADER_CONNECT:
81
0
    if(data->set.sep_headers)
82
0
      h[0] = data->set.proxyheaders;
83
0
    else
84
0
      h[0] = data->set.headers;
85
0
    break;
86
0
  }
87
88
  /* loop through one or two lists */
89
0
  for(i = 0; i < numlists; i++) {
90
0
    for(headers = h[i]; headers; headers = headers->next) {
91
0
      struct Curl_str name;
92
0
      const char *value = NULL;
93
0
      size_t valuelen = 0;
94
0
      const char *ptr = headers->data;
95
96
      /* There are 2 quirks in place for custom headers:
97
       * 1. setting only 'name:' to suppress a header from being sent
98
       * 2. setting only 'name;' to send an empty (illegal) header
99
       */
100
0
      if(!curlx_str_cspn(&ptr, &name, ";:")) {
101
0
        if(!curlx_str_single(&ptr, ':')) {
102
0
          curlx_str_passblanks(&ptr);
103
0
          if(*ptr) {
104
0
            value = ptr;
105
0
            valuelen = strlen(value);
106
0
          }
107
0
          else {
108
            /* quirk #1, suppress this header */
109
0
            continue;
110
0
          }
111
0
        }
112
0
        else if(!curlx_str_single(&ptr, ';')) {
113
0
          curlx_str_passblanks(&ptr);
114
0
          if(!*ptr) {
115
            /* quirk #2, send an empty header */
116
0
            value = "";
117
0
            valuelen = 0;
118
0
          }
119
0
          else {
120
            /* this may be used for something else in the future,
121
             * ignore this for now */
122
0
            continue;
123
0
          }
124
0
        }
125
0
        else
126
          /* neither : nor ; in provided header value. We ignore this
127
           * silently */
128
0
          continue;
129
0
      }
130
0
      else
131
        /* no name, move on */
132
0
        continue;
133
134
0
      DEBUGASSERT(curlx_strlen(&name) && value);
135
0
      if(data->state.aptr.host &&
136
         /* a Host: header was sent already, do not pass on any custom Host:
137
            header as that will produce *two* in the same request! */
138
0
         curlx_str_casecompare(&name, "Host"));
139
0
      else if(data->state.httpreq == HTTPREQ_POST_FORM &&
140
              /* this header (extended by formdata.c) is sent later */
141
0
              curlx_str_casecompare(&name, "Content-Type"));
142
0
      else if(data->state.httpreq == HTTPREQ_POST_MIME &&
143
              /* this header is sent later */
144
0
              curlx_str_casecompare(&name, "Content-Type"));
145
0
      else if(data->req.authneg &&
146
              /* while doing auth neg, do not allow the custom length since
147
                 we will force length zero then */
148
0
              curlx_str_casecompare(&name, "Content-Length"));
149
0
      else if((httpversion >= 20) &&
150
0
              curlx_str_casecompare(&name, "Transfer-Encoding"));
151
      /* HTTP/2 and HTTP/3 do not support chunked requests */
152
0
      else if((curlx_str_casecompare(&name, "Authorization") ||
153
0
               curlx_str_casecompare(&name, "Cookie")) &&
154
              /* be careful of sending this potentially sensitive header to
155
                 other hosts */
156
0
              !Curl_auth_allowed_to_host(data));
157
0
      else {
158
0
        CURLcode result =
159
0
          Curl_dynhds_add(hds, curlx_str(&name), curlx_strlen(&name),
160
0
                          value, valuelen);
161
0
        if(result)
162
0
          return result;
163
0
      }
164
0
    }
165
0
  }
166
167
0
  return CURLE_OK;
168
0
}
169
170
void Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
171
                                     const char **phostname,
172
                                     int *pport, bool *pipv6_ip)
173
0
{
174
0
  DEBUGASSERT(cf);
175
0
  DEBUGASSERT(cf->conn);
176
177
0
  if(cf->conn->bits.conn_to_host)
178
0
    *phostname = cf->conn->conn_to_host.name;
179
0
  else if(cf->sockindex == SECONDARYSOCKET)
180
0
    *phostname = cf->conn->secondaryhostname;
181
0
  else
182
0
    *phostname = cf->conn->host.name;
183
184
0
  if(cf->sockindex == SECONDARYSOCKET)
185
0
    *pport = cf->conn->secondary_port;
186
0
  else if(cf->conn->bits.conn_to_port)
187
0
    *pport = cf->conn->conn_to_port;
188
0
  else
189
0
    *pport = cf->conn->remote_port;
190
191
0
  if(*phostname != cf->conn->host.name)
192
0
    *pipv6_ip = (strchr(*phostname, ':') != NULL);
193
0
  else
194
0
    *pipv6_ip = cf->conn->bits.ipv6_ip;
195
0
}
196
197
struct cf_proxy_ctx {
198
  int httpversion; /* HTTP version used to CONNECT */
199
  BIT(sub_filter_installed);
200
};
201
202
CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
203
                                        struct Curl_cfilter *cf,
204
                                        struct Curl_easy *data,
205
                                        int http_version_major)
206
0
{
207
0
  struct cf_proxy_ctx *ctx = cf->ctx;
208
0
  const char *hostname = NULL;
209
0
  char *authority = NULL;
210
0
  int port;
211
0
  bool ipv6_ip;
212
0
  CURLcode result;
213
0
  struct httpreq *req = NULL;
214
215
0
  Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
216
217
0
  authority = curl_maprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname,
218
0
                            ipv6_ip ?"]" : "", port);
219
0
  if(!authority) {
220
0
    result = CURLE_OUT_OF_MEMORY;
221
0
    goto out;
222
0
  }
223
224
0
  result = Curl_http_req_make(&req, "CONNECT", sizeof("CONNECT")-1,
225
0
                              NULL, 0, authority, strlen(authority),
226
0
                              NULL, 0);
227
0
  if(result)
228
0
    goto out;
229
230
  /* Setup the proxy-authorization header, if any */
231
0
  result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
232
0
                                 req->authority, TRUE);
233
0
  if(result)
234
0
    goto out;
235
236
  /* If user is not overriding Host: header, we add for HTTP/1.x */
237
0
  if(http_version_major == 1 &&
238
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
239
0
    result = Curl_dynhds_cadd(&req->headers, "Host", authority);
240
0
    if(result)
241
0
      goto out;
242
0
  }
243
244
0
  if(data->state.aptr.proxyuserpwd) {
245
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
246
0
                                      data->state.aptr.proxyuserpwd);
247
0
    if(result)
248
0
      goto out;
249
0
  }
250
251
0
  if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
252
0
     data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
253
0
    result = Curl_dynhds_cadd(&req->headers, "User-Agent",
254
0
                              data->set.str[STRING_USERAGENT]);
255
0
    if(result)
256
0
      goto out;
257
0
  }
258
259
0
  if(http_version_major == 1 &&
260
0
    !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
261
0
    result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
262
0
    if(result)
263
0
      goto out;
264
0
  }
265
266
0
  result = dynhds_add_custom(data, TRUE, ctx->httpversion, &req->headers);
267
268
0
out:
269
0
  if(result && req) {
270
0
    Curl_http_req_free(req);
271
0
    req = NULL;
272
0
  }
273
0
  free(authority);
274
0
  *preq = req;
275
0
  return result;
276
0
}
277
278
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
279
                                      struct Curl_easy *data,
280
                                      bool *done)
281
0
{
282
0
  struct cf_proxy_ctx *ctx = cf->ctx;
283
0
  CURLcode result;
284
285
0
  if(cf->connected) {
286
0
    *done = TRUE;
287
0
    return CURLE_OK;
288
0
  }
289
290
0
  CURL_TRC_CF(data, cf, "connect");
291
0
connect_sub:
292
0
  result = cf->next->cft->do_connect(cf->next, data, done);
293
0
  if(result || !*done)
294
0
    return result;
295
296
0
  *done = FALSE;
297
0
  if(!ctx->sub_filter_installed) {
298
0
    int httpversion = 0;
299
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
300
301
0
    if(alpn)
302
0
      infof(data, "CONNECT: '%s' negotiated", alpn);
303
0
    else
304
0
      infof(data, "CONNECT: no ALPN negotiated");
305
306
0
    if(alpn && !strcmp(alpn, "http/1.0")) {
307
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0");
308
0
      result = Curl_cf_h1_proxy_insert_after(cf, data);
309
0
      if(result)
310
0
        goto out;
311
0
      httpversion = 10;
312
0
    }
313
0
    else if(!alpn || !strcmp(alpn, "http/1.1")) {
314
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1");
315
0
      result = Curl_cf_h1_proxy_insert_after(cf, data);
316
0
      if(result)
317
0
        goto out;
318
      /* Assume that without an ALPN, we are talking to an ancient one */
319
0
      httpversion = 11;
320
0
    }
321
#ifdef USE_NGHTTP2
322
    else if(!strcmp(alpn, "h2")) {
323
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
324
      result = Curl_cf_h2_proxy_insert_after(cf, data);
325
      if(result)
326
        goto out;
327
      httpversion = 20;
328
    }
329
#endif
330
0
    else {
331
0
      failf(data, "CONNECT: negotiated ALPN '%s' not supported", alpn);
332
0
      result = CURLE_COULDNT_CONNECT;
333
0
      goto out;
334
0
    }
335
336
0
    ctx->sub_filter_installed = TRUE;
337
0
    ctx->httpversion = httpversion;
338
    /* after we installed the filter "below" us, we call connect
339
     * on out sub-chain again.
340
     */
341
0
    goto connect_sub;
342
0
  }
343
0
  else {
344
    /* subchain connected and we had already installed the protocol filter.
345
     * This means the protocol tunnel is established, we are done.
346
     */
347
0
    DEBUGASSERT(ctx->sub_filter_installed);
348
0
    result = CURLE_OK;
349
0
  }
350
351
0
out:
352
0
  if(!result) {
353
0
    cf->connected = TRUE;
354
0
    *done = TRUE;
355
0
  }
356
0
  return result;
357
0
}
358
359
CURLcode Curl_cf_http_proxy_query(struct Curl_cfilter *cf,
360
                                  struct Curl_easy *data,
361
                                  int query, int *pres1, void *pres2)
362
0
{
363
0
  switch(query) {
364
0
  case CF_QUERY_HOST_PORT:
365
0
    *pres1 = (int)cf->conn->http_proxy.port;
366
0
    *((const char **)pres2) = cf->conn->http_proxy.host.name;
367
0
    return CURLE_OK;
368
0
  case CF_QUERY_ALPN_NEGOTIATED: {
369
0
    const char **palpn = pres2;
370
0
    DEBUGASSERT(palpn);
371
0
    *palpn = NULL;
372
0
    return CURLE_OK;
373
0
  }
374
0
  default:
375
0
    break;
376
0
  }
377
0
  return cf->next ?
378
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
379
0
    CURLE_UNKNOWN_OPTION;
380
0
}
381
382
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
383
                                  struct Curl_easy *data)
384
0
{
385
0
  struct cf_proxy_ctx *ctx = cf->ctx;
386
387
0
  (void)data;
388
0
  CURL_TRC_CF(data, cf, "destroy");
389
0
  free(ctx);
390
0
}
391
392
static void http_proxy_cf_close(struct Curl_cfilter *cf,
393
                                struct Curl_easy *data)
394
0
{
395
0
  CURL_TRC_CF(data, cf, "close");
396
0
  cf->connected = FALSE;
397
0
  if(cf->next)
398
0
    cf->next->cft->do_close(cf->next, data);
399
0
}
400
401
402
struct Curl_cftype Curl_cft_http_proxy = {
403
  "HTTP-PROXY",
404
  CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
405
  0,
406
  http_proxy_cf_destroy,
407
  http_proxy_cf_connect,
408
  http_proxy_cf_close,
409
  Curl_cf_def_shutdown,
410
  Curl_cf_def_adjust_pollset,
411
  Curl_cf_def_data_pending,
412
  Curl_cf_def_send,
413
  Curl_cf_def_recv,
414
  Curl_cf_def_cntrl,
415
  Curl_cf_def_conn_is_alive,
416
  Curl_cf_def_conn_keep_alive,
417
  Curl_cf_http_proxy_query,
418
};
419
420
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
421
                                         struct Curl_easy *data)
422
0
{
423
0
  struct Curl_cfilter *cf;
424
0
  struct cf_proxy_ctx *ctx = NULL;
425
0
  CURLcode result;
426
427
0
  (void)data;
428
0
  ctx = calloc(1, sizeof(*ctx));
429
0
  if(!ctx) {
430
0
    result = CURLE_OUT_OF_MEMORY;
431
0
    goto out;
432
0
  }
433
0
  result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
434
0
  if(result)
435
0
    goto out;
436
0
  ctx = NULL;
437
0
  Curl_conn_cf_insert_after(cf_at, cf);
438
439
0
out:
440
0
  free(ctx);
441
0
  return result;
442
0
}
443
444
#endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */