phil / cors-bundle
Let browsers call your API from the origins you allow: per-path CORS rules for Symfony, preflight answered early, no wildcard with credentials.
Package info
github.com/parler-haut-interagir-librement/cors-bundle
Type:symfony-bundle
pkg:composer/phil/cors-bundle
Requires
- php: >=8.5
- symfony/config: ^8.2
- symfony/dependency-injection: ^8.2
- symfony/http-foundation: ^8.2
- symfony/http-kernel: ^8.2
Requires (Dev)
- phpunit/phpunit: ^13.4
- psr/log: ^3.0
- symfony/browser-kit: ^8.2
- symfony/event-dispatcher: ^8.2
- symfony/framework-bundle: ^8.2
- symfony/routing: ^8.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-26 19:41:02 UTC
README
Browsers block cross-origin calls an API does not allow: per-path CORS rules for Symfony, preflight answered early, no wildcard with credentials.
phil/cors-bundle answers the Cross-Origin Resource Sharing (CORS) protocol for a Symfony application. Each path pattern has its own policy: the origins, exact or by anchored pattern, the methods, the request headers, the response headers the page may read, the credentials, the time a browser keeps a preflight, and the private network access of browsers that ask for it. The bundle:
- answers the preflights of those paths before the router and the firewall, so no
OPTIONSroute is needed; - sends
Vary: Originon every answer of those paths, so a shared cache never gives one origin's headers to another; - removes the
Access-Control-Allow-Origin,Access-Control-Allow-CredentialsandAccess-Control-Expose-Headersheaders a controller set, when the policy does not allow the origin; - refuses, while the container compiles, or on the first request for a value read from the environment, a wildcard
with credentials, the origin
null, an origin with a path or a default port, an unanchored origin pattern and a pattern that does not compile; - keeps no state between requests, so it runs as is in a long-running worker.
Status
| Part | State |
|---|---|
PhilCorsBundle and its configuration phil_cors |
Implemented and tested |
Preflights, response headers and Vary (CorsListener, CorsHeaders) |
Implemented and tested |
Policies and path rules (CorsPolicy, PathRule, PathPolicyResolver) |
Implemented and tested |
The Flex recipe, in recipe/0.1 |
Prepared |
Documentation
| Document | What it covers |
|---|---|
| Installation | Requirements and installing. |
| Configuration | Every option, with an example, and what is refused. |
| How requests are answered | Preflights, responses, Vary, error pages and the bounds on what a request sends. |
| Coming from NelmioCorsBundle | The options of nelmio_cors and their counterparts. |
| Extension points | The services to replace, and when they are stable. |
| Testing | The bundle's tests. |
| Reference | Every class and service of the bundle. |
Quick start
// config/bundles.php Phil\CorsBundle\PhilCorsBundle::class => ['all' => true],
# config/packages/phil_cors.yaml phil_cors: paths: '^/api/': allow_origins: ['https://app.example.com'] allow_methods: [GET, POST, PUT, DELETE] allow_headers: [Content-Type, Authorization] allow_credentials: true max_age: 3600 '^/feed\.json$': allow_origins: '*'
Requirements
PHP 8.5 or later and Symfony 8.2 or later.
Contributing
Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.
License
MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.