From a52b090e19fa526de422832ee29ce14ae37cc94e Mon Sep 17 00:00:00 2001 From: battye Date: Fri, 19 Apr 2019 06:42:40 +0000 Subject: [PATCH 1/2] Add reCaptcha checks. Close #49 --- .../Validator/FileValidator.php | 97 +++++++++++++++++++ tests/FileValidator/ValidateLangTest.php | 22 +++++ 2 files changed, 119 insertions(+) diff --git a/src/Phpbb/TranslationValidator/Validator/FileValidator.php b/src/Phpbb/TranslationValidator/Validator/FileValidator.php index fbc2823..1f92c60 100644 --- a/src/Phpbb/TranslationValidator/Validator/FileValidator.php +++ b/src/Phpbb/TranslationValidator/Validator/FileValidator.php @@ -39,6 +39,81 @@ class FileValidator /** @var \Phpbb\TranslationValidator\Output\OutputInterface */ protected $output; + /** @var array List from https://developers.google.com/recaptcha/docs/language */ + private $reCaptchaLanguages = [ + 'ar', + 'af', + 'am', + 'hy', + 'az', + 'eu', + 'bn', + 'bg', + 'ca', + 'zh-HK', + 'zh-CN', + 'zh-TW', + 'hr', + 'cs', + 'da', + 'nl', + 'en-GB', + 'en', + 'et', + 'fil', + 'fi', + 'fr', + 'fr-CA', + 'gl', + 'ka', + 'de', + 'de-AT', + 'de-CH', + 'el', + 'gu', + 'iw', + 'hi', + 'hu', + 'is', + 'id', + 'it', + 'ja', + 'kn', + 'ko', + 'lo', + 'lv', + 'lt', + 'ms', + 'ml', + 'mr', + 'mn', + 'no', + 'fa', + 'pl', + 'pt', + 'pt-BR', + 'pt-PT', + 'ro', + 'ru', + 'sr', + 'si', + 'sk', + 'sl', + 'es', + 'es-419', + 'sw', + 'sv', + 'ta', + 'te', + 'th', + 'tr', + 'uk', + 'ur', + 'vi', + 'zu', + '', // Allow empty strings + ]; + /** * @param InputInterface $input * @param OutputInterface $output @@ -281,6 +356,28 @@ public function validateLangFile($sourceFile, $originFile) $this->output->addMessage(Output::FATAL, 'Must not contain key: ' . $validateLangKey, $originFile); } } + + // Check reCaptcha file + if ($originFile === $this->originLanguagePath . 'captcha_recaptcha.php') + { + $this->validateReCaptchaValue($originFile, $validate); + } + } + + /** + * Check that the reCaptcha key provided is allowed + * @param $originFile + * @param array $validate + */ + public function validateReCaptchaValue($originFile, $validate) + { + // The key 'RECAPTCHA_LANG' must match the list provided by Google, or be left empty + // If any other key is used, we will show an error + if (array_key_exists('RECAPTCHA_LANG', $validate) && !in_array($validate['RECAPTCHA_LANG'], $this->reCaptchaLanguages)) + { + // The supplied value doesn't match the allowed values + $this->output->addMessage(Output::WARNING, 'reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty', $originFile, 'RECAPTCHA_LANG'); + } } /** diff --git a/tests/FileValidator/ValidateLangTest.php b/tests/FileValidator/ValidateLangTest.php index 268e256..7e165dc 100644 --- a/tests/FileValidator/ValidateLangTest.php +++ b/tests/FileValidator/ValidateLangTest.php @@ -37,4 +37,26 @@ public function testValidateLangFile($file, $expected) $this->validator->validateLangFile($file, $file); $this->assertOutputMessages($expected); } + + /** + * Test the reCaptcha checks + */ + public function testValidateLangReCaptcha() + { + // Failure - as we supply a key that isn't valid + $reCaptchaLanguage = ['RECAPTCHA_LANG' => 'incorrect']; + $this->validator->validateReCaptchaValue('', $reCaptchaLanguage); + + $output = $this->output->getMessages(); + $expected = Output::WARNING . '-reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty--RECAPTCHA_LANG'; + + $this->assertEquals($this->output->getMessageCount(Output::WARNING), 1); + $this->assertEquals($output[0], $expected); + + // Pass - as 'en' is valid + $reCaptchaLanguage['RECAPTCHA_LANG'] = 'en'; + $this->validator->validateReCaptchaValue('', $reCaptchaLanguage); + + $this->assertEquals($this->output->getMessageCount(Output::WARNING), 1); // Shouldn't change in size as no error added + } } From e1b47a7054a6b9785e0cc391554cddb7a376bdc8 Mon Sep 17 00:00:00 2001 From: battye Date: Sun, 5 May 2019 12:45:28 +0000 Subject: [PATCH 2/2] Change error type to error from warning for recaptcha --- src/Phpbb/TranslationValidator/Validator/FileValidator.php | 2 +- tests/FileValidator/ValidateLangTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Phpbb/TranslationValidator/Validator/FileValidator.php b/src/Phpbb/TranslationValidator/Validator/FileValidator.php index 1f92c60..e097c5e 100644 --- a/src/Phpbb/TranslationValidator/Validator/FileValidator.php +++ b/src/Phpbb/TranslationValidator/Validator/FileValidator.php @@ -376,7 +376,7 @@ public function validateReCaptchaValue($originFile, $validate) if (array_key_exists('RECAPTCHA_LANG', $validate) && !in_array($validate['RECAPTCHA_LANG'], $this->reCaptchaLanguages)) { // The supplied value doesn't match the allowed values - $this->output->addMessage(Output::WARNING, 'reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty', $originFile, 'RECAPTCHA_LANG'); + $this->output->addMessage(Output::ERROR, 'reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty', $originFile, 'RECAPTCHA_LANG'); } } diff --git a/tests/FileValidator/ValidateLangTest.php b/tests/FileValidator/ValidateLangTest.php index 7e165dc..cc9316b 100644 --- a/tests/FileValidator/ValidateLangTest.php +++ b/tests/FileValidator/ValidateLangTest.php @@ -48,15 +48,15 @@ public function testValidateLangReCaptcha() $this->validator->validateReCaptchaValue('', $reCaptchaLanguage); $output = $this->output->getMessages(); - $expected = Output::WARNING . '-reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty--RECAPTCHA_LANG'; + $expected = Output::ERROR . '-reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty--RECAPTCHA_LANG'; - $this->assertEquals($this->output->getMessageCount(Output::WARNING), 1); + $this->assertEquals($this->output->getMessageCount(Output::ERROR), 1); $this->assertEquals($output[0], $expected); // Pass - as 'en' is valid $reCaptchaLanguage['RECAPTCHA_LANG'] = 'en'; $this->validator->validateReCaptchaValue('', $reCaptchaLanguage); - $this->assertEquals($this->output->getMessageCount(Output::WARNING), 1); // Shouldn't change in size as no error added + $this->assertEquals($this->output->getMessageCount(Output::ERROR), 1); // Shouldn't change in size as no error added } }