Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add limits

Neeraj Guptafb8f4a393914
diff --git a/server/ente/errors.go b/server/ente/errors.go
index 4e78405f23..aeed88fdb6 100644
--- a/server/ente/errors.go
+++ b/server/ente/errors.go
@@ -166,6 +166,18 @@ var ErrPublicCommentDisabled = ApiError{
HttpStatusCode: http.StatusMethodNotAllowed,
}
+var ErrPublicCommentTooLong = ApiError{
+ Code: PublicCommentTooLong,
+ Message: "Comments are limited to 280 characters",
+ HttpStatusCode: http.StatusBadRequest,
+}
+
+var ErrAnonNameTooLong = ApiError{
+ Code: AnonNameTooLong,
+ Message: "Anonymous names are limited to 50 characters",
+ HttpStatusCode: http.StatusBadRequest,
+}
+
var ErrNotFoundError = ApiError{
Code: NotFoundError,
Message: "",
@@ -252,6 +264,12 @@ const (
// PublicCommentDisabled error code indicates that the user has not enabled public comments
PublicCommentDisabled ErrorCode = "PUBLIC_COMMENT_DISABLED"
+ // PublicCommentTooLong indicates that comment text exceeded allowed limit
+ PublicCommentTooLong ErrorCode = "PUBLIC_COMMENT_TOO_LONG"
+
+ // AnonNameTooLong indicates that anonymous display name exceeded allowed limit
+ AnonNameTooLong ErrorCode = "ANON_NAME_TOO_LONG"
+
// 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/pkg/controller/public/anon_identity.go b/server/pkg/controller/public/anon_identity.go
index 695998b7d0..847ab34218 100644
--- a/server/pkg/controller/public/anon_identity.go
+++ b/server/pkg/controller/public/anon_identity.go
@@ -39,6 +39,9 @@ func (c *AnonIdentityController) Create(ctx *gin.Context, req CreateAnonIdentity
if strings.TrimSpace(req.Cipher) == "" || strings.TrimSpace(req.Nonce) == "" {
return AnonIdentityResponse{}, ente.ErrBadRequest
}
+ if err := validateEncryptedPayloadLength(req.Cipher, maxAnonNameBytes, &ente.ErrAnonNameTooLong); err != nil {
+ return AnonIdentityResponse{}, err
+ }
rawID, err := gonanoid.New()
if err != nil {
return AnonIdentityResponse{}, stacktrace.Propagate(err, "")
diff --git a/server/pkg/controller/public/comments.go b/server/pkg/controller/public/comments.go
index 82f02c1d46..ce641b79ea 100644
--- a/server/pkg/controller/public/comments.go
+++ b/server/pkg/controller/public/comments.go
@@ -63,6 +63,9 @@ func (c *CommentsController) CreateComment(ctx *gin.Context, collectionID int64,
if len(req.Cipher) == 0 || len(req.Cipher) > maxCommentPayloadSize {
return "", ente.ErrBadRequest
}
+ if err := validateEncryptedPayloadLength(req.Cipher, maxCommentBytes, &ente.ErrPublicCommentTooLong); err != nil {
+ return "", err
+ }
if len(req.Nonce) == 0 {
return "", ente.ErrBadRequest
}
@@ -93,6 +96,9 @@ func (c *CommentsController) UpdateComment(ctx *gin.Context, collectionID int64,
if len(req.Cipher) == 0 || len(req.Cipher) > maxCommentPayloadSize {
return ente.ErrBadRequest
}
+ if err := validateEncryptedPayloadLength(req.Cipher, maxCommentBytes, &ente.ErrPublicCommentTooLong); err != nil {
+ return err
+ }
if len(req.Nonce) == 0 {
return ente.ErrBadRequest
}
diff --git a/server/pkg/controller/public/social_limits.go b/server/pkg/controller/public/social_limits.go
new file mode 100644
index 0000000000..a345c511a1
--- /dev/null
+++ b/server/pkg/controller/public/social_limits.go
@@ -0,0 +1,28 @@
+package public
+
+import (
+ "encoding/base64"
+
+ "github.com/ente-io/museum/ente"
+ "github.com/ente-io/stacktrace"
+)
+
+const (
+ secretBoxOverheadBytes = 16
+ maxCommentBytes = 280
+ maxAnonNameBytes = 50
+)
+
+func validateEncryptedPayloadLength(cipher string, maxBytes int, tooLongErr error) error {
+ data, err := base64.StdEncoding.DecodeString(cipher)
+ if err != nil {
+ return stacktrace.Propagate(ente.ErrBadRequest, "invalid cipher encoding")
+ }
+ if len(data) < secretBoxOverheadBytes {
+ return stacktrace.Propagate(ente.ErrBadRequest, "cipher too short")
+ }
+ if len(data)-secretBoxOverheadBytes > maxBytes {
+ return tooLongErr
+ }
+ return nil
+}