Skip to content

Commit 5e606b7

Browse files
committed
Don't use inline expectations when alerts in erb files
1 parent 84e7c2d commit 5e606b7

9 files changed

Lines changed: 50 additions & 53 deletions

File tree

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
query: queries/security/cwe-079/ReflectedXSS.ql
2-
postprocess: utils/test/InlineExpectationsTestQuery.ql
1+
queries/security/cwe-079/ReflectedXSS.ql
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
query: queries/security/cwe-079/StoredXSS.ql
2-
postprocess: utils/test/InlineExpectationsTestQuery.ql
1+
queries/security/cwe-079/StoredXSS.ql
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
query: queries/security/cwe-079/UnsafeHtmlConstruction.ql
2-
postprocess: utils/test/InlineExpectationsTestQuery.ql
1+
queries/security/cwe-079/UnsafeHtmlConstruction.ql

ruby/ql/test/query-tests/security/cwe-079/app/controllers/foo/bars_controller.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@ def index
66
end
77

88
def user_name
9-
return params[:user_name] # $ Source[rb/reflected-xss]
9+
return params[:user_name]
1010
end
1111

1212
def user_name_memo
13-
@user_name ||= params[:user_name] # $ Source[rb/reflected-xss]
13+
@user_name ||= params[:user_name]
1414
end
1515

1616
def show
17-
@user_website = params[:website] # $ Source[rb/reflected-xss]
18-
dt = params[:text] # $ Source[rb/reflected-xss]
17+
@user_website = params[:website]
18+
dt = params[:text]
1919
@instance_text = dt
2020
@safe_foo = params[:text]
2121
@safe_foo = "safe_foo"
2222
@html_escaped = ERB::Util.html_escape(params[:text])
2323
@header_escaped = ERB::Util.html_escape(cookies[:foo]) # OK - cookies not controllable by 3rd party
24-
response.header["content-type"] = params[:content_type] # $ Alert[rb/reflected-xss]
24+
response.header["content-type"] = params[:content_type]
2525
response.header["x-customer-header"] = params[:bar] # OK - header not relevant to XSS
2626
render "foo/bars/show", locals: { display_text: dt, safe_text: "hello" }
2727
end
2828

2929
def make_safe_html
30-
str = params[:user_name] # $ Source[rb/reflected-xss]
31-
str.html_safe # $ Alert[rb/reflected-xss]
30+
str = params[:user_name]
31+
str.html_safe
3232

33-
translate("welcome", name: params[:user_name]).html_safe # $ Alert[rb/reflected-xss] // NOT OK - translate preserves taint
34-
t("welcome", name: params[:user_name]).html_safe # $ Alert[rb/reflected-xss] // NOT OK - t is an alias of translate
33+
translate("welcome", name: params[:user_name]).html_safe # NOT OK - translate preserves taint
34+
t("welcome", name: params[:user_name]).html_safe # NOT OK - t is an alias of translate
3535
t("welcome_html", name: params[:user_name]).html_safe # OK - t escapes html when key ends in _html
36-
I18n.t("welcome_html", name: params[:user_name]).html_safe # $ Alert[rb/reflected-xss] // NOT OK - I18n.t does not escape html
37-
I18n.translate("welcome_html", name: params[:user_name]).html_safe # $ Alert[rb/reflected-xss] // NOT OK - alias
36+
I18n.t("welcome_html", name: params[:user_name]).html_safe # NOT OK - I18n.t does not escape html
37+
I18n.translate("welcome_html", name: params[:user_name]).html_safe # NOT OK - alias
3838
end
3939
end

ruby/ql/test/query-tests/security/cwe-079/app/controllers/foo/stores_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def user_handle
55
end
66

77
def show
8-
dt = File.read("foo.txt") # $ Source[rb/stored-xss]
8+
dt = File.read("foo.txt")
99
@instance_text = dt
1010
@user = User.find 1
1111
@safe_user_handle = ERB::Util.html_escape(@user.handle)

ruby/ql/test/query-tests/security/cwe-079/app/views/foo/bars/_widget.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<%= raw @display_text %>
33

44
<%# BAD: A local rendered raw as a local variable %>
5-
<%= raw display_text %> <%# $ Alert[rb/reflected-xss], Alert[rb/stored-xss] %>
5+
<%= raw display_text %>
66

77
<%# BAD: A local rendered raw via the local_assigns hash %>
8-
<%= raw local_assigns[:display_text] %> <%# $ Alert[rb/reflected-xss], Alert[rb/stored-xss] %>
8+
<%= raw local_assigns[:display_text] %>
99

1010
<%# GOOD: A local rendered with default escaping via the local_assigns hash %>
1111
<%= local_assigns[:display_text] %>

