Skip to content

Commit 462bdcd

Browse files
mssskdylans
authored andcommitted
Merge pull request from GHSA-cxjc-r2fp-7mq6
* Add config option `allowUnsafeHtml`: default is `false` which results in `<` being replaced with `&lt;` * Add config option `linkFilter`: can be a function or array of filter pairs to control exactly what filtering is applied This update should minimally affect production applications: * The behavior of existing links with HTML content will be unchanged * Existing links that are edited and saved will be filtered (this is only if the link is edited, other content within the editor can be edited without affecting the link) * Newly created links will be filtered by default * For production code to continue working as-is with new data the application code will have to be updated to specify `true` for the `LinkDialog` plugin's `allowUnsafeHtml` option (cherry picked from commit 7d9d492)
1 parent d7294be commit 462bdcd

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

_editor/plugins/LinkDialog.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
define([
22
"require",
3+
"dojo/_base/array",
34
"dojo/_base/declare", // declare
45
"dojo/dom-attr", // domAttr.get
56
"dojo/keys", // keys.ENTER
@@ -11,7 +12,7 @@ define([
1112
"../_Plugin",
1213
"../../form/DropDownButton",
1314
"../range"
14-
], function(require, declare, domAttr, keys, lang, on, has, query, string,
15+
], function(require, array, declare, domAttr, keys, lang, on, has, query, string,
1516
_Plugin, DropDownButton, rangeapi){
1617

1718
// module:
@@ -26,6 +27,21 @@ define([
2627
//
2728
// - createLink
2829

30+
// allowUnsafeHtml: boolean
31+
// If false (default), the link description will be filtered to prevent HTML content.
32+
// If true no filtering is done, allowing for HTML content within the link element.
33+
// The filter can be specified with the 'linkFilter' option.
34+
allowUnsafeHtml: false,
35+
36+
// linkFilter: function or array of replacement pairs
37+
// If 'allowUnsafeHtml' is false then this filter will be applied to the link Description value.
38+
// function: the function will be invoked with the string value of the Description field and its
39+
// return value will be used
40+
// array: each array item should be an array of two values to pass to String#replace
41+
linkFilter: [
42+
[/</g, "&lt;"]
43+
],
44+
2945
// Override _Plugin.buttonClass. This plugin is controlled by a DropDownButton
3046
// (which triggers a TooltipDialog).
3147
buttonClass: DropDownButton,
@@ -252,6 +268,16 @@ define([
252268
if(args && args.urlInput){
253269
args.urlInput = args.urlInput.replace(/"/g, "&quot;");
254270
}
271+
if(!this.allowUnsafeHtml && args && args.textInput){
272+
if(typeof this.linkFilter === 'function'){
273+
args.textInput = this.linkFilter(args.textInput);
274+
}
275+
else{
276+
array.forEach(this.linkFilter, function (currentFilter) {
277+
args.textInput = args.textInput.replace(currentFilter[0], currentFilter[1]);
278+
});
279+
}
280+
}
255281
return args;
256282
},
257283

@@ -629,8 +655,15 @@ define([
629655
});
630656

631657
// Register these plugins
632-
_Plugin.registry["createLink"] = function(){
633-
return new LinkDialog({command: "createLink"});
658+
_Plugin.registry["createLink"] = function(args){
659+
var pluginOptions = {
660+
command: "createLink",
661+
allowUnsafeHtml: ("allowUnsafeHtml" in args) ? args.allowUnsafeHtml : false
662+
};
663+
if("linkFilter" in args){
664+
pluginOptions.linkFilter = args.linkFilter;
665+
}
666+
return new LinkDialog(pluginOptions);
634667
};
635668
_Plugin.registry["insertImage"] = function(){
636669
return new ImgLinkDialog({command: "insertImage"});

tests/editor/test_LinkDialog.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<script type="text/javascript" src="../boilerplate.js"></script>
88

99
<script type="text/javascript">
10+
function filterLink () {
11+
return 'Filtered Value';
12+
}
13+
1014
require([
1115
"dojo/parser",
1216
"dijit/Editor",
@@ -36,6 +40,22 @@
3640
</div>
3741
</div>
3842

43+
<p>Editor with <code>allowUnsafeHtml</code> set to <code>true</code></p>
44+
<div style="border: 1px dotted black;">
45+
<div id="editorUnsafe" data-dojo-type="dijit/Editor" data-dojo-props='"aria-label":"editor",extraPlugins:[{name: "createLink", allowUnsafeHtml: true}, "insertImage", "viewSource"]'>
46+
<p>This editor will allow unrestricted HTML in the Description field of links</p>
47+
<br>
48+
</div>
49+
</div>
50+
51+
<p>Editor with custom <code>linkFilter</code> function</p>
52+
<div style="border: 1px dotted black;">
53+
<div id="editorLinkFilter" data-dojo-type="dijit/Editor" data-dojo-props='"aria-label":"editor",extraPlugins:[{name: "createLink", linkFilter: filterLink}, "insertImage", "viewSource"]'>
54+
<p>Links created in this editor will always have a description of "Filtered Value", which is the value returned by the custom <code>linkFilter</code> function.</p>
55+
<br>
56+
</div>
57+
</div>
58+
3959
<p>RTL Editor:</p>
4060
<div style="border: 1px dotted black;">
4161
<div id="reditor" data-dojo-type="dijit/Editor" dir="rtl" data-dojo-props='"aria-label":"reditor",extraPlugins:["createLink", "insertImage"]'>

0 commit comments

Comments
 (0)