Skip to content

armgit / ente

Public Imported from GitHub
0

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

Alert for origin mismatch

Neeraj Guptae9c41aed1e75
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index f7c291b6d3..abdd9e58ce 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -378,6 +378,7 @@ func main() {
Cache: accessTokenCache,
BillingCtrl: billingController,
DiscordController: discordController,
+ RemoteStoreRepo: remoteStoreRepository,
}
fileLinkMiddleware := &middleware.FileLinkMiddleware{
FileLinkRepo: fileLinkRepo,
diff --git a/server/configurations/local.yaml b/server/configurations/local.yaml
index 806add1428..70d7589280 100644
--- a/server/configurations/local.yaml
+++ b/server/configurations/local.yaml
@@ -96,7 +96,7 @@ apps:
# Default is https://family.ente.io
family:
custom-domain:
- # Default is https://my.ente.io
+ # Default is my.ente.io
cname:
# Database connection parameters
diff --git a/server/pkg/middleware/collection_link.go b/server/pkg/middleware/collection_link.go
index 9c20362559..4bdb2de285 100644
--- a/server/pkg/middleware/collection_link.go
+++ b/server/pkg/middleware/collection_link.go
@@ -5,7 +5,12 @@ import (
"context"
"crypto/sha256"
"fmt"
+ "github.com/ente-io/museum/pkg/repo/remotestore"
+ "github.com/gin-contrib/requestid"
+ "github.com/spf13/viper"
"net/http"
+ "net/url"
+ "strings"
public2 "github.com/ente-io/museum/pkg/controller/public"
"github.com/ente-io/museum/pkg/repo/public"
@@ -35,6 +40,7 @@ type CollectionLinkMiddleware struct {
Cache *cache.Cache
BillingCtrl *controller.BillingController
DiscordController *discord.DiscordController
+ RemoteStoreRepo *remotestore.Repository
}
// Authenticate returns a middle ware that extracts the `X-Auth-Access-Token`
@@ -65,7 +71,7 @@ func (m *CollectionLinkMiddleware) Authenticate(urlSanitizer func(_ *gin.Context
return
}
// validate if user still has active paid subscription
- if err = m.validateOwnersSubscription(publicCollectionSummary.CollectionID); err != nil {
+ if err = m.validateOwnersSubscription(c, publicCollectionSummary.CollectionID); err != nil {
logrus.WithError(err).Warn("failed to verify active paid subscription")
c.AbortWithStatusJSON(http.StatusGone, gin.H{"error": "no active subscription"})
return
@@ -115,12 +121,17 @@ func (m *CollectionLinkMiddleware) Authenticate(urlSanitizer func(_ *gin.Context
c.Next()
}
}
-func (m *CollectionLinkMiddleware) validateOwnersSubscription(cID int64) error {
+func (m *CollectionLinkMiddleware) validateOwnersSubscription(c *gin.Context, cID int64) error {
userID, err := m.CollectionRepo.GetOwnerID(cID)
if err != nil {
return stacktrace.Propagate(err, "")
}
- return m.BillingCtrl.HasActiveSelfOrFamilySubscription(userID, false)
+ err = m.BillingCtrl.HasActiveSelfOrFamilySubscription(userID, false)
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to validate owners subscription")
+ }
+ m.validateOrigin(c, userID)
+ return nil
}
func (m *CollectionLinkMiddleware) isDeviceLimitReached(ctx context.Context,
@@ -177,6 +188,43 @@ func (m *CollectionLinkMiddleware) validatePassword(c *gin.Context, reqPath stri
return m.PublicCollectionCtrl.ValidateJWTToken(c, accessTokenJWT, *collectionSummary.PassHash)
}
+func (m *CollectionLinkMiddleware) validateOrigin(c *gin.Context, ownerID int64) {
+ origin := c.Request.Header.Get("Origin")
+
+ if origin == "" || origin == viper.GetString("apps.public-albums") {
+ return
+ }
+ reqId := requestid.Get(c)
+ logger := logrus.WithFields(logrus.Fields{
+ "ownerID": ownerID,
+ "req_id": reqId,
+ "origin": origin,
+ })
+ alertMessage := fmt.Sprintf("custom domain check failed %s", reqId)
+ domain, err := m.RemoteStoreRepo.GetDomain(c, ownerID)
+ if err != nil {
+ logger.WithError(err).Error("failed to fetch custom domain for owner")
+ m.DiscordController.NotifyPotentialAbuse(alertMessage)
+ return
+ }
+ if domain == nil || *domain == "" {
+ logger.Warn("custom domain is nil or empty")
+ m.DiscordController.NotifyPotentialAbuse(alertMessage)
+ return
+ }
+ parse, err := url.Parse(origin)
+ if err != nil {
+ logger.WithError(err).Error("failed to parse origin URL")
+ m.DiscordController.NotifyPotentialAbuse(alertMessage + " - failed to parse origin URL")
+ return
+ }
+ if !strings.Contains(strings.ToLower(parse.Host), strings.ToLower(*domain)) {
+ logger.Warnf("custom domain check failed for owner %d, origin %s, domain %s", ownerID, origin, *domain)
+ m.DiscordController.NotifyPotentialAbuse(alertMessage)
+ }
+ return
+}
+
func computeHashKeyForList(list []string, delim string) string {
var buffer bytes.Buffer
for i := range list {
diff --git a/server/pkg/repo/remotestore/repository.go b/server/pkg/repo/remotestore/repository.go
index f2bd8d7789..245d05b4eb 100644
--- a/server/pkg/repo/remotestore/repository.go
+++ b/server/pkg/repo/remotestore/repository.go
@@ -65,6 +65,25 @@ func (r *Repository) DomainOwner(ctx context.Context, domain string) (*int64, er
return &userID, nil
}
+func (r *Repository) GetDomain(ctx context.Context, userID int64) (*string, error) {
+ // Fetch the custom domain for the user
+ rows := r.DB.QueryRowContext(ctx, `SELECT key_value FROM remote_store
+ WHERE user_id = $1 AND key_name = $2`,
+ userID, // $1
+ ente.CustomDomain, // $2
+ )
+ var domain string
+ err := rows.Scan(&domain)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
+ return nil, stacktrace.Propagate(err, "failed to fetch custom domain")
+ }
+ return &domain, nil
+
+}
+
// GetValue fetches and return the value for given user_id and key
func (r *Repository) GetValue(ctx context.Context, userID int64, key string) (string, error) {
rows := r.DB.QueryRowContext(ctx, `SELECT key_value FROM remote_store