Skip to content

Commit 3a19987

Browse files
committed
urldata: move cookiehost to struct SingleRequest
To make it scoped for the single request appropriately. Reported-by: Muhamad Arga Reksapati Verify with libtest 2504: a custom Host *disabled* on reused handle Closes #21312
1 parent 86f1e5b commit 3a19987

9 files changed

Lines changed: 162 additions & 12 deletions

File tree

lib/http.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,9 @@ static CURLcode http_set_aptr_host(struct Curl_easy *data)
20462046
data->state.first_remote_protocol = conn->scheme->protocol;
20472047
}
20482048
curlx_safefree(aptr->host);
2049+
#ifndef CURL_DISABLE_COOKIES
2050+
curlx_safefree(data->req.cookiehost);
2051+
#endif
20492052

20502053
ptr = Curl_checkheaders(data, STRCONST("Host"));
20512054
if(ptr && (!data->state.this_is_a_follow ||
@@ -2081,8 +2084,7 @@ static CURLcode http_set_aptr_host(struct Curl_easy *data)
20812084
if(colon)
20822085
*colon = 0; /* The host must not include an embedded port number */
20832086
}
2084-
curlx_free(aptr->cookiehost);
2085-
aptr->cookiehost = cookiehost;
2087+
data->req.cookiehost = cookiehost;
20862088
}
20872089
#endif
20882090

