Skip to content

Commit

Permalink
sanitize iframe, srcdoc, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
antopalidi committed Dec 20, 2023
1 parent af40e0c commit bfdc997
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ def iframe

def sanitize(html)
sanitizer = Rails::Html::SafeListSanitizer.new
sanitizer.sanitize(html, tags: %w(iframe), attributes: ALLOWED_ATTRIBUTES)
partially_sanitized_html = sanitizer.sanitize(html, tags: %w(iframe), attributes: ALLOWED_ATTRIBUTES)

document = Nokogiri::HTML::DocumentFragment.parse(partially_sanitized_html)
document.css("iframe").each do |iframe|
iframe["srcdoc"] = Loofah.fragment(iframe["srcdoc"]).scrub!(:prune).to_s if iframe["srcdoc"]
end

document.to_s
end

def remove_margins?
Expand Down
13 changes: 13 additions & 0 deletions spec/system/awesome_iframe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,17 @@
end
end
end

context "when iframe code contains a script in srcdoc" do
let(:iframe) { '<iframe srcdoc="<script>alert(\'XSS\');</script>"></iframe>' }

it "removes the script" do
within ".awesome-iframe" do
expect(page).not_to have_selector("script")
expect(page).to have_selector("iframe")
expect(page).not_to have_text("XSS")
expect { page.driver.browser.switch_to.alert }.to raise_error(Selenium::WebDriver::Error::NoSuchAlertError)
end
end
end
end

0 comments on commit bfdc997

Please sign in to comment.