Skip to content

Commit

Permalink
Font Library: remove insecure properties (#56230)
Browse files Browse the repository at this point in the history
* Add additional sanitization to fonts.

* Add test case to remove insecure font family.

* Format php.
  • Loading branch information
jffng authored Nov 18, 2023
1 parent bb8adcd commit 2969b59
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,17 @@ private function sanitize() {
'version' => '2',
'settings' => array(
'typography' => array(
'fontFamilies' => array( $this->data ),
'fontFamilies' => array(
'custom' => array(
$this->data,
),
),
),
),
);
// Creates a new WP_Theme_JSON object with the new fonts to
// leverage sanitization and validation.
$fonts_json = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $fonts_json );
$theme_json = new WP_Theme_JSON_Gutenberg( $fonts_json );
$theme_data = $theme_json->get_data();
$sanitized_font = ! empty( $theme_data['settings']['typography']['fontFamilies'] )
Expand Down
46 changes: 46 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,52 @@ public function test_remove_invalid_element_pseudo_selectors() {
$this->assertEqualSetsWithIndex( $expected, $actual );
}

public function test_remove_invalid_font_family_settings() {
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'typography' => array(
'fontFamilies' => array(
'custom' => array(
array(
'name' => 'Open Sans',
'slug' => 'open-sans',
'fontFamily' => '"Open Sans", sans-serif</style><script>alert("xss")</script>',
),
array(
'name' => 'Arial',
'slug' => 'arial',
'fontFamily' => 'Arial, serif',
),
),
),
),
),
),
true
);

$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'typography' => array(
'fontFamilies' => array(
'custom' => array(
array(
'name' => 'Arial',
'slug' => 'arial',
'fontFamily' => 'Arial, serif',
),
),
),
),
),
);

$this->assertEqualSetsWithIndex( $expected, $actual );
}

public function test_get_element_class_name_button() {
$expected = 'wp-element-button';
$actual = WP_Theme_JSON_Gutenberg::get_element_class_name( 'button' );
Expand Down

0 comments on commit 2969b59

Please sign in to comment.