Skip to content

Commit 4aa0cdf

Browse files
committed
Remove interpretation of string beginning with '0x' as hex values
1 parent 8e02ad9 commit 4aa0cdf

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

_mssql.pyx

-3
Original file line numberDiff line numberDiff line change
@@ -1712,9 +1712,6 @@ cdef _quote_simple_value(value, charset='utf8'):
17121712
return b'0x' + binascii.hexlify(bytes(value))
17131713

17141714
if isinstance(value, (str, bytes)):
1715-
if value[0:2] == b'0x':
1716-
return value
1717-
17181715
# see if it can be decoded as ascii if there are no null bytes
17191716
if b'\0' not in value:
17201717
try:

tests/test_queries.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ def createTestTable(cls):
3636
data_binary varbinary(40),
3737
decimal_no decimal(38,2),
3838
numeric_no numeric(38,8),
39-
stamp_time timestamp
39+
stamp_time timestamp,
40+
bin_data varbinary(16)
4041
)""")
4142
cls.tableCreated = True
42-
cls.testTableColCount = 15
43+
cls.testTableColCount = 16
4344
except _mssql.MSSQLDatabaseException as e:
4445
if e.number != 2714:
4546
raise
@@ -64,16 +65,18 @@ def insertSampleData(self):
6465
comment_text,
6566
comment_nvch,
6667
decimal_no,
67-
numeric_no
68+
numeric_no,
69+
bin_data
6870
) VALUES (
6971
%d, %d, %d, getdate(), %d,
7072
'comment %d',
7173
'detail %d',
7274
'hmm',
7375
'bhmme',
7476
234.99,
75-
894123.09
76-
);""" % (y, y, y, (y % 2), y, y)
77+
894123.09,
78+
%#x
79+
);""" % (y, y, y, (y % 2), y, y, y)
7780
self.mssql.execute_non_query(query)
7881

7982
def test01SimpleSelect(self):
@@ -112,3 +115,8 @@ def test19MultipleResults(self):
112115

113116
rows = tuple(self.mssql)
114117
self.assertEquals(rows[0][0], 'ret3')
118+
119+
def test04BinaryTypeSqlInjection(self):
120+
self.mssql.execute_query('SELECT * FROM pymssql WHERE bin_data=%s', ('0x OR 1=1;',))
121+
rows = tuple(self.mssql)
122+
self.assertEqual(len(rows), 0)

tests/test_types.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def test_varchar(self):
9191
typeeq(u'foobar', colval)
9292
self.hasheq(u'foobar', colval)
9393

94+
def test_varchar_hex(self):
95+
testval = '0xf00'
96+
colval = self.insert_and_select('comment_vch', testval, 's')
97+
typeeq(u'0xf00', colval)
98+
self.hasheq(u'0xf00', colval)
99+
94100
def test_varchar_unicode(self):
95101
testval = u'foobär'
96102
colval = self.insert_and_select('comment_vch', testval, 's')
@@ -103,13 +109,6 @@ def test_nvarchar_unicode(self):
103109
typeeq(testval, colval)
104110
eq_(testval, colval)
105111

106-
def test_binary_string(self):
107-
bindata = '{z\n\x03\x07\x194;\x034lE4ISo'.encode('ascii')
108-
testval = '0x'.encode('ascii') + binascii.hexlify(bindata)
109-
colval = self.insert_and_select('data_binary', testval, 's')
110-
typeeq(bindata, colval)
111-
eq_(bindata, colval)
112-
113112
def test_binary_bytearray(self):
114113
bindata = '{z\n\x03\x07\x194;\x034lE4ISo'.encode('ascii')
115114
colval = self.insert_and_select('data_binary', bytearray(bindata), 's')

0 commit comments

Comments
 (0)