-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(auth): close nOAuth account takeover via email-based OAuth linking #5156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+265
−53
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d6c816d
fix(auth): close nOAuth account takeover via email-based OAuth linking
waleedlatif1 82a1d17
test(auth): cover Microsoft id-token emailVerified derivation
waleedlatif1 dc5cf93
fix(auth): check the provider field the sign-in handler actually uses
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| getRequestedSignInProviderId, | ||
| isSignInProviderAllowed, | ||
| SIGN_IN_PROVIDER_IDS, | ||
| } from '@/lib/auth/constants' | ||
|
|
||
| describe('sign-in provider allowlist', () => { | ||
| it('permits only the first-party login providers', () => { | ||
| expect([...SIGN_IN_PROVIDER_IDS]).toEqual(['google', 'github', 'microsoft']) | ||
| }) | ||
|
|
||
| it('allows first-party login providers to sign in', () => { | ||
| for (const providerId of SIGN_IN_PROVIDER_IDS) { | ||
| expect(isSignInProviderAllowed(providerId)).toBe(true) | ||
| } | ||
| }) | ||
|
|
||
| it('rejects integration connectors from the sign-in endpoints', () => { | ||
| const connectors = [ | ||
| 'microsoft-ad', | ||
| 'microsoft-teams', | ||
| 'microsoft-excel', | ||
| 'outlook', | ||
| 'onedrive', | ||
| 'sharepoint', | ||
| 'salesforce', | ||
| 'jira', | ||
| 'confluence', | ||
| 'hubspot', | ||
| 'box', | ||
| 'wordpress', | ||
| 'google-drive', | ||
| 'google-sheets', | ||
| 'vertex-ai', | ||
| ] | ||
| for (const providerId of connectors) { | ||
| expect(isSignInProviderAllowed(providerId)).toBe(false) | ||
| } | ||
| }) | ||
|
|
||
| it('rejects missing or malformed provider identifiers', () => { | ||
| expect(isSignInProviderAllowed(undefined)).toBe(false) | ||
| expect(isSignInProviderAllowed(null)).toBe(false) | ||
| expect(isSignInProviderAllowed('')).toBe(false) | ||
| expect(isSignInProviderAllowed(123)).toBe(false) | ||
| expect(isSignInProviderAllowed({ provider: 'google' })).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getRequestedSignInProviderId', () => { | ||
| it('reads the `provider` field on /sign-in/social', () => { | ||
| expect(getRequestedSignInProviderId('/sign-in/social', { provider: 'microsoft' })).toBe( | ||
| 'microsoft' | ||
| ) | ||
| }) | ||
|
|
||
| it('reads the `providerId` field on /sign-in/oauth2', () => { | ||
| expect(getRequestedSignInProviderId('/sign-in/oauth2', { providerId: 'salesforce' })).toBe( | ||
| 'salesforce' | ||
| ) | ||
| }) | ||
|
|
||
| it('checks the field the /sign-in/oauth2 handler uses, ignoring a decoy `provider`', () => { | ||
| const body = { provider: 'google', providerId: 'salesforce' } | ||
| const resolved = getRequestedSignInProviderId('/sign-in/oauth2', body) | ||
| expect(resolved).toBe('salesforce') | ||
| expect(isSignInProviderAllowed(resolved)).toBe(false) | ||
| }) | ||
|
|
||
| it('checks the field the /sign-in/social handler uses, ignoring a decoy `providerId`', () => { | ||
| const body = { provider: 'salesforce', providerId: 'google' } | ||
| const resolved = getRequestedSignInProviderId('/sign-in/social', body) | ||
| expect(resolved).toBe('salesforce') | ||
| expect(isSignInProviderAllowed(resolved)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns undefined for unrelated paths and missing bodies', () => { | ||
| expect(getRequestedSignInProviderId('/sign-in/email', { provider: 'google' })).toBeUndefined() | ||
| expect(getRequestedSignInProviderId('/sign-in/social', undefined)).toBeUndefined() | ||
| expect(getRequestedSignInProviderId('/sign-in/oauth2', null)).toBeUndefined() | ||
| }) | ||
| }) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { deriveMicrosoftEmailVerified, isMicrosoftProvider } from '@/lib/oauth/microsoft' | ||
|
|
||
| const EMAIL = 'user@contoso.com' | ||
|
|
||
| describe('deriveMicrosoftEmailVerified', () => { | ||
| it('honors an explicit email_verified=true claim', () => { | ||
| expect(deriveMicrosoftEmailVerified({ email_verified: true }, EMAIL)).toBe(true) | ||
| }) | ||
|
|
||
| it('honors an explicit email_verified=false claim over verified-email claims', () => { | ||
| expect( | ||
| deriveMicrosoftEmailVerified( | ||
| { email_verified: false, verified_primary_email: [EMAIL] }, | ||
| ) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('treats a verified primary email matching the email as verified', () => { | ||
| expect(deriveMicrosoftEmailVerified({ verified_primary_email: [EMAIL] }, EMAIL)).toBe(true) | ||
| }) | ||
|
|
||
| it('treats a verified secondary email matching the email as verified', () => { | ||
| expect( | ||
| deriveMicrosoftEmailVerified({ verified_secondary_email: ['x@y.com', EMAIL] }, EMAIL) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('does not verify when the verified-email claims do not include the email', () => { | ||
| expect( | ||
| deriveMicrosoftEmailVerified( | ||
| { | ||
| verified_primary_email: ['other@contoso.com'], | ||
| verified_secondary_email: ['another@contoso.com'], | ||
| }, | ||
| ) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('defaults to false when no verification claim is present (typical Azure AD token)', () => { | ||
| expect(deriveMicrosoftEmailVerified({ name: 'User', oid: 'abc' }, EMAIL)).toBe(false) | ||
| }) | ||
|
|
||
| it('defaults to false for an empty claim set', () => { | ||
| expect(deriveMicrosoftEmailVerified({}, EMAIL)).toBe(false) | ||
| }) | ||
|
|
||
| it('coerces a truthy non-boolean email_verified claim', () => { | ||
| expect(deriveMicrosoftEmailVerified({ email_verified: 'true' }, EMAIL)).toBe(true) | ||
| }) | ||
|
|
||
| it('treats a malformed verified-email claim as unverified', () => { | ||
| expect(deriveMicrosoftEmailVerified({ verified_primary_email: 'not-an-array' }, EMAIL)).toBe( | ||
| false | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('isMicrosoftProvider', () => { | ||
| it('recognizes Microsoft connector provider IDs', () => { | ||
| expect(isMicrosoftProvider('microsoft-ad')).toBe(true) | ||
| expect(isMicrosoftProvider('outlook')).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects non-Microsoft provider IDs', () => { | ||
| expect(isMicrosoftProvider('google')).toBe(false) | ||
| expect(isMicrosoftProvider('microsoft')).toBe(false) | ||
| }) | ||
| }) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.