Skip to content
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

Validation can succeed only on first attempt #322

Open
plai2010 opened this issue Dec 11, 2024 · 0 comments
Open

Validation can succeed only on first attempt #322

plai2010 opened this issue Dec 11, 2024 · 0 comments

Comments

@plai2010
Copy link

Validation succeeds only if the correct answer is given to the first check() or check_api() call. If a user enters an incorrect value, they cannot try again; instead a new captcha must be generated. I think that is because we use Cache::pull() rather than Cache::get() in the check code:

if (!Cache::pull($this->get_cache_key($key))) {
    return false;
}

The problem can be illustrated with artisan tinker running this:

$testCaptcha = function(bool $positive) {
	echo ">>> Testing Captcha ...\n";
	$captcha = app('captcha');

	$challenge = (object)$captcha->create(api:true);
	$key = $challenge->key;
	echo "Challenge Key: {$key}\n";

	$ans = implode('', Cache::get('captcha_'.md5($key)));
	echo "Answer: $ans\n";

	$attempts = $positive ? [ $ans ] : [
		"bad-{$ans}",
		$ans,
	];

	foreach ($attempts as $try) {
		$label = $try == $ans ? 'correct' : 'incorrect';
		echo "Attempting {$label} '{$try}' ... ";
		$res = $captcha->check_api($try, $key);
		echo json_encode($res), "\n";
	}
};

$testCaptcha(true);
$testCaptcha(false);

Example output follows:

Psy Shell v0.12.4 (PHP 8.3.13 — cli) by Justin Hileman
>>> Testing Captcha ...
Challenge Key: $2y$12$p7oNGQVf9zp3UxVjfImhz.Hf2RKvEcRuI6/FdWeX2JBuHjMBH89eu
Answer: 410204101
Attempting correct '410204101' ... true
>>> Testing Captcha ...
Challenge Key: $2y$12$luzJOGY3ULHhHx9ZP.W8BeGH.BwXylczwZO8XthleQFwoI2GKrumy
Answer: 906279280
Attempting incorrect 'bad-906279280' ... false
Attempting correct '906279280' ... false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant