Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit d9fdda2

Browse files
mikejolleyopr
authored andcommitted
Replace sanitization functions to enforce string values (#10242)
1 parent cd76f58 commit d9fdda2

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

src/StoreApi/Schemas/V1/AbstractAddressSchema.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ public function sanitize_callback( $address, $request, $param ) {
9292
$validation_util = new ValidationUtils();
9393

9494
$address = array_merge( array_fill_keys( array_keys( $this->get_properties() ), '' ), (array) $address );
95-
$address['country'] = wc_strtoupper( wc_clean( wp_unslash( $address['country'] ) ) );
96-
$address['first_name'] = wc_clean( wp_unslash( $address['first_name'] ) );
97-
$address['last_name'] = wc_clean( wp_unslash( $address['last_name'] ) );
98-
$address['company'] = wc_clean( wp_unslash( $address['company'] ) );
99-
$address['address_1'] = wc_clean( wp_unslash( $address['address_1'] ) );
100-
$address['address_2'] = wc_clean( wp_unslash( $address['address_2'] ) );
101-
$address['city'] = wc_clean( wp_unslash( $address['city'] ) );
102-
$address['state'] = $validation_util->format_state( wc_clean( wp_unslash( $address['state'] ) ), $address['country'] );
103-
$address['postcode'] = $address['postcode'] ? wc_format_postcode( wc_clean( wp_unslash( $address['postcode'] ) ), $address['country'] ) : '';
104-
$address['phone'] = wc_clean( wp_unslash( $address['phone'] ) );
95+
$address['country'] = wc_strtoupper( sanitize_text_field( wp_unslash( $address['country'] ) ) );
96+
$address['first_name'] = sanitize_text_field( wp_unslash( $address['first_name'] ) );
97+
$address['last_name'] = sanitize_text_field( wp_unslash( $address['last_name'] ) );
98+
$address['company'] = sanitize_text_field( wp_unslash( $address['company'] ) );
99+
$address['address_1'] = sanitize_text_field( wp_unslash( $address['address_1'] ) );
100+
$address['address_2'] = sanitize_text_field( wp_unslash( $address['address_2'] ) );
101+
$address['city'] = sanitize_text_field( wp_unslash( $address['city'] ) );
102+
$address['state'] = $validation_util->format_state( sanitize_text_field( wp_unslash( $address['state'] ) ), $address['country'] );
103+
$address['postcode'] = $address['postcode'] ? wc_format_postcode( sanitize_text_field( wp_unslash( $address['postcode'] ) ), $address['country'] ) : '';
104+
$address['phone'] = sanitize_text_field( wp_unslash( $address['phone'] ) );
105105
return $address;
106106
}
107107

src/StoreApi/Schemas/V1/BillingAddressSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function get_properties() {
5454
*/
5555
public function sanitize_callback( $address, $request, $param ) {
5656
$address = parent::sanitize_callback( $address, $request, $param );
57-
$address['email'] = wc_clean( wp_unslash( $address['email'] ) );
57+
$address['email'] = sanitize_text_field( wp_unslash( $address['email'] ) );
5858
return $address;
5959
}
6060

tests/php/StoreApi/Routes/Checkout.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
/**
2121
* Checkout Controller Tests.
22+
*
23+
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_print_r, WooCommerce.Commenting.CommentHooks.MissingHookComment
2224
*/
2325
class Checkout extends MockeryTestCase {
2426
/**
@@ -385,4 +387,50 @@ public function test_checkout_force_create_account() {
385387
$customer = get_user_by( 'id', $data['customer_id'] );
386388
$this->assertEquals( $customer->user_email, '[email protected]' );
387389
}
390+
391+
/**
392+
* Test account creation options.
393+
*/
394+
public function test_checkout_invalid_address_data() {
395+
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
396+
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
397+
$request->set_body_params(
398+
array(
399+
'billing_address' => (object) array(
400+
'first_name' => 'test',
401+
'last_name' => array(
402+
'invalid' => 'invalid_data',
403+
),
404+
'company' => '',
405+
'address_1' => 'test',
406+
'address_2' => '',
407+
'city' => 'test',
408+
'state' => '',
409+
'postcode' => 'cb241ab',
410+
'country' => 'GB',
411+
'phone' => '',
412+
'email' => '[email protected]',
413+
),
414+
'shipping_address' => (object) array(
415+
'first_name' => 'test',
416+
'last_name' => 'test',
417+
'company' => '',
418+
'address_1' => 'test',
419+
'address_2' => '',
420+
'city' => 'test',
421+
'state' => '',
422+
'postcode' => 'cb241ab',
423+
'country' => 'GB',
424+
'phone' => '',
425+
),
426+
'payment_method' => 'bacs',
427+
)
428+
);
429+
430+
$response = rest_get_server()->dispatch( $request );
431+
$status = $response->get_status();
432+
$data = $response->get_data();
433+
434+
$this->assertEquals( 400, $status, print_r( $data, true ) );
435+
}
388436
}

0 commit comments

Comments
 (0)