Skip to content

Commit

Permalink
Detect invalid href attributes (#817)
Browse files Browse the repository at this point in the history
Closes #816
  • Loading branch information
joeldrapper authored Nov 25, 2024
1 parent a52388b commit dc1248a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,16 @@ def __attributes__(attributes, buffer = +"")
unless Phlex::SGML::SafeObject === v
normalized_name = lower_name.delete("^a-z-")

if value != true && REF_ATTRIBUTES.include?(normalized_name) && value.downcase.delete("^a-z:").start_with?("javascript:")
# We just ignore these because they were likely not specified by the developer.
next
if value != true && REF_ATTRIBUTES.include?(normalized_name)
case value
when String
if value.downcase.delete("^a-z:").start_with?("javascript:")
# We just ignore these because they were likely not specified by the developer.
next
end
else
raise Phlex::ArgumentError.new("Invalid attribute value for #{k}: #{v.inspect}.")
end
end

if normalized_name.bytesize > 2 && normalized_name.start_with?("on") && !normalized_name.include?("-")
Expand Down
8 changes: 8 additions & 0 deletions quickdraw/sgml/attributes.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
end
end

test "href with hash" do
expect {
phlex { a(href: {}) }
}.to_raise(Phlex::ArgumentError) do |error|
expect(error.message) == "Invalid attribute value for href: #{{}.inspect}."
end
end

test "unsafe href attribute" do
expect(
phlex { div(href: "javascript:alert('hello')") },
Expand Down

0 comments on commit dc1248a

Please sign in to comment.