-
Notifications
You must be signed in to change notification settings - Fork 9
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
Fix: reCAPTCHA + HTML5 form validation #1071
Conversation
use as selector in JS
not multiple times per-form
we don't need the recaptcha attributes on this button
allows HTML5 field validation to occur before the recaptcha check adapted from https://stackoverflow.com/a/63290578/453168
* adds settings directly to request.recaptcha * middleware instead of context processor --> can be applied per-view
only present if recaptcha enabled for the view with the form
ensure the verifier selection form checks the recaptcha value
otherwise returning the view function directly uses the same request object, with the recaptcha data from the original confirm request this also causes the URL to change from /eligibility/confirm to /eligibility/unverified (wasn't shown before, but is a valid route)
12737dc
to
efac987
Compare
I just wrote these notes for myself to help document what the app is doing for forms and what JS each form has These are the 4 forms that use the
The header's language selector button is also a form, but it does not use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really like the way the JS in form.html is cleaned up! I also really like the instructions/documentation on how to run recaptcha locally - this will be very helpful.
I added the note comment there just to make sure we never make a CSS ID that collides with the ID name of a form. (ie - Ensuring we never make another #verifier-selection
ID).
Once this PR is merged, I believe the unverified
page should have the proper new layout that is on dev
already.
That's a good point - we could prefix these new form IDs with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactoring!
Yeah. This could be done in another PR. |
help avoid future CSS selector collision
e783ce1
Fixes #1068
Based on an answer to this SO post: https://stackoverflow.com/q/41665935/453168
What this PR does
Uses programmatic reCAPTCHA validation
This is the main issue being solved. reCAPTCHA v3 supports two options:
Automatically bind the challenge to a button (existing implementation)
Programmatically invoke the challenge (new implementation in this PR)
Using a programmatic challenge allows the browser's HTML5 form validation a chance to kick-in before reCAPTCHA's checks,
which isn't possible with the challenge button auto-bind.
Refactors how templates get reCAPTCHA data
Before we used a Django context processor, which gave the data to every request context, even those that didn't need it:
This PR introduces a Middleware, following our usual pattern where we can apply the middleware per-view that needs it, which adds the reCAPTCHA data directly to the
request
:This makes it more explicit which views use reCAPTCHA, and also makes using reCAPTCHA data easy from templates. Like in
base.html
where we include the reCAPTCHA API library only once and only if needed:Makes the
eligibility:unverified
path a redirectThis has a couple advantages:
/eligiblity/confirm
requesteligibility:verified
path works (ultimately a redirect toenrollment:index
)/eligiblity/unverified
instead of/eligibility/confirm
, a more accurate UXTesting this PR
Setup
Add this to your
.vscode/launch.json
under theDjango: Benefits Client, Debug=False
entry'senv
:Eligibility index
reCAPTCHA library included once at the bottom of
<body>
:HTML5 validation works, form submits with valid data:
Eligibility confirm
reCAPTCHA library included once at the bottom of
<body>
:HTML5 validation works, form submits with valid data:
Other pages
Every other page should not have the reCAPTCHA library or any reCAPTCHA handling for form elements.
E.g. the homepage does not include reCAPTCHA:
Nor does the
eligibility:start
page: