Skip to content

Commit f94eb0e

Browse files
committed
Always wrap arguments in parentheses in the SQL sanitizer
1 parent 826a892 commit f94eb0e

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

internal/sanitize/sanitize.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,8 @@ func (q *Query) Sanitize(args ...interface{}) (string, error) {
4444
str = "null"
4545
case int64:
4646
str = strconv.FormatInt(arg, 10)
47-
// Prevent SQL injection via Line Comment Creation
48-
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
49-
if arg < 0 {
50-
str = "(" + str + ")"
51-
}
5247
case float64:
53-
// Prevent SQL injection via Line Comment Creation
54-
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
5548
str = strconv.FormatFloat(arg, 'f', -1, 64)
56-
if arg < 0 {
57-
str = "(" + str + ")"
58-
}
5949
case bool:
6050
str = strconv.FormatBool(arg)
6151
case []byte:
@@ -68,6 +58,10 @@ func (q *Query) Sanitize(args ...interface{}) (string, error) {
6858
return "", fmt.Errorf("invalid arg type: %T", arg)
6959
}
7060
argUse[argIdx] = true
61+
62+
// Prevent SQL injection via Line Comment Creation
63+
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
64+
str = "(" + str + ")"
7165
default:
7266
return "", fmt.Errorf("invalid Part type: %T", part)
7367
}

internal/sanitize/sanitize_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -127,52 +127,52 @@ func TestQuerySanitize(t *testing.T) {
127127
{
128128
query: sanitize.Query{Parts: []sanitize.Part{"select 42"}},
129129
args: []interface{}{},
130-
expected: `select 42`,
130+
expected: `select (42)`,
131131
},
132132
{
133133
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
134134
args: []interface{}{int64(42)},
135-
expected: `select 42`,
135+
expected: `select (42)`,
136136
},
137137
{
138138
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
139139
args: []interface{}{float64(1.23)},
140-
expected: `select 1.23`,
140+
expected: `select (1.23)`,
141141
},
142142
{
143143
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
144144
args: []interface{}{true},
145-
expected: `select true`,
145+
expected: `select (true)`,
146146
},
147147
{
148148
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
149149
args: []interface{}{[]byte{0, 1, 2, 3, 255}},
150-
expected: `select '\x00010203ff'`,
150+
expected: `select ('\x00010203ff')`,
151151
},
152152
{
153153
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
154154
args: []interface{}{nil},
155-
expected: `select null`,
155+
expected: `select (null)`,
156156
},
157157
{
158158
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
159159
args: []interface{}{"foobar"},
160-
expected: `select 'foobar'`,
160+
expected: `select ('foobar')`,
161161
},
162162
{
163163
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
164164
args: []interface{}{"foo'bar"},
165-
expected: `select 'foo''bar'`,
165+
expected: `select ('foo''bar')`,
166166
},
167167
{
168168
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
169169
args: []interface{}{`foo\'bar`},
170-
expected: `select 'foo\''bar'`,
170+
expected: `select ('foo\''bar')`,
171171
},
172172
{
173173
query: sanitize.Query{Parts: []sanitize.Part{"insert ", 1}},
174174
args: []interface{}{time.Date(2020, time.March, 1, 23, 59, 59, 999999999, time.UTC)},
175-
expected: `insert '2020-03-01 23:59:59.999999Z'`,
175+
expected: `insert ('2020-03-01 23:59:59.999999Z')`,
176176
},
177177
{
178178
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},

0 commit comments

Comments
 (0)