Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions inc/integrations/providers/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ),
),
);
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 `<letters/digits>` 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' ),
Expand Down Expand Up @@ -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 ),
),
);
Expand Down Expand Up @@ -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 ),
),
);
Expand Down
113 changes: 113 additions & 0 deletions tests/test-mailchimp-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Test Mailchimp provider API key validation.
*
* @package otter-blocks
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/

use ThemeIsle\GutenbergBlocks\Integration\Mailchimp_Integration;
use ThemeIsle\GutenbergBlocks\Integration\Form_Data_Response;

/**
* Class Test_Mailchimp_Provider.
*/
class Test_Mailchimp_Provider extends WP_UnitTestCase {

/**
* The data center is taken from the key and becomes the request host, so an injected host,
* port or path must be rejected before any request is built (SSRF, otter-internals#318).
*/
public function test_rejects_a_data_center_that_is_not_bare_alphanumeric() {
$payloads = array(
'x-attacker:8443/r#', // the reported SSRF payload.
'key-attacker.example.com',
'key-127.0.0.1',
'key-us1/../x',
'key-US1', // real data centers are lowercase.
'key-us 1',
);

foreach ( $payloads 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'], "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.' );
}
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}
Loading