diff --git a/inc/integrations/providers/class-mailchimp.php b/inc/integrations/providers/class-mailchimp.php index 0917e050e..61afdc6e7 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 ), ), ); @@ -136,11 +137,12 @@ public function make_subscribe_request( $email ) { } $args = array( - 'method' => 'PUT', - 'headers' => array( + 'method' => 'PUT', + 'redirection' => 0, + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), - 'body' => wp_json_encode( $payload ), + 'body' => wp_json_encode( $payload ), ); return wp_remote_post( $url, $args ); @@ -226,7 +228,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' ), @@ -254,8 +258,9 @@ 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, + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), ); @@ -311,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, + 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), ), ); diff --git a/tests/test-mailchimp-provider.php b/tests/test-mailchimp-provider.php new file mode 100644 index 000000000..67f0ae124 --- /dev/null +++ b/tests/test-mailchimp-provider.php @@ -0,0 +1,113 @@ +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'] ); + } + + /** + * 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' ); + $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(), 'merge_fields' => array() ) ), + 'response' => array( 'code' => 200, 'message' => 'OK' ), + 'cookies' => array(), + ); + }; + + add_filter( 'pre_http_request', $filter, 10, 3 ); + $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 ); + + // 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'], 'Every Mailchimp request must disable redirects.' ); + } + } +}