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.7.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.7-py3-none-any.whl (37.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for contractshield-1.5.7.tar.gz
Algorithm Hash digest
SHA256 5d9bbe2abfd3e7971d90cc12e2e577180ffb34cea4d6ea4de0ebd88aba5f8207
MD5 de5a4e2df7217e85d836d847e7b48a3a
BLAKE2b-256 2c264873b9d4b2a83b69a11042933eeb95096873bdf579d1a2c112349c4c8f02

See more details on using hashes here.

Provenance

The following attestation bundles were made for contractshield-1.5.7.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.7-py3-none-any.whl.

File metadata

  • Download URL: contractshield-1.5.7-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.7

File hashes

Hashes for contractshield-1.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 bf90b47f4d7fd1efd89f18a60033bd1f5b02d795d13f52ff9b5fd846ce4217ae
MD5 730475d0664746a7eb0b93e1b1b9d4a2
BLAKE2b-256 5e539614797d2edd19ded3ad3613b465069b3f3ac865e8eb857386076cd69652

See more details on using hashes here.

Provenance

The following attestation bundles were made for contractshield-1.5.7-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