π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Enforce configured browser origins
Manav Rathi2818807f1b74
diff --git a/docs/docs/self-hosting/installation/config.md b/docs/docs/self-hosting/installation/config.mdindex 86c6947507..7415fb1f41 100644--- a/docs/docs/self-hosting/installation/config.md+++ b/docs/docs/self-hosting/installation/config.md@@ -15,6 +15,10 @@ By default, Museum runs in local environment, thus `local.yaml` configuration isIf `ENVIRONMENT` environment variable is set (say, to `production`), Museum will attempt to load `configurations/production.yaml`.+> [!NOTE]+>+> Ente uses the `production` configuration for its own deployments, and it may change between releases.+If `credentials-file` is defined and found, it overrides the defaults.Use `museum.yaml` file for declaring configuration over `local.yaml`.@@ -53,6 +57,8 @@ in `museum.yaml` is read as `s3.b2-eu-cen.endpoint` by Museum.The `apps` section tells Museum where each Ente web app is hosted. Configure the public URL of every web app you deploy; these URLs form Museum's list of trusted browser origins.+If you set any of `apps.photos`, `apps.auth`, `apps.locker` or `apps.extra-origins`, Museum rejects browser requests from origins that are not in this list. If you set none of these keys, Museum only logs the unknown origins. This is a fallback so that older setups do not break; a future version of Museum will reject unknown origins by default. To choose the behavior explicitly, set `apps.cors-report-only` to `true` or `false`.+| Variable | Description | Default || ---------------------- | ------------------------------------------------------- | --------------------------- || `apps.photos` | Photos web app base endpoint | `https://photos.ente.com` |@@ -70,7 +76,7 @@ The `apps` section tells Museum where each Ente web app is hosted. Configure the| `apps.family` | Family web app base endpoint | `https://family.ente.io` || `apps.space` | Space web app base endpoint | `https://ente.space` || `apps.legacy` | Legacy recovery web app base endpoint | `https://legacy.ente.com` |-| `apps.extra-origins` | Additional trusted browser origins | `["https://ente.com", "https://embed.ente.io"]` |+| `apps.extra-origins` | Additional trusted browser origins | Ente's other production origins |### Databasediff --git a/docs/docs/self-hosting/installation/post-install/index.md b/docs/docs/self-hosting/installation/post-install/index.mdindex c360fc939c..73c1c165c1 100644--- a/docs/docs/self-hosting/installation/post-install/index.md+++ b/docs/docs/self-hosting/installation/post-install/index.md@@ -107,7 +107,9 @@ apps:legacy: https://legacy.ente.yourdomain.tld```-If you are using quickstart, note that only Photos and Albums are exposed by default in `compose.yaml`. Uncomment the Share / Paste / Memories port mappings there, or deploy them separately, before pointing `apps.public-locker` / `apps.public-paste` / `apps.public-memories` to those URLs.+When you set these keys, Museum rejects browser requests from origins that are not in the list. List every web app that you deploy. A web app that is not in the list will get 403 errors in the browser.++If you are using quickstart, note that only Photos and Albums are exposed by default in `compose.yaml`. Uncomment the port mappings of the other apps there, or deploy them separately, before pointing their `apps.*` keys to those URLs.## Step 4: Make it publicly accessiblediff --git a/docs/docs/self-hosting/troubleshooting/misc.md b/docs/docs/self-hosting/troubleshooting/misc.mdindex 53fcbe561f..78870d220c 100644--- a/docs/docs/self-hosting/troubleshooting/misc.md+++ b/docs/docs/self-hosting/troubleshooting/misc.md@@ -21,6 +21,14 @@ How do you know if this is the problem you're facing? The browser console _mightThis is not guaranteed, each browsers handles CSP errors differently, and some may silently swallow it.+## Browser requests are rejected with 403++Museum only accepts browser requests from the origins listed in the `apps` section of `museum.yaml`. If a request comes from an origin that is not in the list, Museum rejects it with a 403 status code and logs `rejecting unknown CORS origin`.++To fix this, add the URL of the web app to the `apps` section. Learn more in [App Endpoints](/self-hosting/installation/config#app-endpoints).++This check is separate from the CORS configuration of your S3 bucket. The bucket configuration affects uploads and downloads; this check affects API requests to Museum.+## Storage not freed after deleting filesDeleted files don't immediately free up S3/MinIO storage:diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.goindex fe349f9ddb..be42c6f778 100644--- a/server/cmd/museum/main.go+++ b/server/cmd/museum/main.go@@ -113,6 +113,11 @@ func main() {setupLogger(environment)log.Infof("Booting up %s server with commit #%s", environment, os.Getenv("GIT_COMMIT"))+ if viper.GetBool("apps.cors-report-only") {+ log.Info("CORS report-only: allowing unknown browser origins (configure apps.* to enforce)")+ } else {+ log.Info("CORS: rejecting unknown browser origins")+ }secretEncryptionKey := viper.GetString("key.encryption")hashingKey := viper.GetString("key.hash")@@ -1089,10 +1094,16 @@ func main() {}func setAppDefaults() {+ // In the future, the default will become:+ // viper.SetDefault("apps.cors-report-only", false)+ viper.SetDefault("apps.cors-report-only", viper.GetString("apps.photos") == "" &&+ viper.GetString("apps.auth") == "" &&+ viper.GetString("apps.locker") == "" &&+ len(viper.GetStringSlice("apps.extra-origins")) == 0)+viper.SetDefault("apps.photos", "https://photos.ente.com")viper.SetDefault("apps.public-albums", "https://albums.ente.com")viper.SetDefault("apps.embed-albums", "https://embed.ente.com")- viper.SetDefault("apps.extra-origins", []string{"https://ente.com", "https://embed.ente.io"})viper.SetDefault("apps.auth", "https://auth.ente.com")viper.SetDefault("apps.locker", "https://locker.ente.com")viper.SetDefault("apps.public-locker", "https://share.ente.com")@@ -1105,6 +1116,7 @@ func setAppDefaults() {viper.SetDefault("apps.family", "https://family.ente.io")viper.SetDefault("apps.space", "https://ente.space")viper.SetDefault("apps.legacy", "https://legacy.ente.com")+ viper.SetDefault("apps.extra-origins", []string{"https://ente.com", "https://embed.ente.io", "https://cast.ente.io", "https://staff.ente.sh"})viper.SetDefault("apps.custom-domain.cname", "my.ente.com")}@@ -1416,10 +1428,10 @@ func cors() gin.HandlerFunc {}}origins = append(origins, viper.GetStringSlice("apps.extra-origins")...)- return corsForOrigins(origins)+ return corsForOrigins(origins, !viper.GetBool("apps.cors-report-only"))}-func corsForOrigins(origins []string) gin.HandlerFunc {+func corsForOrigins(origins []string, enforce bool) gin.HandlerFunc {knownOrigins := make(map[string]bool, len(origins))for _, origin := range origins {if origin = normalizeOrigin(origin); origin != "" {@@ -1438,12 +1450,22 @@ func corsForOrigins(origins []string) gin.HandlerFunc {return func(c *gin.Context) {origin := c.GetHeader("Origin")knownOrigin := isKnownOrigin(c, origin)- if c.Request.Method != http.MethodOptions && !knownOrigin {- log.WithFields(log.Fields{- "clientPackage": c.GetHeader("X-Client-Package"),- "origin": origin,- "path": c.FullPath(),- }).Warn("unknown CORS origin")+ if !knownOrigin {+ if c.Request.Method != http.MethodOptions || enforce {+ message := "unknown CORS origin"+ if enforce {+ message = "rejecting unknown CORS origin"+ }+ log.WithFields(log.Fields{+ "clientPackage": c.GetHeader("X-Client-Package"),+ "origin": origin,+ "path": c.FullPath(),+ }).Warn(message)+ }+ if enforce {+ c.AbortWithStatus(http.StatusForbidden)+ return+ }}c.Writer.Header().Set("Access-Control-Allow-Origin", origin)diff --git a/server/cmd/museum/main_cors_test.go b/server/cmd/museum/main_cors_test.goindex 5a48043012..efb64cd40b 100644--- a/server/cmd/museum/main_cors_test.go+++ b/server/cmd/museum/main_cors_test.go@@ -23,24 +23,28 @@ func TestCORS(t *testing.T) {wantStatus intwantHandle boolwantWarn bool+ enforce bool}{- {"configured origin", http.MethodGet, "/users/details", "https://photos.ente.com", "api.ente.com", http.StatusNoContent, true, false},- {"unknown origin", http.MethodGet, "/users/details", "https://unknown.example", "api.ente.com", http.StatusNoContent, true, true},- {"malformed origin", http.MethodGet, "/users/details", "%", "api.ente.com", http.StatusNoContent, true, true},- {"native client", http.MethodGet, "/users/details", "", "api.ente.com", http.StatusNoContent, true, false},- {"unconfigured same host", http.MethodPost, "/users/ott", "https://ente.example", "ente.example", http.StatusNoContent, true, true},- {"desktop app", http.MethodGet, "/users/details", "ente://app", "api.ente.com", http.StatusNoContent, true, false},- {"localhost", http.MethodGet, "/users/details", "http://localhost:3000", "api.ente.com", http.StatusNoContent, true, false},- {"localhost subdomain", http.MethodGet, "/users/details", "https://photos.localhost:3000", "api.ente.com", http.StatusNoContent, true, false},- {"IPv4 loopback", http.MethodGet, "/users/details", "http://127.1.2.3:3000", "api.ente.com", http.StatusNoContent, true, false},- {"IPv6 loopback", http.MethodGet, "/users/details", "http://[::1]:3000", "api.ente.com", http.StatusNoContent, true, false},- {"opaque origin", http.MethodGet, "/users/details", "null", "api.ente.com", http.StatusNoContent, true, true},- {"private address", http.MethodGet, "/users/details", "http://192.168.1.2:3000", "api.ente.com", http.StatusNoContent, true, true},- {"localhost lookalike", http.MethodGet, "/users/details", "http://localhost.example:3000", "api.ente.com", http.StatusNoContent, true, true},- {"non-HTTP loopback", http.MethodGet, "/users/details", "ftp://localhost", "api.ente.com", http.StatusNoContent, true, true},- {"custom album domain", http.MethodGet, "/public-collection/info", "https://gallery.example", "api.ente.com", http.StatusNoContent, true, false},- {"album prefix lookalike", http.MethodGet, "/public-collection-evil", "https://gallery.example", "api.ente.com", http.StatusNoContent, true, true},- {"unknown preflight", http.MethodOptions, "/users/details", "https://unknown.example", "api.ente.com", http.StatusOK, false, false},+ {"configured origin", http.MethodGet, "/users/details", "https://photos.ente.com", "api.ente.com", http.StatusNoContent, true, false, false},+ {"unknown origin", http.MethodGet, "/users/details", "https://unknown.example", "api.ente.com", http.StatusNoContent, true, true, false},+ {"malformed origin", http.MethodGet, "/users/details", "%", "api.ente.com", http.StatusNoContent, true, true, false},+ {"native client", http.MethodGet, "/users/details", "", "api.ente.com", http.StatusNoContent, true, false, false},+ {"unconfigured same host", http.MethodPost, "/users/ott", "https://ente.example", "ente.example", http.StatusNoContent, true, true, false},+ {"desktop app", http.MethodGet, "/users/details", "ente://app", "api.ente.com", http.StatusNoContent, true, false, false},+ {"localhost", http.MethodGet, "/users/details", "http://localhost:3000", "api.ente.com", http.StatusNoContent, true, false, false},+ {"localhost subdomain", http.MethodGet, "/users/details", "https://photos.localhost:3000", "api.ente.com", http.StatusNoContent, true, false, false},+ {"IPv4 loopback", http.MethodGet, "/users/details", "http://127.1.2.3:3000", "api.ente.com", http.StatusNoContent, true, false, false},+ {"IPv6 loopback", http.MethodGet, "/users/details", "http://[::1]:3000", "api.ente.com", http.StatusNoContent, true, false, false},+ {"opaque origin", http.MethodGet, "/users/details", "null", "api.ente.com", http.StatusNoContent, true, true, false},+ {"private address", http.MethodGet, "/users/details", "http://192.168.1.2:3000", "api.ente.com", http.StatusNoContent, true, true, false},+ {"localhost lookalike", http.MethodGet, "/users/details", "http://localhost.example:3000", "api.ente.com", http.StatusNoContent, true, true, false},+ {"non-HTTP loopback", http.MethodGet, "/users/details", "ftp://localhost", "api.ente.com", http.StatusNoContent, true, true, false},+ {"custom album domain", http.MethodGet, "/public-collection/info", "https://gallery.example", "api.ente.com", http.StatusNoContent, true, false, false},+ {"album prefix lookalike", http.MethodGet, "/public-collection-evil", "https://gallery.example", "api.ente.com", http.StatusNoContent, true, true, false},+ {"unknown preflight", http.MethodOptions, "/users/details", "https://unknown.example", "api.ente.com", http.StatusOK, false, false, false},+ {"enforced configured origin", http.MethodGet, "/users/details", "https://photos.ente.com", "api.ente.com", http.StatusNoContent, true, false, true},+ {"enforced unknown origin", http.MethodGet, "/users/details", "https://unknown.example", "api.ente.com", http.StatusForbidden, false, true, true},+ {"enforced unknown preflight", http.MethodOptions, "/users/details", "https://unknown.example", "api.ente.com", http.StatusForbidden, false, true, true},}for _, tt := range tests {@@ -53,7 +57,7 @@ func TestCORS(t *testing.T) {handled := falseserver := gin.New()- server.Use(corsForOrigins([]string{"https://photos.ente.com", "ente://app"}))+ server.Use(corsForOrigins([]string{"https://photos.ente.com", "ente://app"}, tt.enforce))handler := func(c *gin.Context) {handled = truec.Status(http.StatusNoContent)@@ -72,8 +76,12 @@ func TestCORS(t *testing.T) {if resp.Code != tt.wantStatus {t.Errorf("status = %d, want %d", resp.Code, tt.wantStatus)}- if got := resp.Header().Get("Access-Control-Allow-Origin"); got != tt.origin {- t.Errorf("allowed origin = %q, want %q", got, tt.origin)+ wantOrigin := tt.origin+ if tt.wantStatus == http.StatusForbidden {+ wantOrigin = ""+ }+ if got := resp.Header().Get("Access-Control-Allow-Origin"); got != wantOrigin {+ t.Errorf("allowed origin = %q, want %q", got, wantOrigin)}if handled != tt.wantHandle {t.Errorf("handler called = %t, want %t", handled, tt.wantHandle)@@ -107,6 +115,8 @@ func TestCORSProductionOriginsAreDefaults(t *testing.T) {"https://embed.ente.com","https://ente.com","https://embed.ente.io",+ "https://cast.ente.io",+ "https://staff.ente.sh","https://auth.ente.com","https://locker.ente.com","https://share.ente.com",@@ -135,6 +145,37 @@ func TestCORSProductionOriginsAreDefaults(t *testing.T) {}}+func TestCORSReportOnlyDefault(t *testing.T) {+ tests := []struct {+ name string+ values map[string]interface{}+ reportOnly bool+ }{+ {"legacy configuration", nil, true},+ {"blank web apps", map[string]interface{}{"apps.photos": "", "apps.auth": "", "apps.locker": ""}, true},+ {"photos configured", map[string]interface{}{"apps.photos": "https://photos.example"}, false},+ {"auth configured", map[string]interface{}{"apps.auth": "https://auth.example"}, false},+ {"locker configured", map[string]interface{}{"apps.locker": "https://locker.example"}, false},+ {"extra origin configured", map[string]interface{}{"apps.extra-origins": []string{"https://extra.example"}}, false},+ {"explicit report-only", map[string]interface{}{"apps.photos": "https://photos.example", "apps.cors-report-only": true}, true},+ {"explicit enforcement", map[string]interface{}{"apps.cors-report-only": false}, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ viper.Reset()+ t.Cleanup(viper.Reset)+ for key, value := range tt.values {+ viper.Set(key, value)+ }+ setAppDefaults()+ if got := viper.GetBool("apps.cors-report-only"); got != tt.reportOnly {+ t.Errorf("report-only = %t, want %t", got, tt.reportOnly)+ }+ })+ }+}+func TestCORSUsesConfiguredAppOrigins(t *testing.T) {viper.Reset()viper.Set("apps.photos", "https://photos.example/app")diff --git a/server/configurations/local.yaml b/server/configurations/local.yamlindex 69bd697a48..37f6ab278a 100644--- a/server/configurations/local.yaml+++ b/server/configurations/local.yaml@@ -150,7 +150,15 @@ apps:# Default is:# - https://ente.com# - https://embed.ente.io+ # - https://cast.ente.io+ # - https://staff.ente.shextra-origins:+ # Default is true if none of photos, auth, locker or extra-origins above+ # are set, and false otherwise.+ #+ # When false, museum rejects browser requests from origins not listed in+ # this section. When true, it only logs them.+ cors-report-only:custom-domain:# Default is my.ente.comcname: