-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto escape views: implicit and concrete methods.
- Loading branch information
Showing
8 changed files
with
178 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'lotus/utils/escape' | ||
|
||
module Lotus | ||
module View | ||
module Escape | ||
def self.html(input) | ||
case input | ||
when String | ||
Utils::Escape.html(input) | ||
else | ||
input | ||
end | ||
end | ||
|
||
def self.extended(base) | ||
base.class_eval do | ||
include Lotus::Utils::ClassAttribute | ||
include InstanceMethods | ||
|
||
class_attribute :autoescape_methods | ||
self.autoescape_methods = {} | ||
end | ||
end | ||
|
||
def method_added(method_name) | ||
unless autoescape_methods[method_name] | ||
prepend Module.new { | ||
module_eval %{ | ||
def #{ method_name }(*args, &blk); ::Lotus::View::Escape.html super; end | ||
} | ||
} | ||
|
||
autoescape_methods[method_name] = true | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def raw(input) | ||
Lotus::Utils::Escape::SafeString.new(input) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'test_helper' | ||
|
||
describe 'Escape' do | ||
before do | ||
path = Pathname.new(__dir__ + '/fixtures/templates/users/show.html.erb') | ||
template = Lotus::View::Template.new(path) | ||
|
||
@user = User.new(%(<script>alert('username')</script>)) | ||
@view = Users::Show.new(template, user: @user, code: %(<script>alert('code')</script>)) | ||
end | ||
|
||
it 'escapes concrete method' do | ||
@view.custom.must_equal %(<script>alert('custom')</script>) | ||
end | ||
|
||
it 'escapes concrete methods with user input' do | ||
@view.username.must_equal %(<script>alert('username')</script>) | ||
end | ||
|
||
it 'escapes implicit methods' do | ||
@view.code.must_equal %(<script>alert('code')</script>) | ||
end | ||
|
||
it "doesn't escape concrete raw methods" do | ||
@view.raw_username.must_equal %(<script>alert('username')</script>) | ||
end | ||
|
||
it "doesn't interfer with other views" do | ||
Users::Show.autoescape_methods.must_equal({custom: true, username: true, raw_username: true}) | ||
Users::Extra.autoescape_methods.must_equal({username: true}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype HTML> | ||
<html> | ||
<head> | ||
<title><%= page_title username %></title> | ||
</head> | ||
<body> | ||
<div id="content"> | ||
<%= yield %> | ||
</div> | ||
|
||
<aside id="sidebar"> | ||
<span class="username"><%= username %></span> | ||
</aside> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= custom %> | ||
<%= username %> | ||
<%= code %> | ||
<%= raw_username %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'test_helper' | ||
|
||
describe 'Automatic escape' do | ||
before do | ||
@user = User.new(%(<script>alert('username')</script>)) | ||
@rendered = Users::Show.render(format: :html, user: @user, code: %(<script>alert('code')</script>)) | ||
end | ||
|
||
it 'escapes concrete methods' do | ||
@rendered.must_match %(<script>alert('custom')</script>) | ||
end | ||
|
||
it 'escapes concrete methods with user input' do | ||
@rendered.must_match %(<script>alert('username')</script>) | ||
end | ||
|
||
it 'escapes implicit methods' do | ||
@rendered.must_match %(<script>alert('code')</script>) | ||
end | ||
|
||
it "doesn't escape concrete raw methods" do | ||
@rendered.must_match %(<script>alert('username')</script>) | ||
end | ||
|
||
it 'escapes concrete methods in layout' do | ||
@rendered.must_match %(<span class="username"><script>alert('username')</script></span>) | ||
end | ||
|
||
it 'escapes concrete helpers in layout' do | ||
@rendered.must_match %(<title>User: <script>alert('username')</script></title>) | ||
end | ||
end |