WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 2 months ago

#47164 new defect (bug)

map_deep in formatting.php do not handle null-byte

Reported by: bitcomplex Owned by:
Milestone: Future Release Priority: normal
Severity: critical Version: 5.2.2
Component: Formatting Keywords: needs-patch
Focuses: Cc:
PR Number:

Description

<?php
foreach ( $object_vars as $property_name => $property_value ) {
        $value->$property_name = map_deep( $property_value, $callback );
}

The above code snippet in the function map_deep in formatting.php will trigger a fatal error if for some reason $property_name starts with a null-byte. null-bytes can exist in this context if $object_vars for some reason is from an object cast to an array. private and protected properties will be prefixed with null * null

We've encountered it in the wild with serialized objects, and even though this is because of faulty programming (child classes with stricter access for properties than the parents) wordpress should handle this.

The simples solution I can think of id to add:

<?php
foreach ( $object_vars as $property_name => $property_value ) {
                        **if (ord($property_name) === 0) {
                                continue;
                        }**
                        $value->$property_name = map_deep( $property_value, $callback );
                }

Change History (3)

#1 @bitcomplex
4 months ago

  • Severity changed from normal to critical
  • Version set to 5.2.2

I was pretty sure this was an issue related to a php bug patched in php7.2 ( https://bugs.php.net/bug.php?id=49649 )
But after upgrading to 7.3.7 (from 7.1.x) we get issues with this.

serialized objects fetched from db fails here. It is possible because of the objectes in the db is serialized with an old php version where the bug exists. But wp could handle it better.

Error: Cannot access property started with '\0'
#12 /home/httpd/rackesbutiken/rackesbutiken.se/wp-includes/formatting.php(4742): map_deep
#11 /home/httpd/rackesbutiken/rackesbutiken.se/wp-includes/formatting.php(2691): stripslashes_deep
#10 /home/httpd/rackesbutiken/rackesbutiken.se/wp-includes/formatting.php(5342): wp_unslash
#9 /home/httpd/rackesbutiken/rackesbutiken.se/wp-includes/meta.php(182): update_metadata
#8 /home/httpd/rackesbutiken/rackesbutiken.se/wp-includes/post.php(2061): update_post_meta

#2 @bitcomplex
3 months ago

Even though this is old, it pretty much explains the problem in a short and concise way: https://cweiske.de/tagebuch/php-property-started-nul.htm

#3 @SergeyBiryukov
2 months ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

Hi @bitcomplex, welcome to WordPress Trac! Thanks for the report.

Just noting that I was able to reproduce the fatal error with this code:

class Dummy {
    public $pub = 0;
    protected $prot = 1;
    private $priv = 2;
}

$test = (object) (array) new Dummy();
$test = stripslashes_deep( $test );
Note: See TracTickets for help on using tickets.