-
Notifications
You must be signed in to change notification settings - Fork 2
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
User authentication using OpenID Connect #114
base: main
Are you sure you want to change the base?
Conversation
return errors.New("auth init failure") | ||
} | ||
|
||
redirectUrl := a.oauth2Config.AuthCodeURL("TODO:STATE") |
Check failure
Code scanning / CodeQL
Use of constant `state` value in OAuth 2.0 URL High
state string
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 4 months ago
To fix the problem, we need to replace the constant state value with a unique, non-guessable value for each authentication request. This can be achieved by generating a random state value using a secure random number generator and encoding it. Additionally, we should store this state value in a way that it can be validated later during the callback to ensure it matches the original request.
- Generate a unique state value: Create a function to generate a random state value using
crypto/rand
and encode it usingbase64.URLEncoding
. - Store the state value: Store the generated state value in a cookie or session to validate it later during the callback.
- Validate the state value: During the callback, retrieve and validate the state value to ensure it matches the original request.
-
Copy modified lines R124-R125 -
Copy modified lines R143-R148 -
Copy modified lines R187-R204
@@ -123,3 +123,4 @@ | ||
|
||
redirectUrl := a.oauth2Config.AuthCodeURL("TODO:STATE") | ||
state := generateStateOauthCookie(c.Response()) | ||
redirectUrl := a.oauth2Config.AuthCodeURL(state) | ||
// Check redirect URL to prevent invalid/recursive redirects | ||
@@ -141,2 +142,8 @@ | ||
} | ||
// Validate state | ||
stateCookie, err := c.Cookie("oauthstate") | ||
if err != nil || stateCookie.Value != httpRequest.URL.Query().Get("state") { | ||
log.Logger.Warn().Err(err).Msg("Invalid state parameter") | ||
return c.NoContent(http.StatusUnauthorized) | ||
} | ||
// Check if the user is authenticated | ||
@@ -179 +186,19 @@ | ||
} | ||
|
||
func generateStateOauthCookie(w http.ResponseWriter) string { | ||
b := make([]byte, 16) | ||
_, err := rand.Read(b) | ||
if err != nil { | ||
log.Logger.Error().Err(err).Msg("Failed to generate random state") | ||
return "" | ||
} | ||
state := base64.URLEncoding.EncodeToString(b) | ||
http.SetCookie(w, &http.Cookie{ | ||
Name: "oauthstate", | ||
Value: state, | ||
Expires: time.Now().Add(10 * time.Minute), | ||
HttpOnly: true, | ||
Path: "/", | ||
}) | ||
return state | ||
} |
No description provided.