J4.x

Http Header Management: Difference between revisions

From Joomla! Documentation

No edit summary
Webby1212 (talk | contribs)
Amend double entry "set set" to specify in section Content Security Policy (CSP) Configuration. Amend {{Joomla version|version=4.x}} to {{Joomla version|version=4.x}} {{Joomla version|version=5.x}}. Add J5.x category
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<noinclude><languages /></noinclude>
<noinclude><languages /></noinclude>
<noinclude>{{Joomla version|version=4.0}}</noinclude>
<noinclude>{{Joomla version|version=4.x}}{{Joomla version|version=5.x}}</noinclude>
{{Top portal heading|color=white-bkgd|icon=magic|icon-color=#5091cd|size=3x|text-color=#333|title=<translate>
{{Top portal heading|color=white-bkgd|icon=magic|icon-color=#5091cd|size=3x|text-color=#333|title=<translate>
<!--T:1-->
<!--T:1-->
Line 21: Line 21:
===Plugin Configuration=== <!--T:8-->
===Plugin Configuration=== <!--T:8-->
</translate>
</translate>
 
<translate><!--T:10--> From this page you can choose to enable that the headers are written to the server configuration files (.htaccess and web.config) and configure whether the following http headers are enabled</translate>
<translate><!--T:10--> From this page you can choose to enable the headers written to the server configuration files (''.htaccess'' and ''web.config'') and configure whether the following HTTP headers are enabled</translate>
* [https://scotthelme.co.uk/hardening-your-http-response-headers/#x-frame-options X-Frame-Options]
* [https://scotthelme.co.uk/hardening-your-http-response-headers/#x-frame-options X-Frame-Options]
* [https://scotthelme.co.uk/a-new-security-header-referrer-policy/ Referrer-Policy]
* [https://scotthelme.co.uk/a-new-security-header-referrer-policy/ Referrer-Policy]
* [https://www.chromestatus.com/feature/5432089535053824 Cross-Origin-Opener-Policy]
* [https://www.chromestatus.com/feature/5432089535053824 Cross-Origin-Opener-Policy]


<translate><!--T:11--> Using the "Force Header" form you can also force the following headers with its values:</translate>
<translate><!--T:11--> Using the ''Force Header'' form you can also force the following headers with its values:</translate>
* [https://scotthelme.co.uk/hsts-the-missing-link-in-tls/ Strict-Transport-Security]
* [https://scotthelme.co.uk/hsts-the-missing-link-in-tls/ Strict-Transport-Security]
* [https://scotthelme.co.uk/content-security-policy-an-introduction/ Content-Security-Policy]
* [https://scotthelme.co.uk/content-security-policy-an-introduction/ Content-Security-Policy]
Line 40: Line 40:
===Strict-Transport-Security (HSTS) Configuration=== <!--T:12-->
===Strict-Transport-Security (HSTS) Configuration=== <!--T:12-->
</translate>
</translate>
 
<translate><!--T:14--> From this page you can choose to enable that the Strict-Transport-Security (HSTS) header as well as configure the max-age value whether subdomains should be included and whether you want to be added to the browsers Preload List.</translate>
<translate><!--T:14--> From this page you can choose to enable the Strict-Transport-Security (HSTS) header as well as configure the ''max-age'' value, whether subdomains should be included and whether you want to be added to the browsers Preload List.</translate>
 


[[File:Plg-system-httpheaders-options-hsts-<translate><!--T:38--> en</translate>.png|center|800px]]
[[File:Plg-system-httpheaders-options-hsts-<translate><!--T:38--> en</translate>.png|center|800px]]


===Content-Security-Policy (CSP) Configuration===
===Content-Security-Policy (CSP) Configuration===
 
From this page you can choose to enable and configure the Content-Security-Policy (CSP) header set by the plugin.
From this page you can choose to enable and configure the Content-Security-Policy (CSP) header set by the plugin.
 
[[File:Plg-system-httpheaders-options-csp-en.png|center|800px]]
[[File:Plg-system-httpheaders-options-csp-en.png|center|800px]]


Once enabled you can set the client where you want to enforce the configured CSP on, allowing you to set set "site", "administrator" or "both".
Once enabled you can set the client where you want to enforce the configured CSP, allowing you to specify ''site'', ''administrator'' or ''both''.


The following settings need a bit more explanation, thats also the reason there are additional descriptions on all of them.
The following settings need a bit more explanation, that's also the reason there are additional descriptions on all of them.


The final option called "Add Directive" allows you to configure the allowlist per directive as you need them. For example the directive "script-src" where the option "Value" you enter the origins you want to allow to load scripts from.  
The final option called ''Add Directive'' allows you to configure the ''allowlist'' per directive as you need them. For example the directive ''script-src'' where the option ''Value'' you enter the origins you want to allow to load scripts from.


<translate>
<translate>
Line 63: Line 63:
<translate>
<translate>
<!--T:42-->
<!--T:42-->
When you have configured some HTTP Security Headers directly on the server, then our tooling might create double entries.
When you have configured some HTTP Security Headers directly on the server, our tooling might create double entries.


<!--T:43-->
<!--T:43-->
Check the output of your HTTP Headers after configuring in the browser console. In Google Chrome: Inspect > Network > the output under Headers). You can than disable the headers that cause double entries. Also check the console of your browser for possible errors.
Check the output of your HTTP Headers after configuring in the browser console. In Google Chrome: Inspect > Network > the output under Headers. You can than disable the headers that cause double entries. Also check the console of your browser for possible errors.


==Extension Developers== <!--T:28-->
==Extension Developers== <!--T:28-->
</translate>
</translate>
<translate><!--T:29--> As you might know the big security advantage concerning Content Security Policy jumps in when we can use the Header to block all inline JavaScript and inline CSS affecting for example JavaScript event handlers via HTML attributes.</translate>
<translate><!--T:29--> As you might know the big security advantage concerning Content Security Policy jumps in when we can use the Header to block all inline JavaScript and inline CSS affecting for example JavaScript event handlers via HTML attributes.</translate>
So with this browser protection enabled we will block inline JavaScript and inline CSS usage also for your extensions. That protection is not enabled by default but can be enabled by your users.
With this browser protection enabled we will block inline JavaScript and inline CSS usage also for your extensions. That protection is not enabled by default but can be enabled by your users.


For 4.0 it would be recommended to get the frontend of your extension running with strict Content Security Policy enabled. For 4.1 compatibility it would be recommended that this also applies to your backend.
For 4.0 it is recommended to get the frontend of your extension running with strict Content Security Policy enabled. For 4.1 compatibility it is recommended that this also applies to your backend.


<translate><!--T:30--> We know that it is still a requirement to have inline JavaScript and CSS, for that reason we have implemented nonce and hash support into our Document APIs when you use them the core will make sure they are whitelisted but we will still block any malicious to protect our sites.</translate>
<translate><!--T:30--> It is still a requirement to have inline JavaScript and CSS. For that reason we have implemented nonce and hash support into our Document APIs. When you use them the core will make sure they are whitelisted but we will still block anything malicious to protect our sites.</translate>


=== Important notes for Extension Developers ===
=== Important Notes for Extension Developers ===
Starting with Joomla 4.0 Content Security Policy:
Starting with Joomla 4.0, Content Security Policy:
* is shipped by the core
* is shipped by the core
* is disabled by default
* is disabled by default
* can be enabled by your users
* can be enabled by your users
* it is strongly recommended that your extension frontend works by 4.0 with Content Security Policy enabled
* it is strongly recommended that your extension frontend works with 4.0 with Content Security Policy enabled
* it is recommended that your extension backend works by 4.1 with Content Security Policy enabled
* it is recommended that your extension backend works with 4.1 with Content Security Policy enabled


With strict Content Security Policy enabled the following features will be blocked:
With strict Content Security Policy enabled the following features will be blocked:
* the execution of JavaScript via the HTML event handlers (onXXX handlers like onClick and similar)
* the execution of JavaScript via the HTML event handlers (''onXXX'' handlers like ''onClick'' and similar)
* the execution of in-page JavaScript not passed to the page via the Document API
* the execution of in-page JavaScript not passed to the page via the Document API
* the execution of JavaScript code injected into DOM APIs such as eval()
* the execution of JavaScript code injected into DOM APIs such as ''eval()''
* the usage of inline in-page CSS not passed to the page via the Document API
* the usage of inline in-page CSS not passed to the page via the Document API
* the usage of inline CSS using the HTML style attribute
* the usage of inline CSS using the HTML style attribute


<translate><!--T:31--> To get your extensions work even with strict Content Security Policy enabled, the easiest way is to use the Document API to apply your inline JavaScript and CSS, please check the examples below.</translate>  
<translate><!--T:31--> To get your extensions to work even with strict Content Security Policy enabled, use the Document API to apply your inline JavaScript and CSS. Check the examples below.</translate>


<translate>
<translate>
===Adding JavaScript using the Joomla API=== <!--T:32-->
===Adding JavaScript Using the Joomla API=== <!--T:32-->
</translate>
</translate>


<source lang="php">
<syntaxhighlight lang="php">
use Joomla\CMS\Factory;
use Joomla\CMS\Factory;


Line 113: Line 113:
     });
     });
');
');
</source>
</syntaxhighlight>


<translate>
<translate>
===Adding CSS using the Joomla API=== <!--T:33-->
===Adding CSS Using the Joomla API=== <!--T:33-->
</translate>
</translate>


<source lang="php">
<syntaxhighlight lang="php">
use Joomla\CMS\Factory;
use Joomla\CMS\Factory;


Line 135: Line 135:
}
}
');
');
</source>
</syntaxhighlight>


<translate><!--T:34--> More details can be found here:</translate>[[S:MyLanguage/J3.x:Adding_JavaScript_and_CSS_to_the_page|<translate><!--T:35--> Adding JavaScript and CSS to the page</translate>]]
<translate><!--T:34--> More details can be found here:</translate> [[S:MyLanguage/J3.x:Adding_JavaScript_and_CSS_to_the_page|<translate><!--T:35--> Adding JavaScript and CSS to the page</translate>]]


== Additional resources about Content Security Policy and HTTP Headers ==
== Additional Resources about Content Security Policy and HTTP Headers ==


* [https://scotthelme.co.uk/csp-cheat-sheet/ CSP Cheat Sheet]
* [https://scotthelme.co.uk/csp-cheat-sheet/ CSP Cheat Sheet]
Line 154: Line 154:
[[Category:Joomla! 4.0{{#translation:}}]]
[[Category:Joomla! 4.0{{#translation:}}]]
[[Category:Joomla! 4.x{{#translation:}}]]
[[Category:Joomla! 4.x{{#translation:}}]]
[[Category:Joomla! 5.x{{#translation:}}]]
[[Category:Plugins{{#translation:}}]]
[[Category:Plugins{{#translation:}}]]
[[Category:Security{{#translation:}}]]
[[Category:Security{{#translation:}}]]
</noinclude>
</noinclude>

Latest revision as of 15:32, 31 January 2024

Joomla! 
4.x
Joomla! 
5.x
Tutorial
How to Use the New HTTP Header Management in Joomla 4.0


As of Joomla 4.0, Joomla introduced an HTTP Header Management System. This System is designed to help site owners to configure the HTTP Security Headers from the Backend

In this tutorial, you will find information on how to set up this new system on your site.

Plugin

System - HTTP Headers (plg_system_httpheaders)

Navigate to System  Plugins  System - HTTP Headers to access the plugin configuration.

Plugin Configuration

From this page you can choose to enable the headers written to the server configuration files (.htaccess and web.config) and configure whether the following HTTP headers are enabled

Using the Force Header form you can also force the following headers with its values:

Strict-Transport-Security (HSTS) Configuration

From this page you can choose to enable the Strict-Transport-Security (HSTS) header as well as configure the max-age value, whether subdomains should be included and whether you want to be added to the browsers Preload List.


Content-Security-Policy (CSP) Configuration

From this page you can choose to enable and configure the Content-Security-Policy (CSP) header set by the plugin.

Once enabled you can set the client where you want to enforce the configured CSP, allowing you to specify site, administrator or both.

The following settings need a bit more explanation, that's also the reason there are additional descriptions on all of them.

The final option called Add Directive allows you to configure the allowlist per directive as you need them. For example the directive script-src where the option Value you enter the origins you want to allow to load scripts from.

Notes

When you have configured some HTTP Security Headers directly on the server, our tooling might create double entries.

Check the output of your HTTP Headers after configuring in the browser console. In Google Chrome: Inspect > Network > the output under Headers. You can than disable the headers that cause double entries. Also check the console of your browser for possible errors.

Extension Developers

As you might know the big security advantage concerning Content Security Policy jumps in when we can use the Header to block all inline JavaScript and inline CSS affecting for example JavaScript event handlers via HTML attributes. With this browser protection enabled we will block inline JavaScript and inline CSS usage also for your extensions. That protection is not enabled by default but can be enabled by your users.

For 4.0 it is recommended to get the frontend of your extension running with strict Content Security Policy enabled. For 4.1 compatibility it is recommended that this also applies to your backend.

It is still a requirement to have inline JavaScript and CSS. For that reason we have implemented nonce and hash support into our Document APIs. When you use them the core will make sure they are whitelisted but we will still block anything malicious to protect our sites.

Important Notes for Extension Developers

Starting with Joomla 4.0, Content Security Policy:

  • is shipped by the core
  • is disabled by default
  • can be enabled by your users
  • it is strongly recommended that your extension frontend works with 4.0 with Content Security Policy enabled
  • it is recommended that your extension backend works with 4.1 with Content Security Policy enabled

With strict Content Security Policy enabled the following features will be blocked:

  • the execution of JavaScript via the HTML event handlers (onXXX handlers like onClick and similar)
  • the execution of in-page JavaScript not passed to the page via the Document API
  • the execution of JavaScript code injected into DOM APIs such as eval()
  • the usage of inline in-page CSS not passed to the page via the Document API
  • the usage of inline CSS using the HTML style attribute

To get your extensions to work even with strict Content Security Policy enabled, use the Document API to apply your inline JavaScript and CSS. Check the examples below.

Adding JavaScript Using the Joomla API

use Joomla\CMS\Factory;

/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();

// Add JavaScript from URL
$wa->registerAndUseScript('com_example.sample', 'https://example.org/sample.js', [], ['defer' => true]);

// Add inline JavaScript
$wa->addInlineScript('
    document.addEventListener("DOMContentLoaded", function(event) {
        alert("An inline JavaScript Declaration");
    });
');

Adding CSS Using the Joomla API

use Joomla\CMS\Factory;

/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();

// Add Style from URL
$wa->registerAndUseStyle('com_example.sample', 'https://example.org/sample.css');

// Add inline Style
$wa->addInlineStyle('
	body {
		background: #00ff00;
		color: rgb(0,0,255);
	}
');

More details can be found here: Adding JavaScript and CSS to the page

Additional Resources about Content Security Policy and HTTP Headers