Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape filename to avoid XSS from malicious input #117

Merged
merged 1 commit into from
Mar 23, 2020
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/inline_svg/action_view/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def read_svg(filename)

def placeholder(filename)
css_class = InlineSvg.configuration.svg_not_found_css_class
not_found_message = "'#{filename}' #{extension_hint(filename)}"
not_found_message = "'#{ERB::Util.html_escape_once(filename)}' #{extension_hint(filename)}"

if css_class.nil?
return "<svg><!-- SVG file not found: #{not_found_message}--></svg>".html_safe
Expand Down
11 changes: 11 additions & 0 deletions spec/helpers/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def transform(doc)
expect(output).to be_html_safe
end

it "escapes malicious input" do
malicious = "--></svg><script>alert(1)</script><svg>.svg"
allow(InlineSvg::AssetFile).to receive(:named).
with(malicious).
and_raise(InlineSvg::AssetFile::FileNotFound.new)

output = helper.send(helper_method, malicious)
expect(output).to eq "<svg><!-- SVG file not found: '#{ERB::Util.html_escape_once(malicious)}' --></svg>"
expect(output).to be_html_safe
end

it "gives a helpful hint when no .svg extension is provided in the filename" do
allow(InlineSvg::AssetFile).to receive(:named).
with('missing-file-with-no-extension').
Expand Down