Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Fix sub domain cors

Smaug6739aec8a7ecfa08
diff --git a/backend/router/router.go b/backend/router/router.go
index 88b3c73c..1580c335 100644
--- a/backend/router/router.go
+++ b/backend/router/router.go
@@ -25,7 +25,39 @@ func InitRouter(app *app.App) *gin.Engine {
// Config proxy and CORS
router.SetTrustedProxies([]string{"127.0.0.1", "localhost"})
router.Use(cors.New(cors.Config{
- AllowOrigins: []string{os.Getenv("DOMAIN_CLIENT")},
+ AllowOriginFunc: func(origin string) bool {
+ // Main domain from environment variable
+ domain := os.Getenv("DOMAIN_CLIENT")
+
+ if origin == domain {
+ return true
+ }
+
+ // Allow all sub domains (www, api, etc.)
+ // simple example for mondomaine.com
+ // remove the protocol for comparison
+ originHost := origin
+ if len(originHost) > 0 {
+ // remove the protocol
+ if idx := len("http://"); len(originHost) > idx && originHost[:7] == "http://" {
+ originHost = originHost[7:]
+ } else if idx := len("https://"); len(originHost) > idx && originHost[:8] == "https://" {
+ originHost = originHost[8:]
+ }
+ }
+
+ mainDomain := domain
+ if len(mainDomain) > 0 {
+ if idx := len("http://"); len(mainDomain) > idx && mainDomain[:7] == "http://" {
+ mainDomain = mainDomain[7:]
+ } else if idx := len("https://"); len(mainDomain) > idx && mainDomain[:8] == "https://" {
+ mainDomain = mainDomain[8:]
+ }
+ }
+
+ // Allow all sub domains
+ return len(originHost) >= len(mainDomain) && originHost[len(originHost)-len(mainDomain):] == mainDomain
+ },
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
ExposeHeaders: []string{"Content-Length"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},