mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-16 23:27:14 +00:00
fix(api): return 401 for invalid Bearer token instead of 404 (#6459)
When Authorization: Bearer is present but does not match (or is disabled), respond with 401 Unauthorized so script authors can distinguish auth failure from a wrong webBasePath. Requests with no Authorization header still get 404 masking; wrong base paths continue to 404 via NoRoute. Fixes #6255 Co-authored-by: mrchatam <mrchatam@users.noreply.github.com>
This commit is contained in:
@@ -61,7 +61,11 @@ func (a *APIController) checkAPIAuth(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
if !session.IsLogin(c) {
|
||||
if c.GetHeader("X-Requested-With") == "XMLHttpRequest" {
|
||||
// A presented Bearer token is not an anonymous scan: return 401 so
|
||||
// callers can distinguish a bad/disabled token from a wrong base path
|
||||
// (NoRoute still 404s). XHR keeps 401; bare unauthenticated stays 404.
|
||||
authHdr := c.GetHeader("Authorization")
|
||||
if strings.HasPrefix(authHdr, "Bearer ") || c.GetHeader("X-Requested-With") == "XMLHttpRequest" {
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
} else {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
|
||||
Reference in New Issue
Block a user