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

Recaptcha validator does not use field data #370

Open
sr105 opened this issue Jun 11, 2019 · 1 comment
Open

Recaptcha validator does not use field data #370

sr105 opened this issue Jun 11, 2019 · 1 comment

Comments

@sr105
Copy link

sr105 commented Jun 11, 2019

The Recaptcha validator grabs a specific field by name instead of using field.data (link to line below). This means that an HTML form must send the recaptcha value as g-recaptcha-response or the validator fails. Our frontend is a react app that does not use wtforms to generate the HTML. And uses the actual form field names inside POST data.

https://github.com/lepture/flask-wtf/blob/master/flask_wtf/recaptcha/validators.py#L40

class TokenForm(Form):
    """Evaluate login attempts with optional recaptcha."""

    email = StringField(validators=[validators.DataRequired()])
    password = StringField(validators=[validators.DataRequired()])
    recaptcha = RecaptchaField()

If the form data is passed as {... "recaptcha": "recaptcha_response_value"}, the validator fails even though it has the data in the passed field instance.

Our REST api and frontend rely on field names matching. Is there a workaround for this?

@sr105
Copy link
Author

sr105 commented Jul 9, 2019

Workaround:

from flask_wtf.recaptcha import Recaptcha

def validate_recaptcha(form, field):
    """Validate recaptcha response."""
    # Recaptcha validator only looks for data in a hard-coded field name
    # https://github.com/lepture/flask-wtf/issues/370
    request.json["g-recaptcha-response"] = field.data
    return Recaptcha()(form, field)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants