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

Uploaded Python 3

File details

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

File metadata

  • Download URL: contractshield-1.5.6.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.6.tar.gz
Algorithm Hash digest
SHA256 e048c91dd75bd205e8bb014272394b566726c1aacb20e265f5b5f39c95a62d1c
MD5 f8d95dc010b928b5987b2bfe3ec47107
BLAKE2b-256 a4214aa2994448c98fc886d9a4f007235be5d091061301159ac2db236934ee46

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: contractshield-1.5.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0aee70f57d1dc81eac3a0cbce2b88b2bf4437e03c8c4f387326374c2d62dec57
MD5 a29a11516d68fadafddb69627dcfd978
BLAKE2b-256 f0d660c761985aa20d57c8c9bfe425df5bad1b66c9ae15e1f2c3ab3d032d4fc2

See more details on using hashes here.

Provenance

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