Repository navigation
fix(security): validate Mailchimp data center to prevent authenticated SSRF #3193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4654cc1
fix: reject non-alphanumeric Mailchimp data centers to prevent SSRF
vytisbulkevicius e7737cd
fix: keep wp_remote_* for Mailchimp and disable redirects instead of …
vytisbulkevicius 8ed34f8
test: cover redirect guard on every Mailchimp request; de-duplicate c…
vytisbulkevicius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.' ); | ||
| } | ||
| } | ||
|
pirate-bot marked this conversation as resolved.
Resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.