Skip to content

Commit

Permalink
[Sanitize] sanitize content in Displayer and in Playground
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaireDagan committed Apr 14, 2022
1 parent efb92d9 commit 864ffc3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/draft-displayer.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { domElementHelper } from "@kisskissbankbank/kitten";
import classNames from "classnames";
import DOMPurify from 'dompurify'
import {
CompositeDecorator,
ContentState,
Expand Down Expand Up @@ -75,7 +76,8 @@ const DraftDisplayer = ({
configResponsiveImageHandler,
}) => {
if (!domElementHelper.canUseDom()) return renderRaw(text);
return isJSONContent(text) ? (
const clean = DOMPurify.sanitize(text)
return isJSONContent(clean) ? (
<EditorProvider configResponsiveImageHandler={configResponsiveImageHandler}>
<EditorStyle />
<section
Expand All @@ -85,7 +87,7 @@ const DraftDisplayer = ({
>
<Editor
onChange={() => null}
editorState={getinitialValue(text)}
editorState={getinitialValue(clean)}
blockStyleFn={styleBlock({ isDisabled, useRichTextStyle, compact })}
blockRenderMap={customBlockRenderMap}
readOnly
Expand All @@ -95,7 +97,7 @@ const DraftDisplayer = ({
) : (
<EditorProvider configResponsiveImageHandler={configResponsiveImageHandler}>
<HtmlEditor
html={text}
html={clean}
perfEnabled={perfEnabled}
useRichTextStyle={useRichTextStyle}
/>
Expand Down
8 changes: 5 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Modifier,
SelectionState,
} from "draft-js";
import DOMPurify from 'dompurify'
import { OrderedMap } from "immutable";
import flow from "lodash/fp/flow";
import get from "lodash/fp/get";
Expand Down Expand Up @@ -44,10 +45,11 @@ export const getEditorValue = (value) => {
return EditorState.createEmpty();
}
try {
const clean = DOMPurify.sanitize(value)
return EditorState.createWithContent(
isJSONContent(value)
? convertFromRaw(JSON.parse(value))
: convertFromRaw(getRawContent(value, true))
isJSONContent(clean)
? convertFromRaw(JSON.parse(clean))
: convertFromRaw(getRawContent(clean, true))
);
} catch (e) {
return EditorState.createEmpty();
Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"@kisskissbankbank/kitten": "^10.0.1",
"classnames": "^2.3.1",
"dompurify": "^2.3.6",
"draft-convert": "^2.1.12",
"draft-js": "^0.11.7",
"draft-js-export-html": "^1.4.1",
Expand Down

0 comments on commit 864ffc3

Please sign in to comment.