Skip to content

Commit e196a39

Browse files
jackcclaude
andcommitted
Add fuzz test for SQL lexer in sanitize package
The existing fuzz tests only covered QuoteString and QuoteBytes. Add FuzzNewQuery to exercise the SQL lexer (comments, quotes, escape strings, placeholders) and Sanitize with arbitrary input. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 7f969f8 commit e196a39

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

‎internal/sanitize/sanitize_fuzz_test.go‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,100 @@ import (
77
"github.com/jackc/pgx/v5/internal/sanitize"
88
)
99

10+
func FuzzNewQuery(f *testing.F) {
11+
// Basic placeholders
12+
f.Add("select $1")
13+
f.Add("select $1, $2, $3")
14+
f.Add("select $1 from t where a = $2 and b = $1")
15+
f.Add("$1")
16+
f.Add("$0")
17+
f.Add("$$")
18+
f.Add("$")
19+
f.Add("$abc")
20+
21+
// Single-quoted strings with placeholders inside (should be ignored)
22+
f.Add("select 'quoted $42', $1")
23+
f.Add("select 'foo''bar', $1")
24+
f.Add("select '''', $1")
25+
f.Add("'unterminated")
26+
f.Add("'ends with backslash\\")
27+
28+
// Double-quoted identifiers
29+
f.Add(`select "doubled quoted $42", $1`)
30+
f.Add(`select "foo""bar", $1`)
31+
f.Add(`select """", $1`)
32+
f.Add(`"unterminated`)
33+
34+
// Escape strings (E'...')
35+
f.Add("select E'escape \\'string', $1")
36+
f.Add("select e'lower case escape \\' $1', $2")
37+
f.Add("E'unterminated")
38+
f.Add("E'backslash at end\\")
39+
f.Add("E''")
40+
f.Add("E'\\''")
41+
42+
// Line comments
43+
f.Add("select $1 -- line comment")
44+
f.Add("select $1 -- comment with $2\nand $2")
45+
f.Add("--entire line is comment $1")
46+
f.Add("-- unterminated comment")
47+
f.Add("select $1--$2")
48+
49+
// Block comments including nesting
50+
f.Add("select $1 /* block comment */")
51+
f.Add("select /* nested /* comment */ */ $1")
52+
f.Add("/* unterminated")
53+
f.Add("/* also /* nested /* unterminated */")
54+
f.Add("select $1 /* comment with $2 */ and $2")
55+
f.Add("/**/")
56+
57+
// Edge cases: empty, whitespace, unicode
58+
f.Add("")
59+
f.Add(" ")
60+
f.Add("\n\r\t")
61+
f.Add("select '日本語', $1")
62+
f.Add("select $1 -- コメント")
63+
f.Add("\xc0\xc0") // invalid UTF-8
64+
f.Add("select \xff $1") // invalid UTF-8 mid-query
65+
f.Add("select '\xef\xbf\xbd' $1") // U+FFFD replacement character (width 3)
66+
67+
// SQL injection patterns the lexer must handle
68+
f.Add("select $1; drop table users--")
69+
f.Add("select $1 union select $2")
70+
f.Add("select $1 where '1'='1'")
71+
72+
// Transitions between states
73+
f.Add("E'esc' 'norm' \"ident\" $1 -- comment\n$2 /* block */ $3")
74+
f.Add("$1'$2'$3\"$4\"$5--$6\n$7/*$8*/$9")
75+
76+
f.Fuzz(func(t *testing.T, input string) {
77+
query, err := sanitize.NewQuery(input)
78+
if err != nil {
79+
return
80+
}
81+
82+
// Count the number of placeholder args the query expects.
83+
maxArg := 0
84+
for _, part := range query.Parts {
85+
if n, ok := part.(int); ok && n > maxArg {
86+
maxArg = n
87+
}
88+
}
89+
90+
// Skip sanitization for unreasonably large placeholder numbers to avoid OOM in the test itself.
91+
if maxArg > 65536 {
92+
return
93+
}
94+
95+
// Build args list and sanitize. Should not panic.
96+
args := make([]any, maxArg)
97+
for i := range args {
98+
args[i] = int64(i + 1)
99+
}
100+
_, _ = query.Sanitize(args...)
101+
})
102+
}
103+
10104
func FuzzQuoteString(f *testing.F) {
11105
const prefix = "prefix"
12106
f.Add("new\nline")

0 commit comments

Comments
 (0)