forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolate_test.go
309 lines (298 loc) · 5.51 KB
/
interpolate_test.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package dbr
import (
"strings"
"testing"
"time"
"github.com/gocraft/dbr/v2/dialect"
"github.com/stretchr/testify/require"
)
func TestInterpolateIgnoreBinary(t *testing.T) {
for _, test := range []struct {
query string
value []interface{}
wantQuery string
wantValue []interface{}
}{
{
query: "?",
value: []interface{}{1},
wantQuery: "1",
wantValue: nil,
},
{
query: "?",
value: []interface{}{[]byte{1, 2, 3}},
wantQuery: "?",
wantValue: []interface{}{[]byte{1, 2, 3}},
},
{
query: "? ?",
value: []interface{}{[]byte{1}, []byte{2}},
wantQuery: "? ?",
wantValue: []interface{}{[]byte{1}, []byte{2}},
},
{
query: "? ?",
value: []interface{}{Expr("|?| ?", []byte{1}, Expr("|?|", []byte{2})), []byte{3}},
wantQuery: "|?| |?| ?",
wantValue: []interface{}{[]byte{1}, []byte{2}, []byte{3}},
},
} {
i := interpolator{
Buffer: NewBuffer(),
Dialect: dialect.MySQL,
IgnoreBinary: true,
}
err := i.interpolate(test.query, test.value, true)
require.NoError(t, err)
require.Equal(t, test.wantQuery, i.String())
require.Equal(t, test.wantValue, i.Value())
}
}
func TestInterpolateForDialect(t *testing.T) {
for _, test := range []struct {
query string
value []interface{}
want string
}{
{
query: "?",
value: []interface{}{nil},
want: "NULL",
},
{
query: "?",
value: []interface{}{`'"'"`},
want: "'\\'\\\"\\'\\\"'",
},
{
query: "? ?",
value: []interface{}{true, false},
want: "1 0",
},
{
query: "? ?",
value: []interface{}{1, 1.23},
want: "1 1.23",
},
{
query: "?",
value: []interface{}{time.Date(2008, 9, 17, 20, 4, 26, 123456000, time.UTC)},
want: "'2008-09-17 20:04:26.123456'",
},
{
query: "?",
value: []interface{}{[]string{"one", "two"}},
want: "('one','two')",
},
{
query: "?",
value: []interface{}{[]byte{0x1, 0x2, 0x3}},
want: "0x010203",
},
{
query: "start?end",
value: []interface{}{new(int)},
want: "start0end",
},
{
query: "?",
value: []interface{}{Select("a").From("table")},
want: "SELECT a FROM table",
},
{
query: "?",
value: []interface{}{I("a1").As("a2")},
want: "`a1` AS `a2`",
},
{
query: "?",
value: []interface{}{Select("a").From("table").As("a1")},
want: "(SELECT a FROM table) AS `a1`",
},
{
query: "?",
value: []interface{}{
UnionAll(
Select("a").From("table1"),
Select("b").From("table2"),
).As("t"),
},
// parentheses around union subqueries are not supported in sqlite
// but supported in both mysql and postgres.
want: "(SELECT a FROM table1 UNION ALL SELECT b FROM table2) AS `t`",
},
{
query: "?",
value: []interface{}{time.Month(7)},
want: "7",
},
{
query: "?",
value: []interface{}{(*int64)(nil)},
want: "NULL",
},
{
query: "???? ? ?? ? ??",
value: []interface{}{1, 2},
want: "?? 1 ? 2 ?",
},
{
query: "???",
value: []interface{}{1},
want: "?1",
},
} {
s, err := InterpolateForDialect(test.query, test.value, dialect.MySQL)
require.NoError(t, err)
require.Equal(t, test.want, s)
}
}
// Attempts to test common SQL injection strings. See `InjectionAttempts` for
// more information on the source and the strings themselves.
func TestCommonSQLInjections(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
for _, injectionAttempt := range strings.Split(injectionAttempts, "\n") {
// Create a user with the attempted injection as the email address
_, err := sess.InsertInto("dbr_people").
Pair("name", injectionAttempt).
Exec()
require.NoError(t, err)
// SELECT the name back and ensure it's equal to the injection attempt
var name string
err = sess.Select("name").From("dbr_people").OrderDesc("id").Limit(1).LoadOne(&name)
require.NoError(t, err)
require.Equal(t, injectionAttempt, name)
}
}
}
// InjectionAttempts is a newline separated list of common SQL injection exploits
// taken from https://wfuzz.googlecode.com/svn/trunk/wordlist/Injections/SQL.txt
const injectionAttempts = `
'
"
#
-
--
'%20--
--';
'%20;
=%20'
=%20;
=%20--
\x23
\x27
\x3D%20\x3B'
\x3D%20\x27
\x27\x4F\x52 SELECT *
\x27\x6F\x72 SELECT *
'or%20select *
admin'--
<>"'%;)(&+
'%20or%20''='
'%20or%20'x'='x
"%20or%20"x"="x
')%20or%20('x'='x
0 or 1=1
' or 0=0 --
" or 0=0 --
or 0=0 --
' or 0=0 #
" or 0=0 #
or 0=0 #
' or 1=1--
" or 1=1--
' or '1'='1'--
"' or 1 --'"
or 1=1--
or%201=1
or%201=1 --
' or 1=1 or ''='
" or 1=1 or ""="
' or a=a--
" or "a"="a
') or ('a'='a
") or ("a"="a
hi" or "a"="a
hi" or 1=1 --
hi' or 1=1 --
hi' or 'a'='a
hi') or ('a'='a
hi") or ("a"="a
'hi' or 'x'='x';
@variable
,@variable
PRINT
PRINT @@variable
select
insert
as
or
procedure
limit
order by
asc
desc
delete
update
distinct
having
truncate
replace
like
handler
bfilename
' or username like '%
' or uname like '%
' or userid like '%
' or uid like '%
' or user like '%
exec xp
exec sp
'; exec master..xp_cmdshell
'; exec xp_regread
t'exec master..xp_cmdshell 'nslookup www.google.com'--
--sp_password
\x27UNION SELECT
' UNION SELECT
' UNION ALL SELECT
' or (EXISTS)
' (select top 1
'||UTL_HTTP.REQUEST
1;SELECT%20*
to_timestamp_tz
tz_offset
<>"'%;)(&+
'%20or%201=1
%27%20or%201=1
%20$(sleep%2050)
%20'sleep%2050'
char%4039%41%2b%40SELECT
'%20OR
'sqlattempt1
(sqlattempt2)
|
%7C
*|
%2A%7C
*(|(mail=*))
%2A%28%7C%28mail%3D%2A%29%29
*(|(objectclass=*))
%2A%28%7C%28objectclass%3D%2A%29%29
(
%28
)
%29
&
%26
!
%21
' or 1=1 or ''='
' or ''='
x' or 1=1 or 'x'='y
/
//
//*
*/*
`