Make WordPress Core


Ignore:
Timestamp:
08/28/2026 12:04:49 AM (6 weeks ago)
Author:
dmsnell
Message:

HTML API: Refactor wp_get_admin_notice().

Refactors wp_get_admin_notice() to rely on the HTML API for HTML generation. This provides more reliable parsing and hardening for and against the provided arguments, but introduces behavioral changes that should only affect already-broken cases.

Namely, the function previously generated its output and then mandated that calling code run it through wp_kses(). This meant that certain kinds of corruption were possible which would break boundaries within the generated HTML and result in mangled output through wp_kses(). Now, the function preserves all boundaries when generating the output, meaning that wp_kses() will receive already-normalized content.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13273
Discussed in: https://core.trac.wordpress.org/ticket/65984

Props dmsnell, joedolson, johnbillion, jorbin.
See #65984.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/wpGetAdminNotice.php

    r57100 r63380  
    2222         */
    2323        public function test_should_return_admin_notice( $message, $args, $expected ) {
    24                 $this->assertSame( $expected, wp_get_admin_notice( $message, $args ) );
     24                $this->assertEqualHTML( $expected, wp_get_admin_notice( $message, $args ) );
    2525        }
    2626
    … …  
    151151                                        'type' => '"><script>alert("Howdy,admin!");</script>',
    152152                                ),
    153                                 'expected' => '<div class="notice notice-"><script>alert("Howdy,admin!");</script>"><p>A notice with an unsafe type.</p></div>',
     153                                'expected' => '<div class="notice notice-&quot;><script>alert(&quot;Howdy,admin!&quot;);</script>"><p>A notice with an unsafe type.</p></div>',
    154154                        ),
    155155                        'an unsafe ID'                              => array(
    … …  
    158158                                        'id' => '"><script>alert( "Howdy, admin!" );</script> <div class="notice',
    159159                                ),
    160                                 'expected' => '<div id=""><script>alert( "Howdy, admin!" );</script> <div class="notice" class="notice"><p>A notice with an unsafe ID.</p></div>',
     160                                'expected' => '<div id="&quot;><script>alert( &quot;Howdy, admin!&quot; );</script> <div class=&quot;notice" class="notice"><p>A notice with an unsafe ID.</p></div>',
    161161                        ),
    162162                        'unsafe additional classes'                 => array(
    … …  
    165165                                        'additional_classes' => array( '"><script>alert( "Howdy, admin!" );</script> <div class="notice' ),
    166166                                ),
    167                                 'expected' => '<div class="notice "><script>alert( "Howdy, admin!" );</script> <div class="notice"><p>A notice with unsafe additional classes.</p></div>',
     167                                'expected' => '<div class="notice &quot;><script>alert( &quot;Howdy, admin!&quot; );</script> <div class=&quot;notice"><p>A notice with unsafe additional classes.</p></div>',
    168168                        ),
    169169                        'a type that is not a string'               => array(
Note: See TracChangeset for help on using the changeset viewer.