Skip to content

Commit dc1248a

Browse files
authored
Detect invalid href attributes (#817)
Closes #816
1 parent a52388b commit dc1248a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/phlex/sgml.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,16 @@ def __attributes__(attributes, buffer = +"")
401401
unless Phlex::SGML::SafeObject === v
402402
normalized_name = lower_name.delete("^a-z-")
403403

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

409416
if normalized_name.bytesize > 2 && normalized_name.start_with?("on") && !normalized_name.include?("-")

quickdraw/sgml/attributes.test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
end
2727
end
2828

29+
test "href with hash" do
30+
expect {
31+
phlex { a(href: {}) }
32+
}.to_raise(Phlex::ArgumentError) do |error|
33+
expect(error.message) == "Invalid attribute value for href: #{{}.inspect}."
34+
end
35+
end
36+
2937
test "unsafe href attribute" do
3038
expect(
3139
phlex { div(href: "javascript:alert('hello')") },

0 commit comments

Comments
 (0)