Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/sqlite_guard_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
DBShield SQL Guardrail & Cursor Wrapper Example (SQLite + LangChain Wrapper)
Run: python examples/sqlite_guard_example.py
"""

import sqlite3

from dbshield.guard import DBShieldGuard, DBShieldSecurityViolation
from dbshield.integrations.langchain import DBShieldSQLDatabase
from dbshield.middleware import DBShieldCursorWrapper


class _SqliteAdapter:
def __init__(self, conn: sqlite3.Connection) -> None:
self._conn = conn

def run(self, command: str) -> list:
return self._conn.execute(command).fetchall()


def main() -> None:
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT)")
conn.execute("INSERT INTO users (id, email) VALUES (1, 'alice@example.com')")

guard = DBShieldGuard(safety_tier="BALANCED")
cursor = DBShieldCursorWrapper(conn.cursor(), guard)
print("Allowed SELECT:", cursor.execute("SELECT id, email FROM users WHERE id = 1").fetchall())

wrapped_db = DBShieldSQLDatabase(_SqliteAdapter(conn), safety_tier="STRICT")
try:
wrapped_db.run("DROP TABLE users")
except DBShieldSecurityViolation as err:
print("Blocked dangerous DDL:", err)


if __name__ == "__main__":
main()
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ classifiers = [
"Programming Language :: Python :: 3.13",
"Topic :: Security",
"Topic :: Database",
"Topic :: Scientific/Engineering :: Artificial Intelligence"
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Typing :: Typed"
]
requires-python = ">=3.10"
dependencies = []
Expand All @@ -58,6 +59,9 @@ llamaindex = [
sqlalchemy = [
"sqlalchemy>=2.0.57"
]
all = [
"dbshield-defense[langchain,crewai,llamaindex,sqlalchemy]"
]

[project.urls]
Homepage = "https://ritualdev.in/dbshield"
Expand All @@ -68,3 +72,6 @@ Issues = "https://github.com/RitualDev-Lab/dbshield-python/issues"

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
dbshield = ["py.typed"]
Empty file added src/dbshield/py.typed
Empty file.