Make WordPress Core

Changeset 61516


Ignore:
Timestamp:
01/23/2026 05:56:40 AM (10 months ago)
Author:
isabel_brison
Message:

Editor: guard against non-string values in style engine.

Checks that the value passed to add_declaration is a string to prevent fatal errors due to malformed block attributes.

Props andrewserong.
Fixes #64545.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php

    r55819 r61516  
    5757                // Bails early if the property is empty.
    5858                if ( empty( $property ) ) {
     59                        return $this;
     60                }
     61
     62                // Bail early if value is not a string. Prevents fatal errors from malformed block markup.
     63                if ( ! is_string( $value ) ) {
    5964                        return $this;
    6065                }
  • trunk/tests/phpunit/tests/style-engine/wpStyleEngineCssDeclarations.php

    r54156 r61516  
    291291                );
    292292        }
     293
     294        /**
     295         * Tests that non-string values are rejected without causing fatal errors.
     296         *
     297         * @ticket 64545
     298         *
     299         * @covers ::add_declaration
     300         */
     301        public function test_should_reject_non_string_values() {
     302                $css_declarations = new WP_Style_Engine_CSS_Declarations();
     303
     304                // Add valid string value first.
     305                $css_declarations->add_declaration( 'color', 'red' );
     306
     307                // Try to add array value - should be silently rejected.
     308                $css_declarations->add_declaration( 'padding-margin', array( 'top' => '10px' ) );
     309
     310                // Try to add other non-string values.
     311                $css_declarations->add_declaration( 'font-size', 123 );
     312                $css_declarations->add_declaration( 'margin', null );
     313
     314                // Only the valid string value should be stored.
     315                $this->assertSame(
     316                        array( 'color' => 'red' ),
     317                        $css_declarations->get_declarations(),
     318                        'Non-string values should be rejected without causing errors.'
     319                );
     320        }
    293321}
Note: See TracChangeset for help on using the changeset viewer.