How do I use Content Security Policy (CSP) in Codeigniter4 application

Viewed 21

I am developing a codeigniter4 web-application. I use apache as my web server. When I enable CSP in app/Config/App.php using public $CSPEnabled = true; , I get the image in my web page blocked. How would I unblock images from same origin and disable others by customizing CSP behaviour?.

The head tag of my code is:

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png" />
    <link rel="icon" type="image/png" href="../assets/img/favicon.png" />
    <title><?=$title?></title>
    <!-- Fonts and icons -->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" />
    <!-- Font Awesome Icons -->
    <script src="https://kit.fontawesome.com/42d5adcbca.js" crossorigin="anonymous"></script>
   
    <!-- Nucleo Icons -->
    <link href="../assets/css/nucleo-icons.css" rel="stylesheet" />
    <link href="../assets/css/nucleo-svg.css" rel="stylesheet" />
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Main Styling -->

    <link href="../assets/css/soft-ui-dashboard-tailwind.css?v=1.0.4" rel="stylesheet" />

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/additional-methods.min.js"></script>
    <style>
    .errors {font-size:small;color:red;}
    .error {font-size:small;color:red;}
    .alert {font-size:small;color:red;}
    </style>
 
  </head>

The portion of the page that contains the image which gets blocked after enabling csp is:

<div class="w-full max-w-full px-3 lg:flex-0 shrink-0 md:w-6/12">
                <div class="absolute top-0 hidden w-3/5 h-full -mr-32 overflow-hidden -skew-x-10 -right-40 rounded-bl-xl md:block">
                  <div class="absolute inset-x-0 top-0 z-0 h-full -ml-16 bg-cover skew-x-10" style="background-image: url('../assets/img/curved-images/curved6.jpg')"></div>
                </div>
              </div>
            </div>

My ContentSecurityPolicy.php file has the following settings:

public $reportOnly = true;



public $styleSrc = ['self','https://fonts.googleapis.com/css','https://cdn.tailwindcss.com'];




public $imageSrc = ['self', 'https://cdn.tailwindcss.com'];

Some of the stylings also doesnt work after enabling CSP.

1 Answers

You will have to whitelist the domain from which you load the image in your website at, app\Config\ContentSecurityPolicy.php.

The line to be modified is, public $imageSrc = 'self';

self - You can load images from the same domain as your website.

Example:

public $imageSrc = ['self', 'data: w3.org/svg/2000', 'https://www.google.com'];

With this you can load images from your same domain, google.com and inline SVG images which is usually used in Twitter bootstrap.

Related