From 559b315d0d997dba0ef14457e0c9de867a586a15 Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Sun, 1 Oct 2017 10:30:41 -0600 Subject: [PATCH 1/5] Initial test for Markdown ` + const el = render() + expect(el.html()).toEqual(`

script

\n
`) + }) + }) + + describe("OAS 3", function() { + it("sanitizes ` + const el = render() + expect(el.html()).toEqual(`

script

`) + }) + }) +}) From 5a69603beb454a0d15002bfbd82902d9480e27b1 Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Sun, 1 Oct 2017 13:59:28 -0600 Subject: [PATCH 2/5] Test for sanitizing elements. Test sanitization of the component --- test/xss/info-sanitization.js | 33 ++++++++++++++++++++++++ test/xss/markdown-script-sanitization.js | 12 +++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/xss/info-sanitization.js diff --git a/test/xss/info-sanitization.js b/test/xss/info-sanitization.js new file mode 100644 index 00000000..6549aa11 --- /dev/null +++ b/test/xss/info-sanitization.js @@ -0,0 +1,33 @@ +/* eslint-env mocha */ +import React from "react" +import expect from "expect" +import { render } from "enzyme" +import { fromJS } from "immutable" +import Info from "components/info" +import Markdown from "components/providers/markdown" + +describe.only(" Sanitization", function(){ + const dummyComponent = () => null + const components = { + Markdown + } + const props = { + getComponent: c => components[c] || dummyComponent, + info: fromJS({ + title: "Test Title **strong** ", + description: "Description *with* " + }), + host: "example.test", + basePath: "/api" + } + + it("renders sanitized .title content", function(){ + let wrapper = render() + expect(wrapper.find(".title").html()).toEqual("Test Title **strong** <script>alert(1)</script>") + }) + + it("renders sanitized .description content", function() { + let wrapper = render() + expect(wrapper.find(".description").html()).toEqual("

Description with

\n
") + }) +}) diff --git a/test/xss/markdown-script-sanitization.js b/test/xss/markdown-script-sanitization.js index 4a353316..ef374dd7 100644 --- a/test/xss/markdown-script-sanitization.js +++ b/test/xss/markdown-script-sanitization.js @@ -12,6 +12,12 @@ describe.only("Markdown Script Sanitization", function() { const el = render() expect(el.html()).toEqual(`

script

\n
`) }) + + it("sanitizes elements", function() { + const str = `` + const el = render() + expect(el.html()).toEqual(`

\n
`) + }) }) describe("OAS 3", function() { @@ -20,5 +26,11 @@ describe.only("Markdown Script Sanitization", function() { const el = render() expect(el.html()).toEqual(`

script

`) }) + + it("sanitizes elements", function() { + const str = `` + const el = render() + expect(el.html()).toEqual(`
`) + }) }) }) From 729fd71546a156ebb66346f63078d9771c1bda8d Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Sun, 8 Oct 2017 09:09:29 -0600 Subject: [PATCH 3/5] Fixes #3734 Add

and

elements to sanitizer options. --- src/core/components/providers/markdown.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/components/providers/markdown.jsx b/src/core/components/providers/markdown.jsx index 2b21c10b..ef95c6ae 100644 --- a/src/core/components/providers/markdown.jsx +++ b/src/core/components/providers/markdown.jsx @@ -29,7 +29,7 @@ Markdown.propTypes = { export default Markdown const sanitizeOptions = { - allowedTags: sanitize.defaults.allowedTags.concat([ "img" ]), + allowedTags: sanitize.defaults.allowedTags.concat([ "h1", "h2", "img" ]), textFilter: function(text) { return text.replace(/"/g, "\"") } From 1785d48746274f7e6d9727b2edb7ae0920caaab0 Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Sun, 8 Oct 2017 10:13:09 -0600 Subject: [PATCH 4/5] Remove .only from existing tests. Add markdown test cases for heading elements. --- test/components/markdown.js | 38 ++++++++++++++++++++++++ test/xss/info-sanitization.js | 2 +- test/xss/markdown-script-sanitization.js | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/components/markdown.js diff --git a/test/components/markdown.js b/test/components/markdown.js new file mode 100644 index 00000000..6dd87053 --- /dev/null +++ b/test/components/markdown.js @@ -0,0 +1,38 @@ +/* eslint-env mocha */ +import React from "react" +import expect from "expect" +import { render } from "enzyme" +import Markdown from "components/providers/markdown" +import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.js" + +describe.only("Markdown component", function() { + describe("Swagger 2.0", function() { + it("allows heading elements", function() { + const str = ` +# h1 +## h2 +### h3 +#### h4 +##### h5 +###### h6 + ` + const el = render() + expect(el.html()).toEqual(`

h1

\n

h2

\n

h3

\n

h4

\n
h5
\n
h6
\n
`) + }) + }) + + describe("OAS 3", function() { + it("allows heading elements", function() { + const str = ` + # h1 + ## h2 + ### h3 + #### h4 + ##### h5 + ###### h6 + ` + const el = render() + expect(el.html()).toEqual(`

h1

\n

h2

\n

h3

\n

h4

\n
h5
\n
h6
`) + }) + }) +}) diff --git a/test/xss/info-sanitization.js b/test/xss/info-sanitization.js index 6549aa11..e868fe9f 100644 --- a/test/xss/info-sanitization.js +++ b/test/xss/info-sanitization.js @@ -6,7 +6,7 @@ import { fromJS } from "immutable" import Info from "components/info" import Markdown from "components/providers/markdown" -describe.only(" Sanitization", function(){ +describe(" Sanitization", function(){ const dummyComponent = () => null const components = { Markdown diff --git a/test/xss/markdown-script-sanitization.js b/test/xss/markdown-script-sanitization.js index ef374dd7..9d6624c7 100644 --- a/test/xss/markdown-script-sanitization.js +++ b/test/xss/markdown-script-sanitization.js @@ -5,7 +5,7 @@ import { render } from "enzyme" import Markdown from "components/providers/markdown" import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.js" -describe.only("Markdown Script Sanitization", function() { +describe("Markdown Script Sanitization", function() { describe("Swagger 2.0", function() { it("sanitizes ` From 60e8091eedde05244a1d8c498b5f85f7390ed9af Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Sun, 8 Oct 2017 10:26:32 -0600 Subject: [PATCH 5/5] Add unit test for images in markdown --- src/core/components/providers/markdown.jsx | 3 ++ test/components/markdown.js | 56 +++++++++++++--------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/core/components/providers/markdown.jsx b/src/core/components/providers/markdown.jsx index ef95c6ae..2ef8b6a6 100644 --- a/src/core/components/providers/markdown.jsx +++ b/src/core/components/providers/markdown.jsx @@ -30,6 +30,9 @@ export default Markdown const sanitizeOptions = { allowedTags: sanitize.defaults.allowedTags.concat([ "h1", "h2", "img" ]), + allowedAttributes: { + "img": sanitize.defaults.allowedAttributes.img.concat(["title"]) + }, textFilter: function(text) { return text.replace(/"/g, "\"") } diff --git a/test/components/markdown.js b/test/components/markdown.js index 6dd87053..01a55e1c 100644 --- a/test/components/markdown.js +++ b/test/components/markdown.js @@ -5,34 +5,44 @@ import { render } from "enzyme" import Markdown from "components/providers/markdown" import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.js" -describe.only("Markdown component", function() { - describe("Swagger 2.0", function() { - it("allows heading elements", function() { - const str = ` +describe("Markdown component", function() { + describe("Swagger 2.0", function() { + it("allows image elements", function() { + const str = `![Image alt text](http://image.source "Image title")` + const el = render() + expect(el.html()).toEqual(`

\n
`) + }) + + it("allows heading elements", function() { + const str = ` # h1 ## h2 ### h3 #### h4 ##### h5 -###### h6 - ` - const el = render() - expect(el.html()).toEqual(`

h1

\n

h2

\n

h3

\n

h4

\n
h5
\n
h6
\n
`) +###### h6` + const el = render() + expect(el.html()).toEqual(`

h1

\n

h2

\n

h3

\n

h4

\n
h5
\n
h6
\n
`) + }) }) - }) - describe("OAS 3", function() { - it("allows heading elements", function() { - const str = ` - # h1 - ## h2 - ### h3 - #### h4 - ##### h5 - ###### h6 - ` - const el = render() - expect(el.html()).toEqual(`

h1

\n

h2

\n

h3

\n

h4

\n
h5
\n
h6
`) - }) - }) + describe("OAS 3", function() { + it("allows image elements", function() { + const str = `![Image alt text](http://image.source "Image title")` + const el = render() + expect(el.html()).toEqual(`

`) + }) + + it("allows heading elements", function() { + const str = ` +# h1 +## h2 +### h3 +#### h4 +##### h5 +###### h6` + const el = render() + expect(el.html()).toEqual(`

h1

\n

h2

\n

h3

\n

h4

\n
h5
\n
h6
`) + }) + }) })