fix(web): serve panel SPA routes from NoRoute (#5536)

* fix(web): serve panel SPA routes from NoRoute

Return the React shell for authenticated panel document routes that are not explicitly registered in Gin, such as /panel/hosts. Keep API, CSRF, static-file, method, and Accept exclusions so API misses remain 404 and auth semantics stay unchanged.

* fix(web): remove unreachable panel path guard

The panel path is always built by appending /panel, so it can never be empty.
Remove the redundant fallback branch without changing SPA routing behavior.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix(web): allowlist static-asset extensions in SPA fallback

The blanket path.Ext check rejected any panel route whose last segment contained a dot, which would reintroduce the refresh 404 for a future client route carrying a dotted parameter (version, domain, or email-like value). Restrict the static-asset exclusion to a known, case-insensitive extension allowlist and add predicate regression cases.
This commit is contained in:
w3struk
2026-06-25 00:19:12 +05:00
committed by GitHub
parent 2830f97f50
commit ae9bbdf267
4 changed files with 320 additions and 7 deletions
+81
View File
@@ -2,6 +2,8 @@ package controller
import (
"net/http"
"path"
"strings"
"github.com/mhsanaei/3x-ui/v3/internal/web/entity"
"github.com/mhsanaei/3x-ui/v3/internal/web/middleware"
@@ -57,6 +59,85 @@ func (a *XUIController) panelSPA(c *gin.Context) {
serveDistPage(c, "index.html")
}
// HandleNoRoutePanelSPA serves the React shell for client-side routes that were
// not explicitly registered in Gin. It intentionally runs from engine.NoRoute
// instead of a /panel/*path wildcard so explicit JSON/API routes keep their
// normal routing semantics.
func (a *XUIController) HandleNoRoutePanelSPA(c *gin.Context) bool {
if !isPanelSPAFallbackRequest(c) {
return false
}
if !session.IsLogin(c) {
if isAjax(c) {
pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
} else {
c.Header("Cache-Control", "no-store")
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}
c.Abort()
return true
}
a.panelSPA(c)
return true
}
func isPanelSPAFallbackRequest(c *gin.Context) bool {
if c.Request.Method != http.MethodGet {
return false
}
if !acceptsHTML(c.GetHeader("Accept")) {
return false
}
basePath := c.GetString("base_path")
if basePath == "" {
basePath = "/"
}
panelPath := strings.TrimRight(basePath, "/") + "/panel"
reqPath := c.Request.URL.Path
if reqPath != panelPath && !strings.HasPrefix(reqPath, panelPath+"/") {
return false
}
if reqPath == panelPath+"/csrf-token" || strings.HasPrefix(reqPath, panelPath+"/csrf-token/") {
return false
}
if reqPath == panelPath+"/api" || strings.HasPrefix(reqPath, panelPath+"/api/") {
return false
}
if isStaticAssetPath(reqPath) {
return false
}
return true
}
var staticAssetExts = map[string]struct{}{
".js": {}, ".mjs": {}, ".cjs": {}, ".css": {}, ".map": {}, ".json": {},
".png": {}, ".jpg": {}, ".jpeg": {}, ".gif": {}, ".svg": {}, ".ico": {},
".webp": {}, ".avif": {}, ".woff": {}, ".woff2": {}, ".ttf": {}, ".eot": {},
".otf": {}, ".wasm": {}, ".txt": {}, ".xml": {}, ".webmanifest": {},
}
func isStaticAssetPath(reqPath string) bool {
ext := strings.ToLower(path.Ext(reqPath))
if ext == "" {
return false
}
_, ok := staticAssetExts[ext]
return ok
}
func acceptsHTML(accept string) bool {
if accept == "" {
return true
}
accept = strings.ToLower(accept)
return strings.Contains(accept, "text/html") || strings.Contains(accept, "*/*")
}
// csrfToken returns the session CSRF token to authenticated SPA clients.
// The endpoint is GET (a safe method) so it bypasses CSRFMiddleware itself,
// but checkLogin still gates the response — anonymous callers get 401/redirect.