Skip to content

Commit 38e5eee

Browse files
authored
Fix markdown escaping wrongly passing html through (#28363)
* Fix markdown escaping wrongly passing html through Signed-off-by: Michael Telatynski <[email protected]> * Add comment Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 1ccbdb2 commit 38e5eee

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Markdown.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ export default class Markdown {
383383
if (isMultiLine(node) && node.next) this.lit("\n\n");
384384
};
385385

386-
return renderer.render(this.parsed);
386+
// We inhibit the default escape function as we escape the entire output string to correctly handle backslashes
387+
renderer.esc = (input: string) => input;
388+
389+
return escape(renderer.render(this.parsed));
387390
}
388391
}

test/unit-tests/editor/serialize-test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Please see LICENSE files in the repository root for full details.
99
import { mocked } from "jest-mock";
1010

1111
import EditorModel from "../../../src/editor/model";
12-
import { htmlSerializeIfNeeded } from "../../../src/editor/serialize";
12+
import { htmlSerializeFromMdIfNeeded, htmlSerializeIfNeeded } from "../../../src/editor/serialize";
1313
import { createPartCreator } from "./mock";
1414
import { IConfigOptions } from "../../../src/IConfigOptions";
1515
import SettingsStore from "../../../src/settings/SettingsStore";
@@ -71,6 +71,12 @@ describe("editor/serialize", function () {
7171
const html = htmlSerializeIfNeeded(model, {});
7272
expect(html).toBe("*hello* world");
7373
});
74+
it("escaped markdown should not retain backslashes around other markdown", function () {
75+
const pc = createPartCreator();
76+
const model = new EditorModel([pc.plain("\\*hello\\* **world**")], pc);
77+
const html = htmlSerializeIfNeeded(model, {});
78+
expect(html).toBe("*hello* <strong>world</strong>");
79+
});
7480
it("escaped markdown should convert HTML entities", function () {
7581
const pc = createPartCreator();
7682
const model = new EditorModel([pc.plain("\\*hello\\* world < hey world!")], pc);
@@ -153,6 +159,14 @@ describe("editor/serialize", function () {
153159
const html = htmlSerializeIfNeeded(model, { forceHTML: true, useMarkdown: false });
154160
expect(html).toBe("hello world");
155161
});
162+
it("should treat tags not in allowlist as plaintext", () => {
163+
const html = htmlSerializeFromMdIfNeeded("<b>test</b>", {});
164+
expect(html).toBeUndefined();
165+
});
166+
it("should treat tags not in allowlist as plaintext even if escaped", () => {
167+
const html = htmlSerializeFromMdIfNeeded("\\<b>test</b>", {});
168+
expect(html).toBe("&lt;b&gt;test&lt;/b&gt;");
169+
});
156170
});
157171

158172
describe("feature_latex_maths", () => {

0 commit comments

Comments
 (0)