Skip to content

Commit 700ce56

Browse files
authored
Merge pull request #198 from mwllgr/master
Fix stateless Captcha flaws (infinite usages etc.)
2 parents b156128 + 9e5f8a6 commit 700ce56

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ return [
9696
'height' => 36,
9797
'quality' => 90,
9898
'math' => true, //Enable Math Captcha
99+
'expire' => 60, //Stateless/API captcha expiration
99100
],
100101
// ...
101102
];

Diff for: config/captcha.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'height' => 36,
99
'quality' => 90,
1010
'math' => false,
11+
'expire' => 60,
1112
],
1213
'math' => [
1314
'length' => 9,

Diff for: src/Captcha.php

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Intervention\Image\ImageManager;
2626
use Illuminate\Session\Store as Session;
2727
use Illuminate\Support\HtmlString;
28+
use Illuminate\Support\Facades\Cache;
2829

2930
/**
3031
* Class Captcha
@@ -177,6 +178,11 @@ class Captcha
177178
*/
178179
protected $fontsDirectory;
179180

181+
/**
182+
* @var string
183+
*/
184+
protected $expire;
185+
180186
/**
181187
* Constructor
182188
*
@@ -281,6 +287,10 @@ public function create(string $config = 'default', bool $api = false)
281287
$this->image->blur($this->blur);
282288
}
283289

290+
if ($api) {
291+
Cache::put('captcha_record_' . $generator['key'], $generator['value'], $this->expire);
292+
}
293+
284294
return $api ? [
285295
'sensitive' => $generator['sensitive'],
286296
'key' => $generator['key'],
@@ -472,6 +482,10 @@ public function check(string $value): bool
472482
*/
473483
public function check_api($value, $key): bool
474484
{
485+
if (!Cache::pull('captcha_record_' . $key)) {
486+
return false;
487+
}
488+
475489
return $this->hasher->check($value, $key);
476490
}
477491

0 commit comments

Comments
 (0)