Skip to content

Commit 3c761e4

Browse files
authored
Closes #99
Added measures to work with CSP
2 parents a98f878 + 1d7c08f commit 3c761e4

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

BBCodePlus/BBCodePlus.php

+11-7
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,7 @@ 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-src', "'nonce-$this->t_nonceToken'");
124128
}
125129
}
126130
//-------------------------------------------------------------------
@@ -132,23 +136,23 @@ function csp_headers() {
132136
function resources( $p_event ) {
133137
# includes.
134138
$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>';
139+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'bbcodeplus-init.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
136140

137141
if ( ON == plugin_config_get( 'process_markitup' ) ) {
138142
$resources .= '<link rel="stylesheet" type="text/css" href="' . plugin_file( 'markitup/skins/' . plugin_config_get( 'markitup_skin' ) . '/style.css' ) . '" />';
139143
$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>';
144+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup/jquery_markitup.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
145+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup/sets/mantis/set.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
146+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'markitup-init.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
143147
}
144148

145149
if ( ON == plugin_config_get( 'process_highlight' ) ) {
146150
$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>';
151+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
148152

149153
# load additional languages.
150154
if ( ON == plugin_config_get( 'highlight_extralangs' ) ) {
151-
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism_additional_languages.js' ) . '"></script>';
155+
$resources .= '<script type="text/javascript" src="' . plugin_file( 'prism/prism_additional_languages.js' ) . '" nonce="' . $this->t_nonceToken . '"></script>';
152156
}
153157
}
154158

BBCodePlus/files/markitup/jquery_markitup.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@
220220
}).bind("focusin.markItUp", function(){
221221
$$.focus();
222222
}).bind('mouseup', function(e) {
223-
if (button.call) {
224-
eval(button.call)(e); // Pass the mouseup event to custom delegate
225-
}
223+
if (button.call == 'preview') { preview(); }
226224
setTimeout(function() { markup(button) },1);
227225
return false;
228226
}).bind('mouseenter.markItUp', function() {

BBCodePlus/files/markitup/sets/mantis/set.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ mySettings = {
199199
list.attr("class", "bbcodeplus image-picker");
200200

201201
$(".bug-attachment-preview-image a img").each(function(index, value) {
202-
var imgUrl = this.src;
203-
202+
var imgUrl = $(this).parent().prop('href');
204203
var img = $("<li><a href=\"#\"><img src=\"" + imgUrl + "\"></a></li>");
205204
var link = img.children('a');
206205
link.click(function() {

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ 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+
- Corrected issue with referencing issue images (removed volatile token, now using only file id and type).
33+
2934
### 2.1.17
3035

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

0 commit comments

Comments
 (0)