HTTP security

Atom provides middleware for validating request information, protecting browser forms, and setting response policies. Register a middleware globally when it needs to run before route matching. Attach it to a route or group when the policy applies to a particular part of the application.

Middleware When to use it
Trusted Proxies The app runs behind a reverse proxy you control and needs the original client IP, scheme, or host.
Trusted Hosts Requests should be accepted only for configured hostnames.
CORS Browser code served from another origin needs access to your API.
CSRF Protection A browser form or action changes data and authenticates with a session cookie.
Security Headers Browser responses need policies for framing, referrers, or other browser features.
Rate Limiting An endpoint or group needs a request quota over a fixed time window.

Register global middleware

Use the application's middlewares() hook. In this example, the app is behind a configured proxy and serves an API to a separate browser origin. Add the middleware you need to the existing hook.

app/Application.php (excerpt)
use Atom\Http\CorsMiddleware;
use Atom\Http\MiddlewareRegistry;
use Atom\Http\TrustedHostMiddleware;
use Atom\Http\TrustedProxyMiddleware;

protected function middlewares(MiddlewareRegistry $middlewares): void
{
    $middlewares
        ->add(TrustedProxyMiddleware::class)
        ->add(TrustedHostMiddleware::class)
        ->add(CorsMiddleware::class);
}

Configure the trusted proxy addresses, allowed hosts, and allowed origins in the linked chapters. Put trusted proxies before host validation so Atom normalizes the forwarded host first. Global CORS handles browser preflight requests before route matching. If rate limiting is global, put CORS before it so those preflights do not consume quota.

Protect browser routes

Attach CSRF protection to state-changing browser routes that use cookie-based authentication. Add security headers to the browser routes or groups that should share the same response policy. The snippet assumes $savePreferences is your route handler.

use Atom\Router\Route;
use Atom\Security\CsrfMiddleware;
use Atom\Security\SecurityHeadersMiddleware;

Route::post("/preferences", $savePreferences)
    ->middleware(SecurityHeadersMiddleware::class)
    ->middleware(CsrfMiddleware::class);

The form must include a CSRF token. See CSRF Protection for the template field and Security Headers for the defaults and configuration.

Limit a specific endpoint

Use rate limiting on just the routes that need it. The middleware uses Atom's cache to track the configured quota. Here $createExport stands for your export handler.

use Atom\Http\RateLimitMiddleware;
use Atom\Router\Route;

Route::post("/exports", $createExport)
    ->middleware(RateLimitMiddleware::class);

Use a route group when several endpoints share that policy. See Rate Limiting for configuring the window and quota.