@@ -2577,8 +2579,8 @@ static CURLcode http_cookies(struct Curl_easy *data,
25772579

25782580
if(data->cookies && data->state.cookie_engine) {
25792581
bool okay;
2580-
const char *host = data->state.aptr.cookiehost ?
2581-
data->state.aptr.cookiehost : data->conn->host.name;
2582+
const char *host = data->req.cookiehost ?
2583+
data->req.cookiehost : data->conn->host.name;
25822584
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
25832585
result = Curl_cookie_getlist(data, data->conn, &okay, host, &list);
25842586
if(!result && okay) {
@@ -3578,8 +3580,8 @@ static CURLcode http_header_s(struct Curl_easy *data,
35783580
if(v) {
35793581
/* If there is a custom-set Host: name, use it here, or else use
35803582
* real peer hostname. */
3581-
const char *host = data->state.aptr.cookiehost ?
3582-
data->state.aptr.cookiehost : conn->host.name;
3583+
const char *host = data->req.cookiehost ?
3584+
data->req.cookiehost : conn->host.name;
35833585
const bool secure_context = Curl_secure_context(conn, host);
35843586
CURLcode result;
35853587
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);

lib/request.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
118118
curlx_safefree(req->userpwd);
119119
#ifndef CURL_DISABLE_PROXY
120120
curlx_safefree(req->proxyuserpwd);
121+
#endif
122+
#ifndef CURL_DISABLE_COOKIES
123+
curlx_safefree(req->cookiehost);
121124
#endif
122125
Curl_client_reset(data);
123126
if(req->sendbuf_init)

lib/request.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ struct SingleRequest {
118118
#ifndef CURL_DISABLE_PROXY
119119
char *proxyuserpwd; /* proxy auth header */
120120
#endif
121+
#ifndef CURL_DISABLE_COOKIES
122+
char *cookiehost;
123+
#endif
121124
#ifndef CURL_DISABLE_COOKIES
122125
unsigned char setcookies;
123126
#endif

lib/url.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ CURLcode Curl_close(struct Curl_easy **datap)
290290
curlx_safefree(data->state.aptr.ref);
291291
curlx_safefree(data->state.aptr.host);
292292
#ifndef CURL_DISABLE_COOKIES
293-
curlx_safefree(data->state.aptr.cookiehost);
293+
curlx_safefree(data->req.cookiehost);
294294
#endif
295295
#ifndef CURL_DISABLE_RTSP
296296
curlx_safefree(data->state.aptr.rtsp_transport);

lib/urldata.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,6 @@ struct UrlState {
804804
char *rangeline;
805805
char *ref;
806806
char *host;
807-
#ifndef CURL_DISABLE_COOKIES
808-
char *cookiehost;
809-
#endif
810807
#ifndef CURL_DISABLE_RTSP
811808
char *rtsp_transport;
812809
#endif

tests/data/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ test2309 \
265265
\
266266
test2400 test2401 test2402 test2403 test2404 test2405 test2406 test2407 \
267267
\
268-
test2500 test2501 test2502 test2503 \
268+
test2500 test2501 test2502 test2503 test2504 \
269269
\
270270
test2600 test2601 test2602 test2603 test2604 test2605 \
271271
\

tests/data/test2504

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="US-ASCII"?>
2+
<testcase>
3+
<info>
4+
<keywords>
5+
HTTP
6+
cookies
7+
</keywords>
8+
</info>
9+
10+
# Server-side
11+
<reply>
12+
<data crlf="headers" nocheck="yes">
13+
HTTP/1.1 200 OK
14+
Date: Tue, 09 Nov 2010 14:49:00 GMT
15+
Server: server.example.com
16+
Content-Length: 47
17+
Set-Cookie: sid=SECRET123; Path=/
18+
19+
file contents should appear once for each file
20+
</data>
21+
</reply>
22+
23+
# Client-side
24+
<client>
25+
<server>
26+
http
27+
</server>
28+
<tool>
29+
lib%TESTNUMBER
30+
</tool>
31+
<name>
32+
custom Host with cookie, handle reuse, no custom Host:
33+
</name>
34+
<command>
35+
http://%HOSTIP:%HTTPPORT
36+
</command>
37+
</client>
38+
39+
# Verify data after the test has been "shot"
40+
<verify>
41+
<protocol crlf="headers">
42+
GET / HTTP/1.1
43+
Host: victim.internal
44+
Accept: */*
45+
46+
GET / HTTP/1.1
47+
Host: %HOSTIP:%HTTPPORT
48+
Accept: */*
49+
50+
</protocol>
51+
</verify>
52+
</testcase>

tests/libtest/Makefile.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ TESTS_C = \
112112
lib2023.c lib2032.c lib2082.c \
113113
lib2301.c lib2302.c lib2304.c lib2306.c lib2308.c lib2309.c \
114114
lib2402.c lib2404.c lib2405.c \
115-
lib2502.c \
115+
lib2502.c lib2504.c \
116116
lib2700.c \
117117
lib3010.c lib3025.c lib3026.c lib3027.c lib3033.c lib3034.c \
118118
lib3100.c lib3101.c lib3102.c lib3103.c lib3104.c lib3105.c \

tests/libtest/lib2504.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/***************************************************************************
2+
* _ _ ____ _
3+
* Project ___| | | | _ \| |
4+
* / __| | | | |_) | |
5+
* | (__| |_| | _ <| |___
6+
* \___|\___/|_| \_\_____|
7+
*
8+
* Copyright (C) Linus Nielsen Feltzing <linus@haxx.se>
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+
#include "first.h"
25+
26+
#include "testtrace.h"
27+
28+
static size_t sink2504(char *ptr, size_t size, size_t nmemb, void *ud)
29+
{
30+
(void)ptr;
31+
(void)ud;
32+
return size * nmemb;
33+
}
34+
35+
static void dump_cookies2504(CURL *h, const char *tag)
36+
{
37+
struct curl_slist *cookies = NULL;
38+
struct curl_slist *nc;
39+
CURLcode rc = curl_easy_getinfo(h, CURLINFO_COOKIELIST, &cookies);
40+
41+
curl_mprintf("== %s ==\n", tag);
42+
if(rc) {
43+
curl_mprintf("getinfo error: %d\n", (int)rc);
44+
return;
45+
}
46+
for(nc = cookies; nc; nc = nc->next)
47+
puts(nc->data);
48+
curl_slist_free_all(cookies);
49+
}
50+
51+
static CURLcode test_lib2504(const char *URL)
52+
{
53+
CURL *curl;
54+
CURLcode result = CURLE_OUT_OF_MEMORY;
55+
struct curl_slist *hdrs = NULL;
56+
57+
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
58+
curl_mfprintf(stderr, "curl_global_init() failed\n");
59+
return TEST_ERR_MAJOR_BAD;
60+
}
61+
62+
curl = curl_easy_init();
63+
if(!curl) {
64+
curl_mfprintf(stderr, "curl_easy_init() failed\n");
65+
curl_global_cleanup();
66+
return TEST_ERR_MAJOR_BAD;
67+
}
68+
69+
hdrs = curl_slist_append(hdrs, "Host: victim.internal");
70+
if(hdrs) {
71+
test_setopt(curl, CURLOPT_WRITEFUNCTION, sink2504);
72+
test_setopt(curl, CURLOPT_COOKIEFILE, "");
73+
test_setopt(curl, CURLOPT_HTTPHEADER, hdrs);
74+
test_setopt(curl, CURLOPT_URL, URL);
75+
76+
result = curl_easy_perform(curl);
77+
curl_mprintf("req1=%d\n", (int)result);
78+
dump_cookies2504(curl, "after request 1");
79+
80+
test_setopt(curl, CURLOPT_HTTPHEADER, NULL);
81+
test_setopt(curl, CURLOPT_URL, URL);
82+
83+
result = curl_easy_perform(curl);
84+
curl_mprintf("req2=%d\n", (int)result);
85+
dump_cookies2504(curl, "after request 2");
86+
}
87+
test_cleanup:
88+
curl_slist_free_all(hdrs);
89+
curl_easy_cleanup(curl);
90+
curl_global_cleanup();
91+
92+
return result;
93+
}

0 commit comments

Comments
 (0)