Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
devtoolboxuk committed Jun 2, 2019
1 parent 83def54 commit 0db193e
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 5 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,46 @@ $this->cerberus = new Cerberus();
$cerberus->setOptions($this->getOptions());
```

## Example - Detection of a dodgy website registration
Also see tests/test-registration.php

```php
function testDodgyRegistration()
{

$cerberus = new CerberusService();
$cerberus->setOptions($this->getOptions());

$login_array = [
'email' => 'rob@shotmail.ru',
'name' => 'Visit my website http://www.doajob.org?redirect=https://www.google.com',
'address' => 'Some Street',
'postcode' => 'GL1 1AA',
'country' => 'MX',
];

$detection = $cerberus
->pushHandler($this->createLoginStringHandler('Name', $login_array['name']))
->pushHandler($this->createLoginStringHandler('Address', $login_array['address']))
->pushHandler(new EmailHandler($login_array['email']))
->pushHandler(new CountryHandler($login_array['country']));

$detection->getScore(); //Returns a Score
$detection->getResult(); //Returns a result

}

private function createLoginStringHandler($name, $data)
{
$handler = new DefaultHandler($name, $data);
$handler->pushWrapper(new HtmlWrapper());
$handler->pushWrapper(new UrlWrapper());
$handler->pushWrapper(new XssWrapper());
return $handler;
}

