Skip to content

Commit 824ae7b

Browse files
committed
Use more robust JSON flags
1 parent c51de8b commit 824ae7b

File tree

11 files changed

+20
-12
lines changed

11 files changed

+20
-12
lines changed

plugins/optimization-detective/detection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@ static function ( OD_URL_Metric_Group $group ): array {
153153
sprintf(
154154
"( %s )( %s, %s );\n//# sourceURL=%s",
155155
file_get_contents( __DIR__ . '/' . od_get_asset_path( 'detect-loader.js' ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
156-
wp_json_encode( plugins_url( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, od_get_asset_path( 'detect.js' ) ), __FILE__ ) ),
157-
wp_json_encode( $detect_args ),
156+
wp_json_encode(
157+
add_query_arg(
158+
array( 'ver' => OPTIMIZATION_DETECTIVE_VERSION ),
159+
plugins_url( od_get_asset_path( 'detect.js' ), __FILE__ )
160+
),
161+
JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
162+
),
163+
wp_json_encode( $detect_args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
158164
add_query_arg(
159165
array( 'ver' => OPTIMIZATION_DETECTIVE_VERSION ),
160166
plugins_url( od_get_asset_path( 'detect-loader.js' ), __FILE__ )

plugins/optimization-detective/optimization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function od_print_disabled_reasons( array $reasons ): void {
122122
wp_print_inline_script_tag(
123123
sprintf(
124124
"console.info( %s );\n//# sourceURL=od-print-disabled-reasons-%d",
125-
wp_json_encode( '[Optimization Detective] ' . $reason ),
125+
wp_json_encode( '[Optimization Detective] ' . $reason, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
126126
$i + 1
127127
),
128128
array( 'type' => 'module' )

plugins/optimization-detective/storage/class-od-rest-url-metrics-store-endpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function handle_rest_request( WP_REST_Request $request ) {
232232

233233
// Limit JSON payload size to safeguard against clients sending possibly malicious payloads much larger than allowed.
234234
$max_size = od_get_maximum_url_metric_size();
235-
$content_length = strlen( (string) wp_json_encode( $url_metric ) );
235+
$content_length = strlen( (string) wp_json_encode( $url_metric, JSON_UNESCAPED_SLASHES ) ); // Flags match with \OD_URL_Metrics_Post_Type::update_post().
236236
if ( $content_length > $max_size ) {
237237
return new WP_Error(
238238
'rest_content_too_large',

plugins/optimization-detective/storage/class-od-url-metrics-post-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static function ( OD_URL_Metric $a, OD_URL_Metric $b ): int {
241241

242242
$post_data['post_content'] = wp_json_encode(
243243
$url_metric_group_collection->get_flattened_url_metrics(),
244-
JSON_UNESCAPED_SLASHES // No need for escaping slashes since this JSON is not embedded in HTML.
244+
JSON_UNESCAPED_SLASHES // No need for escaping slashes or hex tags since this JSON is not embedded in HTML.
245245
);
246246
if ( ! is_string( $post_data['post_content'] ) ) {
247247
return new WP_Error( 'json_encode_error', json_last_error_msg() );

plugins/optimization-detective/storage/data.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function od_get_current_url(): string {
117117
* @return non-empty-string Slug.
118118
*/
119119
function od_get_url_metrics_slug( array $query_vars ): string {
120+
// TODO: The JSON_UNESCAPED_SLASHES flag could be used here, but beware this could invalidate URL Metrics. See <https://github.com/WordPress/performance/pull/1949>.
120121
return md5( (string) wp_json_encode( $query_vars ) );
121122
}
122123

@@ -238,6 +239,7 @@ static function ( $post ): ?array {
238239
*/
239240
$data = (array) apply_filters( 'od_current_url_metrics_etag_data', $data );
240241

242+
// TODO: The JSON_UNESCAPED_SLASHES flag could be used here.
241243
return md5( (string) wp_json_encode( $data ) );
242244
}
243245

plugins/optimization-detective/tests/test-detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function test_od_get_detection_script_returns_script( Closure $set_up, ar
224224
$this->assertStringContainsString( '<script type="module">', $script );
225225
$this->assertStringContainsString( 'async function load', $script );
226226
foreach ( $expected_exports as $key => $value ) {
227-
$this->assertStringContainsString( sprintf( '%s:%s', wp_json_encode( $key ), wp_json_encode( $value ) ), $script );
227+
$this->assertStringContainsString( sprintf( '%s:%s', wp_json_encode( $key, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), wp_json_encode( $value, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ), $script );
228228
}
229229
$this->assertStringContainsString( '"urlMetricHMAC":', $script );
230230
$this->assertSame( 1, preg_match( '/"webVitalsLibrarySrc":("[^"]+?")/', $script, $matches ) );

plugins/speculation-rules/plugin-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function plsr_print_speculation_rules(): void {
135135
}
136136

137137
wp_print_inline_script_tag(
138-
(string) wp_json_encode( plsr_get_speculation_rules() ),
138+
(string) wp_json_encode( plsr_get_speculation_rules(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
139139
array( 'type' => 'speculationrules' )
140140
);
141141
}

plugins/view-transitions/includes/theme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function plvt_load_view_transitions(): void {
352352

353353
$init_script = sprintf(
354354
'plvtInitViewTransitions( %s )',
355-
wp_json_encode( $config, JSON_FORCE_OBJECT )
355+
wp_json_encode( $config, JSON_FORCE_OBJECT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
356356
);
357357

358358
/*

plugins/web-worker-offloading/helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function plwwo_register_default_scripts( WP_Scripts $scripts ): void {
107107
'web-worker-offloading',
108108
sprintf(
109109
'window.partytown = {...(window.partytown || {}), ...%s};',
110-
wp_json_encode( plwwo_get_configuration() )
110+
wp_json_encode( plwwo_get_configuration(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
111111
),
112112
'before'
113113
);

plugins/web-worker-offloading/tests/test-web-worker-offloading.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function test_plwwo_register_default_scripts(): void {
8383
$before_data
8484
);
8585
$this->assertStringContainsString(
86-
wp_json_encode( $partytown_config ),
86+
wp_json_encode( $partytown_config, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
8787
$before_data
8888
);
8989
$this->assertEquals( file_get_contents( $partytown_lib . 'partytown.js' ), $after_data );

0 commit comments

Comments
 (0)