Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use matched routes for request policies

Neeraj Gupta3bc7eb8174a2
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index ee6437c6f8..6614ba9a18 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -1105,7 +1105,6 @@ func main() {
publicAPI.POST("/discount/claim", discountCouponHandler.ClaimCoupon)
adminAPI.POST("/discount/add-coupons", discountCouponHandler.AddCoupons)
- setKnownAPIs(server.Routes())
setupAndStartBackgroundJobs(objectCleanupController, replicationController3, fileDataCtrl, contactController, spaceModule)
time.AfterFunc(10*time.Minute, func() {
if err := remoteStoreRepository.MigrateCustomDomainCanonicalValues(context.Background()); err != nil {
@@ -1481,35 +1480,21 @@ func cacheHeaders() gin.HandlerFunc {
}
}
-var knownAPIs = make(map[string]bool)
-
func urlSanitizer(c *gin.Context) string {
if c.Request.Method == http.MethodOptions {
return "/options"
}
- u := *c.Request.URL
- u.RawQuery = ""
- uri := u.RequestURI()
- for _, p := range c.Params {
- uri = strings.Replace(uri, p.Value, fmt.Sprintf(":%s", p.Key), 1)
- }
- if !knownAPIs[uri] {
- log.Warn("Unknown API: " + uri)
- return "/unknown-api"
+ if path := c.FullPath(); path != "" {
+ return path
}
- return uri
+ log.Warn("Unknown API: " + c.Request.URL.EscapedPath())
+ return "/unknown-api"
}
func timeOutResponse(c *gin.Context) {
c.JSON(http.StatusRequestTimeout, gin.H{"handler": true})
}
-func setKnownAPIs(routes []gin.RouteInfo) {
- for _, route := range routes {
- knownAPIs[route.Path] = true
- }
-}
-
func schedule(c *cron.Cron, spec string, cmd func()) (cron.EntryID, error) {
return c.AddFunc(spec, cmd)
}
diff --git a/server/cmd/museum/main_test.go b/server/cmd/museum/main_test.go
new file mode 100644
index 0000000000..1f5fb65265
--- /dev/null
+++ b/server/cmd/museum/main_test.go
@@ -0,0 +1,94 @@
+package main
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "github.com/ente/museum/pkg/controller/discord"
+ "github.com/ente/museum/pkg/middleware"
+ "github.com/gin-gonic/gin"
+ "github.com/stretchr/testify/require"
+)
+
+func TestURLSanitizer(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ router := gin.New()
+ var before, after string
+ router.Use(func(c *gin.Context) {
+ before = urlSanitizer(c)
+ c.Next()
+ after = urlSanitizer(c)
+ }, cors())
+ for _, route := range []string{"/public-collection/info", "/public-collection/diff", "/file-link/info", "/files/download/:fileID"} {
+ router.GET(route, func(c *gin.Context) { c.Status(http.StatusNoContent) })
+ }
+ for _, route := range []string{"/cast/device-info", "/cast/device-info/"} {
+ router.POST(route, func(c *gin.Context) { c.Status(http.StatusNoContent) })
+ }
+
+ for _, tc := range []struct {
+ method string
+ target string
+ path string
+ status int
+ location string
+ }{
+ {http.MethodGet, "/public-collection/info", "/public-collection/info", http.StatusNoContent, ""},
+ {http.MethodGet, "/public-collection/%69nfo?sinceTime=1", "/public-collection/info", http.StatusNoContent, ""},
+ {http.MethodGet, "/public-collection/info?", "/public-collection/info", http.StatusNoContent, ""},
+ {http.MethodGet, "/public-collection/d%69ff", "/public-collection/diff", http.StatusNoContent, ""},
+ {http.MethodGet, "/file-link/%69nfo", "/file-link/info", http.StatusNoContent, ""},
+ {http.MethodGet, "/files/download/%31", "/files/download/:fileID", http.StatusNoContent, ""},
+ {http.MethodGet, "/files/download/files", "/files/download/:fileID", http.StatusNoContent, ""},
+ {http.MethodPost, "/cast/device-info", "/cast/device-info", http.StatusNoContent, ""},
+ {http.MethodPost, "/cast/device-%69nfo/", "/cast/device-info/", http.StatusNoContent, ""},
+ {http.MethodGet, "/public-collection/info/", "", http.StatusMovedPermanently, "/public-collection/info"},
+ {http.MethodPost, "/cast/device-info//", "", http.StatusTemporaryRedirect, "/cast/device-info/"},
+ {http.MethodGet, "/public-collection/info//", "/unknown-api", http.StatusNotFound, ""},
+ {http.MethodGet, "/public-collection/%2569nfo", "/unknown-api", http.StatusNotFound, ""},
+ {http.MethodPost, "/public-collection/info", "/unknown-api", http.StatusNotFound, ""},
+ {http.MethodGet, "/missing", "/unknown-api", http.StatusNotFound, ""},
+ {http.MethodOptions, "/public-collection/info", "/options", http.StatusOK, ""},
+ } {
+ t.Run(tc.method+" "+tc.target, func(t *testing.T) {
+ before, after = "", ""
+ response := httptest.NewRecorder()
+ router.ServeHTTP(response, httptest.NewRequest(tc.method, tc.target, nil))
+ require.Equal(t, tc.status, response.Code)
+ require.Equal(t, tc.location, response.Header().Get("Location"))
+ require.Equal(t, tc.path, before)
+ require.Equal(t, tc.path, after)
+ })
+ }
+}
+
+func TestURLSanitizerSharesRateLimitsAcrossEncodings(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ for _, paths := range [][]string{
+ {"/public-collection/verify-password", "/public-collection/verify-%70assword", "/public-collection%2Fverify-password"},
+ {"/file-link/verify-password", "/file-link/verify-%70assword", "/file-link%2Fverify-password"},
+ } {
+ t.Run(paths[0], func(t *testing.T) {
+ rateLimiter := middleware.NewRateLimitMiddleware(discord.NewDiscordController(nil, "test", "test"), 1000, time.Minute)
+ t.Cleanup(rateLimiter.Stop)
+ router := gin.New()
+ router.POST(paths[0], rateLimiter.APIRateLimitMiddleware(urlSanitizer), func(c *gin.Context) {
+ c.Status(http.StatusNoContent)
+ })
+ for i := range 13 {
+ path := paths[i%len(paths)]
+ request := httptest.NewRequest(http.MethodPost, path, nil)
+ request.RemoteAddr = "192.0.2.1:1234"
+ response := httptest.NewRecorder()
+ router.ServeHTTP(response, request)
+ wantStatus := http.StatusNoContent
+ if i >= 10 {
+ wantStatus = http.StatusTooManyRequests
+ }
+ require.Equal(t, wantStatus, response.Code, "request %d to %s", i+1, path)
+ }
+ })
+ }
+}