Skip to content

Commit ce03869

Browse files
authored
Merge pull request #187 from Automattic/fix/114-token-sanitization
2 parents dcc08a5 + e4d8324 commit ce03869

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

includes/class-syndication-wp-rest-client.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,16 @@ public static function display_settings( $site ) {
299299
}
300300

301301
public static function save_settings( $site_ID ) {
302-
303-
update_post_meta( $site_ID, 'syn_site_token', push_syndicate_encrypt( sanitize_text_field( $_POST['site_token'] ) ) );
304-
update_post_meta( $site_ID, 'syn_site_id', sanitize_text_field( $_POST['site_id'] ) );
305-
update_post_meta( $site_ID, 'syn_site_url', sanitize_text_field( $_POST['site_url'] ) );
302+
// Use wp_strip_all_tags() for the token instead of sanitize_text_field()
303+
// because sanitize_text_field() converts encoded octets (e.g., %B2) which
304+
// can break OAuth tokens. The token is encrypted before storage anyway.
305+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Token sanitized with wp_strip_all_tags.
306+
$token = isset( $_POST['site_token'] ) ? wp_strip_all_tags( wp_unslash( $_POST['site_token'] ) ) : '';
307+
update_post_meta( $site_ID, 'syn_site_token', push_syndicate_encrypt( $token ) );
308+
update_post_meta( $site_ID, 'syn_site_id', isset( $_POST['site_id'] ) ? sanitize_text_field( wp_unslash( $_POST['site_id'] ) ) : '' );
309+
update_post_meta( $site_ID, 'syn_site_url', isset( $_POST['site_url'] ) ? esc_url_raw( wp_unslash( $_POST['site_url'] ) ) : '' );
306310

307311
return true;
308-
309312
}
310313

311314
public function get_post( $ext_ID )

includes/class-syndication-wp-xmlrpc-client.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -566,22 +566,31 @@ public static function display_settings( $site ) {
566566
}
567567

568568
public static function save_settings( $site_ID ) {
569-
570-
$_POST['site_url'] = str_replace( '/xmlrpc.php', '', $_POST['site_url'] );
571-
572-
update_post_meta( $site_ID, 'syn_site_url', esc_url_raw( $_POST['site_url'] ) );
573-
update_post_meta( $site_ID, 'syn_site_username', sanitize_text_field( $_POST['site_username'] ) );
574-
update_post_meta( $site_ID, 'syn_site_password', push_syndicate_encrypt( sanitize_text_field( $_POST['site_password'] ) ) );
575-
576-
if( !filter_var( $_POST['site_url'], FILTER_VALIDATE_URL ) ) {
577-
add_filter('redirect_post_location', function($location) {
578-
return add_query_arg("message", 301, $location);
579-
});
569+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- URL sanitized with esc_url_raw below.
570+
$site_url = isset( $_POST['site_url'] ) ? str_replace( '/xmlrpc.php', '', wp_unslash( $_POST['site_url'] ) ) : '';
571+
$username = isset( $_POST['site_username'] ) ? sanitize_text_field( wp_unslash( $_POST['site_username'] ) ) : '';
572+
573+
// Use wp_strip_all_tags() for the password instead of sanitize_text_field()
574+
// because sanitize_text_field() converts encoded octets (e.g., %B2) which
575+
// can break passwords with special characters. The password is encrypted before storage anyway.
576+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Password sanitized with wp_strip_all_tags.
577+
$password = isset( $_POST['site_password'] ) ? wp_strip_all_tags( wp_unslash( $_POST['site_password'] ) ) : '';
578+
579+
update_post_meta( $site_ID, 'syn_site_url', esc_url_raw( $site_url ) );
580+
update_post_meta( $site_ID, 'syn_site_username', $username );
581+
update_post_meta( $site_ID, 'syn_site_password', push_syndicate_encrypt( $password ) );
582+
583+
if ( ! filter_var( $site_url, FILTER_VALIDATE_URL ) ) {
584+
add_filter(
585+
'redirect_post_location',
586+
function ( $location ) {
587+
return add_query_arg( 'message', 301, $location );
588+
}
589+
);
580590
return false;
581591
}
582592

583593
return true;
584-
585594
}
586595

587596
public function get_post( $ext_ID )

0 commit comments

Comments
 (0)