fix(mfa): refuse rather than panic when RequireStepUp sees no session

This commit is contained in:
2026-09-16 09:03:22 +00:00
parent e2ff0dace9
commit bd0639acfa
2 changed files with 24 additions and 0 deletions
+4
View File
@@ -41,6 +41,10 @@ func RequireStepUp() gin.HandlerFunc {
c.Next()
return
}
if sess == nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authenticated"})
return
}
methods, err := services.MFAMethods(sess.InstanceID, sess.UserID)
if err != nil || len(methods) == 0 {
methods = []string{services.FactorPassword}
+20
View File
@@ -1,8 +1,12 @@
package auth
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func TestStepUpFresh(t *testing.T) {
@@ -32,3 +36,19 @@ func TestStepUpFresh(t *testing.T) {
})
}
}
// A missing session must refuse rather than panic dereferencing sess.
// auth.Middleware guarantees a non-nil session on every route today, but
// RequireStepUp must not rely on that holding forever.
func TestRequireStepUpNilSessionRefusesWithoutPanic(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/console/connect", nil)
RequireStepUp()(c)
if w.Code != http.StatusUnauthorized {
t.Fatalf("status = %d, want %d", w.Code, http.StatusUnauthorized)
}
}