From 16007e01858cc5d36afdc31d22b5644f91a1f935 Mon Sep 17 00:00:00 2001 From: Andrew Mellen Date: Fri, 15 Sep 2017 18:46:35 -0400 Subject: [PATCH] Sanitize url in get_info() function Change-Id: I540f6d636e277d0ad230f844d2db6220edfb672f Closes-Bug: #1698815 --- jenkins/__init__.py | 1 + tests/jobs/base.py | 10 ++++++++++ tests/jobs/test_getall.py | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/jenkins/__init__.py b/jenkins/__init__.py index 4105c89..77ffd31 100755 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -544,6 +544,7 @@ class Jenkins(object): """ url = '/'.join((item, INFO)).lstrip('/') + url = quote(url) if query: url += query try: diff --git a/tests/jobs/base.py b/tests/jobs/base.py index 0ddf67b..4b8bbfa 100644 --- a/tests/jobs/base.py +++ b/tests/jobs/base.py @@ -39,6 +39,16 @@ class JenkinsGetJobsTestBase(JenkinsJobsTestBase): ] ) + jobs_in_unsafe_name_folders = copy.deepcopy(jobs_in_folder) + jobs_in_unsafe_name_folders[1].insert( + 0, {'name': 'my spaced folder', 'jobs': None}) + jobs_in_unsafe_name_folders.append( + # my_folder1/my\ spaced\ folder jobs + [ + {'name': 'my job 5'} + ] + ) + def build_jobs_list_responses(jobs_list, server_url): responses = [] diff --git a/tests/jobs/test_getall.py b/tests/jobs/test_getall.py index fad2570..bc0f549 100644 --- a/tests/jobs/test_getall.py +++ b/tests/jobs/test_getall.py @@ -59,3 +59,20 @@ class JenkinsGetAllJobsTest(JenkinsGetJobsTestBase): self.assertEqual(len(expected_fullnames), len(jobs_info)) got_fullnames = [job[u"fullname"] for job in jobs_info] self.assertEqual(expected_fullnames, got_fullnames) + + @patch.object(jenkins.Jenkins, 'jenkins_open') + def test_unsafe_chars(self, jenkins_mock): + response = build_jobs_list_responses( + self.jobs_in_unsafe_name_folders, 'http://example.com/') + jenkins_mock.side_effect = iter(response) + + jobs_info = self.j.get_all_jobs() + + expected_fullnames = [ + u"my_job1", u"my_job2", + u"my_folder1/my_job3", u"my_folder1/my_job4", + u"my_folder1/my spaced folder/my job 5" + ] + self.assertEqual(len(expected_fullnames), len(jobs_info)) + got_fullnames = [job[u"fullname"] for job in jobs_info] + self.assertEqual(expected_fullnames, got_fullnames)