Skip to content

Commit 826a892

Browse files
committed
Fix SQL injection via line comment creation in simple protocol
1 parent 7d882f9 commit 826a892

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

internal/sanitize/sanitize.go

+10
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,18 @@ 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+
}
4752
case float64:
53+
// Prevent SQL injection via Line Comment Creation
54+
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
4855
str = strconv.FormatFloat(arg, 'f', -1, 64)
56+
if arg < 0 {
57+
str = "(" + str + ")"
58+
}
4959
case bool:
5060
str = strconv.FormatBool(arg)
5161
case []byte:

internal/sanitize/sanitize_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ func TestQuerySanitize(t *testing.T) {
174174
args: []interface{}{time.Date(2020, time.March, 1, 23, 59, 59, 999999999, time.UTC)},
175175
expected: `insert '2020-03-01 23:59:59.999999Z'`,
176176
},
177+
{
178+
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},
179+
args: []interface{}{int64(-1)},
180+
expected: `select 1-(-1)`,
181+
},
182+
{
183+
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},
184+
args: []interface{}{float64(-1)},
185+
expected: `select 1-(-1)`,
186+
},
177187
}
178188

179189
for i, tt := range successfulTests {

0 commit comments

Comments
 (0)