X Tutup
Skip to content

fix: add email validation for google oauth#3966

Open
Nixxx19 wants to merge 3 commits intoprocessing:developfrom
Nixxx19:nityam/fix-google-oauth-email-validation
Open

fix: add email validation for google oauth#3966
Nixxx19 wants to merge 3 commits intoprocessing:developfrom
Nixxx19:nityam/fix-google-oauth-email-validation

Conversation

@Nixxx19
Copy link
Contributor

@Nixxx19 Nixxx19 commented Mar 3, 2026

Issue:

Fixes #3907

The Google OAuth strategy was accessing profile._json.emails[0].value without validating that the emails array exists or has any elements. This caused a TypeError: Cannot read property '0' of undefined crash when Google OAuth profiles didn't include email addresses, breaking the authentication flow.

Changes:

Added email validation helper function:

  • Created getGooglePrimaryEmail() helper function that safely extracts the primary email from Google OAuth profile
  • Validates that emails array exists, is an array, and has at least one element
  • Uses optional chaining for safe property access
  • Trims whitespace from email addresses
  • Returns null for missing or invalid emails

Updated Google OAuth strategy callback:

  • Added validation at the start of the callback to check for email availability
  • Returns user-friendly error message when emails are missing: "Unable to retrieve email from Google account. Please ensure your Google account has an email address and try again."
  • Replaced all 6 unsafe profile._json.emails[0].value accesses with the validated primaryEmail variable
  • Prevents crashes and provides graceful error handling

Files changed:

  • server/config/passport.js: Added email validation helper and updated Google strategy

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test)
  • has no typecheck errors (npm run typecheck)
  • is from a uniquely-named feature branch and is up to date with the develop branch.
  • is descriptively named and links to an issue number, i.e. Fixes #3907
  • meets the standards outlined in the accessibility guidelines

Copy link
Collaborator

@clairep94 clairep94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great otherwise thanks!

@clairep94 clairep94 self-assigned this Mar 7, 2026
@clairep94 clairep94 added the Contributor Follow-up Required Request for changes, or other follow up required. Please see PR comments thread. label Mar 7, 2026
Copilot AI review requested due to automatic review settings March 7, 2026 15:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in the Google OAuth strategy where accessing profile._json.emails[0].value without validating the emails array caused a TypeError crash when Google OAuth profiles didn't include email addresses. The fix introduces a getGooglePrimaryEmail() helper that safely extracts the primary email with proper validation, and uses it throughout the strategy callback.

Changes:

  • Added getGooglePrimaryEmail() helper function that validates the emails array and safely extracts the first email value
  • Added early validation at the start of the Google OAuth callback that returns a user-friendly error when no email is available
  • Replaced all 6 unsafe profile._json.emails[0].value direct accesses with the validated primaryEmail variable

@clairep94 clairep94 removed the Contributor Follow-up Required Request for changes, or other follow up required. Please see PR comments thread. label Mar 8, 2026
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.

Google OAuth: missing email array validation

3 participants

X Tutup