```

## Help Support This Project

[![Help Support This Project](https://raw.githubusercontent.com/devtoolboxuk/cerberus/master/assets/buy-me-a-coffee-button.png)](https://Ko-fi.com/devtoolboxuk)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": ">=5.4.16",
"devtoolboxuk/soteria": "^2.1.0",
"devtoolboxuk/soteria": "^2.1.6",
"devtoolboxuk/ip-address": "^1.0.1",
"devtoolboxuk/lists": "^1.0.0"
},
Expand Down
5 changes: 5 additions & 0 deletions src/BaseOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function getOptions()
'score' => 1,
'params' => '',
],
'ReCaptcha' => [
'active' => 1,
'score' => 1,
'params' => '1.0:0|0.0:100',
],
'StringLength' => [
'active' => 1,
'score' => 1,
Expand Down
15 changes: 15 additions & 0 deletions src/Handlers/HtmlHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace devtoolboxuk\cerberus\Handlers;

use devtoolboxuk\cerberus\Wrappers\HtmlWrapper;

class HtmlHandler extends Handler
{
public function __construct($value = '')
{
parent::__construct($value);
$this->setName(str_replace(__NAMESPACE__ . '\\', '', __CLASS__));
$this->pushWrapper(new HtmlWrapper());
}
}
15 changes: 15 additions & 0 deletions src/Handlers/ReCaptchaHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace devtoolboxuk\cerberus\Handlers;

use devtoolboxuk\cerberus\Wrappers\ReCaptchaWrapper;

class ReCaptchaHandler extends Handler
{
public function __construct($value = '')
{
parent::__construct($value);
$this->setName(str_replace(__NAMESPACE__ . '\\', '', __CLASS__));
$this->pushWrapper(new ReCaptchaWrapper());
}
}
15 changes: 15 additions & 0 deletions src/Handlers/UrlHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace devtoolboxuk\cerberus\Handlers;

use devtoolboxuk\cerberus\Wrappers\UrlWrapper;

class UrlHandler extends Handler
{
public function __construct($value = '')
{
parent::__construct($value);
$this->setName(str_replace(__NAMESPACE__ . '\\', '', __CLASS__));
$this->pushWrapper(new UrlWrapper());
}
}
2 changes: 1 addition & 1 deletion src/Wrappers/EmailWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function process()
{
$this->initWrapper($this->setLocalName());

$sanitise = $this->soteria->sanitise(true);
$sanitise = $this->soteria->sanitise();
$sanitise->disinfect($this->getReference(), 'email');

if (!$sanitise->result()->isValid()) {
Expand Down
19 changes: 19 additions & 0 deletions src/Wrappers/ReCaptchaWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace devtoolboxuk\cerberus\Wrappers;

class ReCaptchaWrapper extends Wrapper
{

public function process()
{
$this->initWrapper($this->setLocalName());

}

private function setLocalName()
{
$name = str_replace(__NAMESPACE__ . '\\', '', __CLASS__);
return str_replace('Wrapper', '', $name);
}
}
2 changes: 1 addition & 1 deletion src/Wrappers/StringLengthWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function detect()
*/
private function getStringLength($data = [])
{
$sanitise = $this->soteria->sanitise(true);
$sanitise = $this->soteria->sanitise();
$length = 0;

if (isset($data[1])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Wrappers/UrlWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function process()
{
$this->initWrapper($this->setLocalName());

$urlSanitise = $this->soteria->sanitise(true);
$urlSanitise = $this->soteria->sanitise();
$urlSanitise->removeUrl($this->getReference());

if (!$urlSanitise->result()->isValid()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Wrappers/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function hasRealScore()

protected function overRideScore($data)
{
$sanitise = $this->soteria->sanitise(true);
$sanitise = $this->soteria->sanitise();
$this->score = $this->getRealScore();

if (isset($data[1])) {
Expand Down
128 changes: 128 additions & 0 deletions tests/test-registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace devtoolboxuk\cerberus;

use devtoolboxuk\cerberus\Handlers\CountryHandler;
use devtoolboxuk\cerberus\Handlers\DefaultHandler;
use devtoolboxuk\cerberus\Handlers\DifferentCountryHandler;
use devtoolboxuk\cerberus\Handlers\EmailHandler;
use devtoolboxuk\cerberus\Wrappers\EmailWrapper;
use devtoolboxuk\cerberus\Wrappers\HtmlWrapper;
use devtoolboxuk\cerberus\Wrappers\UrlWrapper;
use devtoolboxuk\cerberus\Wrappers\XssWrapper;
use PHPUnit\Framework\TestCase;

class RegistrationTest extends TestCase
{

private $options = [];

function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->options = $this->getTestData();
}

private function getTestData()
{
/** @noinspection PhpIncludeInspection */
return include __DIR__ . '/options.php';
}

function testDodgyRegistration()
{

$cerberus = new CerberusService();
$cerberus->setOptions($this->getOptions());

$login_array = [
'email' => 'rob@shotmail.ru',
'name' => 'Visit my website http://www.doajob.org?redirect=https://www.google.com',
'address' => 'Some Street',
'postcode' => 'GL1 1AA',
'country' => 'MX',
];

$detection = $cerberus
->pushHandler($this->createLoginStringHandler('Name', $login_array['name']))
->pushHandler($this->createLoginStringHandler('Address', $login_array['address']))
->pushHandler(new EmailHandler($login_array['email']))
->pushHandler(new CountryHandler($login_array['country']));

$this->assertEquals(59, $detection->getScore());
$this->assertEquals('{"Country":12,"DisposableEmail":"46","Url":1}', $detection->getResult());

}

private function getOptions()
{
return $this->options;
}

private function createLoginStringHandler($name, $data)
{
$handler = new DefaultHandler($name, $data);
$handler->pushWrapper(new HtmlWrapper());
$handler->pushWrapper(new UrlWrapper());
$handler->pushWrapper(new XssWrapper());
return $handler;
}

function testIffyCountryRegistration()
{

$cerberus = new CerberusService();
$cerberus->setOptions($this->getOptions());

$login_array = [
'email' => 'test@hotmail.com',
'name' => 'Rob',
'address' => 'Some Street',
'postcode' => 'GL1 1AA',
'country' => 'MX',
];

$detection = $cerberus
->pushHandler($this->createLoginStringHandler('Name', $login_array['name']))
->pushHandler($this->createLoginStringHandler('Address', $login_array['address']))
->pushHandler(new EmailHandler($login_array['email']))
->pushHandler(new CountryHandler($login_array['country']));

$this->assertEquals(12, $detection->getScore());
$this->assertEquals('{"Country":12}', $detection->getResult());

// print_r($detection->toArray());
// exit();

}

function testValidRegistration()
{

$cerberus = new CerberusService();
$cerberus->setOptions($this->getOptions());

$login_array = [
'email' => 'test@hotmail.com',
'name' => 'Rob',
'address' => 'Some Street',
'postcode' => 'GL1 1AA',
'country' => 'GB',
];

$detection = $cerberus
->pushHandler($this->createLoginStringHandler('Name', $login_array['name']))
->pushHandler($this->createLoginStringHandler('Address', $login_array['address']))
->pushHandler(new EmailHandler($login_array['email']))
->pushHandler(new CountryHandler($login_array['country']));

$this->assertEquals(0, $detection->getScore());
$this->assertEquals('[]', $detection->getResult());

// print_r($detection->toArray());
// exit();

}


}

0 comments on commit 0db193e

Please sign in to comment.