Api security

From williamzujkowski/standards · View interactive version

Api security is an AI agent skill from the GitHub repository williamzujkowski/standards. 1. Broken Object Level Authorization (BOLA) - API fails to validate user access to objects 2. Broken Authentication - Weak or missing authentication mechanisms 3. Broken Object Property Level Authorization - Missing field-level access control 4. Unrestricted Resource Consumption - No rate limiting o

How to use this skill

Skill file

API Security

Level System: Each skill contains three progressive levels

- Level 1: Quick Reference (600-800 tokens) - Essential patterns and checklists

- Level 2: Implementation Guide (3000-4500 tokens) - Detailed practices and examples

- Level 3: Deep Dive - Additional resources and advanced topics

Level 1: Quick Reference

OWASP API Security Top 10 (2023)

  1. Broken Object Level Authorization (BOLA) - API fails to validate user access to objects
  2. Broken Authentication - Weak or missing authentication mechanisms
  3. Broken Object Property Level Authorization - Missing field-level access control
  4. Unrestricted Resource Consumption - No rate limiting or throttling
  5. Broken Function Level Authorization - Missing authorization checks on endpoints
  6. Unrestricted Access to Sensitive Business Flows - Automated abuse of legitimate workflows
  7. Server Side Request Forgery (SSRF) - API accepts URLs without validation
  8. Security Misconfiguration - Insecure default configs, verbose errors
  9. Improper Inventory Management - Undocumented/deprecated APIs in production
  10. Unsafe Consumption of APIs - Trusting third-party API data without validation

Essential Security Checklist

transport_security:
  - [ ] TLS 1.2+ enforced on all endpoints
  - [ ] HSTS headers configured
  - [ ] Certificate pinning for mobile clients

authentication:
  - [ ] API keys rotated regularly
  - [ ] OAuth 2.0 with PKCE for public clients
  - [ ] JWT tokens with short expiration (15-60 min)
  - [ ] Refresh token rotation implemented

authorization:
  - [ ] Object-level authorization on all resources
  - [ ] Field-level authorization for sensitive data
  - [ ] Role-based or attribute-based access control

input_validation:
  - [ ] Schema validation on all inputs
  - [ ] Content-Type verification
  - [ ] Request size limits enforced
  - [ ] SQL injection prevention (parameterized queries)

rate_limiting:
  - [ ] Per-user rate limits (e.g., 100 req/min)
  - [ ] Per-IP rate limits for unauthenticated endpoints
  - [ ] Exponential backoff on failed auth attempts
  - [ ] Resource-specific quotas

monitoring:
  - [ ] Authentication failures logged
  - [ ] Authorization failures alerted
  - [ ] Anomalous patterns detected (sudden spikes)
  - [ ] Security events centralized (SIEM)

cors_configuration:
  - [ ] Allowlist specific origins (no wildcards in prod)
  - [ ] Credentials flag set appropriately
  - [ ] Preflight cache configured

api_versioning:
  - [ ] Deprecated versions documented
  - [ ] Sunset headers on old versions
  - [ ] Security fixes backported to supported versions

Quick Rate Limiting Pattern

# Python with Flask-Limiter
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["200 per day", "50 per hour"],
    storage_uri="redis://localhost:6379"
)

@app.route("/api/resource")
@limiter.limit("10 per minute")
def api_resource():
    return {"data": "protected"}

Quick JWT Validation Pattern

// Node.js with jsonwebtoken
const jwt = require('jsonwebtoken');

function authenticateToken(req, res, next) {
  const token = req.headers['authorization']?.split(' ')[1];

  if (!token) return res.sendStatus(401);

  jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
    if (err) return res.sendStatus(403);
    req.user = user;
    next();
  });
}

Level 2:

📚 Full Examples: See REFERENCE.md for complete code samples, detailed configurations, and production-ready implementations.

Implementation Guide

2.1 Authentication Mechanisms

API Key Authentication

Use Case: Server-to-server communication, simple client auth

Implementation Best Practices:

See REFERENCE.md for complete implementation.

Security Considerations:

OAuth 2.0 with PKCE

Use Case: Third-party integrations, mobile/SPA clients

See REFERENCE.md for complete implementation.

JWT Token Management

Best Practices:

See REFERENCE.md for complete implementation.

JWT Security Checklist:

2.2 Rate Limiting and Throttling

Multi-Tier Rate Limiting

See REFERENCE.md for complete implementation.

Adaptive Rate Limiting

See REFERENCE.md for complete implementation.

