From cc743f5e2e1cea93ce54393a02ab53087164a63d Mon Sep 17 00:00:00 2001
From: Max Strasinsky <98811342+mstrasinskis@users.noreply.github.com>
Date: Thu, 5 Oct 2023 17:08:59 +0200
Subject: [PATCH] Escape raw html tag in markdown by default (#3399)
# Motivation
Escape raw html tags in markdown content to increase the security.
> Disadvantage is that the SVGs inside the markdown code block will be
also escaped and rendered with `<` instead of `<`.
# Changes
- add optional `escapeRawHtmlTags` to `markdownToHTML` function
# Tests
- htmlRenderer
# Todos
- [x] Add entry to changelog (if necessary).
# Screenshots
There is a raw html in the [provided
sample](https://dashboard.internetcomputer.org/proposal/104084):
## before
## after
| NNS-DAPP | Dashboard |
|--------|--------|
|
|
|
---
CHANGELOG-Nns-Dapp.md | 1 +
frontend/src/lib/utils/html.utils.ts | 52 +++++++++++++++++--
.../src/tests/lib/utils/html.utils.spec.ts | 34 ++++++++++++
3 files changed, 82 insertions(+), 5 deletions(-)
diff --git a/CHANGELOG-Nns-Dapp.md b/CHANGELOG-Nns-Dapp.md
index 66b2a74a2da..1cf248d2bc9 100644
--- a/CHANGELOG-Nns-Dapp.md
+++ b/CHANGELOG-Nns-Dapp.md
@@ -27,6 +27,7 @@ The NNS Dapp is released through proposals in the Network Nervous System. Theref
* Review the chunking strategy to enhance the dapp's loading time and prevent random, rare flashes of unstyled content (FOUC).
* New header UI in the canister detail page.
* New labels for min and max participation.
+* Improve security by escaping additional images in the proposal summary markdown.
* Internal change: remove unused snsQueryStore.
#### Deprecated
diff --git a/frontend/src/lib/utils/html.utils.ts b/frontend/src/lib/utils/html.utils.ts
index c031b498b31..7cea06b9734 100644
--- a/frontend/src/lib/utils/html.utils.ts
+++ b/frontend/src/lib/utils/html.utils.ts
@@ -1,3 +1,4 @@
+import { isNullish } from "@dfinity/utils";
import type { marked as markedTypes, Renderer } from "marked";
type Marked = typeof markedTypes;
@@ -42,24 +43,65 @@ export const imageToLinkRenderer = (
}${titleProp ?? ""}>${text}`;
};
-export const renderer = (marked: Marked): Renderer => {
+const escapeHtml = (html: string): string =>
+ html.replace(//g, ">");
+const escapeSvgs = (html: string): string =>
+ html.replace(/