-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sanitize): use same setup for dompurify as FE
- Loading branch information
Showing
4 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import DOMPurify from "isomorphic-dompurify" | ||
|
||
DOMPurify.setConfig({ | ||
ADD_TAGS: ["iframe", "#comment", "script"], | ||
ADD_ATTR: [ | ||
"allow", | ||
"allowfullscreen", | ||
"frameborder", | ||
"scrolling", | ||
"marginheight", | ||
"marginwidth", | ||
"target", | ||
"async", | ||
], | ||
// required in case <script> tag appears as the first line of the markdown | ||
FORCE_BODY: true, | ||
}) | ||
DOMPurify.addHook("uponSanitizeElement", (node, data) => { | ||
// Allow script tags if it has a src attribute | ||
// Script sources are handled by our CSP sanitiser | ||
if ( | ||
data.tagName === "script" && | ||
!(node.hasAttribute("src") && node.innerHTML === "") | ||
) { | ||
// Adapted from https://github.com/cure53/DOMPurify/blob/e0970d88053c1c564b6ccd633b4af7e7d9a10375/src/purify.js#L719-L736 | ||
DOMPurify.removed.push({ element: node }) | ||
try { | ||
node.parentNode.removeChild(node) | ||
} catch (e) { | ||
try { | ||
// eslint-disable-next-line no-param-reassign | ||
node.outerHTML = "" | ||
} catch (ex) { | ||
node.remove() | ||
} | ||
} | ||
} | ||
}) | ||
|
||
// NOTE: Doing a re-export so that clients always use the correct config | ||
export const sanitizer = DOMPurify | ||
export default sanitizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import DOMPurify from "isomorphic-dompurify" | ||
import yaml from "yaml" | ||
|
||
import { sanitizer } from "@services/utilServices/Sanitizer" | ||
|
||
// Note: `yaml.parse()` and `yaml.stringify()` should not be used anywhere | ||
// else in the codebase. | ||
export const sanitizedYamlParse = ( | ||
unparsedContent: string | ||
): Record<string, unknown> => yaml.parse(DOMPurify.sanitize(unparsedContent)) | ||
): Record<string, unknown> => yaml.parse(sanitizer.sanitize(unparsedContent)) | ||
|
||
export const sanitizedYamlStringify = (prestringifiedContent: object): string => | ||
DOMPurify.sanitize(yaml.stringify(prestringifiedContent)) | ||
sanitizer.sanitize(yaml.stringify(prestringifiedContent)) |