2.3 Input Validation and Sanitization

Schema-Based Validation

See REFERENCE.md for complete implementation.

Content-Type Validation

See REFERENCE.md for complete implementation.

SQL Injection Prevention

See REFERENCE.md for complete implementation.

2.4 CORS Configuration

Secure CORS Setup

See REFERENCE.md for complete implementation.

CORS Security Rules:

2.5 API Versioning

Version Management Strategy

See REFERENCE.md for complete implementation.

2.6 OpenAPI Security Schemas

Defining Security in OpenAPI 3.0

See REFERENCE.md for complete implementation.

2.7 OWASP API Security Checklist

Complete Implementation Checklist

See REFERENCE.md for complete implementation.

2.8 Testing API Security

Automated Security Testing

See REFERENCE.md for complete implementation.

Security Test Cases

See REFERENCE.md for complete implementation.

2.9 API Security Anti-Patterns

Common Mistakes to Avoid

See REFERENCE.md for complete implementation.


Level 3: Deep Dive Resources

Official Documentation

Tools

Books & Guides

NIST Controls Deep Dive

SC-8: Transmission Confidentiality and Integrity

Implementation:

Validation:

# Test TLS configuration
testssl.sh https://api.example.com

# Verify cipher suites
nmap --script ssl-enum-ciphers -p 443 api.example.com
SC-13: Cryptographic Protection

Implementation:

IA-5: Authenticator Management

Implementation:

AC-7: Unsuccessful Logon Attempts

Implementation:

class LoginAttemptTracker:
    MAX_ATTEMPTS = 5
    LOCKOUT_DURATION = 900  # 15 minutes

    def record_failed_attempt(self, user_id: str):
        key = f"login_attempts:{user_id}"
        attempts = redis.incr(key)
        redis.expire(key, self.LOCKOUT_DURATION)

        if attempts >= self.MAX_ATTEMPTS:
            redis.setex(f"lockout:{user_id}", self.LOCKOUT_DURATION, "locked")
            alert_security_team(user_id, attempts)

    def is_locked_out(self, user_id: str) -> bool:
        return redis.exists(f"lockout:{user_id}")

Bundled Resources

This skill includes the following resources in this directory:

  1. templates/openapi-security.yaml - Complete OpenAPI 3.0 security schemas
  2. templates/rate-limiter.js - Production-ready Express rate limiting middleware
  3. templates/input-validator.py - Pydantic validation models for common use cases
  4. templates/api-gateway-config.yaml - Kong/Nginx gateway configuration
  5. scripts/api-security-scan.sh - Automated OWASP ZAP security scanning
  6. resources/owasp-api-top10.md - Detailed OWASP API Security Top 10 guide

Practice Exercises

  1. Implement JWT Authentication: Build a complete JWT auth system with refresh tokens
  2. Rate Limiting Strategy: Design multi-tier rate limiting for a SaaS API
  3. OWASP Top 10 Audit: Scan an existing API against OWASP API Top 10
  4. Security Testing Suite: Create Postman collection with 20+ security test cases
  5. API Gateway Configuration: Set up Kong with OAuth2, rate limiting, and logging

Continuous Learning

Examples

Basic Usage

// TODO: Add basic example for api-security
// This example demonstrates core functionality

Advanced Usage

// TODO: Add advanced example for api-security
// This example shows production-ready patterns

Integration Example

// TODO: Add integration example showing how api-security
// works with other systems and services

See examples/api-security/ for complete working examples.

Integration Points

This skill integrates with:

Upstream Dependencies

Downstream Consumers

Related Skills

Common Integration Patterns

  1. Development Workflow: How this skill fits into daily development
  2. Production Deployment: Integration with production systems
  3. Monitoring & Alerting: Observability integration points

Common Pitfalls

Pitfall 1: Insufficient Testing

Problem: Not testing edge cases and error conditions leads to production bugs

Solution: Implement comprehensive test coverage including:

Prevention: Enforce minimum code coverage (80%+) in CI/CD pipeline

Pitfall 2: Hardcoded Configuration

Problem: Hardcoding values makes applications inflexible and environment-dependent

Solution: Use environment variables and configuration management:

Prevention: Use tools like dotenv, config validators, and secret scanners

Pitfall 3: Ignoring Security Best Practices

Problem: Security vulnerabilities from not following established security patterns

Solution: Follow security guidelines:

Prevention: Use security linters, SAST tools, and regular dependency updates

Best Practices:


Next Steps: After mastering API security, explore related skills:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.