-
Notifications
You must be signed in to change notification settings - Fork 175
MM-69450: Surface SAML SSO authorization failures to users #1030
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| package githuberrors | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-github/v54/github" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const samlEnforcementMessage = "organization saml enforcement" | ||
|
|
||
| // IsSAMLSSORequired reports whether err indicates GitHub rejected the request | ||
| // because the OAuth token lacks SAML SSO authorization for an organization. | ||
| func IsSAMLSSORequired(err error) bool { | ||
| if err == nil { | ||
| return false | ||
| } | ||
|
|
||
| var ghErr *github.ErrorResponse | ||
| if errors.As(err, &ghErr) && ghErr.Response != nil { | ||
| if ssoHeader := ghErr.Response.Header.Get("X-GitHub-SSO"); ssoHeader != "" { | ||
| lower := strings.ToLower(ssoHeader) | ||
| if strings.Contains(lower, "required") || strings.Contains(lower, "partial-results") { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| if ghErr.Response.StatusCode == http.StatusForbidden && containsSAMLMessage(ghErr.Message) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return containsSAMLMessage(errorMessage(err)) | ||
| } | ||
|
|
||
| func errorMessage(err error) string { | ||
| var ghErr *github.ErrorResponse | ||
| if errors.As(err, &ghErr) && ghErr.Message != "" { | ||
| return ghErr.Message | ||
| } | ||
|
|
||
| return err.Error() | ||
| } | ||
|
|
||
| func containsSAMLMessage(message string) bool { | ||
| return strings.Contains(strings.ToLower(message), samlEnforcementMessage) | ||
| } |
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,62 @@ | ||
| // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| package githuberrors | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-github/v54/github" | ||
| "github.com/pkg/errors" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestIsSAMLSSORequired(t *testing.T) { | ||
| t.Run("nil error", func(t *testing.T) { | ||
| assert.False(t, IsSAMLSSORequired(nil)) | ||
| }) | ||
|
|
||
| t.Run("unrelated error", func(t *testing.T) { | ||
| assert.False(t, IsSAMLSSORequired(errors.New("something went wrong"))) | ||
| }) | ||
|
|
||
| t.Run("403 github error with SAML message", func(t *testing.T) { | ||
| err := &github.ErrorResponse{ | ||
| Response: &http.Response{StatusCode: http.StatusForbidden}, | ||
| Message: "Resource protected by organization SAML enforcement. You must grant your personal token access to this organization.", | ||
| } | ||
| assert.True(t, IsSAMLSSORequired(err)) | ||
| }) | ||
|
|
||
| t.Run("403 github error without SAML message", func(t *testing.T) { | ||
| err := &github.ErrorResponse{ | ||
| Response: &http.Response{StatusCode: http.StatusForbidden}, | ||
| Message: "Resource not accessible by integration", | ||
| } | ||
| assert.False(t, IsSAMLSSORequired(err)) | ||
| }) | ||
|
|
||
| t.Run("X-GitHub-SSO required header", func(t *testing.T) { | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusForbidden, | ||
| Header: http.Header{"X-Github-Sso": {"required; url=https://github.com/orgs/acme/sso"}}, | ||
| } | ||
| err := &github.ErrorResponse{Response: resp, Message: "Forbidden"} | ||
| assert.True(t, IsSAMLSSORequired(err)) | ||
| }) | ||
|
|
||
| t.Run("X-GitHub-SSO partial-results header", func(t *testing.T) { | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Header: http.Header{"X-Github-Sso": {"partial-results; organizations=123,456"}}, | ||
| } | ||
| err := &github.ErrorResponse{Response: resp} | ||
| assert.True(t, IsSAMLSSORequired(err)) | ||
| }) | ||
|
|
||
| t.Run("graphql wrapped SAML error", func(t *testing.T) { | ||
| err := errors.Wrap(errors.New("GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization."), "error in executing query") | ||
| assert.True(t, IsSAMLSSORequired(err)) | ||
| }) | ||
| } |
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
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,56 @@ | ||
| // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| package plugin | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/mattermost/mattermost-plugin-github/server/githuberrors" | ||
| ) | ||
|
|
||
| const ( | ||
| apiErrorIDSAMLSSORequired = "saml_sso_required" | ||
| samlSSONotifiedKey = "_samlSSONotified" | ||
|
|
||
| samlSSOUserMessage = "GitHub is rejecting API requests because SAML SSO authorization is required for one or more organizations. Run `/github disconnect` and then `/github connect` again. When prompted on GitHub, authorize access for your organizations." | ||
| ) | ||
|
|
||
| func (p *Plugin) notifySAMLSSORequired(userID string) { | ||
| key := userID + samlSSONotifiedKey | ||
| var notified bool | ||
| if err := p.store.Get(key, ¬ified); err == nil && notified { | ||
| return | ||
| } | ||
|
|
||
| p.CreateBotDMPost(userID, samlSSOUserMessage, "custom_git_saml_sso") | ||
| if _, err := p.store.Set(key, true); err != nil { | ||
| p.client.Log.Warn("Failed to store SAML SSO notification state", "userID", userID, "error", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func (p *Plugin) clearSAMLSSONotification(userID string) { | ||
| if err := p.store.Delete(userID + samlSSONotifiedKey); err != nil { | ||
| p.client.Log.Debug("Failed to clear SAML SSO notification state", "userID", userID, "error", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func (p *Plugin) writeSAMLSSOErrorIfNeeded(c *UserContext, w http.ResponseWriter, err error) bool { | ||
| if !githuberrors.IsSAMLSSORequired(err) { | ||
| return false | ||
| } | ||
|
|
||
| p.notifySAMLSSORequired(c.UserID) | ||
| p.writeAPIError(w, &APIErrorResponse{ | ||
| ID: apiErrorIDSAMLSSORequired, | ||
| Message: samlSSOUserMessage, | ||
| StatusCode: http.StatusForbidden, | ||
| }) | ||
| return true | ||
| } | ||
|
|
||
| func (p *Plugin) handleGitHubAPIError(c *UserContext, err error) { | ||
| if githuberrors.IsSAMLSSORequired(err) { | ||
| p.notifySAMLSSORequired(c.UserID) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Make the notification claim atomic.
notifySAMLSSORequireddoesGet→CreateBotDMPost→Set, so two concurrent SAML failures can both observe "not notified" and each send a DM before either writes the flag.server/plugin/api.go, Lines 818-845 already invoke this path from parallel goroutines, so the dedup guarantee is racy. Claim the KV key atomically, or otherwise serialize this section across callers, before posting.🤖 Prompt for AI Agents