Skip to content

Python: Samples: deterministic action-boundary validation middleware (#5366)#6528

Open
eeee2345 wants to merge 1 commit into
microsoft:mainfrom
eeee2345:samples/atr-validation-middleware
Open

Python: Samples: deterministic action-boundary validation middleware (#5366)#6528
eeee2345 wants to merge 1 commit into
microsoft:mainfrom
eeee2345:samples/atr-validation-middleware

Conversation

@eeee2345

Copy link
Copy Markdown

This adds a function-middleware sample that answers #5366: a single deterministic enforcement point that validates a tool call right before it executes.

ATRValidationMiddleware subclasses FunctionMiddleware, inspects the validated arguments in FunctionInvocationContext.arguments, and raises MiddlewareTermination before calling call_next() when the arguments match a known attack pattern, so the tool never runs. The decision is deterministic (no model call, no network), which is what #5366 asks for at the execution boundary.

The check is a small, self-contained deny-list illustrating the pattern. To enforce a full, maintained ruleset instead of the illustrative subset, install the open-source engine (pip install pyatr) and swap the matcher; the sample documents this inline.

Files

  • python/samples/02-agents/middleware/atr_validation_middleware.py
  • a row in the middleware samples README

Local checks

  • ruff clean (line-length 120; long patterns split across adjacent string literals)
  • runs standalone: prints one allowed call and one blocked call

Disclosure: the deny-list patterns mirror the open-source Agent Threat Rules (ATR) project, which I maintain. The sample has no ATR dependency; the reference is for users who want the full ruleset.

…ary validation, microsoft#5366)

Adds python/samples/02-agents/middleware/atr_validation_middleware.py: a
FunctionMiddleware that validates tool arguments at the execution boundary and
raises MiddlewareTermination before call_next() when they match an attack
pattern, so the tool never runs. This is the deterministic, single-enforcement-
point pattern named in microsoft#5366 and answers its open follow-up about a recommended
validation-at-execution-boundary sample.

The check is a small self-contained deny-list mirroring Agent Threat Rules (ATR)
intent (prompt injection, exfiltration, credential access in tool args); a
docstring notes how to swap in the full open ruleset via pyatr. No external
dependency, so the sample stays import-clean.

Updates the middleware README Files table.

Signed-off-by: Adam Lin <adam@agentthreatrule.org>
Copilot AI review requested due to automatic review settings June 15, 2026 19:26
@moonbox3 moonbox3 added documentation Improvements or additions to documentation python labels Jun 15, 2026
@github-actions github-actions Bot changed the title Samples: deterministic action-boundary validation middleware (#5366) Python: Samples: deterministic action-boundary validation middleware (#5366) Jun 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new middleware sample demonstrating deterministic validation of tool arguments at the tool-execution boundary (ATR-style deny-list), and documents it in the middleware samples index.

Changes:

  • Introduces ATRValidationMiddleware sample that blocks suspicious tool calls via MiddlewareTermination before tool execution.
  • Adds a small regex-based deny-list to detect common prompt-injection/exfil/credential-access patterns in tool arguments.
  • Updates the middleware README to include the new sample.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
python/samples/02-agents/middleware/atr_validation_middleware.py New sample middleware + demo agent that blocks tool calls when arguments match ATR-like patterns
python/samples/02-agents/middleware/README.md Adds an entry describing the new ATR validation middleware sample

Comment on lines +18 to +22
from dotenv import load_dotenv
from pydantic import Field

# Load environment variables from .env file
load_dotenv()
Comment on lines +49 to +50
r"\b(?:ignore|disregard|forget|override)\b.{0,40}"
r"\b(?:previous|prior|above|earlier)\b.{0,40}\binstructions?\b",

def _matches_attack_pattern(arguments: dict[str, object]) -> str | None:
"""Return the first matched pattern string, or None when the arguments look benign."""
text = " ".join(str(value) for value in arguments.values())
Comment on lines +105 to +108
print(
f"[ATRValidationMiddleware] Blocked tool '{context.function.name}': "
f"arguments matched an ATR-style attack pattern."
)
# Raise BEFORE call_next() so the tool is never executed.
raise MiddlewareTermination(f"ATR validation blocked tool '{context.function.name}'")

print(f"[ATRValidationMiddleware] Tool '{context.function.name}' passed ATR validation.")
f"arguments matched an ATR-style attack pattern."
)
# Raise BEFORE call_next() so the tool is never executed.
raise MiddlewareTermination(f"ATR validation blocked tool '{context.function.name}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants