From 1b809e25b0f32e9a8a4d0f11983f9989b6dd9088 Mon Sep 17 00:00:00 2001 From: Andrew Titmuss Date: Wed, 26 Jun 2024 22:10:40 +1000 Subject: [PATCH] utils: fix parsing of urls with IPv6 literals 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 --- src/flb_utils.c | 23 +++++++++++++++++++++-- tests/internal/utils.c | 4 ++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index 2f445980d84..6cc341160be 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -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, "://"); @@ -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; diff --git a/tests/internal/utils.c b/tests/internal/utils.c index bcb9053d86f..73591f634ef 100644 --- a/tests/internal/utils.c +++ b/tests/internal/utils.c @@ -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}, };