Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/docx/containers/text_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def to_s

# Return text as a HTML fragment with formatting based on properties.
def to_html
html = @text
html = CGI.escapeHTML(@text.to_s)
html = html_tag(:em, content: html) if italicized?
html = html_tag(:strong, content: html) if bolded?
html = html_tag(:s, content: html) if striked?
Expand Down
3 changes: 2 additions & 1 deletion lib/docx/elements/element.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'cgi'
require 'nokogiri'
require 'docx/elements'
require 'docx/containers'
Expand Down Expand Up @@ -87,7 +88,7 @@ def html_tag(name, options = {})

unless attributes.nil? || attributes.empty?
attributes.each do |attr_name, attr_value|
html << " #{attr_name}=\"#{attr_value}\""
html << " #{attr_name}=\"#{CGI.escapeHTML(attr_value.to_s)}\""
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@
it 'handles internal (anchor) links' do
expect { described_class.open(fixture_path('internal-links.docx')).to_html }.not_to raise_error
end

it 'escapes the document text' do
doc = described_class.open(fixture_path('basic.docx'))
doc.paragraphs.first.text = 'a <img src=x onerror=alert(1)> & c'

expect(doc.paragraphs.first.to_html).to include('a &lt;img src=x onerror=alert(1)&gt; &amp; c')
end
end

describe '#to_xml' do
Expand Down
Loading