Harden auth: same-origin check, live admin flag, 90-day refresh
- Refuse non-GET requests whose Origin (or Sec-Fetch-Site) is not the apex or www. SameSite=Lax lets sibling subdomains such as radar. and git. send the auth cookies. The WebSocket check shares the helper and no longer accepts localhost in production. - Take the admin flag from the user row the token check already loads, so a demotion applies on the next request, not on token expiry. - Cut the refresh token lifetime from 365 to 90 days; refreshes renew it. - Docs: fix the stale revocation notes in backend/README.md and the Cloudflare Access comment in routes.caddy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -41,8 +41,8 @@ func InitAuth(config *AuthConfig) *Auth {
|
||||
return &Auth{Config: config}
|
||||
}
|
||||
|
||||
// GenerateJWT issues an access/refresh pair. Only the access token carries
|
||||
// admin; refresh re-reads the row, so a revoked admin flag applies then.
|
||||
// GenerateJWT issues an access/refresh pair. The admin claim is informational:
|
||||
// AuthenticateTokenWithClaims overwrites it from the user row.
|
||||
func (auth *Auth) GenerateJWT(user *models.User) (*Tokens, error) {
|
||||
accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"username": user.Username,
|
||||
@@ -140,6 +140,10 @@ func (auth *Auth) AuthenticateTokenWithClaims(db *gorm.DB, tokenStr string) (*mo
|
||||
return nil, nil, ErrTokenRevoked
|
||||
}
|
||||
|
||||
// The row is already loaded, so it decides admin: a demotion applies on
|
||||
// the next request rather than when the token expires.
|
||||
(*claims)["admin"] = user.Admin
|
||||
|
||||
return &user, claims, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// IsSiteOrigin reports whether an Origin header names the site itself (the
|
||||
// apex or www). In dev DOMAIN is localhost, which covers Vite on any port.
|
||||
func IsSiteOrigin(origin, domain string) bool {
|
||||
u, err := url.Parse(origin)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
return domain != "" && (host == domain || host == "www."+domain)
|
||||
}
|
||||
|
||||
// RequireSameOrigin refuses state-changing requests a browser sent from
|
||||
// elsewhere. SameSite=Lax does not cover this: radar. and git. are the same
|
||||
// site, so their pages get the auth cookies. A request with neither header is
|
||||
// not from a browser and passes.
|
||||
func RequireSameOrigin(domain string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
switch c.Request.Method {
|
||||
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
allowed := true
|
||||
if origin := c.GetHeader("Origin"); origin != "" {
|
||||
allowed = IsSiteOrigin(origin, domain)
|
||||
} else if site := c.GetHeader("Sec-Fetch-Site"); site != "" {
|
||||
allowed = site == "same-origin" || site == "none"
|
||||
}
|
||||
if !allowed {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "cross-origin request refused"})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestIsSiteOrigin(t *testing.T) {
|
||||
cases := []struct {
|
||||
origin, domain string
|
||||
want bool
|
||||
}{
|
||||
{"https://www.example.com", "example.com", true},
|
||||
{"https://example.com", "example.com", true},
|
||||
{"https://radar.example.com", "example.com", false},
|
||||
{"https://git.example.com", "example.com", false},
|
||||
{"https://www.example.com.evil.test", "example.com", false},
|
||||
{"https://evilexample.com", "example.com", false},
|
||||
{"null", "example.com", false},
|
||||
{"http://localhost:5173", "localhost", true},
|
||||
{"http://localhost:5173", "example.com", false},
|
||||
{"https://anything", "", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := IsSiteOrigin(c.origin, c.domain); got != c.want {
|
||||
t.Errorf("IsSiteOrigin(%q, %q) = %v, want %v", c.origin, c.domain, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireSameOrigin(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
cases := []struct {
|
||||
name string
|
||||
method string
|
||||
headers map[string]string
|
||||
want int
|
||||
}{
|
||||
{"GET is never checked", "GET", map[string]string{"Origin": "https://evil.test"}, 200},
|
||||
{"POST from www", "POST", map[string]string{"Origin": "https://www.example.com"}, 200},
|
||||
{"POST from a sibling subdomain", "POST", map[string]string{"Origin": "https://radar.example.com"}, 403},
|
||||
{"POST from another site", "POST", map[string]string{"Origin": "https://evil.test"}, 403},
|
||||
{"POST with an opaque origin", "POST", map[string]string{"Origin": "null"}, 403},
|
||||
{"DELETE from another site", "DELETE", map[string]string{"Origin": "https://evil.test"}, 403},
|
||||
{"no Origin, same-site fetch", "POST", map[string]string{"Sec-Fetch-Site": "same-site"}, 403},
|
||||
{"no Origin, same-origin fetch", "POST", map[string]string{"Sec-Fetch-Site": "same-origin"}, 200},
|
||||
{"no browser headers (curl, server to server)", "POST", nil, 200},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
r := gin.New()
|
||||
r.Use(RequireSameOrigin("example.com"))
|
||||
r.Handle(c.method, "/x", func(ctx *gin.Context) { ctx.Status(200) })
|
||||
req := httptest.NewRequest(c.method, "/x", nil)
|
||||
for k, v := range c.headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
if rec.Code != c.want {
|
||||
t.Fatalf("status %d, want %d", rec.Code, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package services
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -28,15 +27,7 @@ var Upgrader = websocket.Upgrader{
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
origin := r.Header.Get("Origin")
|
||||
if origin == "" {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(origin)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
return host == allowedDomain || host == "www."+allowedDomain || host == "localhost"
|
||||
return origin != "" && IsSiteOrigin(origin, allowedDomain)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user