Skip to content

Commit 5f4cac6

Browse files
peeyushguptaGeert Vanderkelen
authored andcommitted
BUG21492428: Fix using passwords with leading/trailing whitespaces
Connector/Python was stripping starting and trailing spaces in passwords before sending to the server causing access denied error. We fix this by removing the password.strip() statement. A test case has been added for BUG#21492428. (cherry picked from commit edaa11b)
1 parent 9e55fb9 commit 5f4cac6

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

lib/mysql/connector/abstracts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def set_login(self, username=None, password=None):
484484
else:
485485
self._user = ''
486486
if password is not None:
487-
self._password = password.strip()
487+
self._password = password
488488
else:
489489
self._password = ''
490490

tests/test_bugs.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_fetchall(self):
164164
self.cnx.close()
165165

166166
@unittest.skipIf(tests.MYSQL_VERSION >= (5, 6, 6),
167-
"Bug380528 not tested with MSQL version >= 5.6.6")
167+
"Bug380528 not tested with MySQL version >= 5.6.6")
168168
class Bug380528(tests.MySQLConnectorTests):
169169

170170
"""lp:380528: we do not support old passwords"""
@@ -3669,6 +3669,7 @@ def sleepy_select(cnx):
36693669
"1317 (70100): Query execution was interrupted")
36703670
self.cnx.close()
36713671

3672+
36723673
class BugOra21420633(tests.MySQLConnectorTests):
36733674
"""BUG#21420633: CEXTENSION CRASHES WHILE FETCHING LOTS OF NULL VALUES
36743675
"""
@@ -3709,3 +3710,48 @@ def test_null(self):
37093710

37103711
res = cur.fetchall()
37113712
cur.close()
3713+
3714+
3715+
class BugOra21492428(tests.MySQLConnectorTests):
3716+
"""BUG#21492428: CONNECT FAILS WHEN PASSWORD STARTS OR ENDS WITH SPACES
3717+
"""
3718+
3719+
def setUp(self):
3720+
config = tests.get_mysql_config()
3721+
self.cnx = connection.MySQLConnection(**config)
3722+
self.cursor = self.cnx.cursor()
3723+
3724+
if config['unix_socket'] and os.name != 'nt':
3725+
self.host = 'localhost'
3726+
else:
3727+
self.host = config['host']
3728+
3729+
grant = u"CREATE USER '{user}'@'{host}' IDENTIFIED BY '{password}'"
3730+
3731+
self._credentials = [
3732+
('ABCD', ' XYZ'),
3733+
(' PQRS', ' 1 2 3 '),
3734+
('XYZ1 ', 'XYZ123 '),
3735+
(' A B C D ', ' ppppp '),
3736+
]
3737+
for user, password in self._credentials:
3738+
self.cursor.execute(grant.format(
3739+
user=user, host=self.host, password=password))
3740+
3741+
def tearDown(self):
3742+
for user, password in self._credentials:
3743+
self.cursor.execute(u"DROP USER '{user}'@'{host}'".format(
3744+
user=user, host=self.host))
3745+
3746+
def test_password_with_spaces(self):
3747+
config = tests.get_mysql_config()
3748+
for user, password in self._credentials:
3749+
config['user'] = user
3750+
config['password'] = password
3751+
config['database'] = None
3752+
try:
3753+
cnx = connection.MySQLConnection(**config)
3754+
except errors.ProgrammingError:
3755+
self.fail('Failed using password with spaces')
3756+
else:
3757+
cnx.close()

tests/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ def test_set_login(self):
11721172
exp = ('Ham ', ' Spam ')
11731173
self.cnx.set_login(*exp)
11741174
self.assertEqual(exp[0].strip(), self.cnx._user)
1175-
self.assertEqual(exp[1].strip(), self.cnx._password)
1175+
self.assertEqual(exp[1], self.cnx._password)
11761176

11771177
self.cnx.set_login()
11781178
self.assertEqual('', self.cnx._user)

0 commit comments

Comments
 (0)