Skip to content

Commit 32584da

Browse files
gagnieraytrasher
andcommitted
Throttle public form submissions
Co-authored-by: Johan Cwiklinski <trasher@x-tnd.be>
1 parent 44f2f06 commit 32584da

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎lib/GaletteHelloasso/Controllers/HelloassoController.php‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use Analog\Analog;
1414
use DI\Attribute\Inject;
1515
use Galette\Controllers\AbstractPluginController;
16+
use Galette\Core\AuthThrottle;
1617
use Galette\Core\History;
18+
use Galette\Core\Login;
1719
use Galette\Entity\Adherent;
1820
use Galette\Entity\Contribution;
1921
use Galette\Entity\ContributionsTypes;
@@ -45,14 +47,22 @@ class HelloassoController extends AbstractPluginController
4547
#[Inject]
4648
protected Helloasso $helloasso;
4749

50+
private const string THROTTLE_SCOPE = 'helloasso-payment';
51+
4852
/**
4953
* Main form
5054
*/
51-
public function form(Response $response): Response
55+
public function form(Response $response, AuthThrottle $throttle, Login $login): Response
5256
{
5357
$helloasso = $this->helloasso;
5458

5559
$current_url = $this->preferences->getURL();
60+
$address = $helloasso->getUserIPAddress();
61+
62+
// When a member is logged there is nothing left to hold against its address
63+
if ($login->isLogged() && $address !== '') {
64+
$throttle->clearEvent(self::THROTTLE_SCOPE, $address);
65+
}
5666

5767
$params = [
5868
'helloasso' => $helloasso,
@@ -90,12 +100,38 @@ public function form(Response $response): Response
90100
/**
91101
* Checkout form
92102
*/
93-
public function formCheckout(Request $request, Response $response): Response
103+
public function formCheckout(Request $request, Response $response, AuthThrottle $throttle, Login $login): Response
94104
{
95105
$helloasso_request = $request->getParsedBody();
96106
$helloasso = $this->helloasso;
97107
$adherent = new Adherent($this->zdb);
98108

109+
$address = $helloasso->getUserIPAddress();
110+
111+
// Throttle public form submissions
112+
if (!$login->isLogged() && $address !== '') {
113+
// Asked before anything is done, so that a caller being refused costs
114+
// nothing but a lookup
115+
$delay = $throttle->getDelayForEvent(self::THROTTLE_SCOPE, $address);
116+
if ($delay > 0) {
117+
$this->flash->addMessage(
118+
'error_detected',
119+
str_replace(
120+
'%seconds',
121+
(string)$delay,
122+
_T("Too many requests. Please try again in %seconds seconds.", "helloasso")
123+
)
124+
);
125+
return $response
126+
->withStatus(301)
127+
->withHeader('Location', $this->routeparser->urlFor('helloasso_form'));
128+
}
129+
130+
// The attempt is counted, not its outcome: what is limited is how many
131+
// times the form can be submitted, whatever it answers
132+
$throttle->recordEvent(self::THROTTLE_SCOPE, $address, 120, 3600);
133+
}
134+
99135
// Only reasons proposed to the current user can be paid
100136
$item_id = (int)($helloasso_request['item_id'] ?? 0);
101137
$helloasso_amounts = $helloasso->getAmounts($this->login);

‎lib/GaletteHelloasso/Helloasso.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
namespace GaletteHelloasso;
1212

1313
use Analog\Analog;
14+
use Galette\Core\AuthThrottle;
1415
use Galette\Core\Db;
16+
use Galette\Core\History;
1517
use Galette\Core\Login;
1618
use Galette\Core\Preferences;
1719
use Galette\Entity\ContributionsTypes;
@@ -733,4 +735,14 @@ public function unsetInactives(): void
733735
{
734736
$this->inactives = [];
735737
}
738+
739+
/**
740+
* Get the user IP address
741+
*/
742+
public function getUserIPAddress(): string
743+
{
744+
// History resolves the address the way the rest of Galette does, honouring
745+
// GALETTE_X_FORWARDED_FOR_INDEX when the site sits behind a proxy
746+
return AuthThrottle::normalizeAddress(History::findUserIPAddress());
747+
}
736748
}

0 commit comments

Comments
 (0)