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

srcdoc should be considered unsafe in version of Phlex that don’t escape attribute values #777

Merged
merged 1 commit into from
Sep 12, 2024
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/phlex/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Phlex::HTML < Phlex::SGML
autoload :VoidElements, "phlex/html/void_elements"

# A list of HTML attributes that have the potential to execute unsafe JavaScript.
EVENT_ATTRIBUTES = Set.new(%w[onabort onafterprint onbeforeprint onbeforeunload onblur oncanplay oncanplaythrough onchange onclick oncontextmenu oncopy oncuechange oncut ondblclick ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop ondurationchange onemptied onended onerror onfocus onhashchange oninput oninvalid onkeydown onkeypress onkeyup onload onloadeddata onloadedmetadata onloadstart onmessage onmousedown onmousemove onmouseout onmouseover onmouseup onmousewheel onoffline ononline onpagehide onpageshow onpaste onpause onplay onplaying onpopstate onprogress onratechange onreset onresize onscroll onsearch onseeked onseeking onselect onstalled onstorage onsubmit onsuspend ontimeupdate ontoggle onunload onvolumechange onwaiting onwheel]).freeze
UNSAFE_ATTRIBUTES = Set.new(%w[onabort onafterprint onbeforeprint onbeforeunload onblur oncanplay oncanplaythrough onchange onclick oncontextmenu oncopy oncuechange oncut ondblclick ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop ondurationchange onemptied onended onerror onfocus onhashchange oninput oninvalid onkeydown onkeypress onkeyup onload onloadeddata onloadedmetadata onloadstart onmessage onmousedown onmousemove onmouseout onmouseover onmouseup onmousewheel onoffline ononline onpagehide onpageshow onpaste onpause onplay onplaying onpopstate onprogress onratechange onreset onresize onscroll onsearch onseeked onseeking onselect onstalled onstorage onsubmit onsuspend ontimeupdate ontoggle onunload onvolumechange onwaiting onwheel srcdoc]).freeze

extend Phlex::Elements
include VoidElements, StandardElements
Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def __attributes__(attributes, buffer = +"")
end

# Detect unsafe attribute names. Attribute names are considered unsafe if they match an event attribute or include unsafe characters.
if Phlex::HTML::EVENT_ATTRIBUTES.include?(lower_name.delete("^a-z-"))
if Phlex::HTML::UNSAFE_ATTRIBUTES.include?(lower_name.delete("^a-z-"))
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{k}.")
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def view_template
def view_template
srcdoc = capture { yield } if block_given?

iframe srcdoc:
iframe srcdoc: safe(srcdoc)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/naughty_business.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def view_template
end
end

Phlex::HTML::EVENT_ATTRIBUTES.each do |event_attribute|
Phlex::HTML::UNSAFE_ATTRIBUTES.each do |event_attribute|
with "with naughty #{event_attribute} attribute" do
naughty_attributes = { event_attribute => "alert(1);" }

Expand Down