Skip to content

Fix ReferenceError on Reset/backhome when grecaptcha is undefined#221

Open
HomesONE wants to merge 1 commit into
gmazoyer:mainfrom
HomesONE:fix/js-grecaptcha-undefined-guard
Open

Fix ReferenceError on Reset/backhome when grecaptcha is undefined#221
HomesONE wants to merge 1 commit into
gmazoyer:mainfrom
HomesONE:fix/js-grecaptcha-undefined-guard

Conversation

@HomesONE

Copy link
Copy Markdown

Problem

In js/looking-glass.js:

```js
if (typeof grecaptcha.reset === "function") {
grecaptcha.reset();
}
```

typeof grecaptcha.reset evaluates grecaptcha.reset first — which dereferences the grecaptcha identifier before typeof is applied to the result. When no reCAPTCHA script is loaded (captcha disabled, or hCaptcha is the configured type and provides the hcaptcha global 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 #backhome button (line 49). On captcha-disabled deployments the user sees Reset / "Back to home" do nothing on the first click after a query, with a ReferenceError: grecaptcha is not defined in the browser console.

Fix

Guard with the standard typeof idiom for "is this global defined":

```js
if (typeof grecaptcha !== "undefined" && typeof grecaptcha.reset === "function") {
grecaptcha.reset();
}
```

typeof on an undeclared identifier returns the string \"undefined\" without throwing, which is exactly the case we need to handle here.

Impact

  • Captcha disabled / hCaptcha configured: Reset and Back-home buttons work again.
  • reCAPTCHA enabled: no change — both clauses are true, grecaptcha.reset() still called.

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant