rennf93 / psr15-guard
PSR-15 middleware adapter for guard-core-php: translates PSR-7 server requests to the guard-core engine and block verdicts back to PSR-7 responses
Requires
- php: ^8.2
- psr/http-factory: ^1.0
- psr/http-message: ^1.1|^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- rennf93/guard-core-php: ^4.3.2
Requires (Dev)
- nyholm/psr7: ^1.8
- phpunit/php-code-coverage: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-14 18:14:25 UTC
README
PSR-15 middleware adapter for [guard-core-php](https://github.com/Guard-Core/guard-core-php): translates any PSR-7 `ServerRequestInterface` to the guard-core engine and translates block verdicts back to PSR-7 responses. Works with Slim 4, Mezzio, Symfony PSR-15 bridges, or any PSR-7/PSR-15 stack.
Website · Docs · Playground · Dashboard · Discord
Ecosystem
Guard Core is the Python engine. Framework adapters are thin wrappers that translate native request/response types into Guard Core's protocols. The telemetry agents ship security events and metrics to the monitoring backend. Parallel engine implementations exist for Go, PHP, TypeScript (on npm), and Rust (on crates.io) - all ports of the same reference semantics, conformance-tested against the shared adversarial corpus.
Python
| Package | Role | PyPI |
|---|---|---|
| guard-core | Framework-agnostic security engine | |
| guard-agent | Telemetry agent | |
| fastapi-guard | FastAPI / Starlette adapter | |
| flaskapi-guard | Flask adapter | |
| djapi-guard | Django adapter | |
| tornadoapi-guard | Tornado adapter |
Go
Go modules published via GitHub releases. Production-ready.
| Package | Role | Release |
|---|---|---|
| guard-core-go | Go engine | |
| nethttp-guard | net/http adapter | |
| gin-guard | Gin adapter | |
| echo-guard | Echo (v4) adapter | |
| fiber-guard | Fiber (v3) adapter | |
| guard-agent-go | Telemetry agent |
PHP
Published on Packagist under the rennf93 vendor. Production-ready.
| Package | Role | Packagist |
|---|---|---|
| guard-core-php | PHP engine | |
| laravel-guard | Laravel adapter | |
| symfony-guard | Symfony adapter | |
| psr15-guard | PSR-15 adapter | |
| slim-guard | Slim 4 adapter | |
| guard-agent-php | Telemetry agent |
TypeScript / JavaScript
Published under the @guardcore npm scope; source in the guard-core-ts monorepo. Production-ready.
| Package | Role | npm |
|---|---|---|
| @guardcore/core | Core engine | |
| @guardcore/express | Express adapter | |
| @guardcore/nestjs | NestJS adapter | |
| @guardcore/fastify | Fastify adapter | |
| @guardcore/hono | Hono (edge) adapter | |
| guardagent | Telemetry agent |
Rust
Published on crates.io. Production-ready.
| Package | Role | crates.io |
|---|---|---|
| guard-core-engine | Core engine crate | |
| guard-core-rs | Facade crate (consumer entry point) | |
| actix-guard-rs | Actix Web adapter | |
| axum-guard-rs | Axum adapter | |
| tower-guard-rs | Tower adapter | |
| rocket-guard-rs | Rocket adapter | |
| guard-agent-rs | Telemetry agent |
AI Coding Agents
| Package | Role | PyPI |
|---|---|---|
| guard-core-mcp | MCP server: config validation, docs search, detection sandbox |
Features
- The full guard-core-php engine pipeline behind any PSR-15 middleware stack: rate limiting, IP lists, payload inspection, security headers
- Framework-agnostic: works anywhere PSR-7/PSR-15 does
- Composable: the Slim adapter composes it
Documentation
📚 Documentation - full technical documentation for this package.
🛡️ Guard Core - the engine's reference documentation.
🤖 Monitoring Agent Integration - monitor your Guard instance with a monitoring agent.
Install
composer require rennf93/psr15-guard
Usage
use Nyholm\Psr7\Factory\Psr17Factory; use RenzoFranceschini\GuardCore\Config\SecurityConfig; use RenzoFranceschini\GuardCore\Engine\GuardEngine; use RenzoFranceschini\GuardCorePsr15\GuardMiddleware; $config = new SecurityConfig( enableRedis: false, blacklist: ['192.0.2.0/24'], rateLimit: 100, rateLimitWindow: 60, enableRateLimiting: true, ); $factory = new Psr17Factory(); $engine = new GuardEngine($config); $guard = new GuardMiddleware($engine, $factory, $factory); $app->add($guard);
Blocked requests get the engine's BlockResponse translated exactly (status, body, headers), for example 403 Forbidden for a blacklisted IP, 429 Too many requests with Retry-After for a rate limit hit. Passing requests continue down the stack untouched.
Pass-through responses are finished by the middleware too: the engine's security headers and CORS verdict headers are merged on top of the handler's response, and the engine's behavioral return rules observe the response status code plus a body prefix bounded by behaviorMaxResponseBodyInspectBytes (only while behaviorScanResponseBody is on; the PSR-7 stream is rewound after the bounded capture when it is seekable, so emitters still see the full body). Per-route configuration attaches through the middleware's route map or a custom resolver:
use RenzoFranceschini\GuardCore\Routing\RouteConfig; use RenzoFranceschini\GuardCorePsr15\GuardMiddleware; $middleware = new GuardMiddleware( $engine, $responseFactory, $streamFactory, routes: [ '/docs/' => new RouteConfig(enableSuspiciousDetection: false), '/api/' => new RouteConfig(rateLimit: 5, rateLimitWindow: 10), ], // optional: country resolver for RouteConfig geoRateLimits tiers geoRateLimitResolver: $myResolver, // optional: custom resolver receiving the PSR-7 server request routeResolver: fn (ServerRequestInterface $r) => str_starts_with($r->getUri()->getPath(), '/admin') ? new RouteConfig(bypassedChecks: ['rate_limit']) : null, );
routes patterns match a path exactly or as a prefix when they end with /. Geo rate-limit tiers need a country resolver: pass geoRateLimitResolver explicitly, or configure geoIpHandler together with blockedCountries/whitelistCountries (the engine keeps the injected handler only when country lists are set) and the middleware bridges it onto the engine's rate-limit handler automatically.
Lifecycle
PHP shared-nothing applies: construct GuardEngine (and therefore GuardMiddleware) per request in classic FPM, or per worker under Swoole/Octane/RoadRunner. The middleware holds no mutable state of its own. In-memory fallbacks are per-request safety nets; distributed rate limits, IP bans, and cloud-range caches require Redis (set enableRedis: true and point REDIS_HOST/REDIS_PORT at your instance).
Behavior notes
- Fail-closed: if the engine throws, the middleware returns the engine's fail-closed response (
500 Security check failed, honorably overridden bycustomErrorResponses) instead of letting the request through. - Bounded body read: the request body is scanned as a prefix of at most 256 KiB (
PsrGuardRequest::MAX_BODY_BYTES, matching the engine's full-scan window). Payloads beyond the prefix, or signatures split across its boundary, are not detected. - With
redisFailOpen: truethe middleware constructs and serves requests even when Redis is unreachable; withredisFailOpen: falseconstruction fails closed. - No security headers or CORS are added by this adapter.
Testing
composer lint
composer test
composer test runs the plain-PHP suite in bin/test_psr15.php (unit coverage always; set REDIS_HOST to a reachable Redis to include the shared-state integration cases).
License
MIT