]> granicus.if.org Git - python/commitdiff
Bug 3228: Explicitly supply the file mode to avoid creating executable files,
authorAndrew M. Kuchling <amk@amk.ca>
Mon, 4 Aug 2008 01:43:43 +0000 (01:43 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Mon, 4 Aug 2008 01:43:43 +0000 (01:43 +0000)
and add corresponding tests.
Possible 2.5 backport candidate

Lib/mailbox.py
Lib/test/test_mailbox.py

index e3e9cbb3422707d5f313634b8393182c0d666828..7651e53b7f2c15e0ece7f159fb0b3c292788ed2d 100755 (executable)
@@ -398,7 +398,8 @@ class Maildir(Mailbox):
         result = Maildir(path, factory=self._factory)
         maildirfolder_path = os.path.join(path, 'maildirfolder')
         if not os.path.exists(maildirfolder_path):
-            os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY))
+            os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY,
+                0666))
         return result
 
     def remove_folder(self, folder):
@@ -1900,7 +1901,7 @@ def _unlock_file(f):
 
 def _create_carefully(path):
     """Create a file if it doesn't exist and open for reading and writing."""
-    fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
+    fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0666)
     try:
         return open(path, 'rb+')
     finally:
index 7a1225876b22fe01e9a8eb326ad328642ba6c8bd..a6494dcbeabf55b14cf6af8006fd598c74c8916b 100644 (file)
@@ -716,6 +716,16 @@ class TestMaildir(TestMailbox):
         for msg in self._box:
             pass
 
+    def test_file_perms(self):
+        # From bug #3228, we want to verify that the file created inside a Maildir
+        # subfolder isn't marked as executable.
+        subfolder = self._box.add_folder('subfolder')
+        path = os.path.join(subfolder._path, 'maildirfolder')
+        st = os.stat(path)
+        perms = st.st_mode
+        self.assertFalse((perms & 0111)) # Execute bits should all be off.
+
+
 class _TestMboxMMDF(TestMailbox):
 
     def tearDown(self):
@@ -805,11 +815,27 @@ class _TestMboxMMDF(TestMailbox):
         self._box.close()
 
 
-
 class TestMbox(_TestMboxMMDF):
 
     _factory = lambda self, path, factory=None: mailbox.mbox(path, factory)
 
+    def test_file_perms(self):
+        # From bug #3228, we want to verify that the mailbox file isn't executable,
+        # even if the umask is set to something that would leave executable bits set.
+        # We only run this test on platforms that support umask.
+        if hasattr(os, 'umask'):
+            try:
+                old_umask = os.umask(0077)
+                self._box.close()
+                os.unlink(self._path)
+                self._box = mailbox.mbox(self._path, create=True)
+                self._box.add('')
+                self._box.close()
+                st = os.stat(self._path)
+                perms = st.st_mode
+                self.assertFalse((perms & 0111)) # Execute bits should all be off.
+            finally:
+                os.umask(old_umask)
 
 class TestMMDF(_TestMboxMMDF):