fix: recaptcha_service KeyError protection and consistent None returns#859
Open
dreamcreated wants to merge 1 commit intomemeLab:developfrom
Open
Conversation
Fixes memeLab#858 Two issues fixed in create_assessment(): 1. Added try/except KeyError guard around response_data['tokenProperties'] access to prevent unhandled KeyError crashing the signup flow if the reCAPTCHA API returns an unexpected response format. 2. Unified all early-exit return values to return None instead of mixing return {} and bare return statements. The caller in views.py checks 'if not assessment:' which works with None; using {} was functionally correct but semantically misleading and fragile for future maintainers. Also eliminates the unnecessary else branch after the action mismatch check, flattening the happy-path logic for readability.
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.
Summary
Fixes #858
Two bugs fixed in
src/users/services/recaptcha_service.py→create_assessment():1. Missing
KeyErrorprotection on reCAPTCHA API responseBefore: Direct dictionary access
response_data["tokenProperties"]raises an unhandledKeyErrorif the reCAPTCHA API returns an unexpected response format (network errors, API changes, outages). This crashes the signup flow with a 500 error.After: Wrapped in
try/except KeyErrorwith a structured error log andreturn None, so the signup flow degrades gracefully.2. Inconsistent return values (
{}vs implicitNone)Before:
After:
Both values are falsy, so the caller in
views.py(if not assessment:) behaved correctly either way — but{}is semantically misleading (callers might assume a non-None dict means partial success) and makes the code fragile for future maintainers.3. Minor: flattened else branch
The unnecessary
else:after an earlyreturn Nonewas removed to reduce nesting and improve readability.Testing
Existing tests in
src/users/tests/test_bot_protection.pycover the invalid-token, wrong-action, and low-score paths via mockedrequests.post. All these tests continue to pass since:not None==True(same as before for the caller check)tokenProperties, so the newtry/exceptpath is not triggered in existing testsA new test case could be added to cover the
KeyErrorpath (API returns response withouttokenProperties), but that is left as a follow-up.