Skip to content

Commit 19c0808

Browse files
vahidkay-metameta-codesync[bot]
authored andcommitted
Updated the ajax calls to check if the caller is a legit user (#3647)
Summary: ## Description Added extra checks to ensure the plugin ajax calls are only made by the admin user. ### Type of change Please delete options that are not relevant - Fix (non-breaking change which fixes an issue) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas, if any. - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors. - [x] I followed general Pull Request best practices. Meta employees to follow this [wiki]([url](https://fburl.com/wiki/2cgfduwc)). - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes. - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality. - [x] I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this [wiki]([url](https://fburl.com/wiki/nhx73tgs)). ## Changelog entry Fix - Updated the ajax calls to ensure caller is legit Pull Request resolved: #3647 Test Plan: 2 things need to be tested: - The affected actions ( product sync, coupons sync, shipping profile sync, closing banners ) can be called by an admin user - The relevant ajax calls cannot be called by a non-admin user: -- wp_ajax_wc_facebook_opt_out_of_sync -- wp_ajax_wc_banner_close_action -- wp_ajax_wc_facebook_sync_all_products -- wp_ajax_wc_banner_post_update_close_action -- wp_ajax_wc_banner_post_update__master_sync_off_close_action -- wp_ajax_wc_facebook_product_set_banner_closed Reviewed By: rafael-curran Differential Revision: D83840123 Pulled By: vahidkay-meta fbshipit-source-id: 4d3cc6510bd9b39c4844e95288d4422cfeca3343
1 parent f64fbce commit 19c0808

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

includes/AJAX.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ function ( $name, $slug ) use ( $term ) {
135135
* @since 2.0.0
136136
*/
137137
public function sync_products() {
138+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( Shops::ACTION_SYNC_PRODUCTS ) ) {
139+
wp_send_json_error( 'Permission denied' );
140+
}
138141
// Allow opt-out of full batch-API sync, for example if store has a large number of products.
139142
if ( ! facebook_for_woocommerce()->get_integration()->allow_full_batch_api_sync() ) {
140143
wp_send_json_error( __( 'Full product sync disabled by filter.', 'facebook-for-woocommerce' ) );
141144
return;
142145
}
143146

144-
check_admin_referer( Product_Sync::ACTION_SYNC_PRODUCTS, 'nonce' );
145-
146147
try {
147148
facebook_for_woocommerce()->get_products_sync_handler()->create_or_update_all_products();
148149
wp_send_json_success();
@@ -159,8 +160,9 @@ public function sync_products() {
159160
* @since 3.5.0
160161
*/
161162
public function sync_coupons() {
162-
check_admin_referer( Shops::ACTION_SYNC_COUPONS, 'nonce' );
163-
163+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( Shops::ACTION_SYNC_COUPONS ) ) {
164+
wp_send_json_error( 'Permission denied' );
165+
}
164166
try {
165167
facebook_for_woocommerce()->feed_manager->get_feed_instance( 'promotions' )->regenerate_feed();
166168
wp_send_json_success();
@@ -177,8 +179,9 @@ public function sync_coupons() {
177179
* @since 3.5.0
178180
*/
179181
public function sync_shipping_profiles() {
180-
check_admin_referer( Shops::ACTION_SYNC_SHIPPING_PROFILES, 'nonce' );
181-
182+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( Shops::ACTION_SYNC_SHIPPING_PROFILES ) ) {
183+
wp_send_json_error( 'Permission denied' );
184+
}
182185
try {
183186
facebook_for_woocommerce()->feed_manager->get_feed_instance( 'shipping_profiles' )->regenerate_feed();
184187
wp_send_json_success();
@@ -195,7 +198,9 @@ public function sync_shipping_profiles() {
195198
* @since 3.5.0
196199
*/
197200
public function sync_navigation_menu() {
198-
check_admin_referer( Shops::ACTION_SYNC_NAVIGATION_MENU, 'nonce' );
201+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( Shops::ACTION_SYNC_NAVIGATION_MENU ) ) {
202+
wp_send_json_error( 'Permission denied' );
203+
}
199204

200205
try {
201206
facebook_for_woocommerce()->feed_manager->get_feed_instance( 'navigation_menu' )->regenerate_feed();
@@ -214,7 +219,9 @@ public function sync_navigation_menu() {
214219
* @since 2.0.0
215220
*/
216221
public function get_sync_status() {
217-
check_admin_referer( Product_Sync::ACTION_GET_SYNC_STATUS, 'nonce' );
222+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( Product_Sync::ACTION_GET_SYNC_STATUS ) ) {
223+
wp_send_json_error( 'Permission denied' );
224+
}
218225

219226
$remaining_products = 0;
220227

includes/Handlers/PluginRender.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,29 @@ public static function plugin_updated_banner() {
203203
}
204204

205205
public static function opt_out_of_sync_clicked() {
206-
check_admin_referer( self::ACTION_OPT_OUT_OF_SYNC, 'nonce' );
206+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( self::ACTION_OPT_OUT_OF_SYNC ) ) {
207+
wp_send_json_error( 'Permission denied' );
208+
}
209+
207210
$latest_date = gmdate( 'Y-m-d H:i:s' );
208211
update_option( self::MASTER_SYNC_OPT_OUT_TIME, $latest_date );
209212
wp_send_json_success( 'Opted out successfully' );
210213
}
211214

212215
public static function sync_all_clicked() {
213-
check_admin_referer( self::ACTION_SYNC_BACK_IN, 'nonce' );
216+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( self::ACTION_SYNC_BACK_IN ) ) {
217+
wp_send_json_error( 'Permission denied' );
218+
}
219+
214220
update_option( self::MASTER_SYNC_OPT_OUT_TIME, '' );
215221
wp_send_json_success( 'Synced all in successfully' );
216222
}
217223

218224
public static function product_set_banner_closed() {
219-
check_admin_referer( self::ACTION_PRODUCT_SET_BANNER_CLOSED, 'nonce' );
220-
check_ajax_referer( self::ACTION_PRODUCT_SET_BANNER_CLOSED, 'nonce' );
225+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( self::ACTION_PRODUCT_SET_BANNER_CLOSED ) ) {
226+
wp_send_json_error( 'Permission denied' );
227+
}
228+
221229
set_transient( 'fb_product_set_banner_dismissed', true );
222230
}
223231

@@ -226,7 +234,10 @@ public static function product_set_banner_closed() {
226234
* after a week
227235
*/
228236
public static function reset_upcoming_version_banners() {
229-
check_admin_referer( self::ACTION_CLOSE_BANNER, 'nonce' );
237+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( self::ACTION_CLOSE_BANNER ) ) {
238+
wp_send_json_error( 'Permission denied' );
239+
}
240+
230241
set_transient( 'upcoming_woo_all_products_banner_hide', true, 7 * DAY_IN_SECONDS );
231242
}
232243

@@ -236,7 +247,10 @@ public static function reset_upcoming_version_banners() {
236247
* NOTE: We are doing this because anyway we will remove this in cleanup post : 3.5.3
237248
*/
238249
public static function reset_plugin_updated_successfully_banner() {
239-
check_admin_referer( self::ACTION_CLOSE_BANNER, 'nonce' );
250+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( self::ACTION_CLOSE_BANNER ) ) {
251+
wp_send_json_error( 'Permission denied' );
252+
}
253+
240254
set_transient( 'plugin_updated_banner_hide', true, 12 * MONTH_IN_SECONDS );
241255
}
242256

@@ -245,7 +259,10 @@ public static function reset_plugin_updated_successfully_banner() {
245259
* But this will keep showing every week fortnight if user not synced in
246260
*/
247261
public static function reset_plugin_updated_successfully_but_master_sync_off_banner() {
248-
check_admin_referer( self::ACTION_CLOSE_BANNER, 'nonce' );
262+
if ( ! \WC_Facebookcommerce_Utils::is_legit_ajax_call( self::ACTION_CLOSE_BANNER ) ) {
263+
wp_send_json_error( 'Permission denied' );
264+
}
265+
249266
set_transient( 'plugin_updated_with_master_sync_off_banner_hide', true, 2 * WEEK_IN_SECONDS );
250267
}
251268

includes/fbutils.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,17 @@ public static function is_admin_user() {
443443
return current_user_can( 'manage_woocommerce' );
444444
}
445445

446+
/**
447+
* Checks if the ajax caller is admin and the call is stemming from an active admin session.
448+
*
449+
* @param string $action
450+
* @param string $nonce
451+
* @return bool
452+
*/
453+
public static function is_legit_ajax_call( $action, $nonce = 'nonce' ) {
454+
return self::is_admin_user() && check_ajax_referer( $action, $nonce );
455+
}
456+
446457
/**
447458
* Returns whether AJAX permissions are valid.
448459
*

0 commit comments

Comments
 (0)