@@ -34863,29 +34863,33 @@ func FuzzyHashBody(body []byte) uint64 {
3486334863}
3486434864
3486534865// Checks whether e.g. a workflow is calling itself with VERY similar details.
34866- // URL MUST be identical, but body can vary slightly and still match
34866+ // URL MUST be identical, but body can vary slightly and still match.
34867+
34868+ // Implementations (cloud):
34869+ // - /workflow/{workflowId}/run
34870+ // - /apps/{appId}/run
34871+ // - /hooks/{webhookId}
3486734872func IsExecutionRecursion(ctx context.Context, request *http.Request, body []byte) bool {
34868- timestart := time.Now()
34873+ // May not have enough details to know without a body (?)
34874+ if len(body) == 0 {
34875+ return false
34876+ }
34877+
3486934878 urlMd5 := md5.Sum([]byte(request.URL.String()))
3487034879
3487134880 // Hashes the body into "buckets" that look for slight similarities
34881+ // The main point is avoiding replicas with deviations like timestamps
3487234882 hash1 := FuzzyHashBody(body)
3487334883
3487434884 cacheKey := fmt.Sprintf("%s_%s", urlMd5, hash1)
3487534885 cache, err := GetCache(ctx, cacheKey)
3487634886 if err != nil {
34877- log.Printf("ERR: %#v", err)
34878-
3487934887 SetCache(ctx, cacheKey, []byte("1"), 1)
3488034888 return false
3488134889 }
3488234890
34883- timeEnd := time.Now()
34884-
3488534891 foundNumber := 0
34886-
3488734892 cacheData := string(cache.([]uint8))
34888- //if n, err := strconv.Atoi(found.([]uint8)); err == nil {
3488934893 if n, err := strconv.Atoi(cacheData); err == nil {
3489034894 foundNumber = n
3489134895 }
@@ -34896,7 +34900,21 @@ func IsExecutionRecursion(ctx context.Context, request *http.Request, body []byt
3489634900 foundNumber = 1
3489734901 }
3489834902
34899- if foundNumber > 9 {
34903+ // This has monitoring on it and should NEVER happen ideally
34904+ maxRecursionDepthInt := 8
34905+ maxRecursionDepth := os.Getenv("SHUFFLE_MAX_RECURSION_DEPTH")
34906+ if maxRecursionDepth == "" {
34907+ maxRecursionDepthInt, err = strconv.Atoi(maxRecursionDepth)
34908+ if err != nil {
34909+ maxRecursionDepthInt = 8
34910+ }
34911+ }
34912+
34913+ if maxRecursionDepthInt < 5 {
34914+ maxRecursionDepthInt = 5
34915+ }
34916+
34917+ if foundNumber > maxRecursionDepthInt {
3490034918 log.Printf("[ERROR] Detected potential recursion for URL %s. Hash: %d", request.URL.String(), hash1)
3490134919 return true
3490234920 }
0 commit comments