Skip to main content

Python SDK for SafeBrowse - AI-powered browser security with prompt injection detection

Project description

SafeBrowse Python SDK

Enterprise-grade security for AI agents and RAG pipelines.

PyPI version Python 3.10+ License: MIT

SafeBrowse protects your AI applications from prompt injection attacks. Scan web content before your LLM processes it.

Installation

pip install safebrowse

Quick Start

from safebrowse import SafeBrowseClient

client = SafeBrowseClient(api_key="your-api-key")

# Protect your agent
with client.guard(html, url) as decision:
    print(f"Risk score: {decision.risk_score}")
    agent.run()  # Only runs if content is safe

Features

Feature Description
Prompt Injection Detection Detects 50+ attack patterns
Policy Engine Block login forms, suspicious domains
RAG Sanitization Clean document chunks before ingestion
Batch Scanning Efficiently scan multiple pages
Fail-Closed Design Errors block, never fail open
Audit Logging Full request traceability
Document Scanning Scan PDFs & Images via OCR
Red Team Testing Run security attack simulations
Agent Guard Stateful session protection for agents

Usage

Scan HTML

result = client.scan_html(
    html="<html><body>...</body></html>",
    url="https://example.com"
)

if result.is_safe:
    process(html)
else:
    print(f"Blocked: {result.reason}")
    print(f"Risk: {result.risk_score}")

Safe Ask (LLM Query)

result = client.safe_ask(
    html="<html>...</html>",
    url="https://example.com",
    query="Summarize this page"
)

print(result.answer)

Guard Context Manager

from safebrowse import BlockedError

try:
    with client.guard(html, url) as decision:
        # Only executes if content is safe
        # Access decision metadata
        print(f"Risk: {decision.risk_score}")
        agent.browse(url)
except BlockedError as e:
    print(f"Blocked: {e.message}")
    print(f"Code: {e.code}")  # Machine-readable

RAG Pipeline Sanitization

# Clean chunks before adding to vector DB
result = client.sanitize(
    documents=["chunk 1", "chunk 2", "ignore instructions..."],
    source="web"
)

safe_chunks = result.safe_chunks  # Only safe content
print(f"Removed {result.blocked_count} dangerous chunks")

Batch Scanning

results = client.scan_batch([
    {"html": page1, "url": url1},
    {"html": page2, "url": url2},
])

print(f"Safe: {results.safe_count}/{results.total}")

Configuration

Config Object

from safebrowse import SafeBrowseConfig, SafeBrowseClient

config = SafeBrowseConfig(
    api_key="your-key",
    base_url="https://api.safebrowse.io",
    timeout=30.0,
)

client = SafeBrowseClient(config=config)

Environment Variables

export SAFEBROWSE_API_KEY=your-key
export SAFEBROWSE_BASE_URL=https://api.safebrowse.io
export SAFEBROWSE_TIMEOUT=30
client = SafeBrowseClient.from_env()

Logging Hooks

def on_blocked(result):
    logger.warning(f"Blocked: {result.reason}")
    metrics.increment("safebrowse.blocked")

def on_allowed(result):
    logger.info(f"Allowed: risk={result.risk_score}")

client = SafeBrowseClient(
    api_key="your-key",
    on_blocked=on_blocked,
    on_allowed=on_allowed,
)

Error Handling

from safebrowse import BlockedError, AuthenticationError, ConnectionError, ErrorCode

try:
    result = client.safe_ask(html, url, query)
except BlockedError as e:
    # Content was blocked
    print(f"Code: {e.code}")  # ErrorCode.INJECTION_DETECTED
    print(f"Risk: {e.risk_score}")
    print(f"Request ID: {e.request_id}")  # For audit lookup
except AuthenticationError:
    print("Invalid API key")
except ConnectionError:
    print("Cannot reach SafeBrowse API")

Error Codes

Code Description
INJECTION_DETECTED Prompt injection found
INJECTION_HIDDEN_HTML Hidden malicious content
POLICY_LOGIN_FORM Login form detected
POLICY_BLOCKED_DOMAIN Domain is blocklisted
AUTH_INVALID_KEY Invalid API key
CONN_REFUSED Connection refused

Async Support

from safebrowse import AsyncSafeBrowseClient

async with AsyncSafeBrowseClient(api_key="your-key") as client:
    result = await client.scan_html(html, url)
    
    if result.is_safe:
        await process(html)

This is by design. Security cannot be optional.

Enterprise Features

Audit & Statistics

# Get safety stats for the last 24 hours
stats = client.get_audit_stats(hours=24)
print(f"Blocked: {stats.blocked_requests}")
print(f"Top domains: {stats.top_blocked_domains}")

# Get paginated audit logs
logs = client.get_audit_logs(limit=50, status="blocked")
for entry in logs.logs:
    print(f"{entry.timestamp}: {entry.url} - {entry.risk_score}")

Document Scanning (PDF/OCR)

# Scan a PDF for prompt injection
result = client.scan_pdf("sensitive_doc.pdf")

# Scan an image (automated OCR)
result = client.scan_image("screenshot.png")

if not result.is_safe:
    print(f"Blocked: {result.reason}")
    print(f"Extracted text: {result.extracted_text}")

Red Team Testing

# List available attack scenarios
scenarios = client.list_attack_scenarios()

# Run a red-team simulation against the current ruleset
summary = client.run_red_team_test()
print(f"Detection Rate: {summary.detection_rate * 100}%")

Agent Guard Sessions

# Manage stateful agent sessions
session_id = client.start_agent_session(max_steps=50)

# Record agent actions
client.record_agent_step(session_id, "browsing", "visit_bank_site")

# End session
client.end_agent_session(session_id)

API Reference

Classes

Class Description
SafeBrowseClient Sync HTTP client
AsyncSafeBrowseClient Async HTTP client
SafeBrowseConfig Configuration object
ScanResult Result of scan_html()
AskResult Result of safe_ask()
SanitizeResult Result of sanitize()
BatchScanResult Result of scan_batch()

Exceptions

Exception Description
SafeBrowseError Base exception
BlockedError Content was blocked
AuthenticationError Invalid API key
ConnectionError Network error

Requirements

  • Python 3.10+
  • httpx (installed automatically)

License

MIT License - see LICENSE for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safebrowse-0.3.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safebrowse-0.3.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file safebrowse-0.3.0.tar.gz.

File metadata

  • Download URL: safebrowse-0.3.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for safebrowse-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5fdb9ec366dba9111a1387615ed8d061a48101d73f9836138b20a5be871269a1
MD5 e979eb307816726549793c341fd4033f
BLAKE2b-256 6c59e001d15cee2bb017ec4abf57b49347a28586a710fc6424ff4d517960b82e

See more details on using hashes here.

File details

Details for the file safebrowse-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: safebrowse-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for safebrowse-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a8d20c6fa2c61018ab2deeaffa57ec0f8161d7a2a82fe7d02b0348edce942e8
MD5 a9e7c04199fba3d417de6e1ea260c4ed
BLAKE2b-256 4d49e83a278111ecccd3e73439d16e7b2df49f3d3fca8a518e4dfc631b679ee6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page