From f7799b9455f7854697c1e90a2a93b7f4d1dacc07 Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Wed, 14 Apr 2021 21:26:58 +0000 Subject: [PATCH] Privacy: Ensure "Export Personal Data" does not generate invalid JSON. Previously, when exporting personal data, if the JSON encoding of the data failed, the invalid JSON was still written to `export.json`. This change captures the JSON encoding failure and adds a notice to the UI. Props hellofromTonya, jrf, SergeyBiryukov. Fixes #52892. git-svn-id: https://develop.svn.wordpress.org/trunk@50713 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/privacy-tools.php | 10 +++++ ...pPrivacyGeneratePersonalDataExportFile.php | 42 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php index 3f69366020..8b78752a85 100644 --- a/src/wp-admin/includes/privacy-tools.php +++ b/src/wp-admin/includes/privacy-tools.php @@ -413,6 +413,16 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) { // Convert the groups to JSON format. $groups_json = wp_json_encode( $groups ); + if ( false === $groups_json ) { + $error_message = sprintf( + /* translators: %s: Error message. */ + __( 'Unable to encode the personal data for export. Error: %s' ), + json_last_error_msg() + ); + + wp_send_json_error( $error_message ); + } + /* * Handle the JSON export. */ diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php index 8d5bcbe412..1b384b4869 100644 --- a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php +++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php @@ -631,4 +631,46 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC ), ); } + + /** + * Test should generate JSON error when JSON encoding fails. + * + * @ticket 52892 + */ + public function test_should_generate_json_error_when_json_encoding_fails() { + add_filter( 'get_post_metadata', array( $this, 'filter_export_data_grouped_metadata' ), 10, 3 ); + + // Validate JSON encoding fails and returns `false`. + $metadata = get_post_meta( self::$export_request_id, '_export_data_grouped', true ); + $this->assertFalse( wp_json_encode( $metadata ) ); + + $this->expectException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Unable to encode the personal data for export. Error: Type is not supported"}' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + } + + public function filter_export_data_grouped_metadata( $value, $object_id, $meta_key ) { + if ( $object_id !== self::$export_request_id ) { + return $value; + } + + if ( '_export_data_grouped' !== $meta_key ) { + return $value; + } + + $file = fopen( __FILE__, 'r' ); + + $value = array( + 'user' => array( + 'group_label' => 'User', + 'group_description' => 'User’s profile data.', + 'items' => array(), + 'resource' => $file, + ), + ); + + fclose( $file ); + + return array( $value ); + } }