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

fix: Escape content passed to tags via argument #55

Merged
merged 1 commit into from
Sep 9, 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
34 changes: 33 additions & 1 deletion spec/blueprint/html/safety_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ require "../../spec_helper"
private class DummyPage
include Blueprint::HTML

register_element :v_btn

private def blueprint
span { "<script>alert('hello')</script>" }
span "<script>alert('content')</script>"
plain "<script>alert('Plain Text')</script>"
render(DummyComponent.new) { "<script>alert('DummyComponent')</script>" }
div(class: "some-class\" onblur=\"alert('Attribute')")
comment { "--><script>alert('Plain Text')</script><!--" }
v_btn "<script>alert('content')</script>"
v_btn(class: "some-class\" onclick=\"alert('Attribute')") { "<script>alert('hello')</script>" }
end
end

Expand All @@ -21,7 +26,7 @@ private class DummyComponent
end

describe "Blueprint::HTML safety" do
it "escapes content passed to tags" do
it "escapes content passed to tags via block" do
page = DummyPage.new
expected_html = <<-HTML.strip
<span>&lt;script&gt;alert(&#39;hello&#39;)&lt;/script&gt;</span>
Expand All @@ -30,6 +35,15 @@ describe "Blueprint::HTML safety" do
page.to_html.should contain(expected_html)
end

it "escapes content passed to tags via argument" do
page = DummyPage.new
expected_html = <<-HTML.strip
<span>&lt;script&gt;alert(&#39;content&#39;)&lt;/script&gt;</span>
HTML

page.to_html.should contain(expected_html)
end

it "escapes plain text" do
page = DummyPage.new
expected_html = <<-HTML.strip
Expand Down Expand Up @@ -65,4 +79,22 @@ describe "Blueprint::HTML safety" do

page.to_html.should contain(expected_html)
end

it "escapes custom tag content passed via argument" do
page = DummyPage.new
expected_html = <<-HTML.strip
<v-btn>&lt;script&gt;alert(&#39;content&#39;)&lt;/script&gt;</v-btn>
HTML

page.to_html.should contain(expected_html)
end

it "escapes custom tag content passed via block" do
page = DummyPage.new
expected_html = <<-HTML.strip
<v-btn class="some-class&quot; onclick=&quot;alert(&#39;Attribute&#39;)">&lt;script&gt;alert(&#39;hello&#39;)&lt;/script&gt;</v-btn>
HTML

page.to_html.should contain(expected_html)
end
end
6 changes: 3 additions & 3 deletions src/blueprint/html/element_registrar.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Blueprint::HTML
end

private def {{method_name.id}}(**attributes) : Nil
element({{tag}}, **attributes) { "" }
element({{tag}}, "", **attributes)
end

private def {{method_name.id}}(__content__ : String, **attributes) : Nil
Expand All @@ -19,7 +19,7 @@ module Blueprint::HTML
{% tag ||= method_name.tr("_", "-") %}

private def {{method_name.id}}(**attributes) : Nil
element({{tag}}, **attributes) { "" }
element({{tag}}, "", **attributes)
end
end

Expand Down Expand Up @@ -47,7 +47,7 @@ module Blueprint::HTML
@buffer << _tag_name
@buffer << parse_attributes(attributes)
@buffer << ">"
@buffer << __content__
::HTML.escape(__content__, @buffer)
@buffer << "</"
@buffer << _tag_name
@buffer << ">"
Expand Down
Loading