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

Detect invalid href attributes #817

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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