diff --git a/public/app/core/utils/text.ts b/public/app/core/utils/text.ts index faf801b45be..d769f5dc57b 100644 --- a/public/app/core/utils/text.ts +++ b/public/app/core/utils/text.ts @@ -101,3 +101,11 @@ export function sanitize(unsanitizedString: string): string { export function hasAnsiCodes(input: string): boolean { return /\u001b\[\d{1,2}m/.test(input); } + +export function escapeHtml(str: string): string { + return String(str) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); +} diff --git a/public/app/features/panel/panel_ctrl.ts b/public/app/features/panel/panel_ctrl.ts index e81312aa0de..ccac99ae66d 100644 --- a/public/app/features/panel/panel_ctrl.ts +++ b/public/app/features/panel/panel_ctrl.ts @@ -1,5 +1,6 @@ import _ from 'lodash'; import Remarkable from 'remarkable'; +import { sanitize, escapeHtml } from 'app/core/utils/text'; import config from 'app/core/config'; import { profiler } from 'app/core/core'; @@ -250,31 +251,32 @@ export class PanelCtrl { markdown = this.error || this.panel.description; } - const linkSrv = this.$injector.get('linkSrv'); - const sanitize = this.$injector.get('$sanitize'); - const templateSrv = this.$injector.get('templateSrv'); + const linkSrv: any = this.$injector.get('linkSrv'); + const templateSrv: any = this.$injector.get('templateSrv'); const interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars); let html = '
'; - html += new Remarkable().render(interpolatedMarkdown); + const md = new Remarkable().render(interpolatedMarkdown); + html += config.disableSanitizeHtml ? md : sanitize(md); if (this.panel.links && this.panel.links.length > 0) { html += ''; } html += '
'; - return sanitize(html); + return html; } } diff --git a/public/app/plugins/panel/text/module.ts b/public/app/plugins/panel/text/module.ts index ad60fe8aa6a..a1fe0de372c 100644 --- a/public/app/plugins/panel/text/module.ts +++ b/public/app/plugins/panel/text/module.ts @@ -1,7 +1,7 @@ import _ from 'lodash'; import { PanelCtrl } from 'app/plugins/sdk'; import Remarkable from 'remarkable'; -import { sanitize } from 'app/core/utils/text'; +import { sanitize, escapeHtml } from 'app/core/utils/text'; import config from 'app/core/config'; const defaultContent = ` @@ -70,12 +70,8 @@ export class TextPanelCtrl extends PanelCtrl { } renderText(content: string) { - content = content - .replace(/&/g, '&') - .replace(/>/g, '>') - .replace(/'); - this.updateContent(content); + const safeContent = escapeHtml(content).replace(/\n/g, '
'); + this.updateContent(safeContent); } renderMarkdown(content: string) {