- md5
- 51dbb24c429f524caf07d9a51fb9907b
- sha1
- 180ae9acd6408b1a06a19d6ae0d9a498edff1b12
- sha256
- 8ed30f35220f72377d560139a05ebdb7021c2fbfb046779ea4d8a1312c6c0296
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
<?php
/**
* @package Joomla.Plugin
* @subpackage System.HttpHeader
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Uri\Uri;
use Joomla\Event\SubscriberInterface;
/**
* Plugin class for HTTP Header
*
* @since 4.0.0
*/
class PlgSystemHttpHeaders extends CMSPlugin implements SubscriberInterface
{
/**
* If true, language files will be loaded automatically.
*
* @var boolean
* @since 4.0.0
*/
protected $autoloadLanguage = true;
/**
* Application object.
*
* @var JApplicationCms
* @since 4.0.0
*/
protected $app;
/**
* Database object.
*
* @var DatabaseDriver
* @since 4.0.0
*/
protected $db;
/**
* The list of the supported HTTP headers
*
* @var array
* @since 4.0.0
*/
private $supportedHttpHeaders = [
'strict-transport-security',
'content-security-policy',
'content-security-policy-report-only',
'x-frame-options',
'x-xss-protection',
'x-content-type-options',
'referrer-policy',
'expect-ct',
'feature-policy',
];
/**
* The list of special directives that need to be handled
*
* @var array
* @since 4.0.0
*/
private $specialDirectives = [
'script-src',
'style-src',
];
/**
* Constructor.
*
* @param object &$subject The object to observe.
* @param array $config An optional associative array of configuration settings.
*
* @since 4.0.0
*/
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
// Get the application if not done by JPlugin.
if (!$this->app)
{
$this->app = Factory::getApplication();
}
// Get the db if not done by JPlugin.
if (!$this->db)
{
$this->db = Factory::getDbo();
}
}
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since 4.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onAfterInitialise' => 'setHttpHeaders',
];
}
/**
* The `setHttpHeaders` method handle the setting of the configured HTTP Headers
*
* @return void
*
* @since 4.0.0
*/
public function setHttpHeaders()
{
// Set the default header when they are enabled
$this->setDefaultHeader();
// Nonce generation
$cspNonce = base64_encode(bin2hex(random_bytes(64)));
$this->app->set('csp_nonce', $cspNonce);
// Handle CSP Header configuration
$cspOptions = (int) $this->params->get('contentsecuritypolicy', 0);
if ($cspOptions)
{
$this->setCspHeader($cspNonce);
}
// Handle HSTS Header configuration
$hstsOptions = (int) $this->params->get('hsts', 0);
if ($hstsOptions)
{
$this->setHstsHeader();
}
// Handle the additional httpheader
$httpHeaders = $this->params->get('additional_httpheader', array());
foreach ($httpHeaders as $httpHeader)
{
// Handle the client settings for each header
if (!$this->app->isClient($httpHeader->client) && $httpHeader->client != 'both')
{
continue;
}
if (empty($httpHeader->key) || empty($httpHeader->value))
{
continue;
}
if (!in_array(strtolower($httpHeader->key), $this->supportedHttpHeaders))
{
continue;
}
// Allow the custom csp headers to use the random $cspNonce in the rules
if (in_array(strtolower($httpHeader->key), ['content-security-policy', 'content-security-policy-report-only']))
{
$httpHeader->value = str_replace('{nonce}', $cspNonce, $httpHeader->value);
}
$this->app->setHeader($httpHeader->key, $httpHeader->value, true);
}
}
/**
* Set the default headers when enabled
*
* @return void
*
* @since 4.0.0
*/
private function setDefaultHeader()
{
// X-Frame-Options
if ($this->params->get('xframeoptions', '1') === '1')
{
$this->app->setHeader('X-Frame-Options', 'SAMEORIGIN');
}
// X-XSS-Protection
if ($this->params->get('xxssprotection', '1') === '1')
{
$this->app->setHeader('X-XSS-Protection', '1; mode=block');
}
// X-Content-Type-Options
if ($this->params->get('xcontenttypeoptions', '1') === '1')
{
$this->app->setHeader('X-Content-Type-Options', 'nosniff');
}
// Referrer-Policy
$referrerpolicy = $this->params->get('referrerpolicy', 'no-referrer-when-downgrade');
if ($referrerpolicy !== 'disabled')
{
$this->app->setHeader('Referrer-Policy', $referrerpolicy);
}
}
/**
* Set the CSP header when enabled
*
* @param string $cspNonce The one time string for the script and style tag
*
* @return void
*
* @since 4.0.0
*/
private function setCspHeader($cspNonce)
{
// Mode Selector
$cspMode = $this->params->get('contentsecuritypolicy_mode', 'custom');
// In detecting mode we set this default rule so any report gets collected by com_csp
if ($cspMode === 'detect')
{
$frontendUrl = str_replace('/administrator', '', Uri::base());
$this->app->setHeader(
'Content-Security-Policy-Report-Only',
"default-src 'self'; report-uri " . $frontendUrl . "index.php?option=com_csp&task=report.log&client=" . $this->app->getName()
);
return;
}
$cspReadOnly = (int) $this->params->get('contentsecuritypolicy_report_only', 1);
$cspHeader = $cspReadOnly === 0 ? 'content-security-policy' : 'content-security-policy-report-only';
// In automatic mode we compile the automatic header values and append it to the header
if ($cspMode === 'auto')
{
$automaticRules = trim(
implode(
'; ',
$this->compileAutomaticCspHeaderRules($cspNonce)
)
);
// Set the header
$this->app->setHeader($cspHeader, $automaticRules);
return;
}
// In custom mode we compile the header from the values configured
$cspValues = $this->params->get('contentsecuritypolicy_values', array());
$nonceEnabled = (int) $this->params->get('nonce_enabled', 0);
foreach ($cspValues as $cspValue)
{
// Handle the client settings foreach header
if (!$this->app->isClient($cspValue->client) && $cspValue->client != 'both')
{
continue;
}
// We can only use this if this is a valid entry
if (isset($cspValue->directive) && isset($cspValue->value))
{
if (in_array($cspValue->directive, $this->specialDirectives) && $nonceEnabled)
{
$cspValue->value .= "'nonce-" . $cspNonce . "' " . $cspValue->value;
}
$newCspValues[] = trim($cspValue->directive) . ' ' . trim($cspValue->value);
}
}
if (empty($newCspValues))
{
return;
}
$this->app->setHeader($cspHeader, trim(implode('; ', $newCspValues)));
}
/**
* Set the HSTS header when enabled
*
* @return void
*
* @since 4.0.0
*/
private function setHstsHeader()
{
$maxAge = (int) $this->params->get('hsts_maxage', 31536000);
$hstsOptions = array();
$hstsOptions[] = $maxAge < 300 ? 'max-age=300' : 'max-age=' . $maxAge;
if ($this->params->get('hsts_subdomains', 0))
{
$hstsOptions[] = 'includeSubDomains';
}
if ($this->params->get('hsts_preload', 0))
{
$hstsOptions[] = 'preload';
}
$this->app->setHeader('Strict-Transport-Security', trim(implode('; ', $hstsOptions)));
}
/**
* Compile the automatic csp header rules based on com_csp / #__csp
*
* @param string $nonce The System nonce used for script and style tags
*
* @return array An array containing the csp rules found in com_csp
*
* @since 4.0.0
*/
private function compileAutomaticCspHeaderRules($nonce): array
{
// Get the published infos from the database
$query = $this->db->getQuery(true)
->select($this->db->quoteName(['client', 'directive', 'blocked_uri']))
->from('#__csp')
->where($this->db->quoteName('published') . ' = 1');
$this->db->setQuery($query);
try
{
$rows = (array) $this->db->loadObjectList();
}
catch (\RuntimeException $e)
{
$this->app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
return [];
}
$automaticCspHeader = [];
$cspHeaderCollection = [];
$nonceEnabled = (int) $this->params->get('nonce_enabled', 0);
foreach ($rows as $row)
{
// Handle the client information for each rule
if (!$this->app->isClient($row->client))
{
continue;
}
// Make sure the directive exists as a key
if (!isset($cspHeaderCollection[$row->directive]))
{
$cspHeaderCollection = array_merge($cspHeaderCollection, array_fill_keys([$row->directive], ''));
}
// Eval or inline lets us make sure they still work by adding ' before and after
if (in_array($row->blocked_uri, ['unsafe-eval', 'unsafe-inline']))
{
$row->blocked_uri = "'$row->blocked_uri'";
}
// Whitelist the blocked_uri for the given directive
$cspHeaderCollection[$row->directive] .= ' ' . $row->blocked_uri;
}
// We should have a default-src, script-src and style-src rule
if (!empty($cspHeaderCollection))
{
if (!isset($cspHeaderCollection['default-src']))
{
$cspHeaderCollection = array_merge($cspHeaderCollection, array_fill_keys(['default-src'], ''));
}
if (!isset($cspHeaderCollection['script-src']) && $nonceEnabled)
{
$cspHeaderCollection = array_merge($cspHeaderCollection, array_fill_keys(['script-src'], ''));
}
if (!isset($cspHeaderCollection['style-src']) && $nonceEnabled)
{
$cspHeaderCollection = array_merge($cspHeaderCollection, array_fill_keys(['style-src'], ''));
}
}
foreach ($cspHeaderCollection as $cspHeaderkey => $cspHeaderValue)
{
// Append the random $nonce for the script and style tags if enabled
if (in_array($cspHeaderkey, $this->specialDirectives) && $nonceEnabled)
{
$cspHeaderValue = "'nonce-" . $nonce . "'" . $cspHeaderValue;
}
// By default we should whitelist 'self' on any directive
$automaticCspHeader[] = $cspHeaderkey . " 'self' " . trim($cspHeaderValue);
}
return $automaticCspHeader;
}
}
The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.