Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sgupta1979's avatar

header(‘Content-Security-Policy: “frame-ancestors \’self\’ https://*.capgemini.com/;”’);

Hello All,

I need to set below header in my Laravel application header(‘Content-Security-Policy: “frame-ancestors \’self\’ https://*.capgemini.com/;”’);

Please suggest me best possible solution to get this done.

0 likes
3 replies
LaryAI's avatar
Level 58

To set a Content Security Policy (CSP) header in a Laravel application, you can use middleware. Middleware allows you to modify the HTTP response before it is sent to the browser. Here's how you can create a middleware to set the Content-Security-Policy header:

  1. Create a Middleware:

    You can create a new middleware using the Artisan command:

    php artisan make:middleware ContentSecurityPolicy
    
  2. Edit the Middleware:

    Open the newly created middleware file located at app/Http/Middleware/ContentSecurityPolicy.php and modify the handle method to add the CSP header:

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Http\Request;
    
    class ContentSecurityPolicy
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle(Request $request, Closure $next)
        {
            $response = $next($request);
    
            $response->headers->set('Content-Security-Policy', "frame-ancestors 'self' https://*.capgemini.com/;");
    
            return $response;
        }
    }
    
  3. Register the Middleware:

    You need to register your middleware in the app/Http/Kernel.php file. You can add it to the $middleware array if you want it to apply to all requests, or to the $routeMiddleware array if you want to apply it to specific routes.

    For global application, add it to the $middleware array:

    protected $middleware = [
        // Other middleware
        \App\Http\Middleware\ContentSecurityPolicy::class,
    ];
    

    For specific routes, add it to the $routeMiddleware array:

    protected $routeMiddleware = [
        // Other route middleware
        'csp' => \App\Http\Middleware\ContentSecurityPolicy::class,
    ];
    

    Then, apply it to routes in your routes/web.php or routes/api.php:

    Route::get('/example', function () {
        // Your route logic
    })->middleware('csp');
    

By following these steps, you will have successfully added a Content-Security-Policy header to your Laravel application. This header will instruct the browser to only allow framing from the specified sources.

omeratayilmaz's avatar

I am not sure if it’s the best-practice solution, but I mostly create a SecurityHeaders middleware (or a new middleware for each header), then call the middleware in web.php

omeratayilmaz's avatar

By the way, I didn’t show the steps one by one because Lary AI already did it

Please or to participate in this conversation.