Skip to content

Commit c543134

Browse files
committed
SQL sanitizer wraps arguments in parentheses
pgx v5 was not vulnerable to CVE-2024-27289 do to how the sanitizer was being called. But the sanitizer itself still had the underlying issue. This commit ports the fix from pgx v4 to v5 to ensure that the issue does not emerge if pgx uses the sanitizer differently in the future.
1 parent 20344df commit c543134

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

internal/sanitize/sanitize.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func (q *Query) Sanitize(args ...any) (string, error) {
6363
return "", fmt.Errorf("invalid arg type: %T", arg)
6464
}
6565
argUse[argIdx] = true
66+
67+
// Prevent SQL injection via Line Comment Creation
68+
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
69+
str = "(" + str + ")"
6670
default:
6771
return "", fmt.Errorf("invalid Part type: %T", part)
6872
}

internal/sanitize/sanitize_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,47 +132,57 @@ func TestQuerySanitize(t *testing.T) {
132132
{
133133
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
134134
args: []any{int64(42)},
135-
expected: `select 42`,
135+
expected: `select (42)`,
136136
},
137137
{
138138
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
139139
args: []any{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: []any{true},
145-
expected: `select true`,
145+
expected: `select (true)`,
146146
},
147147
{
148148
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
149149
args: []any{[]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: []any{nil},
155-
expected: `select null`,
155+
expected: `select (null)`,
156156
},
157157
{
158158
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
159159
args: []any{"foobar"},
160-
expected: `select 'foobar'`,
160+
expected: `select ('foobar')`,
161161
},
162162
{
163163
query: sanitize.Query{Parts: []sanitize.Part{"select ", 1}},
164164
args: []any{"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: []any{`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: []any{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')`,
176+
},
177+
{
178+
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},
179+
args: []any{int64(-1)},
180+
expected: `select 1-(-1)`,
181+
},
182+
{
183+
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},
184+
args: []any{float64(-1)},
185+
expected: `select 1-(-1)`,
176186
},
177187
}
178188

0 commit comments

Comments
 (0)