Threat Modeling as Code (Python)
A declarative, code-first threat-modeling library for Python. Express your system as components, trust boundaries, and data flows, then analyze it with the STRIDE methodology to get actionable, version-controlled mitigations.
Features
- Declarative
Model,Component,Boundary, andDataFlowtypes - STRIDE threat classification:
- Spoofing
- Tampering
- Repudiation
- Information Disclosure
- Denial of Service
- Elevation of Privilege
- Trust-boundary-aware data flow analysis
- Built-in, context-aware mitigation catalog
- Zero runtime dependencies
- Type hints included (
py.typed) - Supports Python 3.8+
Installation
pip install threat-modeling-py
Quick Start
from threat_modeling import Model, Component, Boundary, DataFlow
app = Model('payment-api')
app.add(Component('api', component_type='api', environment='k8s',
stores=['user-data'], exposed=True))
app.add(Boundary('internet', untrusted=True, trusts=['api']))
app.add(DataFlow('request', 'browser', 'api', protocol='https',
auth='bearer', data_types=['payment-card']))
for threat in app.analyze():
print(threat.kind, '-', threat.target)
for mitigation in threat.mitigations:
print(' -', mitigation)
Core Concepts
Model— the top-level container for components, boundaries, and data flows.Component— a service, API, database, browser, or any architectural element.Boundary— a trust boundary;untrusted=Truemarks external zones such as the internet.DataFlow— a directed interaction between two components.Threat— aSTRIDEfinding with a target, description, and list of mitigations.
Why Threat Modeling as Code?
- Version control: threat models live in the same repo as the architecture.
- Automation: run
analyze()in CI to catch changes in risk posture. - Consistency: use the same model across languages and teams.
Cross-Language Support
This library is also available for Go, TypeScript, and Rust.
Development
cd implementations/security/threat-modeling/python
pip install -e ".[dev]"
pytest test_threat_modeling.py -v
License
MIT License
Metadata
Release files for threat-modeling-py 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| threat_modeling_py-1.1.0.tar.gz | 10.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| threat_modeling_py-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.7 kB
Release files / threat_modeling_py-1.1.0.tar.gz
| Download URL | threat_modeling_py-1.1.0.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
33d8711c6292c8ebc27384c18ab1d2e8f865afcba0a827d90a9e1669cf4a8c79
|
|
BLAKE2b-256 checksum How to use checksums |
a9e704017ab3ac0415a0f953ecdcf6a9c4cafbb094c064d014353d2009c3df45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / threat_modeling_py-1.1.0-py3-none-any.whl
| Download URL | threat_modeling_py-1.1.0-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c2e0d73f2178fb3c229cd08bff86c3a2444a3a8e0e227fe0f38921ba75fa0eac
|
|
BLAKE2b-256 checksum How to use checksums |
4a79299e161ab5ad4cb7ea1cf29473e5ce4b1c9818fd1205966d7011754665ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|