-
Notifications
You must be signed in to change notification settings - Fork 10.7k
[Fraud Protection] Do not track admin users #62813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
25c1551 to
c234da0
Compare
c234da0 to
4dd42e4
Compare
Testing GuidelinesHi @luizreis , Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed. Reminder: PR reviewers are required to document testing performed. This includes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Backwards Compatibility Review - Found 1 potential issue
Generated by Claude via this workflow run
plugins/woocommerce/src/Internal/FraudProtection/FraudProtectionController.php
Show resolved
Hide resolved
📝 WalkthroughWalkthroughThis pull request refactors fraud-protection event tracking across WooCommerce by introducing a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (4)
plugins/woocommerce/src/Internal/FraudProtection/FraudProtectionController.php (1)
163-170: Add missing@sinceannotation and return type hint.Per coding guidelines, the
@sinceannotation should be included with the current WooCommerce version. Additionally, for consistency withfeature_is_enabled(), consider adding theboolreturn type hint.♻️ Suggested improvement
/** * Check if the fraud protection is enabled and should track events for the current user. * + * `@since` 10.5.0 + * * `@return` bool True if fraud protection should track events for the current user, false otherwise. */ - public function should_track() { + public function should_track(): bool { return $this->feature_is_enabled() && ! current_user_can( 'manage_woocommerce' ); }plugins/woocommerce/src/Internal/FraudProtection/FraudProtectionDispatcher.php (1)
94-105: Log message could be more accurate.The debug log message says
"feature disabled"butshould_track()now returnsfalsefor two reasons: (1) feature is disabled, or (2) user is an admin. Consider updating the message for better debugging clarity.♻️ Suggested improvement
// Check if feature is enabled - fail-open if not. if ( ! $this->fraud_protection_controller->should_track() ) { FraudProtectionController::log( 'debug', sprintf( - 'Fraud protection event not dispatched (feature disabled): %s', + 'Fraud protection event not dispatched (tracking disabled or admin user): %s', $event_type ), array( 'event_type' => $event_type ) ); return; }plugins/woocommerce/includes/class-wc-ajax.php (1)
380-384: Avoid double container lookups in the tracking block (minor perf/readability).Cache
FraudProtectionController/CheckoutEventTrackerlocally to avoid multiplewc_get_container()->get(...)calls in the same hot path.Proposed diff
// Track checkout field update for fraud protection. - if ( wc_get_container()->get( FraudProtectionController::class )->should_track() ) { - wc_get_container()->get( CheckoutEventTracker::class ) - ->track_shortcode_checkout_field_update( isset( $_POST['post_data'] ) ? wp_unslash( $_POST['post_data'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - } + $fraud_protection = wc_get_container()->get( FraudProtectionController::class ); + if ( $fraud_protection->should_track() ) { + $checkout_event_tracker = wc_get_container()->get( CheckoutEventTracker::class ); + $checkout_event_tracker->track_shortcode_checkout_field_update( isset( $_POST['post_data'] ) ? wp_unslash( $_POST['post_data'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + }plugins/woocommerce/src/StoreApi/Utilities/CartController.php (1)
228-232: LGTM: correct gating for “don’t track admins”; consider caching container resolves.Proposed diff
// Track cart event for fraud protection. - if ( $product instanceof \WC_Product && wc_get_container()->get( FraudProtectionController::class )->should_track() ) { - wc_get_container()->get( CartEventTracker::class ) - ->track_cart_item_added( $cart_id, $this->get_product_id( $product ), (int) $request_quantity, $this->get_variation_id( $product ) ); - } + if ( $product instanceof \WC_Product ) { + $fraud_protection = wc_get_container()->get( FraudProtectionController::class ); + if ( $fraud_protection->should_track() ) { + $cart_event_tracker = wc_get_container()->get( CartEventTracker::class ); + $cart_event_tracker->track_cart_item_added( $cart_id, $this->get_product_id( $product ), (int) $request_quantity, $this->get_variation_id( $product ) ); + } + }
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. Test this pull request with WordPress Playground. Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit. |
luizreis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth checking with @vbelolapotkov as well, but given Tautvidas' comment:
OK for alpha, for later i'd maybe suggest to track but explicitly allow list admins in the session/transaction list for visibility
Would it make sense to stop tracking admin users for now, just to re-track them later? I'm looking at an angle that perhaps this PR could be simplified and already implement Tautvidas post-alpha suggestion of only whitelisting admins from being blocked, as (based on that comment) it'd be valuable to have that data.
I haven't fully looked at the code yet, but let me know if our MVP/GA plan is to never track admin users and I'll do a proper review.
Fraud Protection: Do Not Track Admin Users
Summary
Modifies the fraud protection system to exclude admin users from tracking. Previously, all users were tracked when fraud protection was enabled. Now, only non-admin users (those without the
manage_woocommercecapability) are tracked.Why This Change?
Admin users performing testing, debugging, or routine store management should not be tracked as potential fraud risks. This reduces noise in fraud detection data and focuses tracking on actual customer behavior.
Test Plan
Manual Testing Instructions
Prerequisites
Test Scenario 1: Admin User Not Tracked
Test Scenario 2: Regular User Tracked
Test Scenario 3: Guest User Tracked
Test Scenario 4: Feature Disabled
Verification Points
For scenarios where tracking is expected, verify events are dispatched at these points:
For admin users, verify these events are not dispatched even when fraud protection is enabled.
Milestone
Changelog entry
Changelog Entry Details
Significance
Type
Message
Changelog Entry Comment
Comment