fix(Markdown): render markdown in more secure way

This commit changes markdown sanitization behaviour in following way:

class, style and data-* attributes are removed by default. These attributes
open possible vulnerability vectors to attackers.

The original behavior of sanitizer (before this commit) can be enabled by *useUnsafeMarkdown* configuration option.
Use this configuration option with caution and only in cases when you know
what you're doing.
This commit is contained in:
Vladimir Gorej
2020-06-11 14:54:40 +02:00
parent 48a0b46942
commit a616cb471d
22 changed files with 83 additions and 33 deletions

View File

@@ -5,7 +5,7 @@ import { fromJS } from "immutable"
const Callbacks = (props) => {
let { callbacks, getComponent, specPath } = props
// const Markdown = getComponent("Markdown")
// const Markdown = getComponent("Markdown", true)
const OperationContainer = getComponent("OperationContainer", true)
if(!callbacks) {

View File

@@ -51,7 +51,7 @@ export default class HttpAuth extends React.Component {
const Row = getComponent("Row")
const Col = getComponent("Col")
const AuthError = getComponent("authError")
const Markdown = getComponent( "Markdown" )
const Markdown = getComponent("Markdown", true)
const JumpToPath = getComponent("JumpToPath", true)
const scheme = (schema.get("scheme") || "").toLowerCase()

View File

@@ -6,7 +6,7 @@ class OperationLink extends Component {
render() {
const { link, name, getComponent } = this.props
const Markdown = getComponent("Markdown")
const Markdown = getComponent("Markdown", true)
let targetOp = link.get("operationId") || link.get("operationRef")
let parameters = link.get("parameters") && link.get("parameters").toJS()

View File

@@ -54,7 +54,7 @@ const RequestBody = ({
onChange(e.target.files[0])
}
const Markdown = getComponent("Markdown")
const Markdown = getComponent("Markdown", true)
const ModelExample = getComponent("modelExample")
const RequestBodyEditor = getComponent("RequestBodyEditor")
const HighlightCode = getComponent("highlightCode")

View File

@@ -9,14 +9,15 @@ const parser = new Remarkable("commonmark")
parser.block.ruler.enable(["table"])
parser.set({ linkTarget: "_blank" })
export const Markdown = ({ source, className = "" }) => {
export const Markdown = ({ source, className = "", getConfigs }) => {
if(typeof source !== "string") {
return null
}
if ( source ) {
const { useUnsafeMarkdown } = getConfigs()
const html = parser.render(source)
const sanitized = sanitizer(html)
const sanitized = sanitizer(html, { useUnsafeMarkdown })
let trimmed
@@ -38,6 +39,11 @@ export const Markdown = ({ source, className = "" }) => {
Markdown.propTypes = {
source: PropTypes.string,
className: PropTypes.string,
getConfigs: PropTypes.func,
}
Markdown.defaultProps = {
getConfigs: () => ({ useUnsafeMarkdown: false }),
}
export default OAS3ComponentWrapFactory(Markdown)