ruby/ql/test/query-tests/security/cwe-079/app/views/foo/bars/show.html.erb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<%# BAD: An instance variable rendered without escaping %>
2-
<a href="<%= raw @user_website %>">website</a> <%# $ Alert[rb/reflected-xss] %>
2+
<a href="<%= raw @user_website %>">website</a>
33

44
<%# BAD: A local rendered raw as a local variable %>
5-
<%= raw display_text %> <%# $ Alert[rb/reflected-xss] %>
5+
<%= raw display_text %>
66

77
<%# BAD: A local rendered raw via the local_assigns hash %>
8-
<%= raw local_assigns[:display_text] %> <%# $ Alert[rb/reflected-xss] %>
8+
<%= raw local_assigns[:display_text] %>
99

1010
<% key = :display_text %>
1111
<%# BAD: A local rendered raw via the locals_assigns hash %>
12-
<%= raw local_assigns[key] %> <%# $ Alert[rb/reflected-xss] %>
12+
<%= raw local_assigns[key] %>
1313

1414
<ul>
1515
<% for key in [:display_text, :safe_text] do %>
1616
<%# BAD: A local rendered raw via the locals hash %>
17-
<li><%= raw local_assigns[key] %></li> <%# $ Alert[rb/reflected-xss] %>
17+
<li><%= raw local_assigns[key] %></li>
1818
<% end %>
1919
</ul>
2020

@@ -32,28 +32,28 @@
3232

3333
<%# BAD: html_safe marks string as not requiring HTML escaping %>
3434
<%=
35-
display_text.html_safe <%# $ Alert[rb/reflected-xss] %>
35+
display_text.html_safe
3636
%>
3737

3838
<%# BAD: html_safe marks string as not requiring HTML escaping %>
3939
<%=
40-
@instance_text.html_safe <%# $ Alert[rb/reflected-xss] %>
40+
@instance_text.html_safe
4141
%>
4242

4343
<%= render partial: 'foo/bars/widget', locals: { display_text: "widget_" + display_text } %>
4444

4545
<%# BAD: user_name is a helper method that returns unsanitized user-input %>
46-
<%= user_name.html_safe %> <%# $ Alert[rb/reflected-xss] %>
46+
<%= user_name.html_safe %>
4747

4848
<%# BAD: user_name_memo is a helper method that returns unsanitized user-input %>
4949
<%# TODO: we miss this because the return value from user_name_memo is not properly linked to this call %>
50-
<%= user_name_memo.html_safe %> <%# $ Alert[rb/reflected-xss] %>
50+
<%= user_name_memo.html_safe %>
5151

5252
<%# BAD: unsanitized user-input should not be passed to link_to as the URL %>
53-
<%= link_to "user website", params[:website] %> <%# $ Alert[rb/reflected-xss] %>
53+
<%= link_to "user website", params[:website] %>
5454

5555
<%# BAD: unsanitized user-input should not be passed to link_to as the URL %>
56-
<%= link_to params[:website], class: "user-link" do %> <%# $ Alert[rb/reflected-xss] %>
56+
<%= link_to params[:website], class: "user-link" do %>
5757
user website
5858
<% end %>
5959

@@ -70,20 +70,20 @@
7070
%>
7171

7272
<%# BAD: simple_format called with sanitize: false %>
73-
<%= simple_format(params[:comment], sanitize: false) %> <%# $ Alert[rb/reflected-xss] %>
73+
<%= simple_format(params[:comment], sanitize: false) %>
7474

7575
<%# BAD: javasript_include_tag called with remote input %>
76-
<%= javascript_include_tag params[:url] %> <%# $ Alert[rb/reflected-xss] %>
76+
<%= javascript_include_tag params[:url] %>
7777

7878
<%# GOOD: input is sanitized %>
7979
<%= sanitize(params[:comment]).html_safe %>
8080

8181
<%# BAD: A local rendered raw as a local variable %>
82-
<%== display_text %> <%# $ Alert[rb/reflected-xss] %>
82+
<%== display_text %>
8383

8484
<%# BAD: translate preserves taint %>
85-
<%= raw translate("welcome", name: display_text) %> <%# $ Alert[rb/reflected-xss] %>
86-
<%= raw t("welcome", name: display_text) %> <%# $ Alert[rb/reflected-xss] %>
85+
<%= raw translate("welcome", name: display_text) %>
86+
<%= raw t("welcome", name: display_text) %>
8787

8888
<%# GOOD: translate sanitizes for html keys %>
8989
<%= raw t("welcome1.html", name: display_text) %>

ruby/ql/test/query-tests/security/cwe-079/app/views/foo/stores/show.html.erb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<%# BAD: A local rendered raw as a local variable %>
2-
<%= raw display_text %> <%# $ Alert[rb/stored-xss] %>
2+
<%= raw display_text %>
33

