CoreFiles by mySites.guru
ReportController.php139 lines, 3,157 bytes, from Joomla_4.0.0-alpha5-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha3
md5
26a0a6bb5ab26bc32e236995e68ac397
sha1
0d259751a0b1fe2093e3024cf7455495802a42b7
sha256
8d5aa8786e7c432f3c1e3ae22419ee28175011dc4f389dceace6346b1108f8b8
<?php
/**
 * @package     Joomla.Site
 * @subpackage  com_csp
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Component\Csp\Site\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;

/**
 * Csp Controller
 *
 * @since  4.0.0
 */
class ReportController extends BaseController
{
	/**
	 * Log the CSP request
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function log()
	{
		$pluginParams = new Registry;

		// Get the httpheaders plugin params
		if (PluginHelper::isEnabled('system', 'httpheaders'))
		{
			$pluginParams->loadString(PluginHelper::getPlugin('system', 'httpheaders')->params);
		}

		// When we are not in detect mode do nothing here
		if ($pluginParams->get('contentsecuritypolicy_mode', 'custom') != 'detect')
		{
			$this->app->close();
		}

		$data = $this->input->json->get('csp-report', array(), 'Array');

		if (empty($data))
		{
			$this->app->close();
		}

		$report = new \stdClass;
		$report->document_uri = $data['document-uri'];
		$report->blocked_uri  = $data['blocked-uri'];

		if (filter_var($report->blocked_uri, FILTER_VALIDATE_URL) !== false)
		{
			$parsedUrl = parse_url($report->blocked_uri);
			$report->blocked_uri = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
		}

		// Eval or inline lets make sure they get reported in the correct way
		if (in_array($report->blocked_uri, ['eval', 'inline']))
		{
			$report->blocked_uri = "'unsafe-" . $report->blocked_uri . "'";
		}

		$report->directive = $data['violated-directive'];

		if (empty($data['violated-directive']) && !empty($data['effective-directive']))
		{
			$report->directive = $data['effective-directive'];
		}

		// Empty report
		if (empty($report->blocked_uri) && empty($report->directive))
		{
			$this->app->close();
		}

		$now = Factory::getDate()->toSql();

		$report->created  = $now;
		$report->modified = $now;
		$report->client   = (string) $this->app->input->get('client', 'site', 'string');

		if ($this->isEntryExisting($report))
		{
			$this->app->close();
		}

		$table = $this->app->bootComponent('com_csp')->getMVCFactory()->createTable('Report', 'Administrator');

		$table->bind($report);
		$table->store();

		$this->app->close();
	}

	/**
	 * Check if we already logged this entry
	 *
	 * @param   object  $report  The generated report row
	 *
	 * @return  boolean
	 *
	 * @since   4.0.0
	 */
	private function isEntryExisting($report)
	{
		$db = Factory::getDbo();

		$query = $db->getQuery(true);

		$query
			->select('count(*)')
			->from('#__csp')
			->where($db->quoteName('blocked_uri') . '=' . $db->quote($report->blocked_uri))
			->where($db->quoteName('directive') . '=' . $db->quote($report->directive))
			->where($db->quoteName('client') . '=' . $db->quote($report->client));

		$db->setQuery($query);

		try
		{
			$result = (int) $db->loadResult();
		}
		catch (\RuntimeException $e)
		{
			return false;
		}

		return $result > 0;
	}
}

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.