Merge lp:~mvo/apt-clone/lp1309447 into lp:apt-clone

Proposed by Michael Vogt
Status: Merged
Merged at revision: 148
Proposed branch: lp:~mvo/apt-clone/lp1309447
Merge into: lp:apt-clone
Diff against target: 113 lines (+59/-5)
4 files modified
apt-clone (+1/-0)
apt_clone.py (+5/-4)
tests/test_clone.py (+2/-1)
tests/test_lp1309447.py (+51/-0)
To merge this branch: bzr merge lp:~mvo/apt-clone/lp1309447
Reviewer Review Type Date Requested Status
Iain Lane Approve
Barry Warsaw Pending
Review via email: mp+222322@code.launchpad.net

Description of the change

This fixes the apt-clone part of bug #1309447.

To post a comment you must log in.
Revision history for this message
Iain Lane (laney) wrote :

This looks good to me, will merge since Barry has been AWOL for months. :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'apt-clone'
2--- apt-clone 2014-03-19 07:27:54 +0000
3+++ apt-clone 2014-06-06 12:27:01 +0000
4@@ -22,6 +22,7 @@
5 import argparse
6 from apt_clone import AptClone
7
8+
9 if __name__ == "__main__":
10
11 # command line parser
12
13=== modified file 'apt_clone.py'
14--- apt_clone.py 2014-03-12 15:34:43 +0000
15+++ apt_clone.py 2014-06-06 12:27:01 +0000
16@@ -247,8 +247,11 @@
17
18 def _add_file_to_tar_with_password_check(self, tar, sources, scrub, arcname):
19 if scrub:
20- with tempfile.NamedTemporaryFile(mode='wb') as source_copy, open(sources, 'r') as f:
21+ with tempfile.NamedTemporaryFile(mode='wb') as source_copy, open(sources, 'rb') as f:
22 for line in f.readlines():
23+ # compat with both py2/py3
24+ if type(line) is bytes:
25+ line = line.decode("UTF-8")
26 if re.search('/[^/@:]*:[^/@:]*@', line):
27 line = re.sub('/[^/@:]*:[^/@:]*@',
28 '/USERNAME:PASSWORD@', line)
29@@ -256,9 +259,7 @@
30 # open in Unicode mode in Python 2. We can remove this
31 # once ubuntu-release-upgrader is run under Python 3
32 # (i.e. after Ubuntu 14.04).
33- if line is not bytes:
34- line = line.encode("UTF-8")
35- source_copy.write(line)
36+ source_copy.write(line.encode("utf-8"))
37 source_copy.flush()
38 tar.add(source_copy.name, arcname=arcname)
39 else:
40
41=== modified file 'tests/test_clone.py'
42--- tests/test_clone.py 2013-07-26 00:59:55 +0000
43+++ tests/test_clone.py 2014-06-06 12:27:01 +0000
44@@ -1,5 +1,5 @@
45 #!/usr/bin/python3
46-
47+# -*- coding: utf-8 -*-
48 from __future__ import print_function
49
50 import apt
51@@ -195,5 +195,6 @@
52 self.assertFalse("/etc/issue" in unowned)
53 #print("\n".join(sorted(unowned)))
54
55+
56 if __name__ == "__main__":
57 unittest.main()
58
59=== added file 'tests/test_lp1309447.py'
60--- tests/test_lp1309447.py 1970-01-01 00:00:00 +0000
61+++ tests/test_lp1309447.py 2014-06-06 12:27:01 +0000
62@@ -0,0 +1,51 @@
63+#!/usr/bin/python3
64+# -*- coding: utf-8 -*-
65+
66+from __future__ import print_function
67+
68+import os
69+import sys
70+import unittest
71+
72+# this is important
73+os.environ["LANG"] = "C"
74+
75+
76+sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
77+from apt_clone import AptClone
78+
79+
80+class MockTar(object):
81+ def add(self, source, arcname):
82+ with open(source, "rb") as f:
83+ self.data = f.read().decode("utf-8")
84+
85+
86+class TestClone(unittest.TestCase):
87+
88+ def setUp(self):
89+ self.apt_clone = AptClone()
90+ self.test_sources_fname = "test-sources.list"
91+ with open(self.test_sources_fname, "wb") as f:
92+ f.write(u"""# äüö
93+deb http://mvo:secret@archive.u.c/ ubuntu main
94+""".encode("utf-8"))
95+
96+ def tearDown(self):
97+ os.unlink(self.test_sources_fname)
98+
99+ def test_scrub_file_from_passwords(self):
100+ """Regression test for utf8 crash LP: #1309447"""
101+ mock_tar = MockTar()
102+ self.apt_clone._add_file_to_tar_with_password_check(
103+ mock_tar, self.test_sources_fname, scrub=True,
104+ arcname="some-archname")
105+ # see if we got the expected data
106+ self.assertNotIn("mvo:secret", mock_tar.data)
107+ self.assertEqual(mock_tar.data, u"""# äüö
108+deb http://USERNAME:PASSWORD@archive.u.c/ ubuntu main
109+""")
110+
111+
112+if __name__ == "__main__":
113+ unittest.main()

Subscribers

People subscribed via source and target branches

to all changes: