CORS Middleware for Lumen micro-framework

Related tags

Security lumen-cors
Overview

Lumen-CORS

Cross-origin resource sharing (CORS) Middleware for Lumen micro-framework.

Latest Stable Version License

Installation

After you install lumen as per lumen docs, install lumen-cors from lumen folder.

Install with Composer

Run composer require "palanik/lumen-cors:dev-master" to install lumen-cors.

Manual Installation

Copy the file LumenCors.php into app/Http/Middleware directory.

Usage

Global CORS

If you want CORS enabled for every HTTP request to your application, simply list the middleware class palanik\lumen\Middleware\LumenCors in the $app->middleware() call of your bootstrap/app.php file.

CORS for Routes

If you would like to enable CORS to specific routes, you should first assign the lumen-cors middleware a short-hand key in your bootstrap/app.php file.

$app->routeMiddleware([
    'cors' => 'palanik\lumen\Middleware\LumenCors',
]);

Then, you use the key in the route options array.

$app->get('/data', ['middleware' => 'cors', function() {
    //
}]);

More info. - http://lumen.laravel.com/docs/middleware#registering-middleware

License

MIT

Comments
  • Got a error.

    Got a error.

    Problem 1
        - Installation request for laravel/lumen dev-develop -> satisfiable by laravel/lumen[dev-develop].
        - palanik/lumen-cors dev-master requires laravel/lumen >=5.0.0 -> satisfiable by laravel/lumen[v5.0.0, v5.0.1].
        - Can only install one of: laravel/lumen[v5.0.0, dev-develop].
        - Can only install one of: laravel/lumen[v5.0.1, dev-develop].
        - Installation request for palanik/lumen-cors dev-master -> satisfiable by palanik/lumen-cors[dev-master].
    

    Are you going to be supporting v5.0.1?

    opened by dragonfire1119 10
  • Not working for return response()->download();

    Not working for return response()->download();

    Not working for return response()->download(); I have cors as global middleware.

    Error: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

    opened by KevinBeckers 6
  • Why I got not allowed access?

    Why I got not allowed access?

    I have defined a route like this:

    $app->group(['middleware' => 'cors', 'namespace' => 'App\Http\Controllers'], function($app)  {
        $app->get('/territory/check_in', 'Territories@check_in');
    });
    

    Currently the page that calls the api is at: http://192.168.99.100:9900 And the end point api is: http://192.168.99.100:9907/territory/check_in

    However, when I call it from javascript on another host, I got this error:

    XMLHttpRequest cannot load http://192.168.99.100:9907/territory/check_in. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.99.100:9900' is therefore not allowed access. The response had HTTP status code 405.

    Although its currently wide open for any origin.

    I call it like this:

    $('#check_in').click(function(){
                    $.ajax({
                        url: 'http://192.168.99.100:9907/territory/check_in',
                        type: "get",
                        data: {},
                        success: function(data){
                            alert(data);
                        }
                    });
                });
    

    Can you please advice?

    opened by hopewise 2
  • Wrong settings access for exposeHeaders

    Wrong settings access for exposeHeaders

    In setExposeHeaders the associative array settings is accessed with arrow operator: $this->settings->exposeHeaders (lines 26 - 27). Should not be $this->settings['exposeHeaders']?

    opened by robycup 1
  • No CORS for 401 response

    No CORS for 401 response

    Hello,

    Whenever i try to access to a "authorization required" area, i get an 405 error which is fine, but the CORS is not sent. That behavior happens on nginx too and is solved by adding a 3rd parameter to the "Access-Control-Allow-Origin", something like this:

    "Access-Control-Allow-Origin * always"

    Because by default, nginx only returns CORS header to 2xx and 3xx status code. Maybe there's something similar to fix this, i dont know.

    So far i dont know how to bypass this, so any help would be really appreciated. Thank you :)

    opened by javisperez 1
  • Symfony Responses break this Middleware

    Symfony Responses break this Middleware

    Occasionally, some controllers or middlewares will return a Symfony Response, rather than an Illuminate Response. When this happens, this Middleware fails due to calls to $rsp->header($headerName, $headerValue). This can be easily fixed by replacing these with $rsp->headers->set($headerName, $headerValue).

    Additionally, for full coverage, you could also replace calls to $req->header($headerName) with $reg->headers->get($headerName) to cover the rare occasion when a Symfony Request is used as well (I haven't actually run into that issue, personally).

    opened by goodevilgenius 0
  • Changing ->header() to ->headers->set()

    Changing ->header() to ->headers->set()

    I recently did a composer update and the old $rsp->header() calls were failing with this error:

    Call to undefined method Symfony\Component\HttpFoundation\Response::header()
    in LumenCors.php (line 21)
    at LumenCors->setOrigin(object(Request), object(Response))in LumenCors.php (line 86)
    at LumenCors->setCorsHeaders(object(Request), object(Response))in LumenCors.php (line 108)
    at LumenCors->handle(object(Request), object(Closure))in Pipeline.php (line 148)
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request))in Pipeline.php (line 32)
    at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request))in Pipeline.php (line 102)
    at Pipeline->then(object(Closure))in RoutesRequests.php (line 778)
    at Application->sendThroughPipeline(array('palanik\\lumen\\Middleware\\LumenCors'),object(Closure))in RoutesRequests.php (line 534)
    at Application->dispatch(null)in RoutesRequests.php (line 475)
    at Application->run()in index.php (line 62)
    

    This PR changes it to use $rsp->headers->set() which now works.

    I don't know exactly when the breaking change was introduced.

    opened by daiplusplus 0
  • Specific route implementation not working

    Specific route implementation not working

    I'm trying to use your custom middleware to allow OPTIONS requests in Lumen, but I only managed to use it globally. When I try to use it in specific routes it still doesn't allow OPTIONS. (MethodNotAllowedHttpException) Am I missing something?

    opened by tiagoraposeira 1
  • FatalThrowableError in LumenCors.php line 21:

    FatalThrowableError in LumenCors.php line 21:

    Hi, I get the following error when I use:

    $headers = array('Content-Type' => 'application/zip');
    return response()->download("file", "test", $headers);
    
    FatalThrowableError in LumenCors.php line 21:
    Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()
    in LumenCors.php line 21
    at LumenCors->setOrigin(object(Request), object(BinaryFileResponse)) in LumenCors.php line 86
    at LumenCors->setCorsHeaders(object(Request), object(BinaryFileResponse)) in LumenCors.php line 108
    at LumenCors->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(LumenCors), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in RoutesRequests.php line 633
    at Application->sendThroughPipeline(array('palanik\lumen\Middleware\LumenCors'), object(Closure)) in RoutesRequests.php line 389
    at Application->dispatch(null) in RoutesRequests.php line 334
    at Application->run() in index.php line 28
    at require('/Users/Kevin/Projecten/api/bapi/public/index.php') in server.php line 106
    

    Thx in advance

    opened by KevinBeckers 3
Owner
Palani Kumanan
Engineer in @WSJ Newsroom https://github.com/save-net-neutrality
Palani Kumanan
PHP CORS (Cross-origin resource sharing) middleware.

CORS PHP CORS (Cross-origin resource sharing) middleware. Support Array, Coding in Native PHP Using PSR-7 PSR-15 Support Symfony Support Laravel Suppo

Seven Du 269 Nov 9, 2022
A Simple Cross Origin Resource Sharing for Lumen Framework (5.*).

Lumen Cors Package A Simple Cross Origin Resource Sharing for Lumen Framework. Note: That should works fine on Laravel Framework too, but the tests ar

Vagner Luz do Carmo 46 Jul 27, 2022
A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.

A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.

Alan Woo 51 Nov 21, 2022
Laravel and Lumen Source Code Encrypter

Laravel Source Encrypter This package encrypts your php code with phpBolt For Laravel and Lumen 6, 7, 8 Installation Usage Installation Step 1 At the

Siavash Bamshadnia 363 Jan 1, 2023
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

Cossack Labs 1.6k Jan 6, 2023
Cryptographic component from Zend Framework

zend-crypt Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-crypt. Zend\Crypt provides support of some cryptographic tools

Zend Framework 89 Dec 4, 2021
CORS Middleware for PHP Slim Framework

