Skip to content

Commit d4a16ac

Browse files
committed
Themes: Check that a JSON file is readable before decoding it.
When the given path is a directory or an unreadable file, `wp_json_file_decode()` now triggers a notice and returns `null` instead of emitting a PHP warning while reading it. The notice for a missing file now also shows the requested path instead of an empty string. Developed in: #10898 Props mindctrl, mukesh27, ozgursar, pbearne, westonruter, wildworks. Fixes #64620. git-svn-id: https://develop.svn.wordpress.org/trunk@65806 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a4ad96e commit d4a16ac

3 files changed

Lines changed: 239 additions & 7 deletions

File tree

‎src/wp-includes/functions.php‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,13 +4810,13 @@ function wp_check_jsonp_callback( $callback ) {
48104810
* }
48114811
*
48124812
* @return mixed Returns the value encoded in JSON in appropriate PHP type.
4813-
* `null` is returned if the file is not found, or its content can't be decoded.
4813+
* `null` is returned if the file is not found, is not a readable file,
4814+
* or its content can't be decoded.
48144815
*/
48154816
function wp_json_file_decode( $filename, $options = array() ) {
4816-
$result = null;
4817-
$filename = wp_normalize_path( realpath( $filename ) );
4817+
$normalized_filename = wp_normalize_path( realpath( $filename ) );
48184818

4819-
if ( ! $filename ) {
4819+
if ( ! $normalized_filename ) {
48204820
wp_trigger_error(
48214821
__FUNCTION__,
48224822
sprintf(
@@ -4825,11 +4825,22 @@ function wp_json_file_decode( $filename, $options = array() ) {
48254825
$filename
48264826
)
48274827
);
4828-
return $result;
4828+
return null;
4829+
}
4830+
if ( ! is_file( $normalized_filename ) || ! is_readable( $normalized_filename ) ) {
4831+
wp_trigger_error(
4832+
__FUNCTION__,
4833+
sprintf(
4834+
/* translators: %s: Path to the JSON file. */
4835+
__( '%s is not a readable file.' ),
4836+
$filename
4837+
)
4838+
);
4839+
return null;
48294840
}
48304841

48314842
$options = wp_parse_args( $options, array( 'associative' => false ) );
4832-
$decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] );
4843+
$decoded_file = json_decode( file_get_contents( $normalized_filename ), $options['associative'] );
48334844

48344845
if ( JSON_ERROR_NONE !== json_last_error() ) {
48354846
wp_trigger_error(
@@ -4841,7 +4852,7 @@ function wp_json_file_decode( $filename, $options = array() ) {
48414852
json_last_error_msg()
48424853
)
48434854
);
4844-
return $result;
4855+
return null;
48454856
}
48464857

48474858
return $decoded_file;

‎tests/phpunit/tests/functions.php‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,112 @@ public function test_wp_json_file_decode_associative_array() {
11021102
$this->assertSame( 'tests/notice', $result['name'] );
11031103
}
11041104

1105+
/**
1106+
* Tests decoding a non-existent file.
1107+
*
1108+
* @ticket 64620
1109+
*/
1110+
public function test_wp_json_file_decode_with_non_existent_file() {
1111+
$non_existent_file = DIR_TESTDATA . '/non-existent.json';
1112+
1113+
list( $result, $messages ) = $this->wp_json_file_decode_and_capture_errors( $non_existent_file );
1114+
1115+
$this->assertNull( $result );
1116+
$this->assertSame( array( "File {$non_existent_file} doesn't exist!" ), $messages );
1117+
}
1118+
1119+
/**
1120+
* Tests decoding an unreadable file.
1121+
*
1122+
* @ticket 64620
1123+
*/
1124+
public function test_wp_json_file_decode_with_unreadable_file() {
1125+
if ( ! function_exists( 'posix_getpwuid' ) || ! function_exists( 'posix_geteuid' ) ) {
1126+
$this->markTestSkipped( 'The POSIX extension is not available.' );
1127+
}
1128+
1129+
$pwuid = posix_getpwuid( posix_geteuid() );
1130+
if ( is_array( $pwuid ) && isset( $pwuid['name'] ) && 'root' === $pwuid['name'] ) {
1131+
$this->markTestSkipped( 'Cannot test unreadable file permissions when running as root.' );
1132+
}
1133+
1134+
$unreadable_file = tempnam( sys_get_temp_dir(), 'unreadable-json' );
1135+
chmod( $unreadable_file, 0000 );
1136+
try {
1137+
list( $result, $messages ) = $this->wp_json_file_decode_and_capture_errors( $unreadable_file );
1138+
} finally {
1139+
chmod( $unreadable_file, 0644 );
1140+
unlink( $unreadable_file );
1141+
}
1142+
1143+
$this->assertNull( $result );
1144+
$this->assertSame( array( "{$unreadable_file} is not a readable file." ), $messages );
1145+
}
1146+
1147+
/**
1148+
* Tests decoding a directory instead of a file.
1149+
*
1150+
* @ticket 64620
1151+
*/
1152+
public function test_wp_json_file_decode_with_directory() {
1153+
$directory = DIR_TESTDATA . '/themedir1/block-theme';
1154+
1155+
list( $result, $messages ) = $this->wp_json_file_decode_and_capture_errors( $directory );
1156+
1157+
$this->assertNull( $result );
1158+
$this->assertSame( array( "{$directory} is not a readable file." ), $messages );
1159+
}
1160+
1161+
/**
1162+
* Tests decoding a file with invalid JSON.
1163+
*
1164+
* @ticket 64620
1165+
*/
1166+
public function test_wp_json_file_decode_with_invalid_json() {
1167+
$invalid_json_file = tempnam( sys_get_temp_dir(), 'invalid-json' );
1168+
file_put_contents( $invalid_json_file, '{ invalid json }' );
1169+
try {
1170+
list( $result, $messages ) = $this->wp_json_file_decode_and_capture_errors( $invalid_json_file );
1171+
} finally {
1172+
unlink( $invalid_json_file );
1173+
}
1174+
1175+
// The error message varies between PHP versions, e.g. PHP 8.6 appends the error location.
1176+
json_decode( '{ invalid json }' );
1177+
$expected_message = "Error when decoding a JSON file at path {$invalid_json_file}: " . json_last_error_msg();
1178+
1179+
$this->assertNull( $result );
1180+
$this->assertSame( array( $expected_message ), $messages );
1181+
}
1182+
1183+
/**
1184+
* Calls wp_json_file_decode() and captures the messages passed to wp_trigger_error().
1185+
*
1186+
* The errors are not triggered, so the test can assert the return value.
1187+
*
1188+
* @param string $filename Path to the JSON file.
1189+
* @return array {
1190+
* @type mixed $0 The value returned by wp_json_file_decode().
1191+
* @type string[] $1 The messages passed to wp_trigger_error().
1192+
* }
1193+
*/
1194+
private function wp_json_file_decode_and_capture_errors( $filename ) {
1195+
$messages = array();
1196+
add_filter(
1197+
'wp_trigger_error_trigger_error',
1198+
static function ( $trigger, $function_name, $message ) use ( &$messages ) {
1199+
$messages[] = $message;
1200+
return false;
1201+
},
1202+
10,
1203+
3
1204+
);
1205+
1206+
$result = wp_json_file_decode( $filename );
1207+
1208+
return array( $result, $messages );
1209+
}
1210+
11051211
/**
11061212
* @ticket 36054
11071213
* @dataProvider data_mysql_to_rfc3339
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* Test WP_Theme_JSON_Resolver::read_json_file().
5+
*
6+
* @package WordPress
7+
* @subpackage Theme
8+
*
9+
* @group themes
10+
*
11+
* @covers WP_Theme_JSON_Resolver::read_json_file
12+
*/
13+
class Tests_Theme_WpThemeJsonResolver_ReadJsonFile extends WP_UnitTestCase {
14+
15+
/**
16+
* WP_Theme_JSON_Resolver::$theme_json_file_cache property.
17+
*
18+
* @var ReflectionProperty
19+
*/
20+
private static $property_theme_json_file_cache;
21+
22+
/**
23+
* Original value of the WP_Theme_JSON_Resolver::$theme_json_file_cache property.
24+
*
25+
* @var array
26+
*/
27+
private static $property_theme_json_file_cache_orig_value;
28+
29+
/**
30+
* WP_Theme_JSON_Resolver::read_json_file() method.
31+
*
32+
* @var ReflectionMethod
33+
*/
34+
private static $method_read_json_file;
35+
36+
public static function set_up_before_class() {
37+
parent::set_up_before_class();
38+
39+
static::$property_theme_json_file_cache = new ReflectionProperty( WP_Theme_JSON_Resolver::class, 'theme_json_file_cache' );
40+
if ( PHP_VERSION_ID < 80100 ) {
41+
static::$property_theme_json_file_cache->setAccessible( true );
42+
}
43+
static::$property_theme_json_file_cache_orig_value = static::$property_theme_json_file_cache->getValue();
44+
45+
static::$method_read_json_file = new ReflectionMethod( WP_Theme_JSON_Resolver::class, 'read_json_file' );
46+
if ( PHP_VERSION_ID < 80100 ) {
47+
static::$method_read_json_file->setAccessible( true );
48+
}
49+
}
50+
51+
public static function tear_down_after_class() {
52+
static::$property_theme_json_file_cache->setValue( null, static::$property_theme_json_file_cache_orig_value );
53+
parent::tear_down_after_class();
54+
}
55+
56+
public function tear_down() {
57+
// Reset data between tests.
58+
static::$property_theme_json_file_cache->setValue( null, array() );
59+
parent::tear_down();
60+
}
61+
62+
/**
63+
* Calls the protected WP_Theme_JSON_Resolver::read_json_file() method.
64+
*
65+
* @param string $file_path Path to file.
66+
* @return array
67+
*/
68+
private static function read_json_file( $file_path ) {
69+
return static::$method_read_json_file->invoke( null, $file_path );
70+
}
71+
72+
/**
73+
* Tests reading a valid JSON file.
74+
*/
75+
public function test_read_json_file_with_valid_file() {
76+
$valid_file = DIR_TESTDATA . '/themedir1/block-theme/theme.json';
77+
$result = self::read_json_file( $valid_file );
78+
79+
$this->assertIsArray( $result );
80+
$this->assertArrayHasKey( 'version', $result );
81+
$this->assertSame( 3, $result['version'] );
82+
}
83+
84+
/**
85+
* Tests that the result is cached after reading a file.
86+
*/
87+
public function test_read_json_file_caches_result() {
88+
$valid_file = DIR_TESTDATA . '/themedir1/block-theme/theme.json';
89+
$result = self::read_json_file( $valid_file );
90+
91+
$cache = static::$property_theme_json_file_cache->getValue();
92+
$this->assertArrayHasKey( $valid_file, $cache );
93+
$this->assertSame( $result, $cache[ $valid_file ] );
94+
}
95+
96+
/**
97+
* Tests that the cached value is returned when available.
98+
*/
99+
public function test_read_json_file_returns_cached_value() {
100+
$valid_file = DIR_TESTDATA . '/themedir1/block-theme/theme.json';
101+
102+
// Seed cache with custom value.
103+
$cache = array(
104+
$valid_file => array(
105+
'version' => 3,
106+
'cached' => true,
107+
),
108+
);
109+
static::$property_theme_json_file_cache->setValue( null, $cache );
110+
111+
$result = self::read_json_file( $valid_file );
112+
$this->assertSame( 3, $result['version'] );
113+
$this->assertTrue( $result['cached'] );
114+
}
115+
}

0 commit comments

Comments
 (0)