Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup with custom json file #27

Closed
MikeHarrison opened this issue Feb 10, 2022 · 5 comments
Closed

Setup with custom json file #27

MikeHarrison opened this issue Feb 10, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@MikeHarrison
Copy link

MikeHarrison commented Feb 10, 2022

Hi,
I have this plugin working with the default setup, but am struggling to use a custom json file to adjust the policy. This is my config.php (included in full in case there is an issue elsewhere):

<?php

  return [

    'bnomei.securityheaders.loader' => function () {
        return kirby()->roots()->site() . '/csp.json';
    },

    'routes' => [
      [
        'pattern' => 'sitemap.xml',
        'action'  => function() {
            $pages = site()->pages()->index();
  
            // fetch the pages to ignore from the config settings,
            // if nothing is set, we ignore the error page
            $ignore = kirby()->option('sitemap.ignore', ['error']);
  
            $content = snippet('sitemap', compact('pages', 'ignore'), true);
  
            // return response with correct header type
            return new Kirby\Cms\Response($content, 'application/xml');
        }
      ],
      [
        'pattern' => 'sitemap',
        'action'  => function() {
          return go('sitemap.xml', 301);
        }
      ]
    ],
  ];

?>

Then in my site root I have the following json file (csp.json):

{
  "report-only": false,
  "base-uri": {
    "self": true
  },
  "default-src": {
    "self": true
  },
  "connect-src": {
    "self": true
  },
  "font-src": {
    "self": true
  },
  "form-action": {
    "allow": [],
    "self": true
  },
  "frame-ancestors": [],
  "frame-src": {
    "allow": [],
    "self": false
  },
  "img-src": {
    "self": true,
    "data": true
  },
  "media-src": [],
  "object-src": [],
  "plugin-types": [],
  "script-src": {
    "allow": [],
    "hashes": [],
    "self": true,
    "unsafe-inline": false,
    "unsafe-eval": true
  },
  "style-src": {
    "self": true
  },
  "upgrade-insecure-requests": true,
  "worker-src": {
    "allow": [],
    "self": false
  }
}

When using this setup I get no CSP generated at all. Can you please advise where I am going wrong? Thanks!

@bnomei bnomei self-assigned this Feb 10, 2022
@bnomei bnomei added the bug Something isn't working label Feb 10, 2022
@bnomei bnomei closed this as completed in c8489a1 Feb 12, 2022
bnomei added a commit that referenced this issue Feb 12, 2022
:arrow-up: upgraded dependencies

closes #27
thanks @MikeHarrison

Signed-off-by: Bruno Meilick <b@bnomei.com>
@bnomei
Copy link
Owner

bnomei commented Feb 12, 2022

@MikeHarrison it was a bug in my code. please upgrade and it should work now.

if you test on localhost try setting

site/config/config.localhost.php

<?php

return [
    'bnomei.securityheaders.enabled' => 'force', // this will work even when debug is true
    // other options...
];

bnomei added a commit that referenced this issue Feb 12, 2022
:arrow-up: upgraded dependencies

closes #27
thanks @MikeHarrison

Signed-off-by: Bruno Meilick <b@bnomei.com>
@MikeHarrison
Copy link
Author

Hi,
Thanks for taking a look at this. Unfortunately I am still seeing a blank security policy when trying to use a custom json file as a source.

I would be happy to zip up the project and send over if that would help with working out what is happening

@andreasba
Copy link

andreasba commented Sep 23, 2023

I can confirm the same behaviour in the current version @bnomei - can you please check?

This is the relevant part of my config.php for testing:

   // Content Security Policy Plugin
    'bnomei.securityheaders.enabled' => true,
    'bnomei.securityheaders.loader' => function () {

        return __DIR__ . '/loader.json';

    },

The result is an empty CSP policy.

If I remove the loader function, the defaults are appplied so unless I made some mistake, this is not fixed yet.

Can you please take a look?

Thanks!

@andreasba
Copy link

If anybody else is running into the same issue - a workaround is using the setter method:

    'bnomei.securityheaders.setter' => function (\Bnomei\SecurityHeaders $instance) {
        // https://github.com/paragonie/csp-builder#build-a-content-security-policy-programmatically
        // Add a new source domain to the whitelist
        $csp = $instance->csp();
        $csp->addSource('script-src', 'https://subdomain.bla.com');
    },

@Joobs
Copy link

Joobs commented Oct 18, 2023

@andreasba
I had a few issues to start with, make sure loader.json is in the same folder as the config (easiest way to make sure the path is correct), and if the config has any formatting errors it will display an empty policy. I got it working after correcting those two issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants