Skip to content

Commit ac207bc

Browse files
committed
Block ability to use '*'. replace origins string to a list to config. enable cors for specific endpoints
1 parent 68ce697 commit ac207bc

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

config-example.yaml

+8-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ grpc_listen_addr: 127.0.0.1:50443
4040
# are doing.
4141
grpc_allow_insecure: false
4242

43-
# The Access-Control-Allow-Origin header specifies which origins are allowed to access resources.
43+
# The allow_origins list will allow you to set the Access-Control-Allow-Origin header to the origin in the list.
44+
# This will allow you to enable cors and set headscale without a reverse proxy.
45+
# Multiple origins can be set in the allow_origins list.
4446
# Options:
45-
# - "*" to allow access from any origin (not recommended for sensitive data).
46-
# - "http://example.com" to only allow access from a specific origin.
47-
# - "" to disable Cross-Origin Resource Sharing (CORS).
48-
access_control_allow_origin: ""
47+
# - "*" is disabled (due to security risks).
48+
# - "https://example.com" to only allow access from a specific origin.
49+
# - "https://example.com:1234" to allow access from a specific origin with a port.
50+
cors:
51+
allow_origins: []
4952

5053
# The Noise section includes specific configuration for the
5154
# TS2021 Noise protocol

hscontrol/app.go

+47-2
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,63 @@ func (h *Headscale) ensureUnixSocketIsAbsent() error {
455455
return os.Remove(h.cfg.UnixSocket)
456456
}
457457

458+
// corsHeaderMiddleware will add an "Access-Control-Allow-Origin" to enable CORS.
458459
func (h *Headscale) corsHeadersMiddleware(next http.Handler) http.Handler {
459460
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
460-
w.Header().Set("Access-Control-Allow-Origin", h.cfg.AccessControlAllowOrigins)
461+
// skip disabled CORS endpoints
462+
if !h.enabledCorsRoutes(r.URL.Path) {
463+
next.ServeHTTP(w, r)
464+
return
465+
}
466+
467+
origin := r.Header.Get("Origin")
468+
// we compare origin from the allowed Origins list. Then add the header with origin
469+
for _, allowedOrigin := range h.cfg.AllowedOrigins.Origins {
470+
if allowedOrigin == origin {
471+
w.Header().Set("Vary", "Origin")
472+
w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
473+
break
474+
}
475+
}
461476
next.ServeHTTP(w, r)
462477
})
463478
}
464479

480+
func (h *Headscale) enabledCorsRoutes(routerPath string) bool {
481+
// enable all api endpoints
482+
if strings.HasPrefix(routerPath, "/api/") {
483+
return true
484+
}
485+
486+
// A list of enabled CORS endpoints
487+
enabledRoutes := []string{
488+
"/health",
489+
"/key",
490+
"/register/{registration_id}",
491+
"/oidc/callback",
492+
"/verify",
493+
"/derp",
494+
"/derp/probe",
495+
"/derp/latency-check",
496+
"/bootstrap-dns",
497+
"/machine/register",
498+
"/machine/map",
499+
}
500+
501+
for _, routes := range enabledRoutes {
502+
if routes == routerPath {
503+
return true
504+
}
505+
}
506+
507+
return false
508+
}
509+
465510
func (h *Headscale) createRouter(grpcMux *grpcRuntime.ServeMux) *mux.Router {
466511
router := mux.NewRouter()
467512
router.Use(prometheusMiddleware)
468513

469-
if h.cfg.AccessControlAllowOrigins != "" {
514+
if len(h.cfg.AllowedOrigins.Origins) != 0 {
470515
router.Use(h.corsHeadersMiddleware)
471516
}
472517

hscontrol/types/config.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type Config struct {
6666
Log LogConfig
6767
DisableUpdateCheck bool
6868

69-
AccessControlAllowOrigins string
69+
AllowedOrigins CorsConfig
7070

7171
Database DatabaseConfig
7272

@@ -210,6 +210,10 @@ type LogTailConfig struct {
210210
Enabled bool
211211
}
212212

213+
type CorsConfig struct {
214+
Origins []string
215+
}
216+
213217
type CLIConfig struct {
214218
Address string
215219
APIKey string
@@ -534,6 +538,14 @@ func logtailConfig() LogTailConfig {
534538
}
535539
}
536540

541+
func corsConfig() CorsConfig {
542+
allowedOrigins := viper.GetStringSlice("cors.allowed_origins")
543+
544+
return CorsConfig{
545+
Origins: allowedOrigins,
546+
}
547+
}
548+
537549
func policyConfig() PolicyConfig {
538550
policyPath := viper.GetString("policy.path")
539551
policyMode := viper.GetString("policy.mode")
@@ -907,7 +919,7 @@ func LoadServerConfig() (*Config, error) {
907919
GRPCAllowInsecure: viper.GetBool("grpc_allow_insecure"),
908920
DisableUpdateCheck: false,
909921

910-
AccessControlAllowOrigins: viper.GetString("access_control_allow_origin"),
922+
AllowedOrigins: corsConfig(),
911923

912924
PrefixV4: prefix4,
913925
PrefixV6: prefix6,

0 commit comments

Comments
 (0)