Skip to content

Commit

Permalink
Add EVENT_CORE_HEADERS event
Browse files Browse the repository at this point in the history
Called before core emits headers enabling plugins to emit their
own headers or call APIs that shape the value of headers emitted by
core like Content-Security-Policy.

Fixes #21263
  • Loading branch information
vboctor authored and dregad committed Aug 27, 2016
1 parent 9f35986 commit c13b325
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions core.php
Expand Up @@ -274,6 +274,7 @@ function __autoload( $p_class ) {

# Set HTTP response headers
require_api( 'http_api.php' );
event_signal( 'EVENT_CORE_HEADERS' );
http_all_headers();

# Push default language to speed calls to lang_get
Expand Down
1 change: 1 addition & 0 deletions core/events_inc.php
Expand Up @@ -33,6 +33,7 @@

# Events specific to the core system
'EVENT_CORE_READY' => EVENT_TYPE_EXECUTE,
'EVENT_CORE_HEADERS' => EVENT_TYPE_EXECUTE,

# MantisBT Layout Events
'EVENT_LAYOUT_RESOURCES' => EVENT_TYPE_OUTPUT,
Expand Down
13 changes: 13 additions & 0 deletions docbook/Developers_Guide/en-US/Events_Reference.xml
Expand Up @@ -83,6 +83,19 @@
</blockquote>
</blockquote>

<blockquote id="dev.eventref.system.coreheaders">
<title>EVENT_CORE_HEADERS (Execute)</title>

<blockquote>
<para>
This event is triggered by the MantisBT bootstrap process just before emitting the
headers. This enables plugins to emit their own headers or use API that enables
tweaking values of headers emitted by core. An example, of headers that can be
tweaked is Content-Security-Policy header which can be tweaked using http_csp_*() APIs.
</para>
</blockquote>
</blockquote>

<blockquote id="dev.eventref.system.coreready">
<title>EVENT_CORE_READY (Execute)</title>

Expand Down
15 changes: 10 additions & 5 deletions plugins/Gravatar/Gravatar.php
Expand Up @@ -104,16 +104,21 @@ function config() {
* Register event hooks for plugin.
*/
function hooks() {
if( config_get( 'show_avatar' ) !== OFF ) {
# Set CSP header
http_csp_add( 'img-src', self::getAvatarUrl() );
}

return array(
'EVENT_USER_AVATAR' => 'user_get_avatar',
'EVENT_CORE_HEADERS' => 'csp_headers',
);
}

/**
* Register gravatar url as an img-src for CSP header
*/
function csp_headers() {
if( config_get( 'show_avatar' ) !== OFF ) {
http_csp_add( 'img-src', self::getAvatarUrl() );
}
}

/**
* Return the user avatar image URL
* in this first implementation, only gravatar.com avatars are supported
Expand Down

0 comments on commit c13b325

Please sign in to comment.