mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-15 05:04:27 +08:00
@@ -39,12 +39,14 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"ragflow/internal/common"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"go.uber.org/zap"
|
||||
|
||||
rediscli "ragflow/internal/engine/redis"
|
||||
)
|
||||
@@ -107,6 +109,7 @@ func validateWebhookSecurity(
|
||||
c *gin.Context,
|
||||
canvasID string,
|
||||
) error {
|
||||
ctx := c.Request.Context()
|
||||
if len(securityCfg) == 0 {
|
||||
return errWebhookFailClosed
|
||||
}
|
||||
@@ -116,7 +119,7 @@ func validateWebhookSecurity(
|
||||
if err := validateIPWhitelist(c, securityCfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateRateLimit(canvasID, securityCfg); err != nil {
|
||||
if err := validateRateLimit(ctx, canvasID, securityCfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateAuth(c, securityCfg)
|
||||
@@ -242,7 +245,7 @@ func validateIPWhitelist(c *gin.Context, cfg map[string]any) error {
|
||||
//
|
||||
// Strict fail-closed: any Redis error → error. The webhook handler
|
||||
// surfaces this as 102 so an operator notices a misconfiguration.
|
||||
func validateRateLimit(canvasID string, cfg map[string]any) error {
|
||||
func validateRateLimit(ctx context.Context, canvasID string, cfg map[string]any) error {
|
||||
rawRL, ok := cfg["rate_limit"].(map[string]any)
|
||||
if !ok || len(rawRL) == 0 {
|
||||
return nil
|
||||
@@ -277,15 +280,20 @@ func validateRateLimit(canvasID string, cfg map[string]any) error {
|
||||
}
|
||||
|
||||
key := fmt.Sprintf("rl:tb:%s", canvasID)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), webhookRateLimitTimeout)
|
||||
newCtx, cancel := context.WithTimeout(ctx, webhookRateLimitTimeout)
|
||||
defer cancel()
|
||||
|
||||
rdb := rediscli.Get()
|
||||
if rdb == nil {
|
||||
return fmt.Errorf("rate limit error: redis not initialised")
|
||||
}
|
||||
allowed, err := rdb.EvalTokenBucketStrict(ctx, key, limitF, limitF/window)
|
||||
allowed, err := rdb.EvalTokenBucketStrict(newCtx, key, limitF, limitF/window)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||
common.Warn("rate limit check ambiguous (timeout/cancel), allowing",
|
||||
zap.String("canvas_id", canvasID), zap.Error(err))
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("rate limit error: %s", err.Error())
|
||||
}
|
||||
if !allowed {
|
||||
|
||||
Reference in New Issue
Block a user