style: apply ruff format

This commit is contained in:
AlphaRex-pixel 2026-08-14 18:06:28 +05:30
parent ea14b53d30
commit cb2ee9ec1f
3 changed files with 50 additions and 21 deletions

View file

@ -2,6 +2,7 @@
TealEngine: deterministic, dependency-free PII / cost / tool-auth policy
evaluation. No network calls in the governance path.
"""
from __future__ import annotations
import threading

View file

@ -10,6 +10,7 @@ the loosest patterns (e.g. generic credit card, generic driver's license).
Callers can narrow scope via TealEngine(policies=[{"patterns": [...]}])
instead of "all", or override this module's PII_PATTERNS entirely.
"""
import re
PII_PATTERNS = {
@ -26,7 +27,6 @@ PII_PATTERNS = {
"uk_nino": re.compile(r"\b[A-CEGHJ-PR-TW-Z]{2}\d{6}[A-D]\b"),
"date_of_birth": re.compile(r"\b(0[1-9]|1[0-2])[/-](0[1-9]|[12]\d|3[01])[/-](19|20)\d{2}\b"),
"vin": re.compile(r"\b[A-HJ-NPR-Z0-9]{17}\b"),
# ---------- financial ----------
"credit_card_visa": re.compile(r"\b4\d{3}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b"),
"credit_card_mastercard": re.compile(r"\b5[1-5]\d{2}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b"),
@ -39,7 +39,6 @@ PII_PATTERNS = {
"us_bank_account": re.compile(r"\b\d{8,17}\b"),
"bitcoin_address": re.compile(r"\b(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}\b"),
"ethereum_address": re.compile(r"\b0x[a-fA-F0-9]{40}\b"),
# ---------- contact info ----------
"email": re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"),
"phone_us": re.compile(r"\b(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b"),
@ -47,14 +46,12 @@ PII_PATTERNS = {
"us_zip_plus4": re.compile(r"\b\d{5}-\d{4}\b"),
"po_box": re.compile(r"\bP\.?O\.?\s?Box\s?\d+\b", re.IGNORECASE),
"lat_long_coordinates": re.compile(r"\b-?\d{1,3}\.\d{3,},\s?-?\d{1,3}\.\d{3,}\b"),
# ---------- network / device identifiers ----------
"ipv4": re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"),
"ipv6": re.compile(r"\b(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}\b"),
"mac_address": re.compile(r"\b(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}\b"),
"imei": re.compile(r"\b\d{15}\b"),
"url_with_credentials": re.compile(r"\bhttps?://[^:\s]+:[^@\s]+@[^\s]+\b"),
# ---------- credentials / API keys / secrets ----------
"aws_access_key": re.compile(r"\b(AKIA|ASIA)[0-9A-Z]{16}\b"),
"aws_secret_key": re.compile(r"\b(?=.*[A-Za-z])(?=.*\d)(?=.*[+/])[A-Za-z0-9+/]{40}\b"),
@ -79,26 +76,60 @@ def default_patterns() -> dict:
PATTERN_CATEGORIES = {
"government_id": [
"ssn", "ssn_no_dash", "itin", "ein", "us_passport", "passport_generic_intl",
"drivers_license_generic", "medicare_id_us", "npi_number", "uk_nino",
"date_of_birth", "vin",
"ssn",
"ssn_no_dash",
"itin",
"ein",
"us_passport",
"passport_generic_intl",
"drivers_license_generic",
"medicare_id_us",
"npi_number",
"uk_nino",
"date_of_birth",
"vin",
],
"financial": [
"credit_card_visa", "credit_card_mastercard", "credit_card_amex",
"credit_card_discover", "credit_card_generic", "iban", "swift_bic",
"us_bank_routing", "us_bank_account", "bitcoin_address", "ethereum_address",
"credit_card_visa",
"credit_card_mastercard",
"credit_card_amex",
"credit_card_discover",
"credit_card_generic",
"iban",
"swift_bic",
"us_bank_routing",
"us_bank_account",
"bitcoin_address",
"ethereum_address",
],
"contact": [
"email", "phone_us", "phone_intl", "us_zip_plus4", "po_box",
"email",
"phone_us",
"phone_intl",
"us_zip_plus4",
"po_box",
"lat_long_coordinates",
],
"network_device": [
"ipv4", "ipv6", "mac_address", "imei", "url_with_credentials",
"ipv4",
"ipv6",
"mac_address",
"imei",
"url_with_credentials",
],
"credentials": [
"aws_access_key", "aws_secret_key", "gcp_api_key", "azure_client_secret",
"github_token", "gitlab_token", "slack_token", "stripe_live_key",
"openai_api_key", "jwt", "private_key_pem", "generic_bearer_token",
"aws_access_key",
"aws_secret_key",
"gcp_api_key",
"azure_client_secret",
"github_token",
"gitlab_token",
"slack_token",
"stripe_live_key",
"openai_api_key",
"jwt",
"private_key_pem",
"generic_bearer_token",
"basic_auth_header",
],
}

View file

@ -9,6 +9,7 @@ implementation of the same interface.
No network calls, no LLM calls, no external API keys required.
"""
from typing import TYPE_CHECKING, Final, List, Literal, Optional
from litellm.integrations.custom_guardrail import (
@ -63,13 +64,9 @@ class TealTigerGuardrail(CustomGuardrail):
if tool_name and not self.engine.check_tool(tool_name):
raise ValueError(f"TealTiger: blocked — TOOL_NOT_ALLOWLISTED ({tool_name})")
over_budget, spent, limit = self.engine.check_budget(
session_id=request_data.get("user") or "default"
)
over_budget, spent, limit = self.engine.check_budget(session_id=request_data.get("user") or "default")
if over_budget:
raise ValueError(
f"TealTiger: blocked — DAILY_BUDGET_EXCEEDED (${spent:.2f} / ${limit:.2f})"
)
raise ValueError(f"TealTiger: blocked — DAILY_BUDGET_EXCEEDED (${spent:.2f} / ${limit:.2f})")
checked_texts: List[str] = []
for text in inputs.get("texts") or []: