Skip to main content

Contract-First API Security for Python

Project description

ContractShield

PyPI version License Python OWASP ASVS Level 1 OpenSSF Scorecard OpenSSF Best Practices SLSA Build Level 1

Contract-First API Security for Python

Protect your business logic, not just your formats. ContractShield validates API requests at the application layer using JSON Schema and CEL expressions.

🛡️ What is ContractShield?

ContractShield is an open-source API security middleware that:

  • Validates contracts - Enforce JSON Schema + CEL business rules
  • Blocks vulnerabilities - Detect SQLi, XSS, path traversal before they hit your code
  • Complements your WAF - Protects business logic that infrastructure can't see
Client → WAF (format) → ContractShield (contract + logic) → Your App

📦 Installation

# Core package
pip install contractshield

# With FastAPI support
pip install contractshield[fastapi]

# With Flask support
pip install contractshield[flask]

# With CEL expressions
pip install contractshield[cel]

# Everything
pip install contractshield[all]

🚀 Quick Start

FastAPI

from fastapi import FastAPI
from contractshield.fastapi import ContractShieldMiddleware

app = FastAPI()

app.add_middleware(
    ContractShieldMiddleware,
    openapi_path="./openapi.yaml",
    enable_vulnerability_scan=True
)

@app.post("/api/transfer")
async def transfer(data: dict):
    # ContractShield has already validated:
    # - JSON Schema from OpenAPI spec
    # - CEL business rules
    # - No SQL injection / XSS in fields
    return {"status": "success"}

Flask

from flask import Flask
from contractshield.flask import ContractShieldMiddleware

app = Flask(__name__)
app.wsgi_app = ContractShieldMiddleware(
    app.wsgi_app,
    openapi_path="./openapi.yaml"
)

@app.route("/api/transfer", methods=["POST"])
def transfer():
    return {"status": "success"}

📋 OpenAPI Example

# openapi.yaml
openapi: 3.0.3
info:
  title: Banking API
  version: 1.0.0
paths:
  /api/transfer:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required: [amount, currency]
              properties:
                amount:
                  type: number
                  minimum: 0.01
                  maximum: 100000
                currency:
                  type: string
                  enum: [CHF, EUR, USD]
              x-cel-expression: "amount > 0 && amount <= user.dailyLimit"

🔒 Vulnerability Detection

ContractShield scans all incoming data for:

Vulnerability Example
SQL Injection ' OR 1=1 --
XSS <script>alert(1)</script>
Path Traversal ../../etc/passwd
Prototype Pollution __proto__, constructor

⚡ Pro Features

Upgrade to Pro for advanced capabilities:

  • Learning Mode - Auto-generate rules from traffic
  • Hot-reload - Update rules without restart
  • Sink-aware RASP - Context-aware protection
  • Analytics Dashboard - Visualize threats
from contractshield import ContractShield

shield = ContractShield(
    license="CSH-XXXX-XXXX-XXXX",
    learning_mode=True  # Pro feature
)

Get your license at contractshield.dev/pricing

📚 Documentation

🔗 Links

Security & Compliance

  • OWASP ASVS Level 1 — Input validation (V5), API security (V13), access control (V4). Full compliance map →
  • OWASP API Security Top 10 — Protection against API1 (BOLA), API6 (Mass Assignment), API7 (SSRF), API8 (Injection)
  • CWE Coverage — CWE-22, CWE-78, CWE-89, CWE-639, CWE-915, CWE-918, CWE-943, CWE-1321

📄 License

Apache 2.0 - See LICENSE


🇨🇭 Made in Switzerland

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

contractshield-1.5.8.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

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

contractshield-1.5.8-py3-none-any.whl (37.0 kB view details)

Uploaded Python 3

File details

Details for the file contractshield-1.5.8.tar.gz.

File metadata

  • Download URL: contractshield-1.5.8.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for contractshield-1.5.8.tar.gz
Algorithm Hash digest
SHA256 1798c05ba19553e7305310c8e116378f7665eb1416da104be0bf30554323e340
MD5 ebc621b689e0e87c0b922473f2ff6e8c
BLAKE2b-256 faeef0611a2e37231350a669e683b0d3fb6b7dd769804cf239634016ea60a23a

See more details on using hashes here.

Provenance

The following attestation bundles were made for contractshield-1.5.8.tar.gz:

Publisher: publish-pypi.yml on zeekmartin/contractshield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file contractshield-1.5.8-py3-none-any.whl.

File metadata

  • Download URL: contractshield-1.5.8-py3-none-any.whl
  • Upload date:
  • Size: 37.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for contractshield-1.5.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d3c545db63b60eb38ad29294afd1481ca22e6d2d3d86f90cad97793422f3afaf
MD5 a446d5fd1eb67f60d35ee553669808e6
BLAKE2b-256 364e9daa4b4971842797e74c8a4d4a696b75953ae7ca768539ed9a0b0ebbca45

See more details on using hashes here.

Provenance

The following attestation bundles were made for contractshield-1.5.8-py3-none-any.whl:

Publisher: publish-pypi.yml on zeekmartin/contractshield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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