Function Calling Security & Indirect Prompt Injection Defense

By AI Agent Hub Editorial Desk · Review method · Corrections

Architecture · 5 min read · Reviewed September 18, 2026

Equipping an AI agent with function calling capabilities (such as shell execution, SQL queries, or file I/O) creates high security attack surfaces. An attacker can perform Indirect Prompt Injection by embedding malicious instructions inside retrieved web pages or user input, tricking the agent into executing arbitrary bash commands or leaking environment API keys.

1. Threat Vectors in Autonomous Function Calling

2. Defense Architecture: Dual-LLM & Hardened Sandboxing

Security Controls:

  1. Strict Pydantic Input Validation: Enforce strict JSON schemas and regex whitelists on tool arguments.
  2. Docker / gVisor Container Isolation: Execute all tool actions inside non-root, read-only filesystem containers.
  3. Human-in-the-Loop Approval: Require explicit user confirmation before executing destructive tools (e.g., rm -rf, DROP TABLE).

3. Hardened Tool Registration Python Example

import re
from pydantic import BaseModel, Field, validator

class FileReadInput(BaseModel):
    file_path: str = Field(..., description="Relative path within workspace directory")

    @validator('file_path')
    def prevent_directory_traversal(cls, v):
        # Prevent ../ directory traversal attacks
        if ".." in v or v.startswith("/"):
            raise ValueError("Directory traversal prohibited")
        return v

4. Put a Policy Enforcement Point in Front of Every Tool

A model-generated function call is a proposal, not authorization. Resolve the authenticated user and tenant in application code, map the requested action to a server-side capability, validate arguments against a strict schema, and apply resource-level authorization before execution. Never accept a model-supplied account ID, callback URL, file path, or shell fragment merely because it is valid JSON.

LayerRequired controlFailure it prevents
Tool catalogExpose only tools needed for the current taskUnnecessary authority and confused-deputy attacks
SchemaEnums, bounds, normalized identifiers, no arbitrary commandsArgument injection and parser ambiguity
AuthorizationCheck caller, tenant, object, and action server-sideCross-user or cross-project access
ExecutionSandbox, network allowlist, timeout, idempotency keyHost compromise, exfiltration, and duplicate effects
ApprovalShow exact target and consequence for high-impact actionsHidden payment, deploy, deletion, or communication
AuditRecord proposal, policy decision, result, and actorUntraceable incidents

5. Treat Tool Results as Untrusted Input

Indirect prompt injection can arrive through a web page, retrieved document, email, issue, log, image text, or another tool. Delimit the data, retain its source, and do not allow its text to redefine system policy. A second model or input filter may detect known patterns, but it is not a security boundary. Limit the consequences of a compromised decision with least privilege, deterministic validation, and human confirmation.

Return structured tool results with typed fields rather than a blob that mixes data and instructions. Strip unnecessary active content, cap result size, and avoid feeding secrets back into the model. For a URL-fetch tool, resolve DNS safely, block private and metadata networks, restrict redirects, and re-check the final destination.

6. Adversarial Verification Checklist

Pass conditions should be observable: the unauthorized call never reaches the executor, the run ends in a typed state, and the audit record explains which policy denied it. Regular expressions alone are not an adequate defense because the attack is semantic and can be transformed.

7. Primary References

8. Related Agent Security Articles

What tool-call auditing costs

Validating every tool call before execution sounds expensive until it is priced, because the audit is a small model's job rather than a large one's. Below: one audited call at 35K input, 20K cached, 3K output. Of the 35,000 input tokens, 20,000 are billed at the cache-read rate and 15,000 at full input rate.

Model Cost per call Monthly at 6,000 calls
GPT-5.6 Luna$0.0070$42.00
Gemini 3.8 Flash$0.024$144
Claude Haiku 4.5$0.032$192

Auditing every call on these models costs single-digit dollars a month at six thousand calls. The defence is affordable; what it needs is the schema discipline, which costs engineering time instead of money.

Rates verified against provider documentation on September 18, 2026. Promotional rates expire, so re-check before budgeting: LLM API cost planning · September 2026 pricing update. Run your own numbers in the cost calculator.