langchain - 💡(How to fix) Fix Security: Recursive tool calling without cycle guard [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35721Fetched 2026-04-08 00:24:59
View on GitHub
Comments
2
Participants
3
Timeline
3
Reactions
0
Timeline (top)
commented ×2labeled ×1
  • 1 MEDIUM severity recursive tool calling without cycle guard
  • Governance Score: 100/100 (otherwise excellent security posture)

Root Cause

  • 1 MEDIUM severity recursive tool calling without cycle guard
  • Governance Score: 100/100 (otherwise excellent security posture)

Code Example

npx -y @inkog-io/cli scan . -deep
RAW_BUFFERClick to expand / collapse

Security Findings Report

We scanned this repository using Inkog, an AI security scanner, and identified 1 MEDIUM severity vulnerability related to recursive tool calling.

Summary

  • 1 MEDIUM severity recursive tool calling without cycle guard
  • Governance Score: 100/100 (otherwise excellent security posture)

Findings

SeverityIssueLocation
MEDIUMRecursive Tool Calling Without Cycle Guardbase.py:668

Details

The code allows recursive tool calling without implementing a cycle detection or depth limit guard. While this might be intentional for certain use cases, it poses risks:

  • Stack overflow: Deep recursion can exhaust memory
  • Infinite loops: Malformed or adversarial inputs could cause the agent to recurse indefinitely
  • Resource exhaustion: Can lead to DoS conditions

How to Reproduce

You can verify these findings by running Inkog yourself:

npx -y @inkog-io/cli scan . -deep

Recommendations

  1. Add recursion depth limit: Implement a maximum recursion depth counter.
  2. Cycle detection: Track call history to detect and prevent circular tool calling patterns.
  3. Timeout mechanisms: Add configurable timeout limits for tool execution chains.
  4. Documentation: If recursive behavior is intentional, document the risks and provide configuration options for users to set their own limits.

Learn More

For detailed remediation guidance and best practices for securing AI applications, visit inkog.io.


This report was generated to help improve the security of your project. We hope you find it useful! Note: Your repository scored 100/100 on governance, which is excellent — this is just a minor improvement opportunity.

extent analysis

Problem Summary Recursive tool calling without cycle guard vulnerability

Root Cause Analysis Recursive function calls without a cycle detection or depth limit guard

Fix Plan Implement a recursion depth limit and cycle detection to prevent stack overflow, infinite loops, and resource exhaustion.

Step-by-Step Solution

1. Implement Recursion Depth Limit

def recursive_tool_call(max_depth=5, current_depth=0):
    if current_depth >= max_depth:
        return  # base case: max depth reached
    # perform tool call
    # ...
    recursive_tool_call(max_depth, current_depth + 1)

2. Cycle Detection

call_history = set()

def recursive_tool_call(max_depth=5, current_depth=0):
    if current_depth >= max_depth:
        return  # base case: max depth reached
    if id(self) in call_history:
        raise ValueError("Circular tool calling detected")
    call_history.add(id(self))
    # perform tool call
    # ...
    call_history.remove(id(self))
    recursive_tool_call(max_depth, current_depth + 1)

3. Timeout Mechanisms

import signal

def recursive_tool_call(max_depth=5, current_depth=0, timeout=10):
    def timeout_handler(signum, frame):
        raise TimeoutError("Tool execution timed out")

    signal.signal(signal.SIGALRM, timeout_handler)
    signal.alarm(timeout)
    try:
        if current_depth >= max_depth:
            return  # base case: max depth reached
        # perform tool call
        # ...
        recursive_tool_call(max_depth, current_depth + 1)
    finally:
        signal.alarm(0)

Verification Run the code with the fixed recursion depth limit and cycle detection to ensure it prevents stack overflow and infinite loops.

Extra Tips

  • Document the risks and provide configuration options for users to set their

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING