|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test Jetpack Application Password Android CORS functionality. |
| 4 | + * |
| 5 | + * @package jetpack |
| 6 | + */ |
| 7 | + |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | + |
| 10 | +require_once JETPACK__PLUGIN_DIR . '/tests/php/lib/Jetpack_REST_TestCase.php'; |
| 11 | +require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-application-password-extras.php'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test class for Android CORS headers. |
| 15 | + * |
| 16 | + * @covers \Jetpack_Application_Password_Extras |
| 17 | + */ |
| 18 | +#[CoversClass( Jetpack_Application_Password_Extras::class )] |
| 19 | +class Jetpack_Application_Password_Android_CORS_Test extends Jetpack_REST_TestCase { |
| 20 | + |
| 21 | + /** |
| 22 | + * Mock user ID. |
| 23 | + * |
| 24 | + * @var int |
| 25 | + */ |
| 26 | + private static $user_id = 0; |
| 27 | + |
| 28 | + /** |
| 29 | + * Original server globals backup. |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $server_backup = array(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Create shared database fixtures. |
| 37 | + * |
| 38 | + * @param WP_UnitTest_Factory $factory Fixture factory. |
| 39 | + */ |
| 40 | + public static function wpSetUpBeforeClass( $factory ) { |
| 41 | + static::$user_id = $factory->user->create( array( 'role' => 'administrator' ) ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Setup the environment for a test. |
| 46 | + */ |
| 47 | + public function set_up() { |
| 48 | + parent::set_up(); |
| 49 | + wp_set_current_user( static::$user_id ); |
| 50 | + $this->server_backup = $_SERVER; |
| 51 | + Jetpack_Application_Password_Extras::init(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Tear down the environment after a test. |
| 56 | + */ |
| 57 | + public function tear_down() { |
| 58 | + parent::tear_down(); |
| 59 | + $_SERVER = $this->server_backup; |
| 60 | + remove_all_filters( 'wp_doing_ajax' ); |
| 61 | + remove_all_filters( 'ajax_allowed_cors_origins' ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test that CORS origin is added with authorization header. |
| 66 | + */ |
| 67 | + public function test_cors_origin_added_with_authorization() { |
| 68 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 69 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 70 | + set_current_screen( 'admin-ajax' ); |
| 71 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 72 | + |
| 73 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 74 | + |
| 75 | + $this->assertContains( 'https://appassets.androidplatform.net', $result, 'Android origin should be added when authorization is present' ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test that CORS origin is not added without authorization. |
| 80 | + */ |
| 81 | + public function test_cors_origin_not_added_without_authorization() { |
| 82 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 83 | + unset( $_SERVER['HTTP_AUTHORIZATION'] ); |
| 84 | + set_current_screen( 'admin-ajax' ); |
| 85 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 86 | + |
| 87 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 88 | + |
| 89 | + $this->assertNotContains( 'https://appassets.androidplatform.net', $result, 'Android origin should not be added without authorization' ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that CORS origin is added for preflight requests with Authorization header. |
| 94 | + */ |
| 95 | + public function test_cors_origin_added_for_preflight_with_auth() { |
| 96 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 97 | + $_SERVER['REQUEST_METHOD'] = 'OPTIONS'; |
| 98 | + $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] = 'Authorization, Content-Type'; |
| 99 | + unset( $_SERVER['HTTP_AUTHORIZATION'] ); |
| 100 | + set_current_screen( 'admin-ajax' ); |
| 101 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 102 | + |
| 103 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 104 | + |
| 105 | + $this->assertContains( 'https://appassets.androidplatform.net', $result, 'Android origin should be added for preflight requests with Authorization header' ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test that CORS is not added for preflight requests without Authorization header. |
| 110 | + */ |
| 111 | + public function test_cors_not_added_for_preflight_without_auth() { |
| 112 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 113 | + $_SERVER['REQUEST_METHOD'] = 'OPTIONS'; |
| 114 | + $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] = 'Content-Type, X-Requested-With'; |
| 115 | + unset( $_SERVER['HTTP_AUTHORIZATION'] ); |
| 116 | + set_current_screen( 'admin-ajax' ); |
| 117 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 118 | + |
| 119 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 120 | + |
| 121 | + $this->assertNotContains( 'https://appassets.androidplatform.net', $result, 'Android origin should not be added for preflight without Authorization header' ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Test that CORS is not added outside admin-ajax context. |
| 126 | + */ |
| 127 | + public function test_cors_not_added_outside_admin_ajax() { |
| 128 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 129 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 130 | + // Not setting admin-ajax context |
| 131 | + |
| 132 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 133 | + |
| 134 | + $this->assertEmpty( $result, 'CORS should not be added outside admin-ajax context' ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test that CORS is not added for non-admin context even with wp_doing_ajax. |
| 139 | + */ |
| 140 | + public function test_cors_not_added_for_non_admin_context() { |
| 141 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 142 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 143 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 144 | + // Not setting admin screen |
| 145 | + |
| 146 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 147 | + |
| 148 | + $this->assertEmpty( $result, 'CORS should not be added when not in admin context' ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test that CORS is not added for different origin. |
| 153 | + */ |
| 154 | + public function test_cors_not_added_for_different_origin() { |
| 155 | + $_SERVER['HTTP_ORIGIN'] = 'https://evil.com'; |
| 156 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 157 | + set_current_screen( 'admin-ajax' ); |
| 158 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 159 | + |
| 160 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 161 | + |
| 162 | + $this->assertNotContains( 'https://evil.com', $result, 'Non-allowed origins should not be added' ); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Test that existing allowed origins are preserved. |
| 167 | + */ |
| 168 | + public function test_preserves_existing_allowed_origins() { |
| 169 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 170 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 171 | + set_current_screen( 'admin-ajax' ); |
| 172 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 173 | + |
| 174 | + $existing = array( 'https://example.com', 'https://test.com' ); |
| 175 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( $existing ); |
| 176 | + |
| 177 | + $this->assertContains( 'https://example.com', $result, 'Existing origins should be preserved' ); |
| 178 | + $this->assertContains( 'https://test.com', $result, 'Existing origins should be preserved' ); |
| 179 | + $this->assertContains( 'https://appassets.androidplatform.net', $result, 'Android origin should be added' ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test that duplicate origins are not added. |
| 184 | + */ |
| 185 | + public function test_no_duplicate_origins() { |
| 186 | + $_SERVER['HTTP_ORIGIN'] = 'https://appassets.androidplatform.net'; |
| 187 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 188 | + set_current_screen( 'admin-ajax' ); |
| 189 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 190 | + |
| 191 | + $existing = array( 'https://appassets.androidplatform.net' ); |
| 192 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( $existing ); |
| 193 | + |
| 194 | + $this->assertCount( 1, $result, 'Should not add duplicate origins' ); |
| 195 | + $this->assertEquals( array( 'https://appassets.androidplatform.net' ), $result ); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Test ajax_allowed_cors_origins filter extensibility. |
| 200 | + */ |
| 201 | + public function test_ajax_allowed_cors_origins_filter_extensibility() { |
| 202 | + add_filter( |
| 203 | + 'ajax_allowed_cors_origins', |
| 204 | + function ( $origins ) { |
| 205 | + $origins[] = 'https://custom.origin.com'; |
| 206 | + return $origins; |
| 207 | + } |
| 208 | + ); |
| 209 | + |
| 210 | + $_SERVER['HTTP_ORIGIN'] = 'https://custom.origin.com'; |
| 211 | + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic xxxxx'; |
| 212 | + set_current_screen( 'admin-ajax' ); |
| 213 | + add_filter( 'wp_doing_ajax', '__return_true' ); |
| 214 | + |
| 215 | + $result = Jetpack_Application_Password_Extras::allow_ajax_cors_origins( array() ); |
| 216 | + |
| 217 | + $this->assertContains( 'https://custom.origin.com', $result, 'Custom origins added via filter should be allowed' ); |
| 218 | + } |
| 219 | +} |
0 commit comments