Installation
Guards are included in the main QWED SDK:Available guards
| Guard | Purpose |
|---|---|
RAGGuard | Prevents Document-Level Retrieval Mismatch (DRM) in RAG pipelines |
ExfiltrationGuard | Blocks data exfiltration to unauthorized endpoints |
MCPPoisonGuard | Detects poisoned MCP tool definitions |
SelfInitiatedCoTGuard | Verifies autonomous reasoning paths |
SovereigntyGuard | Enforces data residency policies |
ProcessVerifier | Validates IRAC structure and milestone completion |
StateGuard | Deterministic workspace rollback using shadow git snapshots |
SystemGuard | Validates shell commands |
ConfigGuard | Scans configuration for exposed secrets |
StartupHookGuard | Detects malicious Python .pth startup hooks (supply chain defense) |
StateGuard | Deterministic rollback for agentic file operations |
RAGGuard
Prevents Document-Level Retrieval Mismatch (DRM) hallucinations by verifying that retrieved chunks originate from the expected source document.Parameters
Maximum tolerable fraction of mismatched chunks. Use
Fraction for symbolic precision. Floats are rejected.If
True, chunks missing document_id in metadata are treated as mismatches.Methods
verify_retrieval_context(target_document_id, retrieved_chunks) - Verify all chunks belong to the target document.
filter_valid_chunks(target_document_id, retrieved_chunks) - Return only chunks that match the target document.
ExfiltrationGuard
Prevents compromised agents from sending sensitive data to unauthorized endpoints. Acts as a runtime control policy layer.Parameters
URL prefixes or hostnames that agents can call. Pass
[] to block all outbound calls. If None, uses a safe default list of AI API endpoints.Subset of PII types to scan for. Available types:
SSN, CREDIT_CARD, EMAIL, PHONE_US, PASSPORT, IBAN, AWS_ACCESS_KEY, PRIVATE_KEY, JWT, BEARER_TOKEN. Default enables all except PASSPORT.Additional
{name: regex_string} patterns to detect.Methods
verify_outbound_call(destination_url, payload, method) - Verify an outbound API call before execution.
scan_payload(payload) - Standalone PII scan without endpoint check.
Detected PII types
- Social Security Numbers (SSN)
- Credit card numbers (Visa, MasterCard, Amex, Discover)
- Email addresses
- US phone numbers
- IBAN numbers
- AWS access keys
- Private keys (RSA/EC)
- JWT tokens
- Bearer tokens
MCPPoisonGuard
Detects poisoned or tampered Model Context Protocol (MCP) tool definitions before agent execution. Scans for prompt injection attempts and unauthorized URLs.Parameters
Hostnames permitted in tool descriptions. Defaults to common AI API domains.
Additional regex patterns to detect injection attempts.
Also scan parameter descriptions and enum values.
Methods
verify_tool_definition(tool_schema) - Scan a single MCP tool schema.
verify_server_config(server_config) - Scan an entire MCP server configuration.
Detected patterns
<important>,<system>,<instruction>tags- “Ignore previous instructions” variants
- “You are now a…” jailbreak attempts
- “DAN mode” references
- Unauthorized external URLs
SelfInitiatedCoTGuard
Verifies Self-Initiated Chain-of-Thought (S-CoT) reasoning paths. Ensures that AI-generated reasoning plans contain all required domain checkpoints before execution.Parameters
List of milestones/nodes that must be present in the AI’s reasoning plan. All elements must be non-empty strings.
Methods
verify_autonomous_path(generated_reasoning_plan) - Validates the structure of an AI-generated reasoning plan.
SovereigntyGuard
Enforces data residency and sovereignty policies. Prevents sensitive data from being routed to external cloud providers.Parameters
List of provider names considered “local” and safe for sensitive data.
Methods
verify_routing(prompt, target_provider) - Verify that a prompt can be safely routed to the target provider.
Detected sensitive patterns
- Social Security Numbers (dash-separated, space-separated, contiguous)
CONFIDENTIALmarkers
StateGuard
Provides deterministic rollback capabilities for agentic file operations using shadow git snapshots. Before an agent executes file-modifying actions,StateGuard captures an immutable snapshot of the workspace. If the agent’s execution causes a failure, you can roll back to the exact pre-execution state.
Parameters
Absolute path to a directory that is a valid git repository. The directory must exist and contain a
.git folder. StateGuard resolves the path and validates it on initialization.Methods
create_pre_execution_snapshot() — Stages all current changes (git add .) and runs git write-tree to produce an immutable 40-character SHA-1 tree hash. Returns the hash as a str. Raises RuntimeError if the snapshot fails.
rollback(tree_hash) — Restores the workspace to the exact state captured by the given tree hash. Checks out the tree (git checkout <hash> -- .) and cleans untracked files (git clean -fd). Gitignored files (e.g., .env) are preserved. Returns True on success, False on failure.
Security
- Tree hashes are validated against the regex
^[0-9a-f]{40}$to prevent command injection. - All subprocess calls are scoped to the validated
workspace_path. - Exceptions propagate gracefully — a failed snapshot does not leave the workspace in a dirty state.
StartupHookGuard
Defends against supply chain attacks that inject malicious.pth files into Python site-packages directories. These files execute automatically on Python startup — before any application code runs — making them a high-impact persistence mechanism for attackers.
This guard was introduced in response to real-world attacks where compromised PyPI packages planted .pth files to exfiltrate AWS credentials, SSH keys, and crypto wallets.
When to use it
RunStartupHookGuard at application startup — before importing any third-party libraries — to detect compromised environments early. It is especially useful in CI/CD pipelines, container entrypoints, and any environment where packages are installed from public registries.
Parameters
Additional
.pth filenames to add to the allowlist. By default, standard files like setuptools.pth, pip.pth, virtualenv.pth, and coverage.pth are allowed. Any .pth file not on the allowlist is flagged as suspicious.When
True, scans file contents for malicious patterns like exec(, eval(, base64, network imports, and hex-encoded payloads. Allowlisted files are always scanned for tampering regardless of this flag (fail-closed design).Methods
verify_environment_integrity() — Scans all site-packages directories for unauthorized or tampered .pth files.
Returns a dict with:
| Key | Type | Description |
|---|---|---|
verified | bool | True if the environment is clean |
status | str | "CLEAN_ENVIRONMENT" or "COMPROMISED" |
suspicious_hooks | list[str] | Paths to suspicious .pth files |
content_findings | list[str] | Specific malicious patterns found |
scan_errors | list[str] | Directories that could not be scanned |
counts | dict | Per-category counts: malicious, unreadable, unauthorized |
message | str | Human-readable summary |
Detected patterns
The guard scans for these indicators of compromise:exec(andeval(callsbase64encoding/decoding- Network imports:
socket,subprocess,urllib,requests,http.client os.system()andos.popen()calls- Dynamic imports via
__import__() - Hex-encoded byte sequences
- Suspicious
sys.pathentries pointing to/tmp,/dev/shm, or relative path traversals
Custom allowlist example
If your environment uses legitimate.pth files from specific packages, add them to the allowlist:
IRAC audit fields
All guards return IRAC-compliant audit fields for compliance reporting:ProcessVerifier
Validates the structural integrity and process adherence of AI reasoning traces. Ensures workflows follow deterministic process steps using IRAC pattern matching and milestone validation.Methods
verify_irac_structure(reasoning_trace) - Checks for Issue, Rule, Application, and Conclusion components. Returns a decimal score (0.0-1.0) and list of missing steps.
verify_trace(text, key_middle) - Verifies presence of required milestones/keywords. Returns process rate and missed milestones.
See the Process Verifier page for detailed documentation.
StateGuard
Provides deterministic rollback for agentic file operations using shadow git snapshots. Creates an immutable snapshot before agent execution and restores the workspace if verification fails.SystemGuard
Validates shell commands before execution. See Code Engine for details on code verification.ConfigGuard
Scans configuration files for exposed secrets and credentials. See Security Hardening for configuration best practices.Server-side guards
The following guards run on the QWED server and are applied automatically during API request processing. You do not need to invoke them directly — they are documented here for transparency and audit purposes.CodeGuard
Statically analyzes code for security risks using AST parsing (Python) or regex heuristics (Bash/Shell). Blocks Remote Code Execution (RCE) vectors before code reaches the verification engine. Blocked Python patterns:- Dangerous functions:
eval,exec,compile,open,system,popen,__import__,spawn - Dangerous modules:
os,subprocess,sys,shutil,socket,pickle,pty
- RCE chains (
curl | bash,wget | sh) - Destructive commands (
rm -rf) - Fork bombs
- Netcat / reverse shell connections
- Sensitive file access (
/etc/passwd,id_rsa) - Credential hunting (
grepfor passwords/tokens) - Privilege escalation (
sudo)
PIIGuard
Local-first PII and secret detection using pre-compiled regex patterns. Scans request payloads before they are processed by verification engines. Detected secret types:- OpenAI API keys (
sk-proj-...) - Anthropic API keys (
sk-ant-...) - AWS access keys (
AKIA...) - SSH private keys
- Email addresses (excludes common public prefixes like
support@,info@) - Password assignments in code
- Obfuscated API keys (whitespace-separated fragments)
- US phone numbers
SQLGuard
Validates SQL syntax usingsqlglot and enforces read-only mutation policies. Blocks DROP, DELETE, INSERT, UPDATE, ALTER, CREATE, and TRUNCATE statements when mutation is not explicitly allowed.
When
false (default), only SELECT and other read-only queries are permitted. Set to true only in trusted contexts.Next steps
Python SDK
Full Python SDK reference
Security hardening
Security best practices
MCP integration
Using QWED with MCP servers
PII masking
Automatic PII detection and masking