# CSRF Protection
#
# Cross-site request forgery protection via origin
# validation. Safe methods (GET, HEAD, OPTIONS)
# bypass the check. State-changing methods require an
# Origin or Referer header matching the trusted origins.
#
# Sec-Fetch-Site validation provides an additional layer
# of defense: requests with Sec-Fetch-Site: cross-site
# are rejected immediately, before origin matching.
#
# The enforce_percentage option enables gradual rollout:
# set to a value below 100 to enforce CSRF protection on
# only a fraction of requests while monitoring impact.
#
# Usage:
#   cargo run -p praxis-proxy -- -c examples/configs/security/csrf.yaml
#
# Exercise:
#   # Safe method (allowed):
#   curl -v http://localhost:8080/api/data
#
#   # POST with trusted origin (allowed):
#   curl -v -X POST \
#     -H "Origin: https://app.example.com" \
#     http://localhost:8080/api/data
#
#   # POST with untrusted origin (rejected 403):
#   curl -v -X POST \
#     -H "Origin: https://evil.com" \
#     http://localhost:8080/api/data
#
#   # POST without origin (rejected 403):
#   curl -v -X POST http://localhost:8080/api/data
#
#   # POST with cross-site Sec-Fetch-Site (rejected 403):
#   curl -v -X POST \
#     -H "Origin: https://app.example.com" \
#     -H "Sec-Fetch-Site: cross-site" \
#     http://localhost:8080/api/data

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

filter_chains:
  - name: main
    filters:
      - filter: csrf
        trusted_origins:
          - "https://app.example.com"
          - "https://*.example.com"
        enforce_percentage: 100
        enable_sec_fetch_site: 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
