Skip to content

Commit 93cf0d0

Browse files
authored
registry: take netdata_registry_id from the Cookie header only (#24494)
1 parent 30375cf commit 93cf0d0

5 files changed

Lines changed: 115 additions & 6 deletions

File tree

‎src/daemon/main.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ int pagecache_flush_drop_unittest(void);
233233
int pluginsd_parser_unittest(void);
234234
int pluginsd_vnodes_unittest(void);
235235
int websocket_compression_unittest(void);
236+
int web_client_registry_cookie_unittest(void);
236237
int web_client_request_size_unittest(void);
237238
int rrdhost_machine_guid_unittest(void);
238239
void replication_initialize(void);
@@ -518,6 +519,7 @@ int netdata_main(int argc, char **argv) {
518519
if (threads_unittest()) return 1;
519520
if (pluginsd_parser_unittest()) return 1;
520521
if (websocket_compression_unittest()) return 1;
522+
if (web_client_registry_cookie_unittest()) return 1;
521523
if (web_client_request_size_unittest()) return 1;
522524
if (rrdhost_machine_guid_unittest()) return 1;
523525
if (stream_conf_unittest()) return 1;

‎src/web/api/http_header.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ static void http_header_x_auth_token(struct web_client *w, const char *v, size_t
126126
w->auth_bearer_token = strdupz(v);
127127
}
128128

129+
static void http_header_cookie(struct web_client *w, const char *v, size_t len __maybe_unused) {
130+
// the browser may send multiple cookies with our name - the first one wins
131+
if(w->auth.registry_person_guid[0])
132+
return;
133+
134+
const size_t name_len = sizeof(NETDATA_REGISTRY_COOKIE_NAME) - 1;
135+
136+
while(*v) {
137+
while(*v == ';' || *v == ' ') v++;
138+
139+
const char *pair = v;
140+
while(*v && *v != ';') v++;
141+
142+
if((size_t)(v - pair) > name_len && pair[name_len] == '=' &&
143+
strncmp(pair, NETDATA_REGISTRY_COOKIE_NAME, name_len) == 0) {
144+
const char *value = &pair[name_len + 1];
145+
size_t value_len = v - value;
146+
strncpyz(w->auth.registry_person_guid, value,
147+
MIN(value_len, sizeof(w->auth.registry_person_guid) - 1));
148+
return;
149+
}
150+
}
151+
}
152+
129153
static void http_header_host(struct web_client *w, const char *v, size_t len) {
130154
char buffer[NI_MAXHOST];
131155
strncpyz(buffer, v, (len < sizeof(buffer) - 1 ? len : sizeof(buffer) - 1));
@@ -369,6 +393,7 @@ struct {
369393
{ .hash = 0, .key = "User-Agent", .cb = http_header_user_agent},
370394
{ .hash = 0, .key = "Accept", .cb = http_header_accept },
371395
{ .hash = 0, .key = "X-Auth-Token", .cb = http_header_x_auth_token },
396+
{ .hash = 0, .key = "Cookie", .cb = http_header_cookie },
372397
{ .hash = 0, .key = "Host", .cb = http_header_host },
373398
{ .hash = 0, .key = "Accept-Encoding", .cb = http_header_accept_encoding },
374399
{ .hash = 0, .key = "X-Forwarded-Host", .cb = http_header_x_forwarded_host },

‎src/web/api/v1/api_v1_registry.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ int api_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
4141

4242
netdata_log_debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
4343

44-
// TODO
45-
// The browser may send multiple cookies with our id
46-
4744
char person_guid[UUID_STR_LEN] = "";
48-
char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
49-
if(cookie)
50-
strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], UUID_STR_LEN - 1);
45+
if(w->auth.registry_person_guid[0])
46+
strncpyz(person_guid, w->auth.registry_person_guid, UUID_STR_LEN - 1);
5147
else if(!extract_bearer_token_from_request(w, person_guid, sizeof(person_guid)))
5248
person_guid[0] = '\0';
5349

‎src/web/server/web_client-unittest.c‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,91 @@ web_client_test_payload_request(const char *method, size_t payload_length, size_
7676
return request;
7777
}
7878

79+
int web_client_registry_cookie_unittest(void)
80+
{
81+
fprintf(stderr, "\n%s() running...\n", __FUNCTION__);
82+
83+
#define REGISTRY_COOKIE_TEST_OURS "b5dd255b-a36c-44ad-a598-115c1e06b964"
84+
#define REGISTRY_COOKIE_TEST_OTHER "38992aab-c97a-490c-b9f7-660b0d78965b"
85+
86+
static const struct {
87+
const char *request;
88+
const char *expected;
89+
} cases[] = {
90+
{
91+
"GET /api/v1/registry?action=hello HTTP/1.1\r\n"
92+
"Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "\r\n\r\n",
93+
REGISTRY_COOKIE_TEST_OURS,
94+
},
95+
{
96+
"GET /api/v1/registry?action=access&x=netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER " HTTP/1.1\r\n"
97+
"Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "\r\n\r\n",
98+
REGISTRY_COOKIE_TEST_OURS,
99+
},
100+
{
101+
"GET /api/v1/registry?action=access&x=netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER " HTTP/1.1\r\n\r\n",
102+
"",
103+
},
104+
{
105+
"GET /api/v1/registry?action=access HTTP/1.1\r\n"
106+
"Referer: http://example.com/?netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER "\r\n\r\n",
107+
"",
108+
},
109+
{
110+
"GET /api/v1/registry?action=access HTTP/1.1\r\n"
111+
"Cookie: a=1; netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "; b=2\r\n\r\n",
112+
REGISTRY_COOKIE_TEST_OURS,
113+
},
114+
{
115+
"GET /api/v1/registry?action=access HTTP/1.1\r\n"
116+
"Cookie: other_netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER "\r\n\r\n",
117+
"",
118+
},
119+
{
120+
"GET /api/v1/registry?action=access HTTP/1.1\r\n"
121+
"Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "; netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER "\r\n\r\n",
122+
REGISTRY_COOKIE_TEST_OURS,
123+
},
124+
{
125+
"GET /api/v1/registry?action=access HTTP/1.1\r\n"
126+
"Cookie: a=1\r\n"
127+
"Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "\r\n\r\n",
128+
REGISTRY_COOKIE_TEST_OURS,
129+
},
130+
};
131+
132+
int errors = 0;
133+
size_t memory_accounting = 0;
134+
struct web_client *w = web_client_create(&memory_accounting);
135+
136+
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
137+
web_client_reuse_from_cache(w);
138+
buffer_flush(w->response.data);
139+
buffer_strcat(w->response.data, cases[i].request);
140+
141+
WEB_CLIENT_TEST(http_request_validate(w) == HTTP_VALIDATION_OK, "case %zu: request did not validate", i);
142+
WEB_CLIENT_TEST(
143+
strcmp(w->auth.registry_person_guid, cases[i].expected) == 0,
144+
"case %zu: registry cookie is '%s', expected '%s'",
145+
i,
146+
w->auth.registry_person_guid,
147+
cases[i].expected);
148+
}
149+
150+
web_client_free(w);
151+
WEB_CLIENT_TEST(memory_accounting == 0, "web client leaked %zu accounted bytes", memory_accounting);
152+
153+
#undef REGISTRY_COOKIE_TEST_OURS
154+
#undef REGISTRY_COOKIE_TEST_OTHER
155+
156+
if (errors)
157+
fprintf(stderr, "%s() failed with %d error(s)\n", __FUNCTION__, errors);
158+
else
159+
fprintf(stderr, "%s() passed\n", __FUNCTION__);
160+
161+
return errors;
162+
}
163+
79164
int web_client_request_size_unittest(void)
80165
{
81166
fprintf(stderr, "\n%s() running...\n", __FUNCTION__);

‎src/web/server/web_client.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ struct web_client {
241241

242242
struct {
243243
nd_uuid_t bearer_token;
244+
char registry_person_guid[UUID_STR_LEN]; // the netdata_registry_id cookie (if sent)
244245
} auth;
245246

246247
struct { // A callback to check if the query should be interrupted / stopped

0 commit comments

Comments
 (0)