karser / KarserRecaptcha3Bundle

Google ReCAPTCHA v3 for Symfony
MIT License
158 stars 21 forks source link

Symfony 5 : Error: Unknown base64 encoding at char #33

Closed Sheitak closed 3 years ago

Sheitak commented 3 years ago

Hello Karser,

I am using your Bundle v3 and followed the installation. I want to integrate the ReCaptcha on a basic contact form,

I install with flex and am on Symfony 5.1, fill in my ContactType file as it should, then also write the two JavaScript scripts in my layout,

All that remains is to fill in my secret key and my Captcha key,

Despite all this, my inspector gives me the following error Error: Unknown base64 encoding at char: The submission works, but does not seem to involve the captcha, I think I am not using the bundle completely correctly, would you have an indication for me please ?

I share with you the status of my current code, thank you very much for your work !

Layout.html.twig

<script>
const recaptchaCallback_form_captcha = function() {
grecaptcha.execute('******************************', {action: 'landing'}).then(function(token) {
document.getElementById('form_captcha').value = token;
});
};
</script>

<script src="https://www.google.com/recaptcha/api.js?render=******************************&onload=recaptchaCallback_form_captcha" async defer></script>

ContactType.php

namespace App\Form;

use App\Entity\Contact;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('pseudo')
            ->add('email')
            ->add('subject')
            ->add('message', TextareaType::class)
            ->add('captcha', Recaptcha3Type::class, [
                'constraints' => new Recaptcha3(['message' => 'There were problems with your captcha. Please try again or contact with support and provide following code(s): {{ errorCodes }}']),
                'action_name' => 'contact',
                // 'script_nonce_csp' => $nonceCSP,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Contact::class,
        ]);
    }
}

contact.html.twig

    {{ form_start(form, {'attr': {'id': 'form-captcha'}}) }}
        {{ form_errors(form) }}
        {{ form_widget(form) }}
        <div class="text-center">
            <button class="btn btn-primary col-2">{{ button_label|default('Send') }}</button>
        </div>
    {{ form_end(form) }}
karser commented 3 years ago
  1. The content of Layout.html.twig is not needed when you use Recaptcha3Type and the Symfony form.
  2. Go through the troubleshooting checklist
Sheitak commented 3 years ago

Okay ! Thank you, I corrected this negligence,

Everything works well apparently, although my inspector always presents this famous error Error : Unknown base64 encoding at char : but I think that this does not prevent the operation, I was just trying to correct this problem, If you have an idea do not hesitate, otherwise thank you to you and have a good day,

karser commented 3 years ago

I haven't encountered this error. Can you please locate the source of this error? At which particulate line it happens?

Sheitak commented 3 years ago

It's a bit fuzzy I admit, I'm sharing a screenshot for you to better understand the thing, I continue to do some research on my side base64


EDIT : Ok, maybe I found something on this : Obviously, this is from an encoding, basically UTF-8, base64. Decoding base64 would restore UTF-8 and raise this error. Source : https://dba.stackexchange.com/questions/212160/why-do-i-get-incorrect-characters-when-decoding-a-base64-string-to-nvarchar-in-s

It is apparently possible to fix it, but it is also possible that it comes from google's ReCaptcha itself,

karser commented 3 years ago

Thank you for the research @Sheitak ! Looks like this issue is related to ReCaptcha itself https://github.com/google/recaptcha/issues/392 . Closing this issue then.