|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# DBShield Python SDK 🛡️ |
| 4 | + |
| 5 | +[](https://pypi.org/project/dbshield-defense/) |
| 6 | +[](https://www.python.org/) |
| 7 | +[](https://opensource.org/licenses/MIT) |
| 8 | +[](https://ritualdev.in/playground) |
| 9 | +[](https://ritualdev.in/trust) |
| 10 | + |
| 11 | +> Enterprise-grade Database Guardrail & Wire Proxy Python SDK for Autonomous AI Agents built with LangChain, CrewAI, LlamaIndex, and SQLAlchemy. |
| 12 | +
|
| 13 | +[Documentation](https://ritualdev.in/docs) • [Web Portal](https://ritualdev.in) • [Trust Center](https://ritualdev.in/trust) • [Issues](https://github.com/RitualDev-Lab/dbshield-python/issues) |
| 14 | + |
| 15 | +</div> |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## ⚡ Key Features |
| 20 | + |
| 21 | +- **AST Query Validator**: In-memory SQL AST query analyzer detecting tautologies (`WHERE 1=1`), destructive DDL (`DROP TABLE`, `TRUNCATE`), unindexed queries, and row read ceilings. |
| 22 | +- **Dynamic PII Redactor**: Format-preserving pseudonymization and regex redaction for SSNs, Luhn-valid credit card numbers, email addresses, and JWT/API tokens. |
| 23 | +- **Two-Phase Commit Distributed Sandbox**: Dry-run simulation with automatic rollback and single-use HMAC-SHA256 commit tokens. |
| 24 | +- **Transparent Middleware**: DB-API 2.0 cursor wrappers (`DBShieldConnectionWrapper`) and SQLAlchemy hooks (`attach_dbshield`) with zero application refactoring. |
| 25 | +- **AI Agent Framework Integrations**: |
| 26 | + - **LangChain**: `DBShieldSQLDatabase` and `DBShieldQueryTool` |
| 27 | + - **CrewAI**: `DBShieldDatabaseTool` |
| 28 | + - **LlamaIndex**: `DBShieldLlamaIndexTransform` |
| 29 | +- **100% Air-Gapped & Zero Telemetry**: Operates strictly local-first within air-gapped private VPC networks. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 📦 Installation |
| 34 | + |
| 35 | +```bash |
| 36 | +pip install dbshield-defense |
| 37 | +``` |
| 38 | + |
| 39 | +*Note: In code, import as standard `import dbshield` or `from dbshield import DBShieldGuard`.* |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 🚀 Usage Guide |
| 44 | + |
| 45 | +### 1. In-Memory AST SQL Guard |
| 46 | + |
| 47 | +Validate queries generated by LLMs before execution: |
| 48 | + |
| 49 | +```python |
| 50 | +from dbshield import DBShieldGuard |
| 51 | + |
| 52 | +guard = DBShieldGuard() |
| 53 | + |
| 54 | +# 1. Block destructive operations |
| 55 | +result = guard.validate_query("DROP TABLE users") |
| 56 | +print(result.allowed) # False |
| 57 | +print(result.risk_level) # "CRITICAL" |
| 58 | +print(result.violations) # [Violation(code='DESTRUCTIVE_DDL_PROHIBITED', ...)] |
| 59 | + |
| 60 | +# 2. Block unconstrained mutations and tautologies |
| 61 | +result = guard.validate_query("DELETE FROM orders WHERE 1=1") |
| 62 | +print(result.allowed) # False |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +### 2. Dynamic PII Redaction & Format Preservation |
| 68 | + |
| 69 | +```python |
| 70 | +from dbshield import PiiRedactor |
| 71 | + |
| 72 | +redactor = PiiRedactor() |
| 73 | + |
| 74 | +text = "User SSN is 000-12-3456 and email is john@acme.com" |
| 75 | +sanitized = redactor.redact_text(text) |
| 76 | +print(sanitized) |
| 77 | +# -> "User SSN is [REDACTED:SSN:9705bb] and email is j***n@a****e.com" |
| 78 | +``` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +### 3. Transparent SQLAlchemy Integration |
| 83 | + |
| 84 | +```python |
| 85 | +from sqlalchemy import create_engine |
| 86 | +from dbshield import attach_dbshield |
| 87 | + |
| 88 | +engine = create_engine("postgresql://user:pass@localhost:5432/proddb") |
| 89 | + |
| 90 | +# Attach DBShield interceptor hook |
| 91 | +attach_dbshield(engine) |
| 92 | + |
| 93 | +# All queries executed via engine are now validated and PII-sanitized automatically! |
| 94 | +``` |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### 4. LangChain Integration |
| 99 | + |
| 100 | +```python |
| 101 | +from dbshield.integrations.langchain import DBShieldSQLDatabase, DBShieldQueryTool |
| 102 | +from langchain_community.utilities import SQLDatabase |
| 103 | + |
| 104 | +raw_db = SQLDatabase.from_uri("sqlite:///analytics.db") |
| 105 | +shield_db = DBShieldSQLDatabase(raw_db) |
| 106 | + |
| 107 | +# Create an agent tool that refuses destructive DDL or tautology queries |
| 108 | +tool = DBShieldQueryTool(db=shield_db) |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +### 5. CrewAI Database Tool |
| 114 | + |
| 115 | +```python |
| 116 | +from dbshield.integrations.crewai import DBShieldDatabaseTool |
| 117 | +import sqlite3 |
| 118 | + |
| 119 | +conn = sqlite3.connect("company.db") |
| 120 | +db_tool = DBShieldDatabaseTool(connection=conn) |
| 121 | + |
| 122 | +# When an autonomous CrewAI agent generates a query: |
| 123 | +output = db_tool._run("SELECT name, email FROM customers WHERE active = 1") |
| 124 | +print(output) # Rows returned with sensitive PII automatically masked! |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### 6. LlamaIndex Transform |
| 130 | + |
| 131 | +```python |
| 132 | +from dbshield.integrations.llamaindex import DBShieldLlamaIndexTransform |
| 133 | + |
| 134 | +transform = DBShieldLlamaIndexTransform() |
| 135 | +safe_response = transform.postprocess_response("Query returned user SSN 000-12-3456") |
| 136 | +``` |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## 🔒 Security & Compliance |
| 141 | + |
| 142 | +- **Zero Cloud Exfiltration**: Runs 100% locally with `DBSHIELD_AIR_GAPPED=1`. |
| 143 | +- **Pre-filled SIG Lite / CAIQ**: Download the Vendor Risk Assessment Packet at [ritualdev.in/trust](https://ritualdev.in/trust). |
| 144 | +- **Statutory Alignment**: Designed for **India DPDP Act 2023**, **EU GDPR**, and **SOC 2 Type II**. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## 📄 License |
| 149 | + |
| 150 | +MIT © [RitualDev Studio](https://ritualdev.in) |
0 commit comments