Skip to content

Commit

Permalink
utils: fix parsing of urls with IPv6 literals
Browse files Browse the repository at this point in the history
This patch fixes the handling of IPv6 literals, which would fail to
parse after not finding balanced `[]` characters.

This happened because it searched for the first `:` before copying the
host portion, so (assuming it didn't return NULL), the endpoint
`[fd00:ec2::23]` would become `fd00`.

I've added some tests around this case, but I wouldn't describe the
tests as in-depth.

This patch is needed for EKS Pod Identities to work on IPv6 clusters.

Signed-off-by: Andrew Titmuss <iandrewt@icloud.com>
  • Loading branch information
iandrewt committed Jun 26, 2024
1 parent 2842b36 commit 1b809e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@ int flb_utils_url_split(const char *in_url, char **out_protocol,
char *p;
char *tmp;
char *sep;
char *v6_start;
char *v6_end;

/* Protocol */
p = strstr(in_url, "://");
Expand All @@ -1057,9 +1059,26 @@ int flb_utils_url_split(const char *in_url, char **out_protocol,

/* Check for first '/' */
sep = strchr(p, '/');
tmp = strchr(p, ':');
v6_start = strchr(p, '[');
v6_end = strchr(p, ']');

/*
* Validate port separator is found before the first slash,
* If IPv6, ensure it is after the ']',
* but only if before the first slash
*/
if (v6_start && v6_end) {
if (sep && v6_end > sep) {
tmp = strchr(p, ':');
}
else {
tmp = strchr(v6_end, ':');
}
}
else {
tmp = strchr(p, ':');
}

/* Validate port separator is found before the first slash */
if (sep && tmp) {
if (tmp > sep) {
tmp = NULL;
Expand Down
4 changes: 4 additions & 0 deletions tests/internal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct url_check url_checks[] = {
{0, "https://fluentbit.io:1234", "https", "fluentbit.io", "1234", "/"},
{0, "https://fluentbit.io:1234/", "https", "fluentbit.io", "1234", "/"},
{0, "https://fluentbit.io:1234/v", "https", "fluentbit.io", "1234", "/v"},
{0, "http://[fd00:ec2::23]", "http", "fd00:ec2::23", "80", "/"},
{0, "http://[fd00:ec2::23]:81", "http", "fd00:ec2::23", "81", "/"},
{0, "http://[fd00:ec2::23]:81/something", "http", "fd00:ec2::23", "81", "/something"},
{0, "http://[fd00:ec2::23]/something", "http", "fd00:ec2::23", "80", "/something"},
{-1, "://", NULL, NULL, NULL, NULL},
};

Expand Down

0 comments on commit 1b809e2

Please sign in to comment.