From b161cfc1ff2566c7be7425339b695aa404bf8dce Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 21 Dec 2021 02:43:18 +0000 Subject: [PATCH] Application Passwords: Show HTTPS required message without filtering when not enabled or not in local environment. When `add_filter( 'wp_is_application_passwords_available', '__return_false' )` exists, HTTPS requirement message is shown even if HTTPS is enabled on the site. This happens because `wp_is_application_passwords_available_for_user()` first invokes `wp_is_application_passwords_available()` which is filterable. The situation could happen if the `'wp_is_application_passwords_available_for_user'` filter returns `false`. To fix this, the check for HTTPS (or if in a 'local' environment) is moved to a new function called `wp_is_application_passwords_supported()`. Then the return from this function is used as an OR condition for the Application Passwords section and for displaying the HTTPS required message. Tests are included for both `wp_is_application_passwords_supported()` and `wp_is_application_passwords_available()`. Follow-up to [51980], [51988]. Props davidbinda, SergeyBiryukov, ocean90, felipeelia, costdev, hellofromTonya. Fixes #53658. git-svn-id: https://develop.svn.wordpress.org/trunk@52398 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/user-edit.php | 4 +- src/wp-includes/load.php | 2 +- src/wp-includes/user.php | 20 ++++- .../rest-application-passwords-controller.php | 80 +++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 8 +- 5 files changed, 107 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/user-edit.php b/src/wp-admin/user-edit.php index 813c5eb5cd..64f81d68bc 100644 --- a/src/wp-admin/user-edit.php +++ b/src/wp-admin/user-edit.php @@ -733,6 +733,7 @@ endif; +

@@ -796,7 +797,7 @@ endif; $application_passwords_list_table->display(); ?>
- +

+ assertErrorResponse( 'rest_application_password_not_found', $response, 500 ); } + /** + * @ticket 53658 + * + * @covers ::wp_is_application_passwords_supported + */ + public function test_wp_is_application_passwords_supported_with_https_only() { + $_SERVER['HTTPS'] = 'on'; + $this->assertTrue( wp_is_application_passwords_supported() ); + } + + /** + * @ticket 53658 + * + * @covers ::wp_is_application_passwords_supported + */ + public function test_wp_is_application_passwords_supported_with_local_environment_only() { + putenv( 'WP_ENVIRONMENT_TYPE=local' ); + + $actual = wp_is_application_passwords_supported(); + + // Revert to default behaviour so that other tests are not affected. + putenv( 'WP_ENVIRONMENT_TYPE' ); + + $this->assertTrue( $actual ); + } + + /** + * @dataProvider data_wp_is_application_passwords_available + * + * @ticket 53658 + * + * @covers ::wp_is_application_passwords_available + * + * @param bool|string $expected The expected value. + * @param string|null $callback Optional. The callback for the `wp_is_application_passwords_available` hook. + * Default: null. + */ + public function test_wp_is_application_passwords_available( $expected, $callback = null ) { + remove_filter( 'wp_is_application_passwords_available', '__return_true' ); + + if ( $callback ) { + add_filter( 'wp_is_application_passwords_available', $callback ); + } + + if ( 'default' === $expected ) { + putenv( 'WP_ENVIRONMENT_TYPE=local' ); + $expected = wp_is_application_passwords_supported(); + } + + $actual = wp_is_application_passwords_available(); + + if ( 'default' === $expected ) { + // Revert to default behaviour so that other tests are not affected. + putenv( 'WP_ENVIRONMENT_TYPE' ); + } + + $this->assertSame( $expected, $actual ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_wp_is_application_passwords_available() { + return array( + 'availability not forced' => array( + 'expected' => 'default', + ), + 'availability forced true' => array( + 'expected' => true, + 'callback' => '__return_true', + ), + 'availability forced false' => array( + 'expected' => false, + 'callback' => '__return_false', + ), + ); + } + /** * Sets up a REST API request to be authenticated using an App Password. * diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 9b71d950fc..e565ddd7cb 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -18,7 +18,13 @@ mockedApiResponse.Schema = { "wp-site-health/v1", "wp-block-editor/v1" ], - "authentication": [], + "authentication": { + "application-passwords": { + "endpoints": { + "authorization": "http://example.org/wp-admin/authorize-application.php" + } + } + }, "routes": { "/": { "namespace": "",