Gregwar / CaptchaBundle

Symfony bundle implementing a "captcha" form type
MIT License
346 stars 127 forks source link

Getting 428 response and no digits in the captcha #177

Open pascalrobert opened 6 years ago

pascalrobert commented 6 years ago

Hi,

We have a Symfony 3 app that have a captcha on a page. It works on a developer's workstation, but not on mine. When the captcha should be generated, I get:

Failed to load resource: the server responded with a status of 428 (Precondition Required)
http://localhost:8000/generate-captcha/gcb_captcha?n=13606794422fa6e0d9847c4d227ea896

The captcha is built like this:

        $form = $this->createFormBuilder()
            ->add('captcha', CaptchaType::class,[
                'width' => 318,
                'height' => 70,
                'as_url' =>true,
                'reload' =>true,
                'invalid_message' => $tr->trans('Ce champ est obligatoire.'),
                'attr'=>['oninvalid'=>""],
                'required' => false
            ])
            ->getForm();

        $form->handleRequest($request);
       $variables['form'] = $form->createView();

What is missing? I have the latest version of Captcha and CapthaBundle. PHP 7.1, with GD support. No error in the logs.

Gregwar commented 6 years ago

Hello, First, be sure that the sessions are enabled in your configuration

The controller that generates the captcha is handling another requests, the field options are transported using the session, and retrieved in the generator controller. The 428 error indicates that the controller can't retrieve valid options for generation from the session and is done here:

https://github.com/Gregwar/CaptchaBundle/blob/master/Controller/CaptchaController.php#L32

The session is filled by the Captcha Genrator when the form is issued. As a side effect, loading directly the URL of the captcha without running the form will result in 428

Maybe you can inspect the contents of the session and check what it contains at this time

pascalrobert commented 6 years ago

Hi,

The session is not empty.

print_r($options, true) gives:

Array ( [charset] => 2054353 [length] => 6 [width] => 130 [height] => 50 [font] => /Users/xxxxx/Code/xxxx-web/xxxxx/vendor/gregwar/captcha-bundle/DependencyInjection/../Generator/Font/captcha.ttf [keep_value] => [as_file] => [as_url] => [reload] => [image_folder] => captcha [web_path] => /Users/xxxxx/Code/xxxxx-web/xxxx/app/../web [gc_freq] => 100 [expiration] => 60 [quality] => 30 [invalid_message] => Bad code value [bypass_code] => [whitelist_key] => captcha_whitelist_key [humanity] => 0 [distortion] => 1 [max_front_lines] => [max_behind_lines] => [interpolation] => 1 [text_color] => Array ( ) [background_color] => Array ( ) [background_images] => Array ( ) [disabled] => [ignore_all_effects] => ) [] print_r($session->has($whitelistKey), true) gives:

[2017-11-08 19:05:13] app.ERROR: []

pascalrobert commented 6 years ago

Looking at the session data in Symfony Profiler, I do see captcha_whitelist_key:

captcha_whitelist_key | [0 => gcb_captcha]

The printout of $session->has($whitelistKey) in my previous comment is from CaptchaController, so it looks like captcha_whitelist_key is not part of the session when coming into CaptchaController.generateCaptchaAction

pascalrobert commented 6 years ago

The problem was with... framework->session->cookie_secure: true. Had to set it to false.

vipinbose commented 3 years ago

The problem was with... framework->session->cookie_secure: true. Had to set it to false.

Yes .. this has helped us too. Thanks..

HishamHuneidie commented 2 years ago

I have the same problem. But I'm trying to change the same that you changed, but it doesn't solve my problem. Do you have any idea? @pascalrobert and @Gregwar thanks