<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[47639] branches/5.4: Customize: Add additional filters to Customizer to prevent JSON corruption.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/47639">47639</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/47639","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>whyisjake</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-04-29 15:45:27 +0000 (Wed, 29 Apr 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Add additional filters to Customizer to prevent JSON corruption.

This solution extends the `wp_insert_post_data` filter to pass in addition to the slashed/sanitized/processed data, and the slashed/sanitized/unprocessed data, to also pass the initial slashed/unsanitized/unprocessed data which was passed into `wp_insert_post()`. This then allows plugins to have complete control over how sanitization is performed based on the post type.

Brings the changes in <a href="https://core.trac.wordpress.org/changeset/47633">[47633]</a> to the 5.4 branch.

Props westonruter, peterwilsoncc, sstoqnov, whyisjake, xknown.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches54srcwpincludesclasswpcustomizemanagerphp">branches/5.4/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#branches54srcwpincludespostphp">branches/5.4/src/wp-includes/post.php</a></li>
<li><a href="#branches54testsphpunittestscustomizemanagerphp">branches/5.4/tests/phpunit/tests/customize/manager.php</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#branches54">branches/5.4/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/5.4
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/5.4 2020-04-29 15:38:43 UTC (rev 47638)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/5.4  2020-04-29 15:45:27 UTC (rev 47639)
</ins><a id="branches54"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/5.4</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/4.9:43557,43622
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43747,43751-43754,43758,43760-43765,43767-43770,43772,43774-43781,43783,43785,43790-43806,43808-43821,43825,43828,43830-43834,43836-43843,43846-43863,43867-43889,43891-43894,43897-43905,43908-43909,43911-43929,43931-43942,43946-43947,43949-43956,43959-43964,43967-43969,43988,43994,44014,44017,44047,44183,44185,44187-44206,44208-44213,44231-44232,44235,44248,44284,44287-44288
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:47432,47436,47439,47441,47443-47444,47449,47455-47456,47458-47459,47462,47475-47476,47495,47500,47502,47511,47513,47515,47517,47521,47532,47536-47537,47539,47547,47549,47555,47558,47560,47564,47568,47576,47578,47580
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:47432,47436,47439,47441,47443-47444,47449,47455-47456,47458-47459,47462,47475-47476,47495,47500,47502,47511,47513,47515,47517,47521,47532,47536-47537,47539,47547,47549,47555,47558,47560,47564,47568,47576,47578,47580,47633
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches54srcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.4/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.4/src/wp-includes/class-wp-customize-manager.php       2020-04-29 15:38:43 UTC (rev 47638)
+++ branches/5.4/src/wp-includes/class-wp-customize-manager.php 2020-04-29 15:45:27 UTC (rev 47639)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2928,17 +2928,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * WP_Customize_Setting::save().
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Prevent content filters from corrupting JSON in post_content.
-               $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
-               if ( $has_kses ) {
-                       kses_remove_filters();
-               }
-               $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
-               if ( $has_targeted_link_rel_filters ) {
-                       wp_remove_targeted_link_rel_filters();
-               }
-
-               // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * Update the changeset post. The publish_customize_changeset action will cause the settings in the
+                * changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
+                * trigger WP_Customize_Manager::publish_changeset_values().
+                */
+               add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $changeset_post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                // See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2969,15 +2964,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset.
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Restore removed content filters.
-               if ( $has_kses ) {
-                       kses_init_filters();
-               }
-               if ( $has_targeted_link_rel_filters ) {
-                       wp_init_targeted_link_rel_filters();
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                remove_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2995,6 +2983,51 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Preserve the initial JSON post_content passed to save into the post.
+        *
+        * This is needed to prevent KSES and other {@see 'content_save_pre'} filters
+        * from corrupting JSON data.
+        *
+        * Note that WP_Customize_Manager::validate_setting_values() have already
+        * run on the setting values being serialized as JSON into the post content
+        * so it is pre-sanitized.
+        *
+        * Also, the sanitization logic is re-run through the respective
+        * WP_Customize_Setting::sanitize() method when being read out of the
+        * changeset, via WP_Customize_Manager::post_value(), and this sanitized
+        * value will also be sent into WP_Customize_Setting::update() for
+        * persisting to the DB.
+        *
+        * Multiple users can collaborate on a single changeset, where one user may
+        * have the unfiltered_html capability but another may not. A user with
+        * unfiltered_html may add a script tag to some field which needs to be kept
+        * intact even when another user updates the changeset to modify another field
+        * when they do not have unfiltered_html.
+        *
+        * @since 5.4.1
+        *
+        * @param array $data                An array of slashed and processed post data.
+        * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
+        * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
+        * @return array Filtered post data.
+        */
+       public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
+               if (
+                       isset( $data['post_type'] ) &&
+                       isset( $unsanitized_postarr['post_content'] ) &&
+                       'customize_changeset' === $data['post_type'] ||
+                       (
+                               'revision' === $data['post_type'] &&
+                               ! empty( $data['post_parent'] ) &&
+                               'customize_changeset' === get_post_type( $data['post_parent'] )
+                       )
+               ) {
+                       $data['post_content'] = $unsanitized_postarr['post_content'];
+               }
+               return $data;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Trash or delete a changeset post.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * The following re-formulates the logic from `wp_trash_post()` as done in
</span></span></pre></div>
<a id="branches54srcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.4/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.4/src/wp-includes/post.php     2020-04-29 15:38:43 UTC (rev 47638)
+++ branches/5.4/src/wp-includes/post.php       2020-04-29 15:45:27 UTC (rev 47639)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3565,6 +3565,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_insert_post( $postarr, $wp_error = false ) {
</span><span class="cx" style="display: block; padding: 0 10px">        global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Capture original pre-sanitized array for passing into filters.
+       $unsanitized_postarr = $postarr;
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $user_id = get_current_user_id();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $defaults = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3889,21 +3892,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filters attachment post data before it is updated in or added to the database.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @since 3.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * @since 5.4.1 `$unsanitized_postarr` argument added.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @param array $data    An array of sanitized attachment post data.
-                * @param array $postarr An array of unsanitized attachment post data.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @param array $data                An array of slashed, sanitized, and processed attachment post data.
+                * @param array $postarr             An array of slashed and sanitized attachment post data, but not processed.
+                * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
+                *                                   as originally passed to wp_insert_post().
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr );
</ins><span class="cx" style="display: block; padding: 0 10px">         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filters slashed post data just before it is inserted into the database.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @since 2.7.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * @since 5.4.1 `$unsanitized_postarr` argument added.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @param array $data    An array of slashed post data.
-                * @param array $postarr An array of sanitized, but otherwise unmodified post data.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @param array $data                An array of slashed, sanitized, and processed post data.
+                * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
+                * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
+                *                                   originally passed to wp_insert_post().
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px">        $data  = wp_unslash( $data );
</span><span class="cx" style="display: block; padding: 0 10px">        $where = array( 'ID' => $post_ID );
</span></span></pre></div>
<a id="branches54testsphpunittestscustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.4/tests/phpunit/tests/customize/manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.4/tests/phpunit/tests/customize/manager.php    2020-04-29 15:38:43 UTC (rev 47638)
+++ branches/5.4/tests/phpunit/tests/customize/manager.php      2020-04-29 15:45:27 UTC (rev 47639)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1241,6 +1241,171 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test saving changeset post without Kses or other content_save_pre filters mutating content.
+        *
+        * @covers WP_Customize_Manager::save_changeset_post()
+        */
+       public function test_save_changeset_post_without_kses_corrupting_json() {
+               global $wp_customize;
+               $lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+
+               $uuid         = wp_generate_uuid4();
+               $wp_customize = new WP_Customize_Manager(
+                       array(
+                               'changeset_uuid' => $uuid,
+                       )
+               );
+
+               add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap_to_disallow_unfiltered_html' ), 10, 2 );
+               kses_init();
+               add_filter( 'content_save_pre', 'capital_P_dangit' );
+               add_post_type_support( 'customize_changeset', 'revisions' );
+
+               $options = array(
+                       'custom_html_1' => '<script>document.write(" Wordpress 1")</script>',
+                       'custom_html_2' => '<script>document.write(" Wordpress 2")</script>',
+                       'custom_html_3' => '<script>document.write(" Wordpress 3")</script>',
+               );
+
+               // Populate setting as user who can bypass content_save_pre filter.
+               wp_set_current_user( self::$admin_user_id );
+               $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
+               $wp_customize->set_post_value( 'custom_html_1', $options['custom_html_1'] );
+               $wp_customize->save_changeset_post(
+                       array(
+                               'status' => 'draft',
+                       )
+               );
+
+               // Populate setting as user who cannot bypass content_save_pre filter.
+               wp_set_current_user( $lesser_admin_user_id );
+               $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
+               $wp_customize->set_post_value( 'custom_html_2', $options['custom_html_2'] );
+               $wp_customize->save_changeset_post(
+                       array(
+                               'autosave' => true,
+                       )
+               );
+
+               /*
+                * Ensure that the unsanitized value (the "POST data") is preserved in the autosave revision.
+                * The value is sent through the sanitize function when it is read from the changeset.
+                */
+               $autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id(), get_current_user_id() );
+               $saved_data        = json_decode( $autosave_revision->post_content, true );
+               $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
+               $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
+
+               // Update post to discard autosave.
+               $wp_customize->save_changeset_post(
+                       array(
+                               'status' => 'draft',
+                       )
+               );
+
+               /*
+                * Ensure that the unsanitized value (the "POST data") is preserved in the post content.
+                * The value is sent through the sanitize function when it is read from the changeset.
+                */
+               $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
+               $saved_data   = json_decode( get_post( $wp_customize->changeset_post_id() )->post_content, true );
+               $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
+               $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
+
+               /*
+                * Ensure that the unsanitized value (the "POST data") is preserved in the revisions' content.
+                * The value is sent through the sanitize function when it is read from the changeset.
+                */
+               $revisions  = wp_get_post_revisions( $wp_customize->changeset_post_id() );
+               $revision   = array_shift( $revisions );
+               $saved_data = json_decode( $revision->post_content, true );
+               $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
+               $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
+
+               /*
+                * Now when publishing the changeset, the unsanitized values will be read from the changeset
+                * and sanitized according to the capabilities of the users who originally updated each
+                * setting in the changeset to begin with.
+                */
+               wp_set_current_user( $lesser_admin_user_id );
+               $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
+               $wp_customize->set_post_value( 'custom_html_3', $options['custom_html_3'] );
+               $wp_customize->save_changeset_post(
+                       array(
+                               'status' => 'publish',
+                       )
+               );
+
+               // User saved as one who can bypass content_save_pre filter.
+               $this->assertContains( '<script>', get_option( 'custom_html_1' ) );
+               $this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
+
+               // User saved as one who cannot bypass content_save_pre filter.
+               $this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
+               $this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
+
+               // User saved as one who also cannot bypass content_save_pre filter.
+               $this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
+               $this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
+       }
+
+       /**
+        * Get a manager for testing JSON corruption protection.
+        *
+        * @param string $uuid UUID.
+        * @return WP_Customize_Manager Manager.
+        */
+       private function get_manager_for_testing_json_corruption_protection( $uuid ) {
+               global $wp_customize;
+               $wp_customize = new WP_Customize_Manager(
+                       array(
+                               'changeset_uuid' => $uuid,
+                       )
+               );
+               for ( $i = 0; $i < 5; $i++ ) {
+                       $wp_customize->add_setting(
+                               sprintf( 'custom_html_%d', $i ),
+                               array(
+                                       'type'              => 'option',
+                                       'sanitize_callback' => array( $this, 'apply_content_save_pre_filters_if_not_main_admin_user' ),
+                               )
+                       );
+               }
+               return $wp_customize;
+       }
+
+       /**
+        * Sanitize content with Kses if the current user is not the main admin.
+        *
+        * @since 5.2.?
+        *
+        * @param string $content Content to sanitize.
+        * @return string Sanitized content.
+        */
+       public function apply_content_save_pre_filters_if_not_main_admin_user( $content ) {
+               if ( get_current_user_id() !== self::$admin_user_id ) {
+                       $content = apply_filters( 'content_save_pre', $content );
+               }
+               return $content;
+       }
+
+       /**
+        * Filter map_meta_cap to disallow unfiltered_html.
+        *
+        * @since 5.2.?
+        *
+        * @param array  $caps User's capabilities.
+        * @param string $cap  Requested cap.
+        * @return array Caps.
+        */
+       public function filter_map_meta_cap_to_disallow_unfiltered_html( $caps, $cap ) {
+               if ( 'unfiltered_html' === $cap ) {
+                       $caps = array( 'do_not_allow' );
+               }
+               return $caps;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Call count for customize_changeset_save_data filter.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @var int
</span></span></pre>
</div>
</div>

</body>
</html>