• 7 min read

ContractShield vs Input Validation: Why You Need Both

How ContractShield complements Spring Validation and AWS API Gateway with business-logic security that input validation cannot provide.

"I already use Spring Validation / AWS API Gateway validation. Why do I need ContractShield?" It's a fair question that comes up often. Let's break it down honestly.

What Input Validation Does Well

Spring Boot Validation

Spring's Bean Validation (JSR 380) via Hibernate Validator is excellent for DTO-level validation:

@PostMapping("/users")
public ResponseEntity<User> createUser(
    @Valid @RequestBody CreateUserRequest request
) {
    // If we reach here, validation passed
}

public class CreateUserRequest {
    @NotBlank
    @Email
    private String email;

    @NotNull
    @Size(min = 8, max = 100)
    private String password;

    @Pattern(regexp = "^(user|admin|moderator)$")
    private String role;
}
  • ✓ Type checking and format validation
  • ✓ Size and range constraints
  • ✓ Validation groups for different scenarios
  • ✓ Custom validators via @Constraint

AWS API Gateway

API Gateway validates requests at the edge before they hit your backend:

# OpenAPI schema for API Gateway
components:
  schemas:
    CreateUserRequest:
      type: object
      required:
        - email
        - password
      properties:
        email:
          type: string
          format: email
        password:
          type: string
          minLength: 8
  • ✓ Schema validation at the edge (lower latency)
  • ✓ Required parameter checking (path, query, headers)
  • ✓ OpenAPI/Swagger integration
  • ✓ Reduces backend load from malformed requests

These are great tools. Keep using them. Input validation is a necessary first line of defense.

What Input Validation Cannot Do

Here's a perfectly valid request that passes all input validation:

POST /api/transfers
{
  "fromAccount": "ACC-12345",
  "toAccount": "ACC-67890",
  "amount": 500.00,
  "currency": "USD"
}
  • ✓ Valid JSON structure
  • ✓ All required fields present
  • ✓ Correct types (strings, number)
  • ✓ Amount is positive

But what if:

  • ? ACC-12345 doesn't belong to the authenticated user?
  • ? The user's daily transfer limit is $200?
  • ? The destination account is on a sanctions list?
  • ? This is the 50th transfer today (velocity check)?

Input validation says "valid". Business logic says "fraud".

The Security Layer Gap

Here's where requests flow through your stack:

AWS API Gateway / WAF
✓ Schema validation   ✓ Rate limiting   ✓ IP filtering
↓
Spring Validation / Express middleware
✓ Type checking   ✓ Format validation   ✓ Constraints
↓
THE GAP
Business logic attacks pass through here
IDOR, privilege escalation, business rule bypass, BOLA
↓
Your Application Code
Where vulnerabilities get exploited

This gap is why Broken Object Level Authorization (BOLA) and Broken Function Level Authorization are #1 and #5 on the OWASP API Security Top 10.

Where ContractShield Fits

ContractShield fills the gap with context-aware business logic validation:

# contracts/transfers-create.yaml
endpoint: /api/transfers
method: POST

schema:
  type: object
  required: [fromAccount, toAccount, amount]
  properties:
    fromAccount:
      type: string
      pattern: "^ACC-[0-9]+$"
    toAccount:
      type: string
      pattern: "^ACC-[0-9]+$"
    amount:
      type: number
      minimum: 0.01

# This is what input validation CANNOT express
invariants:
  - expr: request.body.fromAccount in context.user.accounts
    message: Cannot transfer from accounts you don't own

  - expr: request.body.amount <= context.user.dailyLimit - context.user.todayTotal
    message: Daily transfer limit exceeded

  - expr: context.user.transfersToday < 20
    message: Too many transfers today - please contact support

  - expr: request.body.fromAccount != request.body.toAccount
    message: Cannot transfer to the same account

Comparison Table

Capability Spring Validation AWS API Gateway ContractShield
Type validation ✓ ✓ ✓
Format validation ✓ ✓ ✓
Schema validation ✓ ✓ ✓
Cross-field validation Limited ✗ ✓ CEL
User context access ✗ ✗ ✓
Business rules ✗ ✗ ✓
Runtime protection (RASP) ✗ ✗ ✓ Pro
Learning mode ✗ ✗ ✓ Pro
Framework agnostic Java only AWS only ✓

The Honest Answer

Don't replace your input validation. Layer ContractShield on top.

Input Validation = "Is this data well-formed?"
ContractShield = "Is this request legitimate for this user in this context?"

Think of it like a concert venue:

  • • API Gateway = Bouncer checking IDs at the door
  • • Input Validation = Scanning tickets for valid format
  • • ContractShield = Security checking you're allowed in VIP based on your wristband

Adding ContractShield to Your Stack

ContractShield integrates in minutes alongside your existing validation:

npm install @cshield/pep-express
import express from 'express';
import { contractshield } from '@cshield/pep-express';

const app = express();
app.use(express.json());

// Add ContractShield middleware
app.use(contractshield({
  contractsDir: './contracts',
  context: async (req) => ({
    user: req.user,  // From your auth middleware
  }),
}));

// Your routes continue to work as before
app.post('/api/transfers', (req, res) => {
  // If we reach here, both schema AND business rules passed
});

Key Takeaways

  • 1. Input validation is necessary but not sufficient. It catches malformed data, not malicious intent.
  • 2. Business logic attacks are the #1 API vulnerability. BOLA and BFLA dominate the OWASP API Top 10.
  • 3. ContractShield fills the gap between "valid data" and "legitimate request" with context-aware CEL expressions.
  • 4. Layer, don't replace. Keep your Spring Validation, keep API Gateway — add ContractShield for the business logic layer.

Ready to close the gap?

Start with the quick start guide and have ContractShield running in under 5 minutes.