From acbfbf3de614f03ea5f9d3942558f1661fc202a4 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Mon, 29 Jul 2024 20:53:32 +0200 Subject: check_curl: fix relative redirects on non-standard port Having a webserver respond with a relative redirect as for ex. in `Location: /path/to.html` check_curl would use the wrong standard http/https port instead of crafting the absolute url using the given scheme/hostname and port. Adding a new test case for this for check_http and check_curl. check_http did it correct already, so no fix necessary there. before: %>./check_curl -H 127.0.0.1 -p 50493 -f follow -u /redirect_rel -s redirected -vvv **** HEADER **** HTTP/1.1 302 Found ... Location: /redirect2 ... * Seen redirect location /redirect2 ** scheme: (null) ** host: (null) ** port: (null) ** path: /redirect2 Redirection to http://127.0.0.1:80/redirect2 fixed: %>./check_curl -H 127.0.0.1 -p 50493 -f follow -u /redirect_rel -s redirected -vvv **** HEADER **** HTTP/1.1 302 Found ... Location: /redirect2 ... * Seen redirect location /redirect2 ** scheme: (null) ** host: (null) ** port: (null) ** path: /redirect2 Redirection to http://127.0.0.1:50493/redirect2 Signed-off-by: Sven Nierlein --- plugins/check_curl.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'plugins/check_curl.c') diff --git a/plugins/check_curl.c b/plugins/check_curl.c index c54be5e4..01e2770e 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -1285,10 +1285,12 @@ redir (curlhelp_write_curlbuf* header_buf) } } - if (!uri_strcmp (uri.scheme, "https")) - use_ssl = true; - else - use_ssl = false; + if (uri.scheme.first) { + if (!uri_strcmp (uri.scheme, "https")) + use_ssl = true; + else + use_ssl = false; + } /* we do a sloppy test here only, because uriparser would have failed * above, if the port would be invalid, we just check for MAX_PORT @@ -1306,10 +1308,13 @@ redir (curlhelp_write_curlbuf* header_buf) MAX_PORT, location, display_html ? "" : ""); /* by RFC 7231 relative URLs in Location should be taken relative to - * the original URL, so wy try to form a new absolute URL here + * the original URL, so we try to form a new absolute URL here */ if (!uri.scheme.first && !uri.hostText.first) { new_host = strdup (host_name ? host_name : server_address); + new_port = server_port; + if(use_ssl) + uri_string (uri.scheme, "https", DEFAULT_BUFFER_SIZE); } else { new_host = strdup (uri_string (uri.hostText, buf, DEFAULT_BUFFER_SIZE)); } -- cgit v1.2.3-74-g34f1