-
Notifications
You must be signed in to change notification settings - Fork 2
/
dojo.rb
103 lines (83 loc) · 3.39 KB
/
dojo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require "test/unit"
class BuildQuery
def search_user(params = nil)
if params
clauses = []
params.each { |k, v| clauses << "#{k} = '#{v.gsub("'", "\\\\'")}'" }
where = " where #{clauses.flatten.join(' and ')}"
end
"select * from user#{where};"
end
end
class TestBuildQuery < Test::Unit::TestCase
def test_search_user_no_param
query = BuildQuery.new
expected = "select * from user;"
result = query.search_user
assert_equal(expected, result)
end
def test_search_user_name
query = BuildQuery.new
param_name = "manoel"
expected = "select * from user where name = '#{ param_name }';"
result = query.search_user :name => param_name
assert_equal(expected, result)
end
def test_search_user_security_name
query = BuildQuery.new
param_name_hack = "manoel'; Drop Table user; --"
param_name = "manoel\\'; Drop Table user; --"
expected = "select * from user where name = '#{ param_name }';"
result = query.search_user :name => param_name_hack
assert_equal(expected, result)
end
def test_search_user_email
query = BuildQuery.new
param_email = "[email protected]"
expected = "select * from user where email = '#{ param_email }';"
result = query.search_user :email => param_email
assert_equal(expected, result)
end
def test_search_user_security_email
query = BuildQuery.new
param_email_hack = "[email protected]'; Drop Table user; --"
param_email = "[email protected]\\'; Drop Table user; --"
expected = "select * from user where email = '#{ param_email }';"
result = query.search_user :email => param_email_hack
assert_equal(expected, result)
end
def test_search_user_name_and_email
query = BuildQuery.new
param_name_hack = "manoel'; Drop Table user; --"
param_name = "manoel\\'; Drop Table user; --"
param_email_hack = "[email protected]'; Drop Table user; --"
param_email = "[email protected]\\'; Drop Table user; --"
expected = "select * from user where name = '#{ param_name }' and email = '#{param_email}';"
result = query.search_user :name => param_name_hack, :email => param_email_hack
assert_equal(expected, result)
end
def test_search_user_name_and_email_cpf
query = BuildQuery.new
param_name_hack = "manoel'; Drop Table user; --"
param_name = "manoel\\'; Drop Table user; --"
param_email_hack = "[email protected]'; Drop Table user; --"
param_email = "[email protected]\\'; Drop Table user; --"
param_cpf_hack = "001.914.323-00'; Drop Table user; --"
param_cpf = "001.914.323-00\\'; Drop Table user; --"
expected = "select * from user where name = '#{ param_name }' and email = '#{param_email}' and cpf = '#{param_cpf}';"
result = query.search_user :name => param_name_hack, :email => param_email_hack, :cpf => param_cpf_hack
assert_equal(expected, result)
end
def test_search_user_email_cpf_name
query = BuildQuery.new
param_name_hack = "manoel'; Drop Table user; --"
param_name = "manoel\\'; Drop Table user; --"
param_email_hack = "[email protected]'; Drop Table user; --"
param_email = "[email protected]\\'; Drop Table user; --"
param_cpf_hack = "001.914.323-00'; Drop Table user; --"
param_cpf = "001.914.323-00\\'; Drop Table user; --"
expected = "select * from user where email = '#{param_email}' and cpf = '#{param_cpf}' and name = '#{ param_name }';"
result = query.search_user :email => param_email_hack, :cpf => param_cpf_hack, :name => param_name_hack
assert_equal(expected, result)
end
end