Skip to content
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

Fix infinite redirect of UI init page #1193

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions web_ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,22 @@ func handleWebUIAuth(ctx *gin.Context) {
user, err := GetUser(ctx)

// Skip auth check for static files other than html pages
if path.Ext(requestPath) != ".html" {
if path.Ext(requestPath) != "" && path.Ext(requestPath) != ".html" {
ctx.Next()
return
}

// Handle initialization. If db is nill, then redirect user to the initialization page
if strings.HasPrefix(requestPath, "/initialization") {
if db != nil {
if db != nil { // Password initialized, redirect away from the init page
ctx.Redirect(http.StatusFound, "/view/")
ctx.Abort()
return
} else { // If not init, pass the auth handler and render the page
ctx.Next()
return
}
} else if db == nil {
} else if db == nil { // For all other paths, if the password is not initialized
ctx.Redirect(http.StatusFound, "/view/initialization/code/")
ctx.Abort()
return
Expand Down
59 changes: 57 additions & 2 deletions web_ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestHandleWebUIAuth(t *testing.T) {
route := gin.New()
route.GET("/view/*requestPath", handleWebUIAuth, func(ctx *gin.Context) { ctx.Status(200) })

t.Run("html-redirect-to-init-without-db", func(t *testing.T) {
t.Run("redirect-to-init-without-db", func(t *testing.T) {
cleanupAuthDB()
r := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/view/test.html", nil)
Expand All @@ -143,7 +143,34 @@ func TestHandleWebUIAuth(t *testing.T) {
assert.Equal(t, "/view/initialization/code/", r.Result().Header.Get("Location"))
})

t.Run("html-redirect-to-login-with-db", func(t *testing.T) {
t.Run("init-page-no-redirct-without-db", func(t *testing.T) {
cleanupAuthDB()
r := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/view/initialization/code/", nil)
require.NoError(t, err)
route.ServeHTTP(r, req)

assert.Equal(t, http.StatusOK, r.Result().StatusCode)

r = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/view/initialization/password/", nil)
require.NoError(t, err)
route.ServeHTTP(r, req)

assert.Equal(t, http.StatusOK, r.Result().StatusCode)
})

t.Run("no-redirect-without-db-on-init-page", func(t *testing.T) {
cleanupAuthDB()
r := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/view/initialization/code/", nil)
require.NoError(t, err)
route.ServeHTTP(r, req)

assert.Equal(t, http.StatusOK, r.Result().StatusCode)
})

t.Run("redirect-to-login-with-db-initialzied", func(t *testing.T) {
setupTestAuthDB(t)
t.Cleanup(cleanupAuthDB)

Expand All @@ -154,6 +181,34 @@ func TestHandleWebUIAuth(t *testing.T) {

assert.Equal(t, "/view/login/", r.Result().Header.Get("Location"))

r = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/view/origin/", nil)
require.NoError(t, err)
route.ServeHTTP(r, req)

assert.Equal(t, "/view/login/", r.Result().Header.Get("Location"))

authDB.Store(nil)
})

t.Run("init-page-redirect-to-root-with-db-initialized", func(t *testing.T) {
setupTestAuthDB(t)
t.Cleanup(cleanupAuthDB)

r := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/view/initialization/code/", nil)
require.NoError(t, err)
route.ServeHTTP(r, req)

assert.Equal(t, "/view/", r.Result().Header.Get("Location"))

r = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/view/initialization/password/", nil)
require.NoError(t, err)
route.ServeHTTP(r, req)

assert.Equal(t, "/view/", r.Result().Header.Get("Location"))

authDB.Store(nil)
})

Expand Down
Loading