CaptchaSessionReuseAttackTestCase.php in CAPTCHA 8
File
tests/src/Functional/CaptchaSessionReuseAttackTestCase.php
View source
<?php
namespace Drupal\Tests\captcha\Functional;
class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
protected function assertCaptchaSessionIdReuseAttackDetection() {
$this
->assertSession()
->pageTextContains(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'CAPTCHA response should flagged as wrong.', 'CAPTCHA');
}
public function testCaptchaSessionReuseAttackDetectionOnCommentPreview() {
$node = $this
->drupalCreateNode();
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Test');
$this
->config('captcha.settings')
->set('persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE)
->save();
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid = $this
->getCaptchaSidFromForm();
$captcha_token = $this
->getCaptchaTokenFromForm();
$solution = "Test 123";
$edit = $this
->getCommentFormValues();
$edit['captcha_response'] = $solution;
$this
->submitForm($edit, 'Preview');
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(FALSE);
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$edit = $this
->getCommentFormValues();
$this
->assertSession()
->hiddenFieldExists("captcha_sid")
->setValue((string) $captcha_sid);
$this
->assertSession()
->hiddenFieldExists("captcha_token")
->setValue((string) $captcha_token);
$edit['captcha_response'] = $solution;
$this
->submitForm($edit, 'Preview');
$this
->assertCaptchaSessionIdReuseAttackDetection();
$this
->assertCaptchaPresence(TRUE);
}
public function testCaptchaSessionReuseAttackDetectionOnNodeForm() {
captcha_set_form_id_setting('node_page_form', 'captcha/Test');
$this
->config('captcha.settings')
->set('persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE)
->save();
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('node/add/page');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid = $this
->getCaptchaSidFromForm();
$captcha_token = $this
->getCaptchaTokenFromForm();
$solution = "Test 123";
$edit = $this
->getNodeFormValues();
$edit['captcha_response'] = $solution;
$this
->submitForm($edit, 'Preview');
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(FALSE);
$this
->drupalGet('node/add/page');
$this
->assertCaptchaPresence(TRUE);
$edit = $this
->getNodeFormValues();
$this
->assertSession()
->hiddenFieldExists("captcha_sid")
->setValue((string) $captcha_sid);
$this
->assertSession()
->hiddenFieldExists("captcha_token")
->setValue((string) $captcha_token);
$edit['captcha_response'] = $solution;
$this
->submitForm($edit, 'Preview');
$this
->assertCaptchaSessionIdReuseAttackDetection();
$this
->assertCaptchaPresence(TRUE);
}
public function testCaptchaSessionReuseAttackDetectionOnLoginForm() {
captcha_set_form_id_setting('user_login_form', 'captcha/Test');
$this
->config('captcha.settings')
->set('persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE)
->save();
$this
->drupalGet('<front>');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid = $this
->getCaptchaSidFromForm();
$captcha_token = $this
->getCaptchaTokenFromForm();
$solution = "Test 123";
$edit = [
'name' => $this->normalUser
->getDisplayName(),
'pass' => $this->normalUser->pass_raw,
'captcha_response' => $solution,
];
$this
->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(FALSE);
$this
->assertSession()
->pageTextContains($this->normalUser
->getDisplayName());
$this
->drupalLogout();
$this
->drupalGet('<front>');
$this
->assertSession()
->hiddenFieldExists("captcha_sid")
->setValue((string) $captcha_sid);
$this
->assertSession()
->hiddenFieldExists("captcha_token")
->setValue((string) $captcha_token);
$this
->assertNotEmpty(json_encode($edit));
$this
->submitForm($edit, 'Log in');
$this
->assertCaptchaSessionIdReuseAttackDetection();
$this
->assertCaptchaPresence(TRUE);
}
public function testMultipleCaptchaProtectedFormsOnOnePage() {
\Drupal::service('module_installer')
->install([
'block',
]);
$this
->drupalPlaceBlock('user_login_block');
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Test');
captcha_set_form_id_setting('user_login_form', 'captcha/Test');
$this
->allowCommentPostingForAnonymousVisitors();
$node = $this
->drupalCreateNode();
$edit = $this
->getCommentFormValues();
$comment_subject = $edit['subject[0][value]'];
$edit['captcha_response'] = 'Test 123';
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$this
->submitForm($edit, 'Preview');
$this
->assertCaptchaResponseAccepted();
$this
->assertSession()
->pageTextContains($comment_subject);
}
}