From 4654cc1467ecdfc49290fe24a94b84abaef21580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Fri, 20 Nov 2026 18:01:49 +0200 Subject: [PATCH 1/3] fix: reject non-alphanumeric Mailchimp data centers to prevent SSRF The Mailchimp data center is taken from the part of the API key after the dash and used as the request host (`https://.api.mailchimp.com/...`). validate_api_key() only checked for a single dash, so a Contributor could submit a key such as `x-attacker:8443/r#` through the form editor endpoint and make the server request an arbitrary host, including internal services. - Require the data center to be a bare `[a-z0-9]+` token, so it can only resolve to `*.api.mailchimp.com`. server_name is set only through set_api_key(), which runs this validation first. - Defense in depth: send every Mailchimp request with wp_safe_remote_get()/ wp_safe_remote_post(), so a redirect to a private or loopback address is refused even if a host ever reached the request builder. Reported-by: Deus Rex research team --- .../providers/class-mailchimp.php | 12 ++-- tests/test-mailchimp-provider.php | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tests/test-mailchimp-provider.php diff --git a/inc/integrations/providers/class-mailchimp.php b/inc/integrations/providers/class-mailchimp.php index 0917e050e..c54134a8f 100644 --- a/inc/integrations/providers/class-mailchimp.php +++ b/inc/integrations/providers/class-mailchimp.php @@ -88,7 +88,7 @@ public function get_lists() { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args ); } else { - $response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $response = wp_safe_remote_get( $url, $args ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); @@ -143,7 +143,7 @@ public function make_subscribe_request( $email ) { 'body' => wp_json_encode( $payload ), ); - return wp_remote_post( $url, $args ); + return wp_safe_remote_post( $url, $args ); } /** @@ -226,7 +226,9 @@ public static function validate_api_key( $api_key ) { } $key_info = explode( '-', $api_key ); - if ( 2 !== count( $key_info ) || ! isset( $key_info[1] ) ) { + + // The data center becomes the request host, so it must be a bare `` token (e.g. `us1`), never a host, port or path. + if ( 2 !== count( $key_info ) || ! isset( $key_info[1] ) || ! preg_match( '/^[a-z0-9]+$/', $key_info[1] ) ) { return array( 'valid' => false, 'reason' => __( 'Invalid API Key format!', 'otter-blocks' ), @@ -260,7 +262,7 @@ private function get_new_user_status_mailchimp( $list_id ) { ), ); - $response = wp_remote_post( $url, $args ); + $response = wp_safe_remote_post( $url, $args ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { @@ -320,7 +322,7 @@ public function get_merge_fields() { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args ); } else { - $response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $response = wp_safe_remote_get( $url, $args ); } if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { diff --git a/tests/test-mailchimp-provider.php b/tests/test-mailchimp-provider.php new file mode 100644 index 000000000..f8f9d4539 --- /dev/null +++ b/tests/test-mailchimp-provider.php @@ -0,0 +1,69 @@ +assertFalse( $result['valid'], "Expected '$api_key' to be rejected." ); + $this->assertSame( Form_Data_Response::ERROR_PROVIDER_INVALID_API_KEY_FORMAT, $result['code'], "Expected '$api_key' to fail the format check." ); + } + } + + /** + * A real key (secret dash data center) still validates, so the guard does not break normal use. + */ + public function test_accepts_a_real_mailchimp_key() { + foreach ( array( 'abc123def456-us1', 'abc123def456-us21', '0a1b2c3d4e5f6-gb7' ) as $api_key ) { + $this->assertTrue( Mailchimp_Integration::validate_api_key( $api_key )['valid'], "Expected '$api_key' to validate." ); + } + } + + /** + * A key without exactly one dash keeps failing the format check, as before. + */ + public function test_rejects_a_key_without_a_single_data_center_segment() { + foreach ( array( 'nodash', 'two-dashes-here' ) as $api_key ) { + $result = Mailchimp_Integration::validate_api_key( $api_key ); + + $this->assertFalse( $result['valid'], "Expected '$api_key' to be rejected." ); + $this->assertSame( Form_Data_Response::ERROR_PROVIDER_INVALID_API_KEY_FORMAT, $result['code'] ); + } + } + + /** + * An empty key reports the missing-key reason, not the format one. + */ + public function test_rejects_an_empty_key_as_missing() { + $result = Mailchimp_Integration::validate_api_key( '' ); + + $this->assertFalse( $result['valid'] ); + $this->assertSame( Form_Data_Response::ERROR_PROVIDER_INVALID_KEY, $result['code'] ); + } +} From e7737cd58336eef321169d90e4f09acbaf561db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Fri, 20 Nov 2026 18:50:03 +0200 Subject: [PATCH 2/3] fix: keep wp_remote_* for Mailchimp and disable redirects instead of wp_safe_* wp_safe_remote_get()/wp_safe_remote_post() run wp_http_validate_url() before the HTTP proxy is applied, so on a site that reaches the internet only through WP_PROXY_HOST they would reject the Mailchimp host that local DNS cannot resolve, breaking lists, merge fields and subscriptions. The data-center validation already pins every request host to *.api.mailchimp.com, so the redirect concern is covered by passing 'redirection' => 0 on the normal wp_remote_* calls, with no DNS/proxy regression. - Revert the four Mailchimp requests to wp_remote_get()/wp_remote_post(). - Add 'redirection' => 0 to each request, so a response cannot pivot the request to another address. - Add a test that captures the get_lists() request and asserts the host is the validated data center and redirection is 0. --- .../providers/class-mailchimp.php | 30 +++++++++++-------- tests/test-mailchimp-provider.php | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/inc/integrations/providers/class-mailchimp.php b/inc/integrations/providers/class-mailchimp.php index c54134a8f..b839a68b3 100644 --- a/inc/integrations/providers/class-mailchimp.php +++ b/inc/integrations/providers/class-mailchimp.php @@ -77,8 +77,9 @@ public function get_lists() { $url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists?count=1000'; $args = array( - 'method' => 'GET', - 'headers' => array( + 'method' => 'GET', + 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), ); @@ -88,7 +89,7 @@ public function get_lists() { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args ); } else { - $response = wp_safe_remote_get( $url, $args ); + $response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get } $body = json_decode( wp_remote_retrieve_body( $response ), true ); @@ -136,14 +137,15 @@ public function make_subscribe_request( $email ) { } $args = array( - 'method' => 'PUT', - 'headers' => array( + 'method' => 'PUT', + 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), - 'body' => wp_json_encode( $payload ), + 'body' => wp_json_encode( $payload ), ); - return wp_safe_remote_post( $url, $args ); + return wp_remote_post( $url, $args ); } /** @@ -256,13 +258,14 @@ public static function validate_api_key( $api_key ) { private function get_new_user_status_mailchimp( $list_id ) { $url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists/' . $list_id; $args = array( - 'method' => 'GET', - 'headers' => array( + 'method' => 'GET', + 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), ); - $response = wp_safe_remote_post( $url, $args ); + $response = wp_remote_post( $url, $args ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { @@ -313,8 +316,9 @@ private function is_credential_error( $response_code ) { public function get_merge_fields() { $url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists/' . $this->list_id . '/merge-fields'; $args = array( - 'method' => 'GET', - 'headers' => array( + 'method' => 'GET', + 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), ); @@ -322,7 +326,7 @@ public function get_merge_fields() { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args ); } else { - $response = wp_safe_remote_get( $url, $args ); + $response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get } if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { diff --git a/tests/test-mailchimp-provider.php b/tests/test-mailchimp-provider.php index f8f9d4539..dc3189526 100644 --- a/tests/test-mailchimp-provider.php +++ b/tests/test-mailchimp-provider.php @@ -66,4 +66,34 @@ public function test_rejects_an_empty_key_as_missing() { $this->assertFalse( $result['valid'] ); $this->assertSame( Form_Data_Response::ERROR_PROVIDER_INVALID_KEY, $result['code'] ); } + + /** + * Mailchimp requests go only to the validated data center host and do not follow + * redirects, so a response cannot pivot the request to another address (otter-internals#318). + */ + public function test_request_pins_host_and_disables_redirects() { + $integration = new Mailchimp_Integration(); + $integration->set_api_key( 'abc123def456-us1' ); + + $captured = array(); + $filter = static function ( $pre, $args, $url ) use ( &$captured ) { + $captured[] = array( 'url' => $url, 'args' => $args ); + return array( + 'headers' => array(), + 'body' => wp_json_encode( array( 'lists' => array() ) ), + 'response' => array( 'code' => 200, 'message' => 'OK' ), + 'cookies' => array(), + ); + }; + + add_filter( 'pre_http_request', $filter, 10, 3 ); + $integration->get_lists(); + remove_filter( 'pre_http_request', $filter, 10 ); + + $this->assertNotEmpty( $captured, 'Expected get_lists() to send a request.' ); + foreach ( $captured as $request ) { + $this->assertStringStartsWith( 'https://us1.api.mailchimp.com/', $request['url'] ); + $this->assertSame( 0, $request['args']['redirection'] ); + } + } } From 8ed34f8417a5903fb77224df7f09bcb6dc6acb04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Fri, 20 Nov 2026 19:54:59 +0200 Subject: [PATCH 3/3] test: cover redirect guard on every Mailchimp request; de-duplicate comment - Keep the redirect rationale on one line only, not repeated on all four. - Extend the request test to also drive get_merge_fields() and the subscribe path (status GET + members PUT), asserting the pinned host and redirection => 0 on every captured request, so dropping the guard from any single request builder fails the test. --- .../providers/class-mailchimp.php | 6 ++--- tests/test-mailchimp-provider.php | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/inc/integrations/providers/class-mailchimp.php b/inc/integrations/providers/class-mailchimp.php index b839a68b3..61afdc6e7 100644 --- a/inc/integrations/providers/class-mailchimp.php +++ b/inc/integrations/providers/class-mailchimp.php @@ -138,7 +138,7 @@ public function make_subscribe_request( $email ) { $args = array( 'method' => 'PUT', - 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), @@ -259,7 +259,7 @@ private function get_new_user_status_mailchimp( $list_id ) { $url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists/' . $list_id; $args = array( 'method' => 'GET', - 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), @@ -317,7 +317,7 @@ public function get_merge_fields() { $url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists/' . $this->list_id . '/merge-fields'; $args = array( 'method' => 'GET', - 'redirection' => 0, // Mailchimp returns data directly; never follow a redirect (defense in depth). + 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), diff --git a/tests/test-mailchimp-provider.php b/tests/test-mailchimp-provider.php index dc3189526..67f0ae124 100644 --- a/tests/test-mailchimp-provider.php +++ b/tests/test-mailchimp-provider.php @@ -74,26 +74,40 @@ public function test_rejects_an_empty_key_as_missing() { public function test_request_pins_host_and_disables_redirects() { $integration = new Mailchimp_Integration(); $integration->set_api_key( 'abc123def456-us1' ); + $integration->set_list_id( 'listid' ); + + // make_subscribe_request() reads the linked merge fields off form_data. + $form_data = new class() { + public function get_fields() { + return array(); + } + }; + $property = new ReflectionProperty( Mailchimp_Integration::class, 'form_data' ); + $property->setAccessible( true ); + $property->setValue( $integration, $form_data ); $captured = array(); $filter = static function ( $pre, $args, $url ) use ( &$captured ) { $captured[] = array( 'url' => $url, 'args' => $args ); return array( 'headers' => array(), - 'body' => wp_json_encode( array( 'lists' => array() ) ), + 'body' => wp_json_encode( array( 'lists' => array(), 'merge_fields' => array() ) ), 'response' => array( 'code' => 200, 'message' => 'OK' ), 'cookies' => array(), ); }; add_filter( 'pre_http_request', $filter, 10, 3 ); - $integration->get_lists(); + $integration->get_lists(); // GET /lists + $integration->get_merge_fields(); // GET /lists/{id}/merge-fields + $integration->make_subscribe_request( 'qa@example.com' ); // status GET + members PUT remove_filter( 'pre_http_request', $filter, 10 ); - $this->assertNotEmpty( $captured, 'Expected get_lists() to send a request.' ); + // get_lists, get_merge_fields, the subscribe status GET and the members PUT. + $this->assertGreaterThanOrEqual( 4, count( $captured ), 'Expected every Mailchimp request path to send a request.' ); foreach ( $captured as $request ) { $this->assertStringStartsWith( 'https://us1.api.mailchimp.com/', $request['url'] ); - $this->assertSame( 0, $request['args']['redirection'] ); + $this->assertSame( 0, $request['args']['redirection'], 'Every Mailchimp request must disable redirects.' ); } } }