Skip to main content

Contract-First API Security for Python

Project description

ContractShield

PyPI version License Python

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

📄 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.3.tar.gz (35.0 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.3-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contractshield-1.5.3.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for contractshield-1.5.3.tar.gz
Algorithm Hash digest
SHA256 bab740f1ebfa1d6615a1e5e377ece3c7d1b8f3a1d68e4e43d17dc56ed1783918
MD5 8ae9220774dd9a59d3a8200a96be0925
BLAKE2b-256 dae47c6d1e1ccd3fff89f00c72d08be3436ddc70c6e2cb8630ce231f1fac97f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contractshield-1.5.3-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for contractshield-1.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 539762498acc5cde002941f9e2bf847ab46bad3b1b14ae35cebf364af0a26438
MD5 3be53d5ec8178968942f77d0e05be03a
BLAKE2b-256 05adf747e1337db33aa35aa219d91f2ea40e6312dd6377028554303e4f96fe21

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