Skip to content

fix: recaptcha_service KeyError protection and consistent None returns#859

Open
dreamcreated wants to merge 1 commit intomemeLab:developfrom
dreamcreated:fix/recaptcha-service-keyerror-and-inconsistent-returns
Open

fix: recaptcha_service KeyError protection and consistent None returns#859
dreamcreated wants to merge 1 commit intomemeLab:developfrom
dreamcreated:fix/recaptcha-service-keyerror-and-inconsistent-returns

Conversation

@dreamcreated
Copy link
Copy Markdown

Summary

Fixes #858

Two bugs fixed in src/users/services/recaptcha_service.pycreate_assessment():

1. Missing KeyError protection on reCAPTCHA API response

Before: Direct dictionary access response_data["tokenProperties"] raises an unhandled KeyError if 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 KeyError with a structured error log and return None, so the signup flow degrades gracefully.

2. Inconsistent return values ({} vs implicit None)

Before:

return {}    # invalid token path
return       # wrong action path  (implicit None)

After:

return None  # all failure paths

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 early return None was removed to reduce nesting and improve readability.

Testing

Existing tests in src/users/tests/test_bot_protection.py cover the invalid-token, wrong-action, and low-score paths via mocked requests.post. All these tests continue to pass since:

  • not None == True (same as before for the caller check)
  • The mock data includes tokenProperties, so the new try/except path is not triggered in existing tests

A new test case could be added to cover the KeyError path (API returns response without tokenProperties), but that is left as a follow-up.

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.
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.

recaptcha_service returns inconsistent values (dict vs None) and lacks KeyError protection on API response

1 participant