44
<%# BAD: A local rendered raw via the local_assigns hash %>
5-
<%= raw local_assigns[:display_text] %> <%# $ Alert[rb/stored-xss] %>
5+
<%= raw local_assigns[:display_text] %>
66

77
<% key = :display_text %>
88
<%# BAD: A local rendered raw via the locals_assigns hash %>
9-
<%= raw local_assigns[key] %> <%# $ Alert[rb/stored-xss] %>
9+
<%= raw local_assigns[key] %>
1010

1111
<ul>
1212
<% for key in [:display_text, :safe_text] do %>
1313
<%# BAD: A local rendered raw via the locals hash %>
14-
<li><%= raw local_assigns[key] %></li> <%# $ Alert[rb/stored-xss] %>
14+
<li><%= raw local_assigns[key] %></li>
1515
<% end %>
1616
</ul>
1717

@@ -29,12 +29,12 @@
2929

3030
<%# BAD: html_safe marks string as not requiring HTML escaping %>
3131
<%=
32-
display_text.html_safe <%# $ Alert[rb/stored-xss] %>
32+
display_text.html_safe
3333
%>
3434

3535
<%# BAD: html_safe marks string as not requiring HTML escaping %>
3636
<%=
37-
@instance_text.html_safe <%# $ Alert[rb/stored-xss] %>
37+
@instance_text.html_safe
3838
%>
3939

4040
<%= render partial: 'foo/bars/widget', locals: { display_text: "widget_" + display_text } %>
@@ -43,7 +43,7 @@
4343
<%= user_name_handle.html_safe %>
4444

4545
<%# BAD: Direct to a database value without escaping %>
46-
<%= @user.handle.html_safe %> <%# $ Alert[rb/stored-xss] %>
46+
<%= @user.handle.html_safe %>
4747

4848
<%# BAD: Indirect to a database value without escaping %>
4949
<%= @user.raw_name.html_safe %>
@@ -60,7 +60,7 @@
6060
<%# BAD: Direct to a database value without escaping %>
6161
<%=
6262
some_user = User.find 1
63-
some_user.handle.html_safe <%# $ Alert[rb/stored-xss] %>
63+
some_user.handle.html_safe
6464
%>
6565

6666
<%# BAD: Indirect to a database value without escaping (currently missed due to lack of 'self' handling in ORM tracking) %>
@@ -83,7 +83,7 @@
8383

8484
<%# BAD: Kernel.sprintf is a taint-step %>
8585
<%=
86-
sprintf("%s", @user.handle).html_safe <%# $ Alert[rb/stored-xss] %>
86+
sprintf("%s", @user.handle).html_safe
8787
%>
8888

8989
<%# GOOD: The `foo.bar.baz` is not recognized as a source %>

ruby/ql/test/query-tests/security/cwe-079/lib/unsafeHtml.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
class Foobar
2-
def create_user_description(name) # $ Source[rb/html-constructed-from-input]
3-
"<h2>#{name}</h2>".html_safe # $ Alert[rb/html-constructed-from-input] // NOT OK - the parameter is not escaped
2+
def create_user_description(name)
3+
"<h2>#{name}</h2>".html_safe # NOT OK - the parameter is not escaped
44

55
# escape
66
"<h2>#{ERB::Util.html_escape(name)}</h2>".html_safe # OK - the parameter is escaped
77
end
88

9-
def string_like_literal name # $ Source[rb/html-constructed-from-input]
9+
def string_like_literal name
1010
h = <<-HTML
11-
<h2>#{name}</h2> # $ Alert[rb/html-constructed-from-input]
11+
<h2>#{name}</h2>
1212
HTML
1313
h.html_safe # NOT OK - the parameter is not escaped
1414
end
1515

16-
def sprintf_use name # $ Source[rb/html-constructed-from-input]
17-
sprintf("<h2>%s</h2>", name).html_safe # $ Alert[rb/html-constructed-from-input] // NOT OK - the parameter is not escaped
16+
def sprintf_use name
17+
sprintf("<h2>%s</h2>", name).html_safe # NOT OK - the parameter is not escaped
1818

1919
# escape
2020
sprintf("<h2>%s</h2>", ERB::Util.html_escape(name)).html_safe # OK - the parameter is escaped
2121
end
2222

23-
def create_user_description2(name) # $ Source[rb/html-constructed-from-input]
24-
"<h2>#{name}</h2>".html_safe # $ Alert[rb/html-constructed-from-input] // NOT OK - the value is not necessarily HTML safe
23+
def create_user_description2(name)
24+
"<h2>#{name}</h2>".html_safe # NOT OK - the value is not necessarily HTML safe
2525

2626
if name.html_safe?
2727
"<h2>#{name}</h2>".html_safe # OK - value is marked as being HTML safe

0 commit comments

Comments
 (0)