# CORS (Cross-Origin Resource Sharing)
#
# Spec-compliant CORS filter with preflight handling,
# origin validation, and credential support.
#
# The CORS filter must run before filters that reject
# requests (e.g. ip_acl, guardrails) so that preflight
# OPTIONS are handled before those filters block them.
#
# allow_credentials enables Access-Control-Allow-Credentials: true.
# Per the Fetch spec, credentials require explicit origins (not
# wildcards) for allow_origins, allow_methods, and allow_headers.
#
# Usage:
#   cargo run -p praxis-proxy -- -c examples/configs/security/cors.yaml
#
# Exercise:
#   # Simple request with allowed origin:
#   curl -v -H "Origin: https://app.example.com" \
#     http://localhost:8080/api/data
#
#   # Preflight request:
#   curl -v -X OPTIONS \
#     -H "Origin: https://app.example.com" \
#     -H "Access-Control-Request-Method: PUT" \
#     -H "Access-Control-Request-Headers: Content-Type" \
#     http://localhost:8080/api/data
#
#   # Credentialed request (cookies sent cross-origin):
#   curl -v -H "Origin: https://app.example.com" \
#     --cookie "session=abc123" \
#     http://localhost:8080/api/data
#
#   # Disallowed origin (no CORS headers in response):
#   curl -v -H "Origin: https://evil.com" \
#     http://localhost:8080/api/data

listeners:
  - name: default
    address: "127.0.0.1:8080"
    filter_chains:
      - main

filter_chains:
  - name: main
    filters:
      - filter: cors
        allow_origins:
          - "https://app.example.com"
          - "https://*.example.com"
        allow_methods:
          - GET
          - POST
          - PUT
          - DELETE
        allow_headers:
          - Content-Type
          - Authorization
          - X-Request-ID
        expose_headers:
          - X-Request-ID
          - X-RateLimit-Remaining
        max_age: 3600
        allow_credentials: true

      - filter: router
        routes:
          - path_prefix: "/"
            cluster: backend

      - filter: load_balancer
        clusters:
          - name: backend
            endpoints:
              - "127.0.0.1:3000"

insecure_options:
  allow_private_endpoints: true # example proxies to local backends
