feat: enhance security with IP banning, ownership checks, and SSRF protection
All checks were successful
Build Backend / build (push) Successful in 1m56s
Build Backend / build-docker (push) Successful in 1m15s

Add comprehensive security improvements across the application:

- **IP-based login protection**: Implement IP ban system tracking login failures, auto-banning after threshold exceeded
- **Ownership verification**: Add userID parameter to Delete/Update operations for posts and comments to prevent unauthorized modifications
- **SSRF protection**: Add URL and resolved host validation for image URLs in chat and OpenAI client
- **SQL injection prevention**: Add EscapeLikeWildcard utility to escape special characters in LIKE queries
- **HTTP security**: Configure server timeouts and add security headers middleware
- **Rate limiting**: Refactor to support configurable duration and per-endpoint rate limits for auth routes
- **Error handling**: Standardize error responses using HandleError and proper error types
This commit is contained in:
lafay
2026-04-30 12:26:25 +08:00
parent 67a5660952
commit b2b55ea52d
24 changed files with 517 additions and 80 deletions

View File

@@ -178,9 +178,9 @@ func (h *CommentHandler) Update(c *gin.Context) {
comment.Content = req.Content
err = h.commentService.Update(c.Request.Context(), comment)
err = h.commentService.Update(c.Request.Context(), userID, comment)
if err != nil {
response.InternalServerError(c, "failed to update comment")
response.HandleError(c, err, "failed to update comment")
return
}
@@ -208,9 +208,9 @@ func (h *CommentHandler) Delete(c *gin.Context) {
return
}
err = h.commentService.Delete(c.Request.Context(), id)
err = h.commentService.Delete(c.Request.Context(), userID, id)
if err != nil {
response.InternalServerError(c, "failed to delete comment")
response.HandleError(c, err, "failed to delete comment")
return
}

View File

@@ -56,7 +56,7 @@ func (h *NotificationHandler) MarkAsRead(c *gin.Context) {
err := h.notificationService.MarkAsReadWithUserID(c.Request.Context(), id, userID)
if err != nil {
response.InternalServerError(c, "failed to mark as read")
response.HandleError(c, err, "failed to mark as read")
return
}
@@ -109,7 +109,7 @@ func (h *NotificationHandler) DeleteNotification(c *gin.Context) {
err := h.notificationService.DeleteNotification(c.Request.Context(), id, userID)
if err != nil {
response.InternalServerError(c, "failed to delete notification")
response.HandleError(c, err, "failed to delete notification")
return
}

View File

@@ -496,9 +496,9 @@ func (h *PostHandler) Delete(c *gin.Context) {
return
}
err = h.postService.Delete(c.Request.Context(), id)
err = h.postService.Delete(c.Request.Context(), userID, id)
if err != nil {
response.InternalServerError(c, "failed to delete post")
response.HandleError(c, err, "failed to delete post")
return
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
"with_you/internal/dto"
"with_you/internal/middleware"
"with_you/internal/model"
"with_you/internal/pkg/response"
"with_you/internal/service"
@@ -153,10 +154,13 @@ func (h *UserHandler) Login(c *gin.Context) {
user, err := h.userService.Login(c.Request.Context(), account, req.Password)
if err != nil {
middleware.RecordLoginFailure(c.ClientIP())
response.HandleError(c, err, "failed to login")
return
}
middleware.ResetLoginFailures(c.ClientIP())
// 生成Token
accessToken, _ := h.jwtService.GenerateAccessToken(user.ID, user.Username)
refreshToken, _ := h.jwtService.GenerateRefreshToken(user.ID, user.Username)