jenkins-bot has submitted this change and it was merged.

Change subject: Add GatewayInitialFilter hook
......................................................................


Add GatewayInitialFilter hook

Allows some fraud filtering right after GatewayReady

If a custom filter is run multiple times during a single request,
the last run's score will count as its contribution towards the
overall gateway score.

Change-Id: I2572a26e4c341fa5c03ed582360b939a12768280
---
M DonationInterface.php
M extras/custom_filters/custom_filters.body.php
M extras/custom_filters/filters/functions/functions.body.php
M gateway_common/GatewayType.php
M gateway_common/gateway.adapter.php
M tests/Adapter/Worldpay/WorldpayTest.php
6 files changed, 63 insertions(+), 22 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/DonationInterface.php b/DonationInterface.php
index 072baea..aebd38e 100644
--- a/DonationInterface.php
+++ b/DonationInterface.php
@@ -657,7 +657,14 @@
 $wgDonationInterfaceCustomFiltersSrcRules = array();
 
 //Functions Filter globals
+//These functions fire when we trigger the antifraud hook.
+//Anything that needs access to API call results goes here.
+//FIXME: you need to copy all the initial functions here because
+//individual function scores don't persist like filter scores.
 $wgDonationInterfaceCustomFiltersFunctions = array();
+//These functions fire on GatewayReady, so all they can see is the
+//request and the session.
+$wgDonationInterfaceCustomFiltersInitialFunctions = array();
 
 //IP velocity filter globals
 $wgDonationInterfaceMemcacheHost = 'localhost';
@@ -889,13 +896,21 @@
 $wgSpecialPages['WorldpayGatewayResult'] = 'WorldpayGatewayResult';
 $wgDonationInterfaceGatewayAdapters[] = 'WorldpayAdapter';
 
-//Custom Filters hooks
 $wgHooks['GatewayReady'][] = array( 
'BannerHistoryLogIdProcessor::onGatewayReady' );
 
+// Custom Filters hooks
+$wgHooks['GatewayReady'][] = array( 
'Gateway_Extras_CustomFilters::onGatewayReady' );
 $wgHooks['GatewayValidate'][] = array( 
'Gateway_Extras_CustomFilters::onValidate' );
 
-$wgHooks['GatewayCustomFilter'][] = array( 
'Gateway_Extras_CustomFilters_Referrer::onFilter' );
-$wgHooks['GatewayCustomFilter'][] = array( 
'Gateway_Extras_CustomFilters_Source::onFilter' );
+// These filters are run on each request after collecting the data
+$wgHooks['GatewayInitialFilter'][] = array( 
'Gateway_Extras_CustomFilters_Referrer::onFilter' );
+$wgHooks['GatewayInitialFilter'][] = array( 
'Gateway_Extras_CustomFilters_Source::onFilter' );
+$wgHooks['GatewayInitialFilter'][] = array( 
'Gateway_Extras_CustomFilters_Functions::onInitialFilter' );
+
+// And these are run when we're about ready to charge the credit card.
+// Any test that costs us money should go here. It's ok to run simple
+// filters in both places - their score in the later run will replace
+// the first score.
 $wgHooks['GatewayCustomFilter'][] = array( 
'Gateway_Extras_CustomFilters_Functions::onFilter' );
 $wgHooks['GatewayCustomFilter'][] = array( 
'Gateway_Extras_CustomFilters_MinFraud::onFilter' );
 $wgHooks['GatewayCustomFilter'][] = array( 
'Gateway_Extras_CustomFilters_IP_Velocity::onFilter' );
diff --git a/extras/custom_filters/custom_filters.body.php 
b/extras/custom_filters/custom_filters.body.php
index 4e86dd8..c584d61 100644
--- a/extras/custom_filters/custom_filters.body.php
+++ b/extras/custom_filters/custom_filters.body.php
@@ -71,8 +71,6 @@
                $log_message = "\"$source added a score of $score\"";
                $this->fraud_logger->info( '"addRiskScore" ' . $log_message );
                $this->risk_score[$source] = $score;
-
-               $this->gateway_adapter->addRiskScore( $score );
        }
        
 
@@ -100,13 +98,16 @@
        /**
         * Run the transaction through the custom filters
         */
