Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/lib/services/core/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const apiClient = axios.create({
withCredentials: apiConfig.withCredentials,
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
},
});

Expand Down
16 changes: 16 additions & 0 deletions internal/router/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ limitations under the License.
package router

import (
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
"github.com/linux-do/credit/internal/config"
"github.com/linux-do/credit/internal/logger"
"github.com/linux-do/credit/internal/otel_trace"
"github.com/linux-do/credit/internal/util"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

func csrfMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
if method == http.MethodPost || method == http.MethodPut || method == http.MethodDelete || method == http.MethodPatch {
if c.GetHeader("X-Requested-With") != "XMLHttpRequest" {
c.AbortWithStatusJSON(http.StatusForbidden, util.Err("CSRF 验证失败"))
return
}
}

c.Next()
}
}

func loggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 初始化 Trace
Expand Down
1 change: 1 addition & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func Serve() {
r.GET("/f/:id", upload.ServeFileByID)

apiGroup := r.Group(config.Config.App.APIPrefix)
apiGroup.Use(csrfMiddleware())
{
if !config.Config.App.IsProduction() {
// Swagger
Expand Down
4 changes: 2 additions & 2 deletions internal/service/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func GetTodayUsedAmount(db *gorm.DB, userID uint64) (decimal.Decimal, error) {

var total decimal.Decimal
err := db.Model(&model.Order{}).
Where("payer_user_id = ? AND status = ? AND type IN ? AND trade_time >= ? AND trade_time < ?",
Where("payer_user_id = ? AND status IN ? AND type IN ? AND trade_time >= ? AND trade_time < ?",
userID,
model.OrderStatusSuccess,
[]model.OrderStatus{model.OrderStatusSuccess, model.OrderStatusDisputing, model.OrderStatusRefused},
[]model.OrderType{model.OrderTypePayment, model.OrderTypeOnline, model.OrderTypeDistribute, model.OrderTypeTransfer},
todayStart,
todayEnd).
Expand Down
Loading