Skip to content

Commit 182fb83

Browse files
fix: use scoped DOMPurify instance to avoid mutating global (#11029)
1 parent d715d6c commit 182fb83

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

‎src/core/components/providers/markdown.jsx‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import { linkify } from "remarkable/linkify"
55
import DomPurify from "dompurify"
66
import cx from "classnames"
77

8-
if (DomPurify.addHook) {
9-
DomPurify.addHook("beforeSanitizeElements", function (current, ) {
10-
// Attach safe `rel` values to all elements that contain an `href`,
11-
// i.e. all anchors that are links.
12-
// We _could_ just look for elements that have a non-self target,
13-
// but applying it more broadly shouldn't hurt anything, and is safer.
8+
const scopedDomPurify = typeof window !== "undefined" ? DomPurify(window) : DomPurify
9+
10+
if (scopedDomPurify.addHook) {
11+
scopedDomPurify.addHook("beforeSanitizeElements", function (current) {
1412
if (current.href) {
1513
current.setAttribute("rel", "noopener noreferrer")
1614
}
@@ -62,7 +60,7 @@ export function sanitizer(str, { useUnsafeMarkdown = false } = {}) {
6260
sanitizer.hasWarnedAboutDeprecation = true
6361
}
6462

65-
return DomPurify.sanitize(str, {
63+
return scopedDomPurify.sanitize(str, {
6664
ADD_ATTR: ["target"],
6765
FORBID_TAGS: ["style", "form"],
6866
ALLOW_DATA_ATTR,

‎webpack/dev.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const devConfig = configBuilder(
6767
module: {
6868
rules: [
6969
{
70-
test: /\.jsx?$/,
70+
test: /\.[jt]sx?$/,
7171
include: [
7272
path.join(projectBasePath, "src"),
7373
path.join(projectBasePath, "node_modules", "object-assign-deep"),

0 commit comments

Comments
 (0)