]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
http_client.c: fix redirection in OSSL_HTTP_REQ_CTX_nbio() when non-ASN.1 content...
authorDr. David von Oheimb <dev@ddvo.net>
Wed, 15 Jan 2025 17:24:09 +0000 (18:24 +0100)
committerDr. David von Oheimb <dev@ddvo.net>
Tue, 11 Feb 2025 21:10:43 +0000 (22:10 +0100)
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/25541)

crypto/http/http_client.c
test/http_test.c

index c8829d708f89fbfce1c93fd53eb82b6fd866661a..e23160fe3f6cd6b0daa6a92b3d396eaa6e93b4b1 100644 (file)
@@ -788,6 +788,8 @@ int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
                 rctx->redirection_url = value;
                 if (OSSL_TRACE_ENABLED(HTTP))
                     OSSL_TRACE(HTTP, "]\n");
+                /* stop reading due to redirect */
+                (void)BIO_reset(rctx->rbio);
                 return 0;
             }
             if (OPENSSL_strcasecmp(key, "Content-Type") == 0) {
@@ -1205,7 +1207,7 @@ BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url)
                         && reason == CMP_R_POTENTIALLY_INVALID_CERTIFICATE)
 #endif
                 ) {
-                if (rctx->server != NULL) {
+                if (rctx->server != NULL && *rctx->server != '\0') {
                     BIO_snprintf(buf, sizeof(buf), "server=http%s://%s%s%s",
                                  rctx->use_ssl ? "s" : "", rctx->server,
                                  rctx->port != NULL ? ":" : "",
index 8be07593f324b7070050a84bec45a486cba0e968..eca2a9292214441c800106f19a407675c022ece5 100644 (file)
@@ -121,17 +121,21 @@ static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
 #define REAL_SERVER_URL "http://httpbin.org/"
 #define DOCTYPE_HTML "<!DOCTYPE html>\n"
 
+/* do_get > 1 used for testing redirection */
 static int test_http_method(int do_get, int do_txt)
 {
     BIO *wbio = BIO_new(BIO_s_mem());
     BIO *rbio = BIO_new(BIO_s_mem());
     server_args mock_args = { NULL, NULL, NULL, '0', 0 };
     BIO *req, *rsp;
+    char path[80];
     STACK_OF(CONF_VALUE) *headers = NULL;
     const char *content_type;
     int res = 0;
     int real_server = do_txt && 0; /* remove "&& 0" for using real server */
 
+    snprintf(path, sizeof(path), "%s",
+             do_get > 1 ? "/will-be-redirected" : RPATH);
     if (do_txt) {
         content_type = "text/plain";
         req = BIO_new(BIO_s_mem());
@@ -156,8 +160,7 @@ static int test_http_method(int do_get, int do_txt)
     BIO_set_callback_arg(wbio, (char *)&mock_args);
 
     rsp = do_get ?
-        OSSL_HTTP_get(real_server ? REAL_SERVER_URL :
-                      do_txt ? RPATH : "/will-be-redirected",
+        OSSL_HTTP_get(real_server ? REAL_SERVER_URL : path,
                       NULL /* proxy */, NULL /* no_proxy */,
                       real_server ? NULL : wbio,
                       real_server ? NULL : rbio,
@@ -166,8 +169,8 @@ static int test_http_method(int do_get, int do_txt)
                       real_server ? "text/html; charset=utf-8":  content_type,
                       !do_txt /* expect_asn1 */,
                       OSSL_HTTP_DEFAULT_MAX_RESP_LEN, 0 /* timeout */)
-        : OSSL_HTTP_transfer(NULL, NULL /* host */, NULL /* port */, RPATH,
-                             0 /* use_ssl */,NULL /* proxy */, NULL /* no_pr */,
+        : OSSL_HTTP_transfer(NULL, NULL /* host */, NULL /* port */, path,
+                             0 /* use_ssl */, NULL /* proxy */, NULL /* no_pr */,
                              wbio, rbio, NULL /* bio_fn */, NULL /* arg */,
                              0 /* buf_size */, headers, content_type,
                              req, content_type, !do_txt /* expect_asn1 */,
@@ -362,6 +365,11 @@ static int test_http_get_txt(void)
     return test_http_method(1 /* GET */, 1);
 }
 
+static int test_http_get_txt_redirected(void)
+{
+    return test_http_method(2 /* GET with redirection */, 1);
+}
+
 static int test_http_post_txt(void)
 {
     return test_http_method(0 /* POST */, 1);
@@ -369,7 +377,12 @@ static int test_http_post_txt(void)
 
 static int test_http_get_x509(void)
 {
-    return test_http_method(1 /* GET */, 0); /* includes redirection */
+    return test_http_method(1 /* GET */, 0);
+}
+
+static int test_http_get_x509_redirected(void)
+{
+    return test_http_method(2 /* GET with redirection */, 0);
 }
 
 static int test_http_post_x509(void)
@@ -506,8 +519,10 @@ int setup_tests(void)
     ADD_TEST(test_http_url_invalid_path);
 
     ADD_TEST(test_http_get_txt);
+    ADD_TEST(test_http_get_txt_redirected);
     ADD_TEST(test_http_post_txt);
     ADD_TEST(test_http_get_x509);
+    ADD_TEST(test_http_get_x509_redirected);
     ADD_TEST(test_http_post_x509);
     ADD_TEST(test_http_keep_alive_0_no_no);
     ADD_TEST(test_http_keep_alive_1_no_no);