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

Uploaded Python 3

File details

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

File metadata

  • Download URL: contractshield-1.5.5.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.5.tar.gz
Algorithm Hash digest
SHA256 db99cbcf0659283fecc98e2aaae70b91c9a472ef9de1fa345a1f88d9d6b401ae
MD5 6d4e41ac18869d1395ef764424d9350f
BLAKE2b-256 bd56f33106a360227db410ef7bd23879150f5baefc5a1b3bf080fad5c2f394e1

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: contractshield-1.5.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cf038b93be155dabcc9912a8b8d5d224c44771bab4c556271cd5483e6decb73f
MD5 1ae604ba49084a6bf728088e7bb2f8f1
BLAKE2b-256 e136f5f851f12ab24f3c42e257e4142f51b84b626f11ca081ef1974deba2bf8a

See more details on using hashes here.

Provenance

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