Enable Injection Defense • AI Agents Framework

enable_injection_defense: Enable prompt injection defense globally for all agents.

enable_injection_defense

Function

</div>

This function is defined in the security module.

Enable prompt injection defense globally for all agents.

Registers hooks on BEFORE_TOOL and BEFORE_AGENT events that scan inputs through a 6-check pipeline and block critical threats.

.. warning:: This registers on the process-global `praisonaiagents.hooks registry, so the policy applies to every Agent in the process. In a multi-tenant host (e.g. praisonai serve) do not rely on this for per-tenant isolation. To scope or tear down later, keep the returned hook IDs and remove them via praisonaiagents.hooks.remove_hook`.

Signature

python
def enable_injection_defense(extra_patterns: Optional[List[str]], block_threshold: Optional[int], trusted_sources: Optional[List[str]]) -> Dict[str, str]

Parameters

ParamField Optional

Additional regex patterns to detect (appended to defaults).

ParamField Optional[int]

Override the threat level that triggers blocking. 3 = CRITICAL, 2 = HIGH (default), 1 = MEDIUM.

ParamField Optional

Source names that bypass blocking (in addition to defaults).

Returns

ResponseField Dict[str, str]

&#123;”before_tool”: str, “before_agent”: str&#125;

Usage

python
>>> from praisonai.security import enable_injection_defense
    >>> enable_injection_defense()

Uses

  • ThreatLevel
  • InjectionDefense
  • add_hook
  • defense.create_hook
  • defense.create_agent_hook

Used By

Source

View on GitHub

praisonai/security/__init__.py at line 85