Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Warn on unknown CORS origins

Manav Rathi588fa327f15c
diff --git a/docs/docs/self-hosting/installation/config.md b/docs/docs/self-hosting/installation/config.md
index eb59618f72..59efa0fa82 100644
--- a/docs/docs/self-hosting/installation/config.md
+++ b/docs/docs/self-hosting/installation/config.md
@@ -51,21 +51,26 @@ in `museum.yaml` is read as `s3.b2-eu-cen.endpoint` by Museum.
### App Endpoints
-The web apps for Ente (Accounts, Cast, Albums, Share, Paste, Embed, Memories) use different endpoints.
-
-These endpoints are configurable in `museum.yaml` under the apps.\* section.
-
-Upon configuration, the application will start utilizing the specified endpoints instead of Ente's production instances or local endpoints (overridden values used for Compose and quickstart for ease of use.)
+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.
| Variable | Description | Default |
| ---------------------- | ------------------------------------------------------- | --------------------------- |
+| `apps.photos` | Photos web app base endpoint | `https://photos.ente.com` |
| `apps.public-albums` | Albums app base endpoint for public sharing | `https://albums.ente.com` |
+| `apps.embed-albums` | Embed app base endpoint | `https://embed.ente.com` |
+| `apps.auth` | Auth web app base endpoint | `https://auth.ente.com` |
+| `apps.locker` | Locker web app base endpoint | `https://locker.ente.com` |
| `apps.public-locker` | Public Locker (share) app base endpoint | `https://share.ente.com` |
| `apps.public-paste` | Ente Paste app base endpoint | `https://paste.ente.com` |
| `apps.public-memories` | Memories app base endpoint for public memory shares | `https://memories.ente.com` |
-| `apps.cast` | Cast app base endpoint | `https://cast.ente.com` |
-| `apps.embed-albums` | Embed app base endpoint for embedded sharing | `https://embed.ente.com` |
| `apps.accounts` | Accounts app base endpoint (used for passkey-based 2FA) | `https://accounts.ente.com` |
+| `apps.accounts-legacy` | Legacy Accounts endpoint for passkey migration | `https://accounts.ente.io` |
+| `apps.payments` | Payments web app base endpoint | `https://payments.ente.com` |
+| `apps.cast` | Cast app base endpoint | `https://cast.ente.com` |
+| `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://embed.ente.io"]` |
### Database
diff --git a/docs/docs/self-hosting/installation/env-var.md b/docs/docs/self-hosting/installation/env-var.md
index 5a20b4fe00..9dcb17aacc 100644
--- a/docs/docs/self-hosting/installation/env-var.md
+++ b/docs/docs/self-hosting/installation/env-var.md
@@ -9,7 +9,7 @@ The environment variables needed for running Ente and the default configuration
## Environment Variables
-A self-hosted Ente web app only needs the Museum API endpoint. Web app origins are configured in `museum.yaml` under `apps`.
+Point each self-hosted web app at your Museum API endpoint, and add the app's public URL to the `apps` section of `museum.yaml`.
This document outlines the essential environment variables and port mappings of the web apps.
diff --git a/docs/docs/self-hosting/installation/post-install/index.md b/docs/docs/self-hosting/installation/post-install/index.md
index 4684aa0581..c360fc939c 100644
--- a/docs/docs/self-hosting/installation/post-install/index.md
+++ b/docs/docs/self-hosting/installation/post-install/index.md
@@ -89,18 +89,22 @@ If running Museum without Docker, the code should be visible in the terminal (st
You may wish to access some of the applications such as Albums, Share, Paste, Cast, Accounts via your instance's endpoints through the application instead of our production instances.
-You can do so by editing the `apps` section in `museum.yaml` to use the base endpoints of the corresponding web applications.
+Configure every web app you deploy in the `apps` section of `museum.yaml`.
```yaml
# Replace yourdomain.tld with actual domain
apps:
+ photos: https://photos.ente.yourdomain.tld
public-albums: https://albums.ente.yourdomain.tld
+ auth: https://auth.ente.yourdomain.tld
+ locker: https://locker.ente.yourdomain.tld
public-locker: https://share.ente.yourdomain.tld
public-paste: https://paste.ente.yourdomain.tld
public-memories: https://memories.ente.yourdomain.tld
cast: https://cast.ente.yourdomain.tld
embed-albums: https://embed.ente.yourdomain.tld
accounts: https://accounts.ente.yourdomain.tld
+ 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.
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index fbb6a2d2ba..3c796985f2 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -6,6 +6,7 @@ import (
b64 "encoding/base64"
"fmt"
"net/http"
+ "net/url"
"os"
"os/signal"
"path"
@@ -107,17 +108,7 @@ func main() {
panic(err)
}
- viper.SetDefault("apps.public-albums", "https://albums.ente.com")
- viper.SetDefault("apps.embed-albums", "https://embed.ente.com")
- viper.SetDefault("apps.custom-domain.cname", "my.ente.com")
- viper.SetDefault("apps.public-locker", "https://share.ente.com")
- viper.SetDefault("apps.public-paste", "https://paste.ente.com")
- viper.SetDefault("apps.public-memories", "https://memories.ente.com")
- viper.SetDefault("apps.accounts", "https://accounts.ente.com")
- viper.SetDefault("apps.accounts-legacy", "https://accounts.ente.io")
- viper.SetDefault("apps.cast", "https://cast.ente.com")
- viper.SetDefault("apps.family", "https://family.ente.io")
- viper.SetDefault("apps.space", "https://ente.space")
+ setAppDefaults()
setupLogger(environment)
log.Infof("Booting up %s server with commit #%s", environment, os.Getenv("GIT_COMMIT"))
@@ -1096,6 +1087,26 @@ func main() {
discordController.NotifyShutdown()
}
+func setAppDefaults() {
+ 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://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")
+ viper.SetDefault("apps.public-paste", "https://paste.ente.com")
+ viper.SetDefault("apps.public-memories", "https://memories.ente.com")
+ viper.SetDefault("apps.accounts", "https://accounts.ente.com")
+ viper.SetDefault("apps.accounts-legacy", "https://accounts.ente.io")
+ viper.SetDefault("apps.payments", "https://payments.ente.com")
+ viper.SetDefault("apps.cast", "https://cast.ente.com")
+ 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.custom-domain.cname", "my.ente.com")
+}
+
func runServer(environment string, server *gin.Engine) {
useTLS := viper.GetBool("http.use-tls")
if useTLS {
@@ -1396,8 +1407,48 @@ func setupAndStartCrons(userAuthRepo *repo.UserAuthRepository, collectionLinkRep
}
func cors() gin.HandlerFunc {
+ origins := []string{"ente://app"}
+ for _, value := range viper.GetStringMap("apps") {
+ if origin, ok := value.(string); ok {
+ origins = append(origins, origin)
+ }
+ }
+ origins = append(origins, viper.GetStringSlice("apps.extra-origins")...)
+ return corsForOrigins(origins)
+}
+
+func corsForOrigins(origins []string) gin.HandlerFunc {
+ knownOrigins := make(map[string]bool, len(origins))
+ for _, origin := range origins {
+ if origin = normalizeOrigin(origin); origin != "" {
+ knownOrigins[origin] = true
+ }
+ }
+ isKnownOrigin := func(c *gin.Context, origin string) bool {
+ if origin == "" || knownOrigins[normalizeOrigin(origin)] {
+ return true
+ }
+ parsed, err := url.Parse(origin)
+ if err == nil && strings.EqualFold(parsed.Host, c.Request.Host) {
+ return true
+ }
+ // CollectionLinkMiddleware validates custom album domains.
+ path := c.Request.URL.Path
+ return path == "/public-collection" || strings.HasPrefix(path, "/public-collection/")
+ }
+
return func(c *gin.Context) {
- c.Writer.Header().Set("Access-Control-Allow-Origin", c.GetHeader("Origin"))
+ 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")
+ }
+
+ c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, X-Auth-Token, X-Space-Session-Token, X-Auth-Access-Token, X-Cast-Access-Token, X-Auth-Access-Token-JWT, X-Auth-Link-Device-Token, X-Client-Package, X-Client-Version, X-Paste-Consume, Authorization, accept, origin, Cache-Control, X-Requested-With, upgrade-insecure-requests, Range")
c.Writer.Header().Set("Access-Control-Expose-Headers", "X-Request-Id, X-Ente-Link-Device-Token")
@@ -1414,6 +1465,14 @@ func cors() gin.HandlerFunc {
}
}
+func normalizeOrigin(origin string) string {
+ parsed, err := url.Parse(strings.TrimSpace(origin))
+ if err != nil || parsed.Scheme == "" || parsed.Host == "" {
+ return ""
+ }
+ return strings.ToLower(parsed.Scheme + "://" + parsed.Host)
+}
+
func cacheHeaders() gin.HandlerFunc {
return func(c *gin.Context) {
// Add "Cache-Control: no-store" to HTTP GET API responses.
diff --git a/server/cmd/museum/main_cors_test.go b/server/cmd/museum/main_cors_test.go
new file mode 100644
index 0000000000..7b0f1bdcd6
--- /dev/null
+++ b/server/cmd/museum/main_cors_test.go
@@ -0,0 +1,152 @@
+package main
+
+import (
+ "bytes"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ log "github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
+)
+
+func TestCORS(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ tests := []struct {
+ name string
+ method string
+ path string
+ origin string
+ host string
+ wantStatus int
+ wantHandle bool
+ wantWarn 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},
+ {"same host", http.MethodPost, "/users/ott", "https://ente.example", "ente.example", http.StatusNoContent, true, false},
+ {"desktop app", http.MethodGet, "/users/details", "ente://app", "api.ente.com", http.StatusNoContent, true, false},
+ {"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},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ var logs bytes.Buffer
+ logger := log.StandardLogger()
+ oldOutput := logger.Out
+ logger.SetOutput(&logs)
+ t.Cleanup(func() { logger.SetOutput(oldOutput) })
+
+ handled := false
+ server := gin.New()
+ server.Use(corsForOrigins([]string{"https://photos.ente.com", "ente://app"}))
+ handler := func(c *gin.Context) {
+ handled = true
+ c.Status(http.StatusNoContent)
+ }
+ server.GET("/*path", handler)
+ server.POST("/*path", handler)
+
+ req := httptest.NewRequest(tt.method, tt.path, nil)
+ req.Host = tt.host
+ if tt.origin != "" {
+ req.Header.Set("Origin", tt.origin)
+ }
+ resp := httptest.NewRecorder()
+ server.ServeHTTP(resp, req)
+
+ 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)
+ }
+ if handled != tt.wantHandle {
+ t.Errorf("handler called = %t, want %t", handled, tt.wantHandle)
+ }
+ if got := strings.Contains(logs.String(), "unknown CORS origin"); got != tt.wantWarn {
+ t.Errorf("warning logged = %t, want %t; log: %s", got, tt.wantWarn, logs.String())
+ }
+ })
+ }
+}
+
+func TestCORSProductionOriginsAreDefaults(t *testing.T) {
+ viper.Reset()
+ setAppDefaults()
+ t.Cleanup(viper.Reset)
+
+ var logs bytes.Buffer
+ logger := log.StandardLogger()
+ oldOutput := logger.Out
+ logger.SetOutput(&logs)
+ t.Cleanup(func() { logger.SetOutput(oldOutput) })
+
+ server := gin.New()
+ server.Use(cors())
+ server.GET("/ping", func(c *gin.Context) { c.Status(http.StatusNoContent) })
+
+ origins := []string{
+ "https://photos.ente.com",
+ "https://albums.ente.com",
+ "https://embed.ente.com",
+ "https://embed.ente.io",
+ "https://auth.ente.com",
+ "https://locker.ente.com",
+ "https://share.ente.com",
+ "https://paste.ente.com",
+ "https://memories.ente.com",
+ "https://accounts.ente.com",
+ "https://accounts.ente.io",
+ "https://payments.ente.com",
+ "https://cast.ente.com",
+ "https://family.ente.io",
+ "https://ente.space",
+ "https://legacy.ente.com",
+ "ente://app",
+ }
+ for _, origin := range origins {
+ req := httptest.NewRequest(http.MethodGet, "/ping", nil)
+ req.Header.Set("Origin", origin)
+ resp := httptest.NewRecorder()
+ server.ServeHTTP(resp, req)
+ if resp.Code != http.StatusNoContent {
+ t.Errorf("origin %q: status = %d, want %d", origin, resp.Code, http.StatusNoContent)
+ }
+ }
+ if logs.Len() != 0 {
+ t.Errorf("production origins logged warnings: %s", logs.String())
+ }
+}
+
+func TestCORSUsesConfiguredAppOrigins(t *testing.T) {
+ viper.Reset()
+ viper.Set("apps.photos", "https://photos.example/app")
+ viper.Set("apps.extra-origins", []string{"https://extra.example"})
+ t.Cleanup(viper.Reset)
+
+ var logs bytes.Buffer
+ logger := log.StandardLogger()
+ oldOutput := logger.Out
+ logger.SetOutput(&logs)
+ t.Cleanup(func() { logger.SetOutput(oldOutput) })
+
+ server := gin.New()
+ server.Use(cors())
+ server.GET("/ping", func(c *gin.Context) { c.Status(http.StatusNoContent) })
+ for _, origin := range []string{"https://photos.example", "https://extra.example"} {
+ req := httptest.NewRequest(http.MethodGet, "/ping", nil)
+ req.Header.Set("Origin", origin)
+ server.ServeHTTP(httptest.NewRecorder(), req)
+ }
+
+ if logs.Len() != 0 {
+ t.Errorf("configured app origin logged a warning: %s", logs.String())
+ }
+}
diff --git a/server/config/example.yaml b/server/config/example.yaml
index 581895eb10..b3d4bd335b 100644
--- a/server/config/example.yaml
+++ b/server/config/example.yaml
@@ -49,28 +49,19 @@ s3:
region: eu-central-2
bucket: scw-eu-fr-v3
-# Specify the base endpoints for various web apps
+# Specify the base endpoints for various web apps you're running.
apps:
- # If you're running a self hosted instance and wish to serve public links,
- # set this to the URL where your albums web app is running.
+ photos: http://localhost:3000
public-albums: http://localhost:3002
- # If you're running a self hosted instance and wish to serve public links
- # for locker, set this to the URL where your share web app is running.
+ embed-albums: http://localhost:3006
+ auth: http://localhost:3003
+ locker: http://localhost:3009
public-locker: http://localhost:3005
- # If you're running a self hosted instance and wish to serve Ente Paste,
- # set this to the URL where your paste web app is running.
public-paste: http://localhost:3008
- # If you're running a self hosted instance and wish to serve public memory
- # shares, set this to the URL where your memories web app is running.
- public-memories: http://localhost:3010
cast: http://localhost:3004
- embed-albums: http://localhost:3006
- # Set this to the URL where your accounts web app is running, primarily used for
- # passkey based 2FA.
accounts: http://localhost:3001
- # Set this to the URL where your space web app is running.
space: http://localhost:3013
- # Set this to the URL where your legacy recovery web app is running.
+ public-memories: http://localhost:3010
legacy: http://localhost:3011
space:
diff --git a/server/configurations/local.yaml b/server/configurations/local.yaml
index 9c3a2d40c4..7a3762a706 100644
--- a/server/configurations/local.yaml
+++ b/server/configurations/local.yaml
@@ -82,14 +82,27 @@ http:
# Specify the base endpoints for various apps
apps:
+ # Default is https://photos.ente.com
+ #
+ # Set this to the URL where your photos web app is running.
+ photos:
# Default is https://albums.ente.com
#
# If you're running a self hosted instance and wish to serve public links for photos,
# set this to the URL where your albums web app is running.
public-albums:
# Default is https://embed.ente.com
+ #
# For self-hosted instances, set this to the URL where your embed app is running.
embed-albums:
+ # Default is https://auth.ente.com
+ #
+ # Set this to the URL where your auth web app is running.
+ auth:
+ # Default is https://locker.ente.com
+ #
+ # Set this to the URL where your locker web app is running.
+ locker:
# Default is https://share.ente.com
#
# If you're running a self-hosted instance and wish to serve public links for locker,
@@ -114,9 +127,12 @@ apps:
#
# Default is https://accounts.ente.io
accounts-legacy:
+ # Default is https://payments.ente.com
+ payments:
# Default is https://family.ente.io
family:
# Default is https://ente.space
+ #
# Set this to the URL where your space web app is running.
space:
# Default is https://memories.ente.com
@@ -129,6 +145,11 @@ apps:
# If you're running a self-hosted instance and wish to serve legacy kit recovery,
# set this to the URL where your legacy recovery web app is running.
legacy:
+ # Additional trusted browser origins not represented by an app above.
+ #
+ # Default is:
+ # - https://embed.ente.io
+ extra-origins:
custom-domain:
# Default is my.ente.com
cname:
diff --git a/server/quickstart.sh b/server/quickstart.sh
index 79a31d723a..e62ab711d5 100755
--- a/server/quickstart.sh
+++ b/server/quickstart.sh
@@ -106,6 +106,7 @@ services:
# - 3006:3006 # Embed
# - 3008:3008 # Paste
# - 3009:3009 # Locker
+ # - 3010:3010 # Memories
# Modify these values to your custom subdomains, if using any
environment:
ENTE_API_ORIGIN: http://localhost:8080
@@ -202,21 +203,18 @@ s3:
region: eu-central-2
bucket: scw-eu-fr-v3
-# Specify the base endpoints for various web apps
+# Specify the base endpoints for various web apps you're running.
apps:
- # If you're running a self hosted instance and wish to serve public links,
- # set this to the URL where your albums web app is running.
+ photos: http://localhost:3000
public-albums: http://localhost:3002
- cast: http://localhost:3004
- # Public locker (share) app
+ embed-albums: http://localhost:3006
+ auth: http://localhost:3003
+ locker: http://localhost:3009
public-locker: http://localhost:3005
- # Public paste app
public-paste: http://localhost:3008
- # Embed app for embedded album sharing
- embed-albums: http://localhost:3006
- # Set this to the URL where your accounts web app is running, primarily used for
- # passkey based 2FA.
+ cast: http://localhost:3004
accounts: http://localhost:3001
+ public-memories: http://localhost:3010
key:
encryption: $museum_key