-
Notifications
You must be signed in to change notification settings - Fork 881
chore: ensure github uids are unique #11826
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
de93f28
fix: github changing emails should update coder emails on login
Emyrk 8077aa6
fix was not actually a fix, revert
Emyrk bb2bdf1
fix: ensure all linked accounts use a github id
Emyrk 36b31c3
fix unit testS
Emyrk 0033d9b
github uids to all tests
Emyrk 5821971
Fix language
Emyrk 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
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
"github.com/golang-jwt/jwt/v4" | ||
"github.com/google/go-github/v43/github" | ||
"github.com/google/uuid" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/xerrors" | ||
|
||
|
@@ -25,6 +26,7 @@ import ( | |
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbgen" | ||
"github.com/coder/coder/v2/coderd/database/dbtestutil" | ||
"github.com/coder/coder/v2/coderd/promoauth" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
|
@@ -205,6 +207,7 @@ func TestUserOAuth2Github(t *testing.T) { | |
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("kyle"), | ||
}, nil | ||
}, | ||
|
@@ -264,7 +267,9 @@ func TestUserOAuth2Github(t *testing.T) { | |
}}, nil | ||
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{}, nil | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
}, nil | ||
}, | ||
ListEmails: func(ctx context.Context, client *http.Client) ([]*github.UserEmail, error) { | ||
return []*github.UserEmail{{ | ||
|
@@ -295,7 +300,9 @@ func TestUserOAuth2Github(t *testing.T) { | |
}}, nil | ||
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{}, nil | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
}, nil | ||
}, | ||
ListEmails: func(ctx context.Context, client *http.Client) ([]*github.UserEmail, error) { | ||
return []*github.UserEmail{{ | ||
|
@@ -390,6 +397,7 @@ func TestUserOAuth2Github(t *testing.T) { | |
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("kyle"), | ||
}, nil | ||
}, | ||
|
@@ -442,6 +450,7 @@ func TestUserOAuth2Github(t *testing.T) { | |
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("mathias"), | ||
}, nil | ||
}, | ||
|
@@ -494,6 +503,7 @@ func TestUserOAuth2Github(t *testing.T) { | |
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("mathias"), | ||
}, nil | ||
}, | ||
|
@@ -532,6 +542,7 @@ func TestUserOAuth2Github(t *testing.T) { | |
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("mathias"), | ||
}, nil | ||
}, | ||
|
@@ -574,6 +585,7 @@ func TestUserOAuth2Github(t *testing.T) { | |
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("kyle"), | ||
}, nil | ||
}, | ||
|
@@ -591,6 +603,87 @@ func TestUserOAuth2Github(t *testing.T) { | |
|
||
require.Equal(t, http.StatusUnauthorized, resp.StatusCode) | ||
}) | ||
t.Run("ChangedEmail", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
fake := oidctest.NewFakeIDP(t, | ||
oidctest.WithServing(), | ||
oidctest.WithCallbackPath("/api/v2/users/oauth2/github/callback"), | ||
) | ||
const ghID = int64(7777) | ||
auditor := audit.NewMock() | ||
coderEmail := &github.UserEmail{ | ||
Email: github.String("alice@coder.com"), | ||
Verified: github.Bool(true), | ||
Primary: github.Bool(true), | ||
} | ||
gmailEmail := &github.UserEmail{ | ||
Email: github.String("alice@gmail.com"), | ||
Verified: github.Bool(true), | ||
Primary: github.Bool(false), | ||
} | ||
emails := []*github.UserEmail{ | ||
gmailEmail, | ||
coderEmail, | ||
} | ||
|
||
client := coderdtest.New(t, &coderdtest.Options{ | ||
Auditor: auditor, | ||
GithubOAuth2Config: &coderd.GithubOAuth2Config{ | ||
AllowSignups: true, | ||
AllowEveryone: true, | ||
OAuth2Config: promoauth.NewFactory(prometheus.NewRegistry()).NewGithub("test-github", fake.OIDCConfig(t, []string{})), | ||
ListOrganizationMemberships: func(ctx context.Context, client *http.Client) ([]*github.Membership, error) { | ||
return []*github.Membership{}, nil | ||
}, | ||
TeamMembership: func(ctx context.Context, client *http.Client, org, team, username string) (*github.Membership, error) { | ||
return nil, xerrors.New("no teams") | ||
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
return &github.User{ | ||
Login: github.String("alice"), | ||
ID: github.Int64(ghID), | ||
}, nil | ||
}, | ||
ListEmails: func(ctx context.Context, client *http.Client) ([]*github.UserEmail, error) { | ||
return emails, nil | ||
}, | ||
}, | ||
}) | ||
|
||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
// This should register the user | ||
client, _ = fake.Login(t, client, jwt.MapClaims{}) | ||
user, err := client.User(ctx, "me") | ||
require.NoError(t, err) | ||
userID := user.ID | ||
require.Equal(t, user.Email, *coderEmail.Email) | ||
|
||
// Now the user is registered, let's change their primary email. | ||
coderEmail.Primary = github.Bool(false) | ||
gmailEmail.Primary = github.Bool(true) | ||
|
||
client, _ = fake.Login(t, client, jwt.MapClaims{}) | ||
user, err = client.User(ctx, "me") | ||
require.NoError(t, err) | ||
require.Equal(t, user.ID, userID) | ||
require.Equal(t, user.Email, *gmailEmail.Email) | ||
|
||
// Entirely change emails. | ||
newEmail := "alice@newdomain.com" | ||
emails = []*github.UserEmail{ | ||
{ | ||
Email: github.String("alice@newdomain.com"), | ||
Primary: github.Bool(true), | ||
Verified: github.Bool(true), | ||
}, | ||
} | ||
client, _ = fake.Login(t, client, jwt.MapClaims{}) | ||
user, err = client.User(ctx, "me") | ||
require.NoError(t, err) | ||
require.Equal(t, user.ID, userID) | ||
require.Equal(t, user.Email, newEmail) | ||
}) | ||
Comment on lines
+606
to
+686
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a change email unit test using the fake idp. |
||
} | ||
|
||
// nolint:bodyclose | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this potentially leak another user's email? Would it be safer to log this instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it cannot. This comes from the authenticated request to github using your oauth token.
So this payload comes from:
id=0
.To leak someone else email, you would need to authenticate as that user via github.