fix auth with long username or password
Our auth_headers() helper uses base64.encodestring() which inserts newlines to generate a nicely formatted text. That is surely helpful for display purposes but unwanted when forging an authentication string. encodestring() always append a newline, which was the reason for the [:-1] slicing. Switch to base64.b64encoding() and add a test ensuring no newline is inserted. Closes-Bug: 1039307 Change-Id: I71c3e6d504a500a1f21ff89b1d54a155da1aaf0e
This commit is contained in:
		@@ -136,7 +136,7 @@ def auth_headers(username, password):
 | 
			
		||||
    auth = '%s:%s' % (username, password)
 | 
			
		||||
    if isinstance(auth, six.text_type):
 | 
			
		||||
        auth = auth.encode('utf-8')
 | 
			
		||||
    return b'Basic ' + base64.encodestring(auth)[:-1]
 | 
			
		||||
    return b'Basic ' + base64.b64encode(auth)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Jenkins(object):
 | 
			
		||||
 
 | 
			
		||||
@@ -67,6 +67,16 @@ class JenkinsTest(unittest.TestCase):
 | 
			
		||||
        self.assertEqual(j.auth, b'Basic bm9uYXNjaWk6w6nigqw=')
 | 
			
		||||
        self.assertEqual(j.crumb, None)
 | 
			
		||||
 | 
			
		||||
    def test_constructor_long_user_or_password(self):
 | 
			
		||||
        long_str = 'a' * 60
 | 
			
		||||
        long_str_b64 = 'YWFh' * 20
 | 
			
		||||
 | 
			
		||||
        j = jenkins.Jenkins('http://example.com', long_str, long_str)
 | 
			
		||||
 | 
			
		||||
        self.assertNotIn(b"\n", j.auth)
 | 
			
		||||
        self.assertEqual(j.auth.decode(), 'Basic %s' % (
 | 
			
		||||
            long_str_b64 + 'Om' + long_str_b64[2:] + 'YQ=='))
 | 
			
		||||
 | 
			
		||||
    @patch.object(jenkins.Jenkins, 'jenkins_open')
 | 
			
		||||
    def test_get_job_config_encodes_job_name(self, jenkins_mock):
 | 
			
		||||
        j = jenkins.Jenkins('http://example.com/', 'test', 'test')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user