Skip to content

armgit / ente

Public Imported from GitHub
0

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

Block Auth tokens from entity and contact routes

Neeraj Gupta2d503fa33b06
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 8f29ad6d48..c2f91fcd30 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -975,30 +975,30 @@ func main() {
userEntityController := &userEntityCtrl.Controller{Repo: userEntityRepo}
userEntityHandler := &api.UserEntityHandler{Controller: userEntityController}
- privateAPI.POST("/user-entity/key", userEntityHandler.CreateKey)
- privateAPI.GET("/user-entity/key", userEntityHandler.GetKey)
- privateAPI.POST("/user-entity/entity", userEntityHandler.CreateEntity)
- privateAPI.PUT("/user-entity/entity", userEntityHandler.UpdateEntity)
- privateAPI.DELETE("/user-entity/entity", userEntityHandler.DeleteEntity)
- privateAPI.GET("/user-entity/entity/diff", userEntityHandler.GetDiff)
+ storageAPI.POST("/user-entity/key", userEntityHandler.CreateKey)
+ storageAPI.GET("/user-entity/key", userEntityHandler.GetKey)
+ storageAPI.POST("/user-entity/entity", userEntityHandler.CreateEntity)
+ storageAPI.PUT("/user-entity/entity", userEntityHandler.UpdateEntity)
+ storageAPI.DELETE("/user-entity/entity", userEntityHandler.DeleteEntity)
+ storageAPI.GET("/user-entity/entity/diff", userEntityHandler.GetDiff)
contactController := contactCtrl.New(contactRepository, objectCleanupController, s3Config)
contactHandler := &api.ContactHandler{Controller: contactController}
- privateAPI.POST("/contacts", contactHandler.Create)
- privateAPI.GET("/contacts/:id", contactHandler.Get)
- privateAPI.GET("/contacts/diff", contactHandler.GetDiff)
- privateAPI.PUT("/contacts/:id", contactHandler.Update)
- privateAPI.DELETE("/contacts/:id", contactHandler.Delete)
- privateAPI.POST("/attachments/:type/upload-url", contactHandler.GetAttachmentUploadURL)
- privateAPI.GET("/attachments/:type/:attachmentID", contactHandler.GetAttachment)
- privateAPI.PUT("/contacts/:id/attachments/:type", contactHandler.AttachContactAttachment)
- privateAPI.GET("/contacts/:id/attachments/:type", contactHandler.GetCurrentContactAttachment)
- privateAPI.DELETE("/contacts/:id/attachments/:type", contactHandler.DeleteContactAttachment)
- privateAPI.POST("/contacts/:id/profile-picture/upload-url", contactHandler.GetProfilePictureUploadURL)
- privateAPI.PUT("/contacts/:id/profile-picture", contactHandler.AttachProfilePicture)
- privateAPI.GET("/contacts/:id/profile-picture", contactHandler.GetProfilePicture)
- privateAPI.DELETE("/contacts/:id/profile-picture", contactHandler.DeleteProfilePicture)
+ storageAPI.POST("/contacts", contactHandler.Create)
+ storageAPI.GET("/contacts/:id", contactHandler.Get)
+ storageAPI.GET("/contacts/diff", contactHandler.GetDiff)
+ storageAPI.PUT("/contacts/:id", contactHandler.Update)
+ storageAPI.DELETE("/contacts/:id", contactHandler.Delete)
+ storageAPI.POST("/attachments/:type/upload-url", contactHandler.GetAttachmentUploadURL)
+ storageAPI.GET("/attachments/:type/:attachmentID", contactHandler.GetAttachment)
+ storageAPI.PUT("/contacts/:id/attachments/:type", contactHandler.AttachContactAttachment)
+ storageAPI.GET("/contacts/:id/attachments/:type", contactHandler.GetCurrentContactAttachment)
+ storageAPI.DELETE("/contacts/:id/attachments/:type", contactHandler.DeleteContactAttachment)
+ storageAPI.POST("/contacts/:id/profile-picture/upload-url", contactHandler.GetProfilePictureUploadURL)
+ storageAPI.PUT("/contacts/:id/profile-picture", contactHandler.AttachProfilePicture)
+ storageAPI.GET("/contacts/:id/profile-picture", contactHandler.GetProfilePicture)
+ storageAPI.DELETE("/contacts/:id/profile-picture", contactHandler.DeleteProfilePicture)
authenticatorController := &authenticatorCtrl.Controller{Repo: authRepo, UserRepo: userRepo}
authenticatorHandler := &api.AuthenticatorHandler{Controller: authenticatorController}
diff --git a/server/cmd/museum/main_route_policy_test.go b/server/cmd/museum/main_route_policy_test.go
new file mode 100644
index 0000000000..31d811f31f
--- /dev/null
+++ b/server/cmd/museum/main_route_policy_test.go
@@ -0,0 +1,126 @@
+package main
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+type routeRegistration struct {
+ group string
+ method string
+ path string
+}
+
+func TestStorageContentRoutesUseStorageAPI(t *testing.T) {
+ routes := collectRouteRegistrations(t)
+
+ for _, route := range routes {
+ if route.group == "privateAPI" && requiresStorageAuthBlock(route.path) {
+ t.Errorf("%s %s is registered on privateAPI; use storageAPI", route.method, route.path)
+ }
+ }
+
+ for _, prefix := range []string{"/user-entity/", "/contacts", "/attachments/"} {
+ if !hasStorageRoute(routes, prefix) {
+ t.Errorf("expected %s routes to be registered on storageAPI", prefix)
+ }
+ }
+}
+
+func collectRouteRegistrations(t *testing.T) []routeRegistration {
+ t.Helper()
+
+ fileSet := token.NewFileSet()
+ file, err := parser.ParseFile(fileSet, "main.go", nil, 0)
+ if err != nil {
+ t.Fatalf("failed to parse main.go: %v", err)
+ }
+
+ httpMethods := map[string]bool{
+ "GET": true, "POST": true, "PUT": true, "PATCH": true, "DELETE": true,
+ }
+ var routes []routeRegistration
+ ast.Inspect(file, func(node ast.Node) bool {
+ call, ok := node.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ selector, ok := call.Fun.(*ast.SelectorExpr)
+ if !ok || !httpMethods[selector.Sel.Name] {
+ return true
+ }
+ group, ok := selector.X.(*ast.Ident)
+ if !ok || (group.Name != "privateAPI" && group.Name != "storageAPI") {
+ return true
+ }
+ if len(call.Args) == 0 {
+ return true
+ }
+ literal, ok := call.Args[0].(*ast.BasicLit)
+ if !ok || literal.Kind != token.STRING {
+ return true
+ }
+ path, err := strconv.Unquote(literal.Value)
+ if err != nil {
+ t.Fatalf("failed to parse route path %s: %v", literal.Value, err)
+ }
+ routes = append(routes, routeRegistration{
+ group: group.Name,
+ method: selector.Sel.Name,
+ path: path,
+ })
+ return true
+ })
+ return routes
+}
+
+func requiresStorageAuthBlock(path string) bool {
+ switch {
+ case path == "/files" || strings.HasPrefix(path, "/files/"):
+ return true
+ case strings.HasPrefix(path, "/trash/"):
+ return true
+ case path == "/comments" || strings.HasPrefix(path, "/comments/"):
+ return true
+ case path == "/reactions" || strings.HasPrefix(path, "/reactions/"):
+ return true
+ case strings.HasPrefix(path, "/social/"):
+ return true
+ case strings.HasPrefix(path, "/comments-reactions/"):
+ return true
+ case path == "/collections" || strings.HasPrefix(path, "/collections/"):
+ return true
+ case strings.HasPrefix(path, "/collection-actions/"):
+ return true
+ case path == "/memory-share" || strings.HasPrefix(path, "/memory-share/"):
+ return true
+ case strings.HasPrefix(path, "/cast/"):
+ return true
+ case path == "/users/locker-usage":
+ return true
+ case strings.HasPrefix(path, "/user-entity/"):
+ return true
+ case path == "/contacts" || strings.HasPrefix(path, "/contacts/"):
+ return true
+ case strings.HasPrefix(path, "/attachments/"):
+ return true
+ default:
+ return false
+ }
+}
+
+func hasStorageRoute(routes []routeRegistration, prefix string) bool {
+ for _, route := range routes {
+ if route.group != "storageAPI" {
+ continue
+ }
+ if route.path == strings.TrimSuffix(prefix, "/") || strings.HasPrefix(route.path, prefix) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/server/pkg/middleware/auth_test.go b/server/pkg/middleware/auth_test.go
index 205c39ab93..f4bb8f91e9 100644
--- a/server/pkg/middleware/auth_test.go
+++ b/server/pkg/middleware/auth_test.go
@@ -43,6 +43,27 @@ func TestRejectAuthAppKeepsAuthRoutesAndBlocksStorageRoutes(t *testing.T) {
clientPkg: "io.ente.auth",
wantStatus: http.StatusForbidden,
},
+ {
+ name: "auth token cannot access user entity route",
+ path: "/user-entity/entity/diff?type=contact&sinceTime=0&limit=1",
+ token: tokens.auth,
+ clientPkg: "io.ente.auth",
+ wantStatus: http.StatusForbidden,
+ },
+ {
+ name: "auth token cannot access contact route",
+ path: "/contacts/diff?sinceTime=0&limit=1",
+ token: tokens.auth,
+ clientPkg: "io.ente.auth",
+ wantStatus: http.StatusForbidden,
+ },
+ {
+ name: "auth token cannot access contact attachment route",
+ path: "/attachments/profile_picture/ua_test",
+ token: tokens.auth,
+ clientPkg: "io.ente.auth",
+ wantStatus: http.StatusForbidden,
+ },
{
name: "photos token can access storage route",
path: "/collections/v2",
@@ -125,6 +146,15 @@ func setupAuthRouteTest(t *testing.T) (*gin.Engine, authRouteTestTokens) {
storageAPI.GET("/collections/v2", func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
+ storageAPI.GET("/user-entity/entity/diff", func(c *gin.Context) {
+ c.Status(http.StatusNoContent)
+ })
+ storageAPI.GET("/contacts/diff", func(c *gin.Context) {
+ c.Status(http.StatusNoContent)
+ })
+ storageAPI.GET("/attachments/:type/:attachmentID", func(c *gin.Context) {
+ c.Status(http.StatusNoContent)
+ })
headerMutatingStorageAPI := privateAPI.Group("/")
headerMutatingStorageAPI.Use(func(c *gin.Context) {