Why Your WAF Is Not Enough: The Case for Application-Layer Security
Traditional WAFs protect against known patterns. But modern APIs need protection that understands business logic.
Web Application Firewalls (WAFs) have been the go-to solution for API security for over a decade. They inspect incoming requests, match them against known attack patterns, and block malicious traffic. But as APIs become more complex and business-critical, WAFs are showing their limitations.
The Pattern Matching Problem
WAFs work by pattern matching. They look for SQL injection signatures, XSS payloads, and other known attack vectors. This approach has two fundamental problems:
- 1. False positives: Legitimate requests that happen to match attack patterns get blocked.
- 2. Business logic blindness: WAFs can't detect attacks that exploit business logic, because they don't understand your business.
The Business Logic Gap
Consider an e-commerce API that accepts orders. A WAF will happily pass through a request like this:
POST /api/orders
{
"items": [
{ "id": "SKU-123", "qty": -5, "price": 49.99 }
],
"total": -249.95
} This request is syntactically valid JSON. It doesn't contain SQL injection or XSS. But it's clearly malicious: negative quantities mean the customer gets paid instead of paying. Your WAF can't catch this because it doesn't know that quantities should be positive and totals should match the sum of item prices.
Enter Contract-Based Security
ContractShield takes a different approach. Instead of pattern matching against known attacks, it validates requests against your business contracts. You define what your API accepts using JSON Schema for structure and CEL (Common Expression Language) for invariants.
const contract = {
schema: {
type: 'object',
properties: {
items: {
type: 'array',
minItems: 1,
items: {
properties: {
qty: { type: 'integer', minimum: 1 },
price: { type: 'number', minimum: 0 }
}
}
},
total: { type: 'number', minimum: 0 }
}
},
invariants: [
'request.body.total == sum(request.body.items.map(i, i.price * i.qty))'
]
}; Now the malicious request gets rejected with a clear error: "Invariant violation: total must equal sum of item prices." No pattern matching, no false positives — just business logic enforcement.
Defense in Depth
We're not saying you should throw away your WAF. WAFs still provide value for protecting against known attack patterns, especially at the network edge. But they should be part of a layered defense strategy:
- → Edge (WAF): Block known attack patterns, rate limiting, DDoS protection
- → Application (ContractShield): Business logic validation, contract enforcement, vulnerability checks
- → Database: Parameterized queries, least privilege access
Getting Started
ContractShield is open source and takes minutes to integrate. Install it, define your contracts, and start protecting your API at the application layer.
npm install @cshield/core @cshield/express