Site Health: Validate the test result data format in JS before using it.

This will discard any invalid responses instead of causing fatal errors.

It also makes badges optional, on the same basis as actions are optional. They are expected, but there may be situations where they are not present.

Props Clorith, dogwithblog, kraftbj, whyisjake, SergeyBiryukov.
Fixes #50145.
Built from https://develop.svn.wordpress.org/trunk@49537


git-svn-id: http://core.svn.wordpress.org/trunk@49275 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-11-08 09:52:10 +00:00
parent c66edd0439
commit 5683c46277
4 changed files with 64 additions and 3 deletions

View File

@ -65,6 +65,57 @@ jQuery( document ).ready( function( $ ) {
$( this ).attr( 'aria-expanded', ! goodIssuesWrapper.hasClass( 'hidden' ) ); $( this ).attr( 'aria-expanded', ! goodIssuesWrapper.hasClass( 'hidden' ) );
} ); } );
/**
* Validates the Site Health test result format.
*
* @since 5.6.0
*
* @param {Object} issue
*
* @return {boolean}
*/
function validateIssueData( issue ) {
// Expected minimum format of a valid SiteHealth test response.
var minimumExpected = {
test: 'string',
label: 'string',
description: 'string'
},
passed = true,
key, value, subKey, subValue;
// If the issue passed is not an object, return a `false` state early.
if ( 'object' !== typeof( issue ) ) {
return false;
}
// Loop over expected data and match the data types.
for ( key in minimumExpected ) {
value = minimumExpected[ key ];
if ( 'object' === typeof( value ) ) {
for ( subKey in value ) {
subValue = value[ subKey ];
if ( 'undefined' === typeof( issue[ key ] ) ||
'undefined' === typeof( issue[ key ][ subKey ] ) ||
subValue !== typeof( issue[ key ][ subKey ] )
) {
passed = false;
}
}
} else {
if ( 'undefined' === typeof( issue[ key ] ) ||
value !== typeof( issue[ key ] )
) {
passed = false;
}
}
}
return passed;
}
/** /**
* Appends a new issue to the issue list. * Appends a new issue to the issue list.
* *
@ -78,6 +129,14 @@ jQuery( document ).ready( function( $ ) {
heading, heading,
count; count;
/*
* Validate the issue data format before using it.
* If the output is invalid, discard it.
*/
if ( ! validateIssueData( issue ) ) {
return false;
}
SiteHealth.site_status.issues[ issue.status ]++; SiteHealth.site_status.issues[ issue.status ]++;
count = SiteHealth.site_status.issues[ issue.status ]; count = SiteHealth.site_status.issues[ issue.status ];

File diff suppressed because one or more lines are too long

View File

@ -144,7 +144,9 @@ require_once ABSPATH . 'wp-admin/admin-header.php';
<h4 class="health-check-accordion-heading"> <h4 class="health-check-accordion-heading">
<button aria-expanded="false" class="health-check-accordion-trigger" aria-controls="health-check-accordion-block-{{ data.test }}" type="button"> <button aria-expanded="false" class="health-check-accordion-trigger" aria-controls="health-check-accordion-block-{{ data.test }}" type="button">
<span class="title">{{ data.label }}</span> <span class="title">{{ data.label }}</span>
<span class="badge {{ data.badge.color }}">{{ data.badge.label }}</span> <# if ( data.badge ) { #>
<span class="badge {{ data.badge.color }}">{{ data.badge.label }}</span>
<# } #>
<span class="icon"></span> <span class="icon"></span>
</button> </button>
</h4> </h4>

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.6-beta3-49536'; $wp_version = '5.6-beta3-49537';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.