-       public function validate() {
+       public function validate( $hook ) {
                // expose a hook for custom filters
-               WmfFramework::runHooks( 'GatewayCustomFilter', array( 
$this->gateway_adapter, $this ) );
+               WmfFramework::runHooks( $hook, array( $this->gateway_adapter, 
$this ) );
+               $score = $this->getRiskScore();
+               $this->gateway_adapter->setRiskScore( $score );
                $localAction = $this->determineAction();
                $this->gateway_adapter->setValidationAction( $localAction );
 
-               $log_message = '"' . $localAction . "\"\t\"" . 
$this->getRiskScore() . "\"";
+               $log_message = '"' . $localAction . "\"\t\"" . $score . "\"";
+
                $this->fraud_logger->info( '"Filtered" ' . $log_message );
 
                $log_message = '"' . addslashes( json_encode( $this->risk_score 
) ) . '"';
@@ -129,7 +130,15 @@
                        return true;
                }
                $gateway_adapter->debugarray[] = 'custom filters onValidate 
hook!';
-               return self::singleton( $gateway_adapter )->validate();
+               return self::singleton( $gateway_adapter )->validate( 
'GatewayCustomFilter' );
+       }
+
+       static function onGatewayReady( GatewayType $gateway_adapter ) {
+               if ( !$gateway_adapter->getGlobal( 'EnableCustomFilters' ) ){
+                       return true;
+               }
+               $gateway_adapter->debugarray[] = 'custom filters onGatewayReady 
hook!';
+               return self::singleton( $gateway_adapter )->validate( 
'GatewayInitialFilter' );
        }
 
        static function singleton( GatewayType $gateway_adapter ) {
diff --git a/extras/custom_filters/filters/functions/functions.body.php 
b/extras/custom_filters/filters/functions/functions.body.php
index ce95614..0711983 100644
--- a/extras/custom_filters/filters/functions/functions.body.php
+++ b/extras/custom_filters/filters/functions/functions.body.php
@@ -24,11 +24,17 @@
        }
 
        /**
-        * @throws UnexpectedValueException
+        * @param $filterListGlobal
+        * @return bool
         */
-       public function filter() {
+       public function filter( $filterListGlobal ) {
 
-               $functions = $this->gateway_adapter->getGlobal( 
'CustomFiltersFunctions' );
+               if ( !$this->gateway_adapter->getGlobal( 
'EnableFunctionsFilter' ) ||
+                       !count( $this->gateway_adapter->getGlobal( 
$filterListGlobal ) ) ){
+                       return true;
+               }
+
+               $functions = $this->gateway_adapter->getGlobal( 
$filterListGlobal );
                foreach ( $functions as $function_name => $risk_score_modifier 
) {
                        //run the function specified, if it exists. 
                        if ( method_exists( $this->gateway_adapter, 
$function_name ) ) {
@@ -55,13 +61,20 @@
                GatewayType $gateway_adapter,
                Gateway_Extras_CustomFilters $custom_filter_object
        ) {
-
-               if ( !$gateway_adapter->getGlobal( 'EnableFunctionsFilter' ) ||
-                       !count( $gateway_adapter->getGlobal( 
'CustomFiltersFunctions' ) ) ){
-                       return true;
-               }
                $gateway_adapter->debugarray[] = 'functions onFilter hook!';
-               return self::singleton( $gateway_adapter, $custom_filter_object 
)->filter();
+               return self::singleton( $gateway_adapter, $custom_filter_object 
)->filter(
+                       'CustomFiltersFunctions'
+               );
+       }
+
+       static function onInitialFilter(
+               GatewayType $gateway_adapter,
+               Gateway_Extras_CustomFilters $custom_filter_object
+       ) {
+               $gateway_adapter->debugarray[] = 'functions onInitialFilter 
hook!';
+               return self::singleton( $gateway_adapter, $custom_filter_object 
)->filter(
+                       'CustomFiltersInitialFunctions'
+               );
        }
 
        static function singleton(
diff --git a/gateway_common/GatewayType.php b/gateway_common/GatewayType.php
index 2684972..252b1bd 100644
--- a/gateway_common/GatewayType.php
+++ b/gateway_common/GatewayType.php
@@ -282,11 +282,11 @@
        public function getDataConstraints( $field );
 
        /**
-        * Add the given amount to our fraud score
+        * Set the adapter's fraud score
         *
         * @param float $score
         */
-       public function addRiskScore( $score );
+       public function setRiskScore( $score );
 
        /**
         * Get the current HTTP request
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index 1db7fb9..9c431de 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -2325,8 +2325,8 @@
                }
        }
 
-       public function addRiskScore( $score ) {
-               $this->risk_score += $score;
+       public function setRiskScore( $score ) {
+               $this->risk_score = $score;
        }
 
        public function setValidationAction( $action, $reset = false ) {
diff --git a/tests/Adapter/Worldpay/WorldpayTest.php 
b/tests/Adapter/Worldpay/WorldpayTest.php
index 3e4d13f..e8ea824 100644
--- a/tests/Adapter/Worldpay/WorldpayTest.php
+++ b/tests/Adapter/Worldpay/WorldpayTest.php
@@ -290,6 +290,10 @@
         * Check to make sure we don't run antifraud filters (and burn a 
minfraud query) when we know the transaction has already failed
         */
        function testAntifraudNotPerformedOnGatewayError() {
+               global $wgHooks;
+               $hooksCopy = $wgHooks;
+               $hooksCopy['GatewayInitialFilter'] = array();
+               $this->setMwGlobals( array( 'wgHooks' => $hooksCopy ) );
                $options = $this->getDonorTestData( 'FR' ); //don't really 
care: We'll be using the dummy response directly.
 
                $gateway = $this->getFreshGatewayObject( $options );

-- 
To view, visit https://gerrit.wikimedia.org/r/289801
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2572a26e4c341fa5c03ed582360b939a12768280
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Cdentinger <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to