Fix ReferenceError on Reset/backhome when grecaptcha is undefined#221
Open
HomesONE wants to merge 1 commit into
Open
Fix ReferenceError on Reset/backhome when grecaptcha is undefined#221HomesONE wants to merge 1 commit into
HomesONE wants to merge 1 commit into
Conversation
`typeof grecaptcha.reset === "function"` dereferences the `grecaptcha` identifier before applying `typeof` to `.reset`. When no reCAPTCHA script is loaded (captcha disabled, or hCaptcha is the configured type and provides `hcaptcha` instead), the identifier is undefined and the expression throws ReferenceError, aborting the click handler mid-execution. The visible symptom is that the Reset and "Back to home" buttons do nothing on captcha-disabled deployments. Guard with `typeof grecaptcha !== "undefined"` before touching the object, matching the standard idiom for "is this global defined". No effect on deployments where reCAPTCHA is loaded; the second clause is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
js/looking-glass.js:```js
if (typeof grecaptcha.reset === "function") {
grecaptcha.reset();
}
```
typeof grecaptcha.resetevaluatesgrecaptcha.resetfirst — which dereferences thegrecaptchaidentifier beforetypeofis applied to the result. When no reCAPTCHA script is loaded (captcha disabled, or hCaptcha is the configured type and provides thehcaptchaglobal instead), the identifier is undefined and the expression throws ReferenceError, aborting the click handler mid-execution.The two affected handlers are the inline form Reset button (line 40) and the
#backhomebutton (line 49). On captcha-disabled deployments the user sees Reset / "Back to home" do nothing on the first click after a query, with aReferenceError: grecaptcha is not definedin the browser console.Fix
Guard with the standard
typeofidiom for "is this global defined":```js
if (typeof grecaptcha !== "undefined" && typeof grecaptcha.reset === "function") {
grecaptcha.reset();
}
```
typeofon an undeclared identifier returns the string\"undefined\"without throwing, which is exactly the case we need to handle here.Impact
grecaptcha.reset()still called.