Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AISecurityLab Hackagent Guardrail Defense Wrapping

From Leeroopedia
Knowledge Sources
Domains Security_Testing, Guardrails, Defense_Evasion
Last Updated 2026-10-29 00:00 GMT

Overview

A defense wrapping pattern that intercepts prompts before they reach a target model and replies before they are returned, classifying each as safe or unsafe via a classifier model, with a fail-open policy for unavailable classifiers.

Description

Guardrail defense wrapping measures an attack against a defended target. A GuardedModel wraps the target with optional before and after guardrail classifiers:

  • before: Checks each prompt — a blocked prompt never reaches the target.
  • after: Checks each reply — a flagged response is withheld.

Both fail open: an unavailable or unparseable classifier lets the text through, so a misconfigured guardrail never blocks the whole run. This is critical for red-team testing — a guardrail that blocks everything would produce no attack data at all.

The default guardrail (ModelGuardrail) asks a classifier model for a JSON verdict: {"safe": bool, "categories": [...], "reasoning": "..."}. When the classifier is unavailable or the response is unparseable, it falls back to keyword detection, and ultimately fails open.

The result is a GuardrailResult (is_safe, explanation, categories) or, in the ModelResponse, a GuardrailInfo (side, categories, reasoning). A blocked attempt is recorded as error: "blocked by {side} guardrail" — it is stopped by the defense, not a success.

Usage

Use this principle when measuring an attack against a defended target. Configure guardrails.before and/or guardrails.after in the campaign YAML. The fail-open policy ensures the test runs even if the guardrail classifier is down, but the guardrail's blocks are recorded so defense effectiveness can be measured.

Theoretical Basis

# Abstract algorithm (NOT real implementation)
async def acomplete(self, messages):
    # Before: check the prompt
    before_result = self.before.check(last_user_text(messages))
    if not before_result.is_safe:
        return ModelResponse(text="", guardrail=GuardrailInfo(side="before", ...))

    # Send to target
    response = await self.model.acomplete(messages)

    # After: check the reply
    after_result = self.after.check(response.text)
    if not after_result.is_safe:
        return ModelResponse(text="", guardrail=GuardrailInfo(side="after", ...))

    return response

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment