Giter Club home page Giter Club logo

php-cors's Introduction

CORS for PHP (using the Symfony HttpFoundation)

Unit Tests PHPStan Level 9 Code Coverage Packagist License Latest Stable Version Total Downloads Fruitcake

Library and middleware enabling cross-origin resource sharing for your http-{foundation,kernel} using application. It attempts to implement the W3C Recommendation for cross-origin resource sharing.

Note: This is a standalone fork of https://github.com/asm89/stack-cors and is compatible with the options for CorsService.

Installation

Require fruitcake/php-cors using composer.

Usage

This package can be used as a library. You can use it in your framework using:

Options

Option Description Default value
allowedMethods Matches the request method. []
allowedOrigins Matches the request origin. []
allowedOriginsPatterns Matches the request origin with preg_match. []
allowedHeaders Sets the Access-Control-Allow-Headers response header. []
exposedHeaders Sets the Access-Control-Expose-Headers response header. []
maxAge Sets the Access-Control-Max-Age response header. 0
supportsCredentials Sets the Access-Control-Allow-Credentials header. false

The allowedMethods and allowedHeaders options are case-insensitive.

You don't need to provide both allowedOrigins and allowedOriginsPatterns. If one of the strings passed matches, it is considered a valid origin. A wildcard in allowedOrigins will be converted to a pattern.

If ['*'] is provided to allowedMethods, allowedOrigins or allowedHeaders all methods / origins / headers are allowed.

Note: Allowing a single static origin will improve cacheability.

Example: using the library

<?php

use Fruitcake\Cors\CorsService;

$cors = new CorsService([
    'allowedHeaders'         => ['x-allowed-header', 'x-other-allowed-header'],
    'allowedMethods'         => ['DELETE', 'GET', 'POST', 'PUT'],
    'allowedOrigins'         => ['http://localhost', 'https://*.example.com'],
    'allowedOriginsPatterns' => ['/localhost:\d/'],
    'exposedHeaders'         => ['Content-Encoding'],
    'maxAge'                 => 0,
    'supportsCredentials'    => false,
]);

$cors->addActualRequestHeaders(Response $response, $origin);
$cors->handlePreflightRequest(Request $request);
$cors->isActualRequestAllowed(Request $request);
$cors->isCorsRequest(Request $request);
$cors->isPreflightRequest(Request $request);

License

Released under the MIT License, see LICENSE.

This package is split-off from https://github.com/asm89/stack-cors and developed as stand-alone library since 2022

php-cors's People

Contributors

adityamenon-exp avatar andreiashu avatar ankurk91 avatar asm89 avatar barryvdh avatar bartlangelaan avatar crynobone avatar davidbarratt avatar driesvints avatar grahamcampbell avatar jbrooksuk avatar jetexe avatar jzawadzki avatar nyholm avatar selcukcukur avatar siwinski avatar warlock39 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-cors's Issues

Documentation is not goood

Steps needed to make it work (on Lumen only)

  • copy cors.php to config\cors.php
  • Then on bootstrap\app.php enable configuration, add this line $app->configure('cors');
  • (in my case, I was in question why the cors value is empty)
  • Then add this line \Illuminate\Http\Middleware\HandleCors::class to $app->middleware
  • Once that done. If you have custom header such as in my case x-api-key you must add it to allowed_headers array example: ['x-api-key']
  • Then on your api call such as axios you can add it to your custom headers which is base on this documentation Doc
  • Note that the X-Auth-Token should be pass as x-auth-token as I check, the server returns it as small cases.

PHP 8.1.3 can't install composer

Hi. I have PHP version 8.1.3.
We have an error for updating Laravel to version 9

Root composer.json requires fruitcake/laravel-cors ^1.2.0, found fruitcake/laravel-cors
[dev-feat-lazyoptions, dev-feat-groupmiddleware, dev-test-single, dev-feat-middlewaretest, 
dev-feat-actions, dev-feat-browsertests, dev-master, dev-v1-backport, dev-feat-prependmiddleware, 
dev-develop, dev-barryvdh-test-laravel9, 
v0.1, ..., 0.11.x-dev, v1.0.0, ..., 1.0.x-dev, v2.0.0-beta1, ..., 2.2.x-dev, v3.0.0, 3.0.x-dev 
(alias of dev-master)] 
but it does not match the constraint.

Is the Vary header necessary for non-cacheable CORS requests?

Is it necessary to include the Vary header in responses to non-CORS requests that wouldn't be cached anyway?
For example, if the request is a POST or PUT request, which are typically not cached,
do we still need to respond with "Vary: Origin"? Personally, I don't think it's particularly necessary.

Related information:

#24
#25

[Feature request] Customize Access-Control-Expose-Headers response values for different routes

I have a requirement. The Access-Control-Expose-Headers response header has different response values on different routes. Currently, the cos.php configuration is uniformly configured, but I don't want to return redundant data like this. So I want to ask if there is any way to achieve this requirement?

Can I submit a PR to modify the method at https://github.com/fruitcake/php-cors/blob/master/src/CorsService.php#L261 so that it first obtains the Access-Control-Expose-Headers response header of $response and then responds by merging $this->exposedHeaders?

How to CORS in Lumen

Hello I switched to Lumen 9.1.6. I had composer saying that laravel-cors has to be replaced. Currently, I have in app.php :

<?php

$app->configure('cors');

$app->middleware([
    Fruitcake\Cors\HandleCors::class,
]);

$app->register(Fruitcake\Cors\CorsServiceProvider::class);

I have replaced \Fruitcake\Cors\HandleCors::class, with \Illuminate\Http\Middleware\HandleCors::class but what about the service provider ?

Proposal: Dynamic Access-Control-Expose-Headers Support

Summary

This proposal aims to enhance the php-cors library by adding support for dynamic of the Access-Control-Expose-Headers header. Currently, this header is set globally, which limits flexibility in scenarios where different routes require different exposed headers.

Motivation

In complex applications, different routes often need to expose different custom headers. The current static, global configuration of Access-Control-Expose-Headers in php-cors doesn't provide the necessary flexibility for such cases. This proposal seeks to address this limitation by allowing developers to dynamically set exposed headers at the controller or middleware level.

Proposed Implementation

  1. Modify the CORS middleware to check for an Access-Control-Expose-Headers header in the response object.
  2. If present, merge this dynamically set header with the globally configured exposed headers.
  3. Use the combined list when setting the final Access-Control-Expose-Headers header in the CORS response.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.