skills/constitutional-spec-driven-development-enforcing/SKILL.md
Enforce security by construction in AI-generated code using Constitutional Spec-Driven Development (CSDD). Creates a versioned security constitution document mapping CWE/OWASP vulnerabilities to enforceable constraints, then generates code that satisfies those constraints with full traceability. Triggers: 'generate secure code', 'create a security constitution', 'build with security by construction', 'CSDD for my project', 'enforce CWE constraints', 'secure code generation with traceability'
npx skillsauth add ndpvt-web/arxiv-claude-skills constitutional-spec-driven-development-enforcingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill enables Claude to apply Constitutional Spec-Driven Development (CSDD) — a methodology that embeds non-negotiable security principles into the specification layer before code generation begins. Instead of generating code first and scanning for vulnerabilities later, CSDD produces a machine-readable "constitution" of security constraints derived from CWE/MITRE Top 25 and regulatory frameworks, then generates code that satisfies those constraints by construction. The result is code with 73% fewer security defects and a full compliance traceability matrix mapping every principle to specific code locations.
CSDD inverts the traditional generate-then-scan workflow. A security constitution is authored first — a structured document where each principle has an identifier (e.g., SEC-002), a CWE reference, an RFC 2119 enforcement level (MUST/SHOULD/MAY), a concrete constraint, an implementation pattern, and a rationale. This constitution becomes the governing document for all subsequent code generation. When generating code, only 3-5 relevant principles are selected per generation request to stay within context limits, and the AI is instructed to satisfy each constraint using the specified implementation pattern.
The critical insight is that proactive specification outperforms reactive verification. Rather than generating unconstrained code and running static analysis to find 11+ CWE violations, CSDD front-loads the security thinking into a reusable artifact. The constitution is versioned alongside source code, enabling governance — any change to security constraints goes through the same review process as code changes. The methodology produces a compliance traceability matrix that maps each principle to exact file paths and line numbers, giving auditors and reviewers a complete chain from requirement to implementation.
The methodology is domain-agnostic. While the paper demonstrates it through banking microservices, the constitutional framework extends to architectural principles (layered separation, dependency inversion), design patterns (repository, factory), organizational guidelines (naming, logging), and performance constraints (pagination, circuit breakers).
Identify the domain's threat model. Determine which CWE vulnerabilities and regulatory requirements apply. For a web API, start with CWE-79 (XSS), CWE-89 (SQL injection), CWE-287 (authentication), CWE-862 (authorization), CWE-20 (input validation). For financial systems, add CWE-190 (integer overflow), CWE-312 (cleartext storage), CWE-522 (weak credentials).
Author the security constitution. For each identified threat, write a constitutional principle using this exact template:
Principle: SEC-NNN
CWE: CWE-XXX
Level: MUST | SHOULD | MAY
Constraint: [What code must/must not do]
Pattern: [Specific implementation technique]
Rationale: [Why this constraint exists]
Aim for 10-20 principles. Be specific — "parameterized queries via ORM exclusively" not "prevent SQL injection."
Select the technology stack with constitutional rationale. Choose each technology because it enables specific constitutional principles. Document why: e.g., "SQLAlchemy 2.0 — parameterized queries satisfy SEC-002; Pydantic v2 — declarative schema validation satisfies SEC-006; passlib+bcrypt — adaptive hashing with cost factor ≥12 satisfies SEC-009."
Write feature specifications (spec.md). For each feature, describe functional requirements alongside the constitutional constraints that apply. Explicitly reference principle IDs: "Account retrieval MUST verify customer ownership per SEC-010."
Generate implementation plans (plan.md). Break each feature into components and map each component to the constitutional principles it must satisfy. Identify all code locations where security enforcement will occur.
Generate code in constitutional context. For each generation request, include only the 3-5 relevant constitutional principles in the prompt context. Instruct the model: "Generate code satisfying the following constitutional constraints" followed by the selected principles with their patterns.
Verify constitutional compliance inline. After generating each code unit, check that every referenced principle is satisfied. Look for the specific implementation pattern — not just the absence of a vulnerability, but the presence of the mandated technique (e.g., confirm bcrypt with cost ≥12, not just "password is hashed").
Build the compliance traceability matrix. Create a table mapping each principle to: source file, line numbers, implementation technique used. Every principle must have at least one code location. Zero gaps is the target.
Handle violations through regeneration, not patching. If a generated code block violates a principle, regenerate with the constraint made more explicit — do not manually patch. The paper shows regeneration averages 1.4 iterations vs. 3.2 for manual patching.
Version the constitution alongside code. Store the constitution in the repository root (e.g., CONSTITUTION.md or constitution.yaml). Changes to constitutional principles require the same review rigor as production code changes.
Example 1: Secure Banking API
User: "Build me a customer management API with registration, login, and account retrieval"
Approach:
# constitution.yaml
principles:
- id: SEC-001
cwe: CWE-89
level: MUST
constraint: All database queries use parameterized statements via ORM
pattern: SQLAlchemy ORM queries only; no raw SQL or f-string interpolation
rationale: Prevents SQL injection attacks on customer data
- id: SEC-002
cwe: CWE-522
level: MUST
constraint: Passwords hashed with adaptive algorithm before storage
pattern: bcrypt via passlib with cost factor >= 12
rationale: Protects credentials if database is compromised
- id: SEC-003
cwe: CWE-287
level: MUST
constraint: API endpoints require valid JWT bearer token except /health and /register
pattern: OAuth2 with python-jose HS256, 15-minute token expiry
rationale: Prevents unauthorized access to customer resources
- id: SEC-004
cwe: CWE-862
level: MUST
constraint: Resource access requires ownership verification
pattern: Compare authenticated customer_id against resource owner before returning data
rationale: Prevents IDOR attacks on account data
- id: SEC-005
cwe: CWE-20
level: MUST
constraint: All API inputs validated against strict schemas
pattern: Pydantic v2 models with field constraints, regex patterns, type enforcement
rationale: Rejects malformed input before processing
- id: SEC-006
cwe: CWE-200
level: MUST
constraint: Error responses contain no implementation details
pattern: Generic error messages with correlation IDs; details to server logs only
rationale: Prevents information leakage to attackers
# Satisfies SEC-005: Pydantic schema with strict constraints
class CustomerCreate(BaseModel):
email: EmailStr
password: str = Field(min_length=8, max_length=128)
full_name: str = Field(min_length=1, max_length=200)
phone: str = Field(pattern=r"^\+?[1-9]\d{1,14}$")
# Satisfies SEC-002: bcrypt with cost factor 12
pwd_context = CryptContext(schemes=["bcrypt"], bcrypt__rounds=12)
@router.post("/customers", status_code=201)
async def register_customer(data: CustomerCreate, db: Session = Depends(get_db)):
# SEC-001: ORM parameterized query, no raw SQL
existing = db.query(Customer).filter(Customer.email == data.email).first()
if existing:
raise HTTPException(status_code=409, detail="Registration failed") # SEC-006: no details
customer = Customer(
email=data.email,
password_hash=pwd_context.hash(data.password), # SEC-002
full_name=data.full_name,
phone=data.phone,
)
db.add(customer)
db.commit()
return {"id": customer.id, "email": customer.email}
| Principle | CWE | File | Lines | Technique |
|-----------|---------|---------------------|---------|------------------------------|
| SEC-001 | CWE-89 | api/customers.py | 18, 24 | SQLAlchemy ORM filter |
| SEC-002 | CWE-522 | api/customers.py | 12, 22 | passlib bcrypt rounds=12 |
| SEC-003 | CWE-287 | core/security.py | 27-81 | python-jose HS256 JWT |
| SEC-004 | CWE-862 | api/accounts.py | 34-38 | customer_id ownership check |
| SEC-005 | CWE-20 | schemas/customer.py | 5-12 | Pydantic v2 Field validators |
| SEC-006 | CWE-200 | api/customers.py | 20 | Generic error message |
Example 2: Adding a Feature to an Existing Constitutionally-Governed Project
User: "Add a transaction transfer endpoint to the banking API"
Approach:
- id: SEC-007
cwe: CWE-190
level: MUST
constraint: Financial amounts use decimal types with explicit precision
pattern: "Decimal field with gt=0, le=1000000, decimal_places=2; never float"
rationale: Prevents overflow and rounding errors in monetary calculations
class TransferRequest(BaseModel):
from_account: str = Field(pattern=r"^[A-Z0-9]{10}$") # SEC-005
to_account: str = Field(pattern=r"^[A-Z0-9]{10}$") # SEC-005
amount: Decimal = Field(gt=0, le=1000000, decimal_places=2) # SEC-007
@router.post("/transfers", dependencies=[Depends(get_current_customer)]) # SEC-003
async def transfer(req: TransferRequest, customer_id: int = Depends(get_current_customer), db: Session = Depends(get_db)):
# SEC-004: Verify ownership of source account
source = db.query(Account).filter(
Account.number == req.from_account,
Account.customer_id == customer_id # SEC-001 + SEC-004
).first()
if not source:
raise HTTPException(status_code=404, detail="Account not found") # SEC-006
# ... execute transfer with db transaction
Example 3: Creating a Constitution for a Non-Financial Domain
User: "I'm building a healthcare patient portal. Help me set up secure code generation."
Approach:
principles:
- id: PHI-001
cwe: CWE-312
level: MUST
constraint: Patient health information encrypted at rest using AES-256
pattern: Database column-level encryption via SQLAlchemy-Utils EncryptedType
rationale: HIPAA §164.312(a)(2)(iv) requires encryption of ePHI
- id: PHI-002
cwe: CWE-532
level: MUST
constraint: Audit logs must never contain PHI or PII
pattern: Log patient_id only; exclude names, SSN, diagnoses from all log statements
rationale: HIPAA audit trail requirements without PHI exposure
- id: PHI-003
cwe: CWE-862
level: MUST
constraint: Provider can only access patients in their active care assignment
pattern: Join through care_assignment table with active status check
rationale: Minimum necessary access principle under HIPAA
Paper: Constitutional Spec-Driven Development: Enforcing Security by Construction in AI-Assisted Code Generation by Srinivas Rao Marri (2026). Look for: the five-phase development process, the 15 constitutional principles for banking, the compliance traceability matrix format, and the quantitative comparison showing 73% CWE violation reduction. Reference implementation: github.com/srinivasraom/banking-ms-by-constitution.
development
Audit LLM-based automatic short answer grading (ASAG) systems for adversarial vulnerabilities using token-level and prompt-level attack strategies from the GradingAttack framework. Triggers: 'test grading robustness', 'adversarial attack on grading', 'audit LLM grader', 'red-team answer grading', 'ASAG vulnerability assessment', 'grading fairness attack'
development
Build structured information-seeking agents that decompose complex queries into multi-turn search-and-browse workflows, aggregate results from multiple web sources, and return answers in typed structured formats (items, sets, lists, tables). Applies the GISA benchmark's ReAct-based agent architecture and evaluation methodology. Trigger phrases: "build an information-seeking agent", "search agent pipeline", "multi-turn web research agent", "structured web search workflow", "aggregate information from multiple sources", "web research with structured output"
data-ai
Optimize LLM prompts using GFlowPO's iterative generate-evaluate-refine loop with diversity-preserving exploration and dynamic memory. Use when: 'optimize this prompt', 'find a better prompt for this task', 'prompt engineering with examples', 'auto-tune my system prompt', 'improve prompt accuracy', 'generate prompt variations'.
development
Constrain LLM generation with executable Pydantic schemas and multi-agent pipelines to produce structurally valid, domain-rich artifacts. Uses ontology-as-grammar to eliminate hallucinated structures while preserving creative output. Trigger phrases: "generate a valid game design", "schema-constrained generation", "build a multi-agent pipeline with Pydantic validation", "ontology-driven content generation", "structured creative generation with DSPy", "generate artifacts that pass domain validation".