fix: restrict CORS to same-origin (closes #23)

In dev mode, keep the wildcard origin for local testing convenience.
In production, skip CORS headers entirely since the web UI is
server-rendered and cross-origin requests are not expected.
This commit is contained in:
clawbot 2026-03-01 16:36:56 -08:00
parent 348fd81fe6
commit 45228d9e99

View File

@ -108,18 +108,22 @@ func (s *Middleware) Logging() func(http.Handler) http.Handler {
} }
func (s *Middleware) CORS() func(http.Handler) http.Handler { func (s *Middleware) CORS() func(http.Handler) http.Handler {
return cors.Handler(cors.Options{ if s.params.Config.IsDev() {
// CHANGEME! these are defaults, change them to suit your needs or // In development, allow any origin for local testing.
// read from environment/viper. return cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts AllowedOrigins: []string{"*"},
AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true }, AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, ExposedHeaders: []string{"Link"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, AllowCredentials: false,
ExposedHeaders: []string{"Link"}, MaxAge: 300,
AllowCredentials: false, })
MaxAge: 300, // Maximum value not ignored by any of major browsers }
}) // In production, the web UI is server-rendered so cross-origin
// requests are not expected. Return a no-op middleware.
return func(next http.Handler) http.Handler {
return next
}
} }
// RequireAuth returns middleware that checks for a valid session. // RequireAuth returns middleware that checks for a valid session.