-
Notifications
You must be signed in to change notification settings - Fork 0
feat: sanitize authentication errors for safer browser messages #8
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package main | ||
|
|
||
| // sanitizeOAuthError maps standard OAuth error codes to user-friendly messages | ||
| // that are safe to display in the browser. This prevents information disclosure | ||
| // while maintaining a good user experience. | ||
| // The errorDescription parameter is intentionally ignored to prevent leaking details. | ||
| func sanitizeOAuthError(errorCode, _ string) string { | ||
| switch errorCode { | ||
| case "access_denied": | ||
| return "Authorization was denied. You may close this window." | ||
| case "invalid_request": | ||
| return "Invalid request. Please contact support." | ||
| case "unauthorized_client": | ||
| return "Client is not authorized." | ||
| case "server_error": | ||
| return "Server error. Please try again later." | ||
| case "temporarily_unavailable": | ||
| return "Service is temporarily unavailable. Please try again later." | ||
| default: | ||
| return "Authentication failed. Please check your terminal for details." | ||
| } | ||
| } | ||
|
|
||
| // sanitizeTokenExchangeError sanitizes backend token exchange errors to prevent | ||
| // leaking sensitive implementation details such as service names, internal error | ||
| // codes, or validation mechanisms. | ||
| // The err parameter is intentionally ignored to prevent leaking any details. | ||
| func sanitizeTokenExchangeError(_ error) string { | ||
| // Always return a generic message to prevent information disclosure. | ||
| // The full error is still logged to the terminal for debugging. | ||
| return "Token exchange failed. Please try again." | ||
| } | ||
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,137 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSanitizeOAuthError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| errorCode string | ||
| errorDescription string | ||
| wantContains string | ||
| wantNotContains string | ||
| }{ | ||
| { | ||
| name: "access_denied", | ||
| errorCode: "access_denied", | ||
| errorDescription: "User denied the request", | ||
| wantContains: "Authorization was denied", | ||
| wantNotContains: "User denied", | ||
| }, | ||
| { | ||
| name: "invalid_request", | ||
| errorCode: "invalid_request", | ||
| errorDescription: "Missing required parameter: redirect_uri", | ||
| wantContains: "Invalid request", | ||
| wantNotContains: "redirect_uri", | ||
| }, | ||
| { | ||
| name: "unauthorized_client", | ||
| errorCode: "unauthorized_client", | ||
| errorDescription: "Client authentication failed", | ||
| wantContains: "Client is not authorized", | ||
| wantNotContains: "authentication failed", | ||
| }, | ||
| { | ||
| name: "server_error", | ||
| errorCode: "server_error", | ||
| errorDescription: "Internal database connection failed", | ||
| wantContains: "Server error", | ||
| wantNotContains: "database", | ||
| }, | ||
| { | ||
| name: "temporarily_unavailable", | ||
| errorCode: "temporarily_unavailable", | ||
| errorDescription: "Service overloaded", | ||
| wantContains: "temporarily unavailable", | ||
| wantNotContains: "overloaded", | ||
| }, | ||
| { | ||
| name: "unknown_error", | ||
| errorCode: "custom_error_code", | ||
| errorDescription: "Some internal error details", | ||
| wantContains: "Authentication failed", | ||
| wantNotContains: "internal", | ||
| }, | ||
| { | ||
| name: "empty_description", | ||
| errorCode: "access_denied", | ||
| errorDescription: "", | ||
| wantContains: "Authorization was denied", | ||
| wantNotContains: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := sanitizeOAuthError(tt.errorCode, tt.errorDescription) | ||
|
|
||
| if tt.wantContains != "" && !strings.Contains(got, tt.wantContains) { | ||
| t.Errorf("sanitizeOAuthError() = %q, want to contain %q", got, tt.wantContains) | ||
| } | ||
|
|
||
| if tt.wantNotContains != "" && strings.Contains(got, tt.wantNotContains) { | ||
| t.Errorf( | ||
| "sanitizeOAuthError() = %q, should not contain %q", | ||
| got, | ||
| tt.wantNotContains, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSanitizeTokenExchangeError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| wantContains string | ||
| wantNotContains []string | ||
| }{ | ||
| { | ||
| name: "generic_error", | ||
| err: errors.New("unauthorized_client: client authentication failed"), | ||
| wantContains: "Token exchange failed", | ||
| wantNotContains: []string{"unauthorized_client", "authentication"}, | ||
| }, | ||
| { | ||
| name: "backend_service_error", | ||
| err: errors.New("backend service error: database connection failed"), | ||
| wantContains: "Token exchange failed", | ||
| wantNotContains: []string{"backend", "database", "service"}, | ||
| }, | ||
| { | ||
| name: "internal_error", | ||
| err: errors.New("internal error: validation failed for user account"), | ||
| wantContains: "Token exchange failed", | ||
| wantNotContains: []string{"internal", "validation", "account"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := sanitizeTokenExchangeError(tt.err) | ||
|
|
||
| if !strings.Contains(got, tt.wantContains) { | ||
| t.Errorf( | ||
| "sanitizeTokenExchangeError() = %q, want to contain %q", | ||
| got, | ||
| tt.wantContains, | ||
| ) | ||
| } | ||
|
|
||
| for _, notWant := range tt.wantNotContains { | ||
| if strings.Contains(strings.ToLower(got), strings.ToLower(notWant)) { | ||
| t.Errorf( | ||
| "sanitizeTokenExchangeError() = %q, should not contain %q", | ||
| got, | ||
| notWant, | ||
| ) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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.