Skip to content

Commit

Permalink
add security level antiscript option, to let use rich html format but…
Browse files Browse the repository at this point in the history
… remove all script element.
  • Loading branch information
Toan committed Jun 14, 2020
1 parent 18d2a7f commit 308498f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/diagrams/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ export const getRows = s => {
return str.split('#br#');
};

const removeScript = txt => {
var rs = '';
var idx = 0;

while (idx >= 0) {
idx = txt.indexOf('<script');
if (idx >= 0) {
rs += txt.substr(0, idx);
txt = txt.substr(idx + 1);

idx = txt.indexOf('</script>');
if (idx >= 0) {
idx += 9;
txt = txt.substr(idx);
}
} else {
rs += txt;
idx = -1;
break;
}
}
return rs;
};

export const sanitizeText = (text, config) => {
let txt = text;
let htmlLabels = true;
Expand All @@ -14,12 +38,18 @@ export const sanitizeText = (text, config) => {
)
htmlLabels = false;

if (config.securityLevel !== 'loose' && htmlLabels) {
// eslint-disable-line
txt = breakToPlaceholder(txt);
txt = txt.replace(/</g, '&lt;').replace(/>/g, '&gt;');
txt = txt.replace(/=/g, '&equals;');
txt = placeholderToBreak(txt);
if (htmlLabels) {
var level = config.securityLevel;

if (level == 'antiscript') {
txt = removeScript(txt);
} else if (level !== 'loose') {
// eslint-disable-line
txt = breakToPlaceholder(txt);
txt = txt.replace(/</g, '&lt;').replace(/>/g, '&gt;');
txt = txt.replace(/=/g, '&equals;');
txt = placeholderToBreak(txt);
}
}

return txt;
Expand Down
1 change: 1 addition & 0 deletions src/mermaidAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const config = {
/**
* Sets the level of trust to be used on the parsed diagrams.
* * **strict**: (**default**) tags in text are encoded, click functionality is disabeled
* * **antiscript**: tags in text are allowed, (except script is removed), click functionality is enabled
* * **loose**: tags in text are allowed, click functionality is enabled
*/
securityLevel: 'strict',
Expand Down

0 comments on commit 308498f

Please sign in to comment.