Skip to content

armgit / ente

Public Imported from GitHub
0

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

Honor enableComment flag for public link

Neeraj Gupta3569648dd11b
diff --git a/server/ente/errors.go b/server/ente/errors.go
index 598b738b5c..4e78405f23 100644
--- a/server/ente/errors.go
+++ b/server/ente/errors.go
@@ -160,6 +160,12 @@ var ErrPublicCollectDisabled = ApiError{
HttpStatusCode: http.StatusMethodNotAllowed,
}
+var ErrPublicCommentDisabled = ApiError{
+ Code: PublicCommentDisabled,
+ Message: "User has not enabled public comments for this url",
+ HttpStatusCode: http.StatusMethodNotAllowed,
+}
+
var ErrNotFoundError = ApiError{
Code: NotFoundError,
Message: "",
@@ -243,6 +249,9 @@ const (
// PublicCollectDisabled error code indicates that the user has not enabled public collect
PublicCollectDisabled ErrorCode = "PUBLIC_COLLECT_DISABLED"
+ // PublicCommentDisabled error code indicates that the user has not enabled public comments
+ PublicCommentDisabled ErrorCode = "PUBLIC_COMMENT_DISABLED"
+
// CollectionNotEmpty is thrown when user attempts to delete a collection but keep files but all files from that
// collections have been moved yet.
CollectionNotEmpty ErrorCode = "COLLECTION_NOT_EMPTY"
diff --git a/server/ente/public_collection.go b/server/ente/public_collection.go
index bf042022b9..9103a08e6d 100644
--- a/server/ente/public_collection.go
+++ b/server/ente/public_collection.go
@@ -128,10 +128,11 @@ type PublicURL struct {
}
type PublicAccessContext struct {
- ID int64
- IP string
- UserAgent string
- CollectionID int64
+ ID int64
+ IP string
+ UserAgent string
+ CollectionID int64
+ EnableComment bool
}
func FilterPublicURLsForRole(urls []PublicURL, role CollectionParticipantRole) []PublicURL {
@@ -158,7 +159,8 @@ type PublicCollectionSummary struct {
UpdatedAt int64
DeviceAccessCount int
// not empty value of passHash indicates that the link is password protected.
- PassHash *string
+ PassHash *string
+ EnableComment bool
}
type AbuseReportRequest struct {
diff --git a/server/pkg/api/public_comments.go b/server/pkg/api/public_comments.go
index 155a24c33f..1380a933b5 100644
--- a/server/pkg/api/public_comments.go
+++ b/server/pkg/api/public_comments.go
@@ -71,6 +71,9 @@ func (h *PublicCommentsHandler) CreateComment(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
if len(payload.Cipher) == 0 || len(payload.Cipher) > maxPublicCommentCipherSize || len(payload.Nonce) == 0 {
handler.Error(c, ente.ErrBadRequest)
return
@@ -99,6 +102,9 @@ func (h *PublicCommentsHandler) UpdateComment(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
if len(payload.Cipher) == 0 || len(payload.Cipher) > maxPublicCommentCipherSize || len(payload.Nonce) == 0 {
handler.Error(c, ente.ErrBadRequest)
return
@@ -128,6 +134,9 @@ func (h *PublicCommentsHandler) DeleteComment(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
commentID := c.Param("commentID")
if commentID == "" {
@@ -146,6 +155,9 @@ func (h *PublicCommentsHandler) DeleteComment(c *gin.Context) {
}
func (h *PublicCommentsHandler) CommentDiff(c *gin.Context) {
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
since, err := parseDiffSinceTime(c.Query("sinceTime"))
if err != nil {
@@ -176,6 +188,9 @@ func (h *PublicCommentsHandler) CreateReaction(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
if len(payload.Cipher) == 0 || len(payload.Cipher) > maxPublicReactionCipherSize || len(payload.Nonce) == 0 {
handler.Error(c, ente.ErrBadRequest)
return
@@ -199,6 +214,9 @@ func (h *PublicCommentsHandler) CreateReaction(c *gin.Context) {
}
func (h *PublicCommentsHandler) ReactionDiff(c *gin.Context) {
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
since, err := parseDiffSinceTime(c.Query("sinceTime"))
if err != nil {
@@ -234,6 +252,9 @@ func (h *PublicCommentsHandler) DeleteReaction(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
reactionID := c.Param("reactionID")
if reactionID == "" {
@@ -252,6 +273,9 @@ func (h *PublicCommentsHandler) DeleteReaction(c *gin.Context) {
}
func (h *PublicCommentsHandler) Participants(c *gin.Context) {
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
// ignore includeSharees flag for now
participants, err := h.CommentsCtrl.Participants(c.Request.Context(), collectionID)
@@ -268,6 +292,9 @@ func (h *PublicCommentsHandler) CreateAnonIdentity(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
if len(payload.Cipher) == 0 || len(payload.Nonce) == 0 {
handler.Error(c, ente.ErrBadRequest)
return
@@ -286,6 +313,9 @@ func (h *PublicCommentsHandler) CreateAnonIdentity(c *gin.Context) {
}
func (h *PublicCommentsHandler) AnonProfiles(c *gin.Context) {
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
profiles, err := h.CommentsCtrl.ListAnonProfiles(c, collectionID)
if err != nil {
@@ -297,6 +327,9 @@ func (h *PublicCommentsHandler) AnonProfiles(c *gin.Context) {
// SocialDiff returns both comments and reactions in a single response.
func (h *PublicCommentsHandler) SocialDiff(c *gin.Context) {
+ if !ensurePublicCommentsEnabled(c) {
+ return
+ }
collectionID := auth.MustGetPublicAccessContext(c).CollectionID
since, err := parseDiffSinceTime(c.Query("sinceTime"))
if err != nil {
@@ -333,3 +366,11 @@ func (h *PublicCommentsHandler) SocialDiff(c *gin.Context) {
"hasMoreReactions": hasMoreReactions,
})
}
+
+func ensurePublicCommentsEnabled(c *gin.Context) bool {
+ if auth.MustGetPublicAccessContext(c).EnableComment {
+ return true
+ }
+ handler.Error(c, &ente.ErrPublicCommentDisabled)
+ return false
+}
diff --git a/server/pkg/controller/public/anon_identity.go b/server/pkg/controller/public/anon_identity.go
index 66f31dc622..695998b7d0 100644
--- a/server/pkg/controller/public/anon_identity.go
+++ b/server/pkg/controller/public/anon_identity.go
@@ -33,6 +33,9 @@ type CreateAnonIdentityRequest struct {
}
func (c *AnonIdentityController) Create(ctx *gin.Context, req CreateAnonIdentityRequest) (AnonIdentityResponse, error) {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return AnonIdentityResponse{}, err
+ }
if strings.TrimSpace(req.Cipher) == "" || strings.TrimSpace(req.Nonce) == "" {
return AnonIdentityResponse{}, ente.ErrBadRequest
}
diff --git a/server/pkg/controller/public/comments.go b/server/pkg/controller/public/comments.go
index dea08225dd..82f02c1d46 100644
--- a/server/pkg/controller/public/comments.go
+++ b/server/pkg/controller/public/comments.go
@@ -9,6 +9,7 @@ import (
socialcontroller "github.com/ente-io/museum/pkg/controller/social"
"github.com/ente-io/museum/pkg/repo"
socialrepo "github.com/ente-io/museum/pkg/repo/social"
+ "github.com/ente-io/museum/pkg/utils/auth"
emailUtil "github.com/ente-io/museum/pkg/utils/email"
"github.com/ente-io/stacktrace"
"github.com/gin-gonic/gin"
@@ -56,6 +57,9 @@ type participant struct {
}
func (c *CommentsController) CreateComment(ctx *gin.Context, collectionID int64, req CommentRequest) (string, error) {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return "", err
+ }
if len(req.Cipher) == 0 || len(req.Cipher) > maxCommentPayloadSize {
return "", ente.ErrBadRequest
}
@@ -83,6 +87,9 @@ func (c *CommentsController) CreateComment(ctx *gin.Context, collectionID int64,
}
func (c *CommentsController) UpdateComment(ctx *gin.Context, collectionID int64, commentID string, req CommentUpdateRequest) error {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return err
+ }
if len(req.Cipher) == 0 || len(req.Cipher) > maxCommentPayloadSize {
return ente.ErrBadRequest
}
@@ -106,6 +113,9 @@ func (c *CommentsController) UpdateComment(ctx *gin.Context, collectionID int64,
}
func (c *CommentsController) DeleteComment(ctx *gin.Context, collectionID int64, commentID string, req CommentDeleteRequest) error {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return err
+ }
actor, err := resolvePublicActor(ctx, c.UserAuthRepo, c.JwtSecret, req.AnonUserID, req.AnonToken, true)
if err != nil {
return err
@@ -122,6 +132,9 @@ func (c *CommentsController) DeleteComment(ctx *gin.Context, collectionID int64,
}
func (c *CommentsController) ListComments(ctx *gin.Context, collectionID int64, since int64, limit int, fileID *int64) ([]socialentity.Comment, bool, error) {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return nil, false, err
+ }
diffReq := socialcontroller.CommentDiffRequest{
Actor: socialcontroller.Actor{},
CollectionID: collectionID,
@@ -168,5 +181,18 @@ func (c *CommentsController) Participants(ctx context.Context, collectionID int6
}
func (c *CommentsController) ListAnonProfiles(ctx *gin.Context, collectionID int64) ([]socialentity.AnonUser, error) {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return nil, err
+ }
return c.AnonUsersRepo.ListByCollection(ctx.Request.Context(), collectionID)
}
+
+func ensureCommentsFeatureEnabled(ctx *gin.Context) error {
+ if ctx == nil {
+ return nil
+ }
+ if auth.MustGetPublicAccessContext(ctx).EnableComment {
+ return nil
+ }
+ return &ente.ErrPublicCommentDisabled
+}
diff --git a/server/pkg/controller/public/reactions.go b/server/pkg/controller/public/reactions.go
index 29341a43c3..d7cd0ad0fa 100644
--- a/server/pkg/controller/public/reactions.go
+++ b/server/pkg/controller/public/reactions.go
@@ -36,6 +36,9 @@ type ReactionDeleteRequest struct {
}
func (c *ReactionsController) UpsertReaction(ctx *gin.Context, collectionID int64, req ReactionRequest) (string, error) {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return "", err
+ }
if len(req.Cipher) != reactionCipherLength || len(req.Nonce) == 0 {
return "", ente.ErrBadRequest
}
@@ -60,6 +63,9 @@ func (c *ReactionsController) UpsertReaction(ctx *gin.Context, collectionID int6
}
func (c *ReactionsController) DeleteReaction(ctx *gin.Context, collectionID int64, reactionID string, req ReactionDeleteRequest) error {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return err
+ }
actor, err := resolvePublicActor(ctx, c.UserAuthRepo, c.JwtSecret, req.AnonUserID, req.AnonToken, true)
if err != nil {
return err
@@ -76,6 +82,9 @@ func (c *ReactionsController) DeleteReaction(ctx *gin.Context, collectionID int6
}
func (c *ReactionsController) ListReactions(ctx *gin.Context, collectionID int64, since int64, limit int, fileID *int64, commentID *string) ([]socialentity.Reaction, bool, error) {
+ if err := ensureCommentsFeatureEnabled(ctx); err != nil {
+ return nil, false, err
+ }
diffReq := socialcontroller.ReactionDiffRequest{
Actor: socialcontroller.Actor{},
CollectionID: collectionID,
diff --git a/server/pkg/middleware/collection_link.go b/server/pkg/middleware/collection_link.go
index e4d3f0ba34..856a8eefe1 100644
--- a/server/pkg/middleware/collection_link.go
+++ b/server/pkg/middleware/collection_link.go
@@ -124,10 +124,11 @@ func (m *CollectionLinkMiddleware) Authenticate(urlSanitizer func(_ *gin.Context
}
publicCtx := ente.PublicAccessContext{
- ID: publicCollectionSummary.ID,
- IP: clientIP,
- UserAgent: userAgent,
- CollectionID: publicCollectionSummary.CollectionID,
+ ID: publicCollectionSummary.ID,
+ IP: clientIP,
+ UserAgent: userAgent,
+ CollectionID: publicCollectionSummary.CollectionID,
+ EnableComment: publicCollectionSummary.EnableComment,
}
c.Set(auth.PublicAccessKey, publicCtx)
diff --git a/server/pkg/repo/public/collection_link.go b/server/pkg/repo/public/collection_link.go
index c385512a35..c6a45b4675 100644
--- a/server/pkg/repo/public/collection_link.go
+++ b/server/pkg/repo/public/collection_link.go
@@ -194,7 +194,7 @@ func (pcr *CollectionLinkRepo) AccessedInPast(ctx context.Context, shareID int64
func (pcr *CollectionLinkRepo) GetCollectionSummaryByToken(ctx context.Context, accessToken string) (ente.PublicCollectionSummary, error) {
row := pcr.DB.QueryRowContext(ctx,
- `SELECT sct.id, sct.collection_id, sct.is_disabled, sct.valid_till, sct.device_limit, sct.pw_hash,
+ `SELECT sct.id, sct.collection_id, sct.is_disabled, sct.valid_till, sct.device_limit, sct.pw_hash, sct.enable_comment,
sct.created_at, sct.updated_at, count(ah.share_id)
from public_collection_tokens sct
LEFT JOIN public_collection_access_history ah ON sct.id = ah.share_id
@@ -202,7 +202,7 @@ func (pcr *CollectionLinkRepo) GetCollectionSummaryByToken(ctx context.Context,
group by sct.id`, accessToken)
var result = ente.PublicCollectionSummary{}
err := row.Scan(&result.ID, &result.CollectionID, &result.IsDisabled, &result.ValidTill, &result.DeviceLimit,
- &result.PassHash, &result.CreatedAt, &result.UpdatedAt, &result.DeviceAccessCount)
+ &result.PassHash, &result.EnableComment, &result.CreatedAt, &result.UpdatedAt, &result.DeviceAccessCount)
if err != nil {
return ente.PublicCollectionSummary{}, stacktrace.Propagate(err, "failed to get public collection summary")
}