diff --git a/lib/markdown_renderer.rb b/lib/markdown_renderer.rb index 97c49685..042b3a2c 100644 --- a/lib/markdown_renderer.rb +++ b/lib/markdown_renderer.rb @@ -20,6 +20,7 @@ def header(text, header_level) end def image(url, title, alt_text) + url, title, alt_text = [ url, title, alt_text ].map { ERB::Util.html_escape_once(it) } %(#{alt_text}) end diff --git a/test/lib/markdown_renderer_test.rb b/test/lib/markdown_renderer_test.rb index c3ad2119..ca5315cc 100644 --- a/test/lib/markdown_renderer_test.rb +++ b/test/lib/markdown_renderer_test.rb @@ -8,4 +8,14 @@ class MarkdownRendererTest < ActiveSupport::TestCase assert_includes content, "id='duplicated-header'" assert_includes content, "id='duplicated-header-2'" end + + test "it escapes image attributes" do + markdown = MarkdownRenderer.build + content = markdown.render(%(![a "b" & c](/image.png?a=1&b=2 "x" data-x="y"))) + + assert_includes content, %(title="x" data-x="y") + assert_includes content, %(alt="a "b" & c") + assert_includes content, %(href="/image.png?a=1&b=2") + assert_empty Nokogiri::HTML5.fragment(content).css("[data-x]") + end end