Skip to content

Commit f80e079

Browse files
author
bkraul
committed
Added measures to work with CSP
1 parent 71a2e28 commit f80e079

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

BBCodePlus/BBCodePlus.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BBCodePlusPlugin extends MantisFormattingPlugin {
1212
private $t_MantisCoreFormatting_process_markdown = OFF;
1313
private $t_bbCode = null;
1414
private $t_HTML = null;
15+
private $t_nonceToken = null;
1516
//-------------------------------------------------------------------
1617
/**
1718
* A method that populates the plugin information and minimum requirements.
@@ -22,7 +23,7 @@ function register() {
2223
$this->name = plugin_lang_get( 'title' );
2324
$this->description = plugin_lang_get( 'description' );
2425
$this->page = 'config';
25-
$this->version = '2.1.17';
26+
$this->version = '2.1.18';
2627

2728
$this->requires['MantisCore'] = '2.0.0';
2829
# this plugin can coexist with MantisCoreFormatting.
@@ -74,6 +75,8 @@ function init() {
7475
$this->t_MantisCoreFormatting_process_markdown = OFF;
7576
}
7677
}
78+
# create the random nonce token for allowing unsafe-eval on csp
79+
$this->t_nonceToken = base64_encode(substr(md5(mt_rand()), 0, 12));
7780
}
7881
//-------------------------------------------------------------------
7982
/**
@@ -121,6 +124,8 @@ function csp_headers() {
121124
if ( (ON == plugin_config_get( 'process_markitup' )) && function_exists( 'http_csp_add' ) ) {
122125
http_csp_add( 'img-src', "*" );
123126
http_csp_add( 'frame-ancestors', "'self'" );
127+
http_csp_add( 'script', "'self'" );
128+
http_csp_add( 'script-src', "'nonce-$this->t_nonceToken'");
124129
}
125130
}
126131
//-------------------------------------------------------------------
@@ -132,23 +137,23 @@ function csp_headers() {
132137
function resources( $p_event ) {
133138
# includes.
134139
$resources = '<link rel="stylesheet" type="text/css" href="' . plugin_file( 'bbcodeplus.css' ) . '" />';
135-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'bbcodeplus-init.js' ) . '"></script>';
140+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'bbcodeplus-init.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
136141

137142
if ( ON == plugin_config_get( 'process_markitup' ) ) {
138143
$resources .= '<link rel="stylesheet" type="text/css" href="' . plugin_file( 'markitup/skins/' . plugin_config_get( 'markitup_skin' ) . '/style.css' ) . '" />';
139144
$resources .= '<link rel="stylesheet" type="text/css" href="' . plugin_file( 'markitup/sets/mantis/style.css' ) . '" />';
140-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup/jquery_markitup.js' ) . '"></script>';
141-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup/sets/mantis/set.js' ) . '"></script>';
142-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup-init.js' ) . '"></script>';
145+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup/jquery_markitup.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
146+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup/sets/mantis/set.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
147+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup-init.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
143148
}
144149

145150
if ( ON == plugin_config_get( 'process_highlight' ) ) {
146151
$resources .= '<link rel="stylesheet" type="text/css" href="' . plugin_file( 'prism/styles/' . plugin_config_get( 'highlight_css' ) . '.css' ) . '" />';
147-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism.js' ) . '"></script>';
152+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
148153

149154
# load additional languages.
150155
if ( ON == plugin_config_get( 'highlight_extralangs' ) ) {
151-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism_additional_languages.js' ) . '"></script>';
156+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism_additional_languages.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
152157
}
153158
}
154159

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ If you would like to contribute to BBCode plus, please [read this guide first](h
2626

2727
## Change Log
2828

29+
### 2.1.18
30+
31+
- Added `nonce` random token and directives for included js scripts in order to hopefully address CSP restrictions.
32+
2933
### 2.1.17
3034

3135
- Fixed styling and scripting issues with issue image picker.

0 commit comments

Comments
 (0)