Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix custom domain rate limiting

Manav Rathia62d6ddcddce
diff --git a/server/ente/remotestore.go b/server/ente/remotestore.go
index 568baee2ef..2e97588b96 100644
--- a/server/ente/remotestore.go
+++ b/server/ente/remotestore.go
@@ -148,6 +148,10 @@ func (k FlagKey) IsValidValue(value string) error {
var domainRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
+func ValidatePublicCustomDomain(value string) error {
+ return isValidDomainWithoutScheme(value)
+}
+
const customDomainFamilyPrefix = "_"
// BuildFamilyCustomDomainPointer builds a member-scoped custom domain pointer
diff --git a/server/ente/remotestore_test.go b/server/ente/remotestore_test.go
index 42a4e9430e..f73da846a5 100644
--- a/server/ente/remotestore_test.go
+++ b/server/ente/remotestore_test.go
@@ -37,6 +37,8 @@ func TestIsValidDomainWithoutScheme(t *testing.T) {
// ❌ Wrong format
{"missing dot", "localhost", true},
+ {"ipv4 address", "172.17.0.2", true},
+ {"ipv6 address", "2001:db8::1", true},
{"single label TLD", "com", true},
{"ends with dot", "example.com.", true},
{"ends with dash", "example-.com", true},
@@ -56,6 +58,27 @@ func TestIsValidDomainWithoutScheme(t *testing.T) {
}
}
+func TestValidatePublicCustomDomain(t *testing.T) {
+ tests := []struct {
+ name string
+ input string
+ wantErr bool
+ }{
+ {"valid domain", "example.com", false},
+ {"family pointer", "_123:example.com", true},
+ {"ip address", "172.17.0.2", true},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := ValidatePublicCustomDomain(tt.input)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("ValidatePublicCustomDomain(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
+ }
+ })
+ }
+}
+
func TestFlagKey_IsValidValue(t *testing.T) {
tests := []struct {
name string
diff --git a/server/pkg/api/remotestore.go b/server/pkg/api/remotestore.go
index 5670747ed1..d2514e977f 100644
--- a/server/pkg/api/remotestore.go
+++ b/server/pkg/api/remotestore.go
@@ -1,6 +1,7 @@
package api
import (
+ "errors"
"net/http"
"github.com/ente-io/museum/ente"
@@ -80,9 +81,17 @@ func (h *RemoteStoreHandler) CheckDomain(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(ente.NewBadRequestWithMessage("domain is missing"), ""))
return
}
+ if err := ente.ValidatePublicCustomDomain(domain); err != nil {
+ c.Status(http.StatusBadRequest)
+ return
+ }
_, err := h.Controller.DomainOwner(c, domain)
if err != nil {
- handler.Error(c, stacktrace.Propagate(err, "failed to get feature flags"))
+ if errors.Is(err, &ente.ErrNotFoundError) {
+ c.Status(http.StatusNotFound)
+ return
+ }
+ handler.Error(c, stacktrace.Propagate(err, "failed to check custom domain"))
return
}
c.JSON(http.StatusOK, gin.H{})
diff --git a/server/pkg/middleware/rate_limit.go b/server/pkg/middleware/rate_limit.go
index 8ef3d617d4..556f20b377 100644
--- a/server/pkg/middleware/rate_limit.go
+++ b/server/pkg/middleware/rate_limit.go
@@ -198,13 +198,24 @@ func isAuthenticatedUploadURLPath(reqPath string) bool {
// getLimiter, based on reqPath & reqMethod, return instance of limiter.Limiter which needs to
// be applied for a request. It returns nil if the request is not rate limited
func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limiter.Limiter {
- if reqPath == "/users/public-key" ||
- reqPath == "/custom-domain" {
+ if strings.HasPrefix(reqPath, "/files/preview/") {
+ return r.limit700ReqPerSec
+ }
+ if isAuthenticatedUploadURLPath(reqPath) {
+ return r.limit500ReqPerMin
+ }
+ if isPublicCollectionUploadURLPath(reqPath) {
+ return r.limit250ReqPerMin
+ }
+ if reqPath == "/users/public-key" {
return r.limit200ReqPerMin
}
if reqPath == "/paste/guard" || reqPath == "/paste/consume" {
return r.limit200ReqPerMin
}
+ if reqPath == "/custom-domain" {
+ return r.limit500ReqPerMin
+ }
if reqPath == "/users/ott" ||
reqPath == "/users/verify-email" ||
reqPath == "/users/change-email" ||
@@ -224,9 +235,6 @@ func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limi
strings.HasPrefix(reqPath, "/users/two-factor/") {
return r.limit10ReqPerMin
}
- if strings.HasPrefix(reqPath, "/files/preview/") {
- return r.limit700ReqPerSec
- }
if reqPath == "/public-collection/anon-identity" {
return r.limit10ReqPerMin
}
@@ -235,11 +243,5 @@ func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limi
(reqMethod == http.MethodPost || reqMethod == http.MethodPut || reqMethod == http.MethodDelete) {
return r.limit200ReqPerMin
}
- if isPublicCollectionUploadURLPath(reqPath) {
- return r.limit250ReqPerMin
- }
- if isAuthenticatedUploadURLPath(reqPath) {
- return r.limit500ReqPerMin
- }
return nil
}