Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions library/Feeds/ProvidedHook/Csp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Icinga\Module\Feeds\ProvidedHook;

use Icinga\Application\Hook\CspHook;
use Icinga\Authentication\Auth;
use Icinga\Module\Feeds\Storage\StorageFactory;
use Icinga\User;
use ipl\Web\Common\Csp as CspInstance;
use ipl\Web\Url;

/**
* Csp hook to add the feed images to the content security policy
*
* All users with the permission 'feeds/view' will get the CSP for all feeds.
* This hook assumes that images are sourced from the same domain as the feed.
*/
class Csp extends CspHook
{
public function getCspForAllUsers(): CspInstance
{
$csp = new CspInstance();

$storage = StorageFactory::getStorage();
foreach ($storage->getFeeds() as $feed) {
$url = Url::fromPath($feed->url);
$csp->add('img-src', $url->getScheme() . '://' . $url->getHost());
}

return $csp;
}

public function getCspForUser(User $user): CspInstance
{
if (Auth::getInstance()->hasPermission('feeds/view')) {
return $this->getCspForAllUsers();
}

return new CspInstance();
}
}
4 changes: 4 additions & 0 deletions run.php
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<?php

use Icinga\Module\Feeds\ProvidedHook\Csp;

Csp::register();