mitsuki / cors
Official CORS listener for the Mitsuki PHP framework with IP whitelisting support
v1.0.0
2026-02-22 09:54 UTC
Requires
- php: >=8.1
- mitsuki/listener-contracts: ^1.0
Requires (Dev)
- mitsuki/http: ^1.0
- pestphp/pest: ^4.4
README
Official CORS listener for the Mitsuki PHP framework with configurable IP whitelisting support. Production-ready for secure REST APIs, microservices, and mobile backends.
โจ Features
- Standard CORS headers support (Origin, Methods, Headers, Credentials, Max-Age)
- Configurable IP whitelisting via
.env(exact IPs + CIDR ranges:192.168.1.0/24) - Main request only (
isMainRequest()validation) - Symfony
kernel.responseevent listener - Production-ready CIDR validation (
ip2long+ subnet mask) - Zero runtime dependencies beyond core Mitsuki contracts
๐ฆ Installation
composer require mitsuki/cors
Production Requirements:
- PHP
^8.1 mitsuki/listener-contracts:^1.0
Development Dependencies:
mitsuki/http:^1.0(unit tests only)pestphp/pest:^4.4(testing)
๐ Usage Examples
Development (Allow All)
CORS_ALLOWED_IPS=""
Production Security
# Localhost + Docker + VPS range CORS_ALLOWED_IPS="127.0.0.1,::1,192.168.1.0/24,10.96.0.0/12"
Generated Response Headers
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 86400
๐งช Testing
# Install dev dependencies (including mitsuki/http for tests) composer install # Run full test suite ./vendor/bin/pest # Run specific tests ./vendor/bin/pest tests/Unit/CorsListenerTest.php
Test Coverage:
- Main vs sub-request handling
- Exact IP matching (
127.0.0.1) - CIDR range validation (
192.168.1.55โ192.168.1.0/24) - Fallback behavior (empty config = allow all)
- Full CORS headers verification
๐๏ธ Architecture
Mitsuki\Listeners\CorsListener implements ListenerResponseInterface
โโโ __construct(array $allowedIps = [])
โโโ onKernelResponse(ResponseEvent $event)
โ โโโ if (!$event->isMainRequest()) return
โ โโโ $clientIp = $request->getClientIp()
โ โโโ if (!$this->isIpAllowed($clientIp)) return
โ โโโ $response->headers->set() // CORS headers
โโโ isIpAllowed(string $ip): bool
โ โโโ foreach($allowedIps) { CIDR/exact match }
โโโ ipInCidr(string $ip, string $cidr): bool
โโโ ip2long() + subnet mask logic
๐ง Advanced Usage
IPv6 Support
CORS_ALLOWED_IPS="2001:db8::/32,::1,127.0.0.1"
Custom Headers/Methods
// Extend the listener class CustomCorsListener extends CorsListener { protected function setCorsHeaders(Response $response): void { parent::setCorsHeaders($response); $response->headers->set('Access-Control-Allow-Headers', 'X-API-Key,Authorization'); } }
๐ฏ Perfect For
- REST APIs โ Flutter/React/Vue SPAs
- Microservices โ Docker/Kubernetes networking
- Secure deployments โ VPS/hosting providers
- JWT/OAuth2 โ API authentication flows
๐ Repository Structure
mitsuki/cors/
โโโ src/
โ โโโ CorsListener.php
โโโ tests/
โ โโโ Unit/
โ โโโ CorsListenerTest.php
โโโ composer.json
โโโ README.md
โโโ LICENSE
๐ License
MIT License ยฉ 2026 ZGenius Matondo
Made with โค๏ธ for the Mitsuki PHP framework