Repository navigation
Fix skip_env? to fall back to Rails.env when default_env is nil - #476
Merged
grosser merged 1 commit intoApr 9, 2026
Conversation
When a Rails app uses a multi-threaded server (e.g. Puma) for system tests, the Recaptcha::Configuration singleton can be initialised before RAILS_ENV is visible, leaving default_env as nil. In that state, skip_env?(nil) calls skip_verify_env.include?(nil) which always returns false, bypassing the test-mode skip and causing real HTTP verification calls that intermittently fail. Fix: resolve the env to check as `env || default_env || Rails.env` so that a live Rails.env is always used as a final fallback. Adds four focused tests for Recaptcha.skip_env? covering the explicit env argument, default_env, and the Rails.env fallback path.
grosser
approved these changes
Apr 9, 2026
grosser
left a comment
Collaborator
There was a problem hiding this comment.
I kinda hate it because env being setup correctly should not be the gems problem ... but seems like the easiest fix 🤦
Collaborator
|
5.21.2 |
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 Rails apps using a multi-threaded server (e.g. Puma) for system tests with Capybara,
verify_recaptchaintermittently skips theskip_verify_envbypass and makes real HTTP verification calls to Google's reCAPTCHA API. These calls fail (timeout or invalid token), causing forms to re-render as if reCAPTCHA verification failed — despite the app running inRAILS_ENV=test.Root cause:
Recaptcha::Configuration#default_envis set once at initialization:When the singleton initializes before
RAILS_ENVis fully available in a multi-threaded context,default_envisnil. Thenskip_env?(nil)evaluates:...bypassing the test-mode skip entirely, even though
Rails.envwould correctly return"test"at call time.This is also demonstrated by the gem's own test helper, which explicitly deletes
RAILS_ENVandRACK_ENV(test/helper.rblines 2–3), meaningdefault_envisnilthroughout the test suite.Fix
Fall back to
Rails.envat call time ifdefault_envis nil:This ensures the skip check always reflects the current Rails environment, even if the configuration was initialized before
RAILS_ENVwas set.Tests
Added four tests for
Recaptcha.skip_env?:skip_verify_envskip_verify_envenvargument anddefault_envare nil (existing behavior, correctly documented)default_envis nil butRails.envis inskip_verify_env(the bug this fixes)All 160 tests pass. RuboCop reports no offenses.