Skip to content

Commit 49b6aad

Browse files
committedMar 9, 2024·
Use spaces instead of parentheses for SQL sanitization
This still solves the problem of negative numbers creating a line comment, but this avoids breaking edge cases such as `set foo to $1` where the substition is taking place in a location where an arbitrary expression is not allowed. #1928
1 parent 0cc4c14 commit 49b6aad

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed
 

‎internal/sanitize/sanitize.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (q *Query) Sanitize(args ...any) (string, error) {
6666

6767
// Prevent SQL injection via Line Comment Creation
6868
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
69-
str = "(" + str + ")"
69+
str = " " + str + " "
7070
default:
7171
return "", fmt.Errorf("invalid Part type: %T", part)
7272
}

‎internal/sanitize/sanitize_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -132,57 +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' `,
176176
},
177177
{
178178
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},
179179
args: []any{int64(-1)},
180-
expected: `select 1-(-1)`,
180+
expected: `select 1- -1 `,
181181
},
182182
{
183183
query: sanitize.Query{Parts: []sanitize.Part{"select 1-", 1}},
184184
args: []any{float64(-1)},
185-
expected: `select 1-(-1)`,
185+
expected: `select 1- -1 `,
186186
},
187187
}
188188

0 commit comments

Comments
 (0)