-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsqllexer_fuzz_test.go
348 lines (282 loc) · 8.85 KB
/
sqllexer_fuzz_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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package sqllexer
import (
"testing"
)
func FuzzNormalizer(f *testing.F) {
// Add complex SQL patterns for different DBMS
addComplexTestCases(f)
addObfuscationTestCases(f)
normalizer := NewNormalizer(
WithCollectComments(true),
WithCollectCommands(true),
WithCollectTables(true),
WithCollectProcedures(true),
)
f.Fuzz(func(t *testing.T, input string, dbmsType string) {
// Try different DBMS types with each input
opts := []lexerOption{WithDBMS(DBMSType(dbmsType))}
_, _, err := normalizer.Normalize(input, opts...)
if err != nil {
t.Errorf("error normalizing input: %v", err)
}
})
}
func FuzzObfuscatorAndNormalizer(f *testing.F) {
// Test the combined obfuscation and normalization
addComplexTestCases(f)
addObfuscationTestCases(f)
normalizer := NewNormalizer(
WithCollectComments(true),
WithCollectCommands(true),
WithCollectTables(true),
WithCollectProcedures(true),
)
obfuscator := NewObfuscator(
WithReplaceDigits(true),
)
f.Fuzz(func(t *testing.T, input string, dbmsType string) {
opts := []lexerOption{WithDBMS(DBMSType(dbmsType))}
_, _, err := ObfuscateAndNormalize(input, obfuscator, normalizer, opts...)
if err != nil {
t.Errorf("error obfuscating and normalizing input: %v", err)
}
})
}
func addComplexTestCases(f *testing.F) {
// PostgreSQL specific patterns
postgresPatterns := []string{
// Schema qualified objects with quotes
`SELECT * FROM "public"."users" u CROSS JOIN LATERAL (SELECT * FROM "schema"."table") sub`,
// Custom operators
`SELECT * FROM users WHERE name <-> 'pattern' < 0.7`,
// Array and JSON operations
`SELECT array[1,2,3] @> array[1,2]`,
`SELECT data->>'name' FROM users WHERE data @? '$.age > 20'`,
// WITH ORDINALITY
`SELECT * FROM unnest(ARRAY['a','b','c']) WITH ORDINALITY`,
// Complex type casts
`SELECT CAST(CAST(col AS text) AS integer[])`,
// Dollar quoted strings
`SELECT $func$BEGIN RETURN 1; END$func$`,
`SELECT $tag$string with " and ' quotes$tag$`,
}
// SQL Server specific patterns
sqlServerPatterns := []string{
// Bracketed identifiers
`SELECT * FROM [server].[database].[schema].[table]`,
// CROSS/OUTER APPLY
`SELECT * FROM users CROSS APPLY (SELECT * FROM table(value)) t`,
// Table hints
`SELECT * FROM users WITH (NOLOCK, INDEX(idx))`,
// TOP with ties
`SELECT TOP 1 WITH TIES * FROM users ORDER BY score`,
// OUTPUT clause
`DELETE users OUTPUT deleted.* INTO audit_table`,
// PIVOT/UNPIVOT
`SELECT * FROM users PIVOT (SUM(val) FOR col IN ([A],[B],[C])) p`,
}
// MySQL specific patterns
mysqlPatterns := []string{
// Backtick identifiers
"SELECT * FROM `database`.`table`",
// STRAIGHT_JOIN
"SELECT STRAIGHT_JOIN * FROM t1 INNER JOIN t2",
// Complex index hints
"SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 USE INDEX (idx1, idx2)",
// Group concat with order by
"SELECT GROUP_CONCAT(name ORDER BY id SEPARATOR ';')",
// MySQL comment
"#1",
}
// Oracle specific patterns
oraclePatterns := []string{
// Connect by
`SELECT * FROM users START WITH id = 1 CONNECT BY PRIOR parent_id = id`,
// Hierarchical queries
`SELECT * FROM users CONNECT BY NOCYCLE PRIOR id = parent_id`,
// MINUS operator
`SELECT * FROM t1 MINUS SELECT * FROM t2`,
// Row limiting with offset
`SELECT * FROM users OFFSET 5 ROWS FETCH FIRST 10 ROWS ONLY`,
// Flashback queries
`SELECT * FROM users AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '1' DAY`,
}
// Snowflake specific patterns
snowflakePatterns := []string{
// Semi-structured data
`SELECT PARSE_JSON('{"a":1}'):a::string`,
// Pattern matching
`SELECT REGEXP_SUBSTR(col, '[A-Z]+', 1, 1, 'i')`,
// Time travel
`SELECT * FROM users AT(TIMESTAMP => 'yesterday'::timestamp)`,
`SELECT * FROM users BEFORE(STATEMENT => '8e5d0ca9-005e-44e6-b858-a8f5b37c5726')`,
// External tables
`SELECT $1, $2 FROM @mystage/file.csv`,
}
// Common edge cases across all DBMS
commonEdgeCases := []string{
// Nested subqueries
`SELECT * FROM (SELECT * FROM (SELECT * FROM users) t1) t2`,
// Multiple CTEs
`WITH
cte1 AS (SELECT 1),
cte2 AS (SELECT * FROM cte1),
cte3 AS (SELECT * FROM cte2)
SELECT * FROM cte3`,
// Mixed quoted identifiers
`SELECT "col1", [col2], 'string "with" quotes' FROM users`,
// Comments in various positions
`SELECT /*comment1*/ * --comment2
/*comment3*/ FROM /*comment4*/ users`,
// Unicode identifiers
`SELECT * FROM "表" WHERE "列" = '値'`,
// Empty strings and special characters
`SELECT '', '\'\'\'\'\'''', '\\\\', '\n\r\t'`,
}
// Add all patterns for testing
patterns := []string{}
patterns = append(patterns, postgresPatterns...)
patterns = append(patterns, sqlServerPatterns...)
patterns = append(patterns, mysqlPatterns...)
patterns = append(patterns, oraclePatterns...)
patterns = append(patterns, snowflakePatterns...)
patterns = append(patterns, commonEdgeCases...)
// Add each pattern with different DBMS types
dbmsTypes := []string{
string(DBMSPostgres),
string(DBMSSQLServer),
string(DBMSMySQL),
string(DBMSOracle),
string(DBMSSnowflake),
}
for _, pattern := range patterns {
for _, dbms := range dbmsTypes {
f.Add(pattern, dbms)
}
}
}
func addObfuscationTestCases(f *testing.F) {
// PostgreSQL specific obfuscation patterns
postgresPatterns := []string{
// Dollar quoted strings
`SELECT $tag$string with 'quotes" and $dollars$tag$`,
// Array literals
`SELECT ARRAY[1, 2, 3], '{1,2,3}'::int[]`,
// Postgres specific time/date
`SELECT TIMESTAMP WITH TIME ZONE '2023-01-01 12:00:00+00'`,
// Custom types
`SELECT '127.0.0.1'::inet, '12:34:56:78:90:ab'::macaddr`,
}
// SQL Server specific obfuscation patterns
sqlServerPatterns := []string{
// Money and smallmoney
`SELECT $123.45, £123.45`,
// Unicode strings
`SELECT N'unicode string'`,
// Binary strings
`SELECT 0x1234ABCD`,
// DateTime2
`SELECT CONVERT(datetime2, '2023-01-01 12:00:00.1234567')`,
}
// MySQL specific obfuscation patterns
mysqlPatterns := []string{
// Hex literals
`SELECT X'1234', 0x1234`,
// Bit values
`SELECT b'1010', 0b1010`,
// MySQL date formats
`SELECT DATE '2023-01-01' + INTERVAL 1 DAY`,
// Backtick strings
"SELECT `col1`, `table`.`col2`",
}
// Oracle specific obfuscation patterns
oraclePatterns := []string{
// Oracle date format
`SELECT DATE '2023-01-01', TIMESTAMP '2023-01-01 12:00:00'`,
// INTERVAL literals
`SELECT INTERVAL '1' DAY, INTERVAL '2' YEAR`,
// Q quoted strings
`SELECT Q'[string's with 'quotes']'`,
// ROWID
`SELECT CHARTOROWID('AAAB12AADAAAAwPAAA')`,
}
// Snowflake specific obfuscation patterns
snowflakePatterns := []string{
// Semi-structured data
`SELECT PARSE_JSON('{"a": 1}'):a::number`,
// Variant
`SELECT TO_VARIANT(1234)`,
// Geographic data
`SELECT TO_GEOGRAPHY('POINT(-122.35 37.55)')`,
// Stage references
`SELECT $1, $2, $3 FROM @mystage`,
}
// Common obfuscation patterns for all DBMS
commonPatterns := []string{
// Basic numbers
`SELECT 123, -456, 3.14159, -0.123, 1e10, -1e-10`,
`SELECT * FROM t1 WHERE id IN (1, 2, 3, 4, 5)`,
// Basic strings
`SELECT 'string', 'str''ing'`,
`SELECT * FROM t1 WHERE name IN ('a', 'b', 'c')`,
// Mixed literals
`SELECT * FROM t1 WHERE id = 123 AND name = 'abc'`,
// Complex expressions
`SELECT CASE WHEN id > 100 THEN 'high' ELSE 'low' END`,
// Special characters
`SELECT '\n\r\t\b\f'`,
// Unicode
`SELECT '🙂', '漢字', 'ñ', 'é'`,
// Numbers in identifiers
`SELECT col1 AS alias123`,
// Complex number formats
`SELECT .123, 123., -123.456e-789`,
}
// Quote edge cases for all DBMS
quoteEdgeCases := []string{
// Unmatched quotes (these should be handled gracefully)
`SELECT '`, `SELECT "`, `SELECT [`,
// Multiple types of quotes
`SELECT '"'`, `SELECT "''"`,
`SELECT '''`, `SELECT """`,
// Escaped quotes
`SELECT '\''`, `SELECT "\""`,
// Quotes in comments
`SELECT /* ' */ col1`,
`SELECT -- "`,
`SELECT /* [ */ col1 /* ] */`,
}
// Add Postgres patterns with Postgres DBMS
for _, pattern := range postgresPatterns {
f.Add(pattern, string(DBMSPostgres))
}
// Add SQL Server patterns with SQL Server DBMS
for _, pattern := range sqlServerPatterns {
f.Add(pattern, string(DBMSSQLServer))
}
// Add MySQL patterns with MySQL DBMS
for _, pattern := range mysqlPatterns {
f.Add(pattern, string(DBMSMySQL))
}
// Add Oracle patterns with Oracle DBMS
for _, pattern := range oraclePatterns {
f.Add(pattern, string(DBMSOracle))
}
// Add Snowflake patterns with Snowflake DBMS
for _, pattern := range snowflakePatterns {
f.Add(pattern, string(DBMSSnowflake))
}
// Add common patterns and quote edge cases with all DBMS types
dbmsTypes := []string{
string(DBMSPostgres),
string(DBMSSQLServer),
string(DBMSMySQL),
string(DBMSOracle),
string(DBMSSnowflake),
}
for _, pattern := range append(commonPatterns, quoteEdgeCases...) {
for _, dbms := range dbmsTypes {
f.Add(pattern, dbms)
}
}
}