Skip to content

Commit 52d057e

Browse files
committed
fix: add ID validation and normalization functions for comment handling
1 parent 638fb08 commit 52d057e

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

internal/controller/comment_controller.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/apache/answer/internal/service/comment"
3434
"github.com/apache/answer/internal/service/permission"
3535
"github.com/apache/answer/internal/service/rank"
36-
"github.com/apache/answer/pkg/uid"
3736
"github.com/gin-gonic/gin"
3837
"github.com/segmentfault/pacman/errors"
3938
)
@@ -86,7 +85,6 @@ func (cc *CommentController) AddComment(ctx *gin.Context) {
8685
cc.rateLimitMiddleware.DuplicateRequestClear(ctx, rejectKey)
8786
}
8887
}()
89-
req.ObjectID = uid.DeShortID(req.ObjectID)
9088
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
9189

9290
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
@@ -245,8 +243,6 @@ func (cc *CommentController) GetCommentWithPage(ctx *gin.Context) {
245243
if handler.BindAndCheck(ctx, req) {
246244
return
247245
}
248-
req.ObjectID = uid.DeShortID(req.ObjectID)
249-
req.CommentID = uid.DeShortID(req.CommentID)
250246
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
251247
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
252248
permission.CommentEdit,

internal/repo/comment/comment_repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/apache/answer/internal/service/comment"
3232
"github.com/apache/answer/internal/service/comment_common"
3333
"github.com/apache/answer/internal/service/unique"
34+
"github.com/apache/answer/pkg/uid"
3435
"github.com/segmentfault/pacman/errors"
3536
)
3637

@@ -107,6 +108,9 @@ func (cr *commentRepo) UpdateCommentStatus(ctx context.Context, commentID string
107108
func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
108109
comment *entity.Comment, exist bool, err error) {
109110
comment = &entity.Comment{}
111+
if !uid.IsValidNumericID(commentID) {
112+
return comment, false, nil
113+
}
110114
exist, err = cr.data.DB.Context(ctx).Where("status = ?", entity.CommentStatusAvailable).ID(commentID).Get(comment)
111115
if err != nil {
112116
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -118,6 +122,9 @@ func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
118122
func (cr *commentRepo) GetCommentWithoutStatus(ctx context.Context, commentID string) (
119123
comment *entity.Comment, exist bool, err error) {
120124
comment = &entity.Comment{}
125+
if !uid.IsValidNumericID(commentID) {
126+
return comment, false, nil
127+
}
121128
exist, err = cr.data.DB.Context(ctx).ID(commentID).Get(comment)
122129
if err != nil {
123130
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()

pkg/uid/sid.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package uid
2121

2222
import (
2323
"strconv"
24+
"strings"
2425

2526
"github.com/segmentfault/pacman/utils"
2627
)
@@ -90,3 +91,41 @@ func IsShortID(id string) bool {
9091
}
9192
return false
9293
}
94+
95+
// IsValidNumericID checks whether id can be parsed as a positive int64.
96+
func IsValidNumericID(id string) bool {
97+
id = strings.TrimSpace(id)
98+
if len(id) == 0 {
99+
return false
100+
}
101+
num, err := strconv.ParseInt(id, 10, 64)
102+
if err != nil {
103+
return false
104+
}
105+
return num > 0
106+
}
107+
108+
// NormalizeOptionalID normalizes a raw id parameter.
109+
// It accepts short id and long id, treats empty/null/undefined as "not provided".
110+
// Returns normalized id, whether caller provided a value, and whether value is valid.
111+
func NormalizeOptionalID(raw string) (normalizedID string, provided bool, valid bool) {
112+
raw = strings.TrimSpace(raw)
113+
if len(raw) == 0 || strings.EqualFold(raw, "null") || strings.EqualFold(raw, "undefined") {
114+
return "", false, true
115+
}
116+
normalizedID = DeShortID(raw)
117+
if !IsValidNumericID(normalizedID) {
118+
return "", true, false
119+
}
120+
return normalizedID, true, true
121+
}
122+
123+
// NormalizeRequiredID normalizes a required id parameter.
124+
// Returns normalized id and whether the value is valid.
125+
func NormalizeRequiredID(raw string) (normalizedID string, valid bool) {
126+
normalizedID, provided, valid := NormalizeOptionalID(raw)
127+
if !provided {
128+
return "", false
129+
}
130+
return normalizedID, valid
131+
}

0 commit comments

Comments
 (0)