CorsSlim Cross-origin resource sharing (CORS) Middleware for PHP Slim Framework. Usage Composer Autoloader Install with Composer Update your composer.

Palani Kumanan 92 Nov 30, 2021
CORS (Cross-Origin Resource Sharing) middleware for Hyperf application.

CORS Middleware for Hyperf Implements fruitcake/laravel-cors for Hyperf. Features Handles CORS pre-flight OPTIONS requests Adds CORS headers to your r

Gang Wu 8 Sep 19, 2022
PHP CORS (Cross-origin resource sharing) middleware.

CORS PHP CORS (Cross-origin resource sharing) middleware. Support Array, Coding in Native PHP Using PSR-7 PSR-15 Support Symfony Support Laravel Suppo

Seven Du 269 Nov 9, 2022
CORS (Cross-Origin Resource Sharing) support for Laravel and Lumen

Description This package adds Cross-Origin Resource Sharing (CORS) support to your Laravel application. The package is based on Framework agnostic (PS

null 48 Feb 1, 2020
Lumen rest api demo with Dingo/Api, JWT, CORS, PHPUNIT

lumen-api-demo 这是一个比较完整用 lumen 5.7 写的的 REST API 例子。使用了 dingo/api ,jwt 实现登录,功能上很简单,登录,注册,发帖,评论,单元测试(正在补充)。 lumen5.x 请看对应的分支 有需要随时联系我 lumen/laravel/rest

Yu Li 859 Oct 25, 2022
Rest API boilerplate for Lumen micro-framework.

REST API with Lumen 5.5 A RESTful API boilerplate for Lumen micro-framework. Features included: Users Resource OAuth2 Authentication using Laravel Pas

Hasan Hasibul 484 Sep 16, 2022
API for Symbiota using the Lumen PHP PHP Micro-Framework By Laravel

symbiota-api API for Symbiota using the Lumen PHP PHP Micro-Framework By Laravel Laravel Lumen Official Documentation Documentation for the Lumen fram

Biodiversity Knowledge Integration Center 2 Jan 3, 2022
A PHP project/micro-package generator for PDS compliant projects or micro-packages.

Construct A PHP project/micro-package generator for PDS compliant projects or micro-packages. Installation Construct should be installed globally thro

Jonathan Torres 263 Sep 28, 2022
PSR-7 middleware foundation for building and dispatching middleware pipelines

laminas-stratigility From "Strata", Latin for "layer", and "agility". This package supersedes and replaces phly/conduit. Stratigility is a port of Sen

Laminas Project 47 Dec 22, 2022
This package has framework agnostic Cross-Origin Resource Sharing (CORS) implementation.

Description This package has framework agnostic Cross-Origin Resource Sharing (CORS) implementation. It is complaint with PSR-7 HTTP message interface

null 60 Nov 9, 2022
Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application

CORS Middleware for Laravel Implements https://github.com/asm89/stack-cors for Laravel About The laravel-cors package allows you to send Cross-Origin

Fruitcake 6.2k Jan 8, 2023
A PHP proxy to solve client browser HTTP CORS(cross-origin) restrictions.

cors-bypass-proxy A PHP proxy to solve client browser HTTP CORS(cross-origin) restrictions. A simple way to solve CORS issue when you have no access t

Gracious Emmanuel 15 Nov 17, 2022
Makes WP GraphQL's authetication "just work". It does this by customizing the CORS headers.

WP GraphQL CORS The primary purpose of this plugin is to make the WP GraphQL plugin authentication "just work". It does this by allowing you set the C

Funkhaus 86 Jan 5, 2023