]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130637: Add validation for numeric response data in `stat()` method (#130646)
authorKanishk Pachauri <itskanishkp.py@gmail.com>
Sun, 2 Mar 2025 13:05:40 +0000 (18:35 +0530)
committerGitHub <noreply@github.com>
Sun, 2 Mar 2025 13:05:40 +0000 (08:05 -0500)
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
Lib/poplib.py
Lib/test/test_poplib.py
Misc/NEWS.d/next/Library/2025-03-01-02-19-28.gh-issue-130637.swet54w4rs.rst [new file with mode: 0644]

index beb93a0d57cf933dcf2dfe7afad54bef63a524f4..4469bff44b4c45593e1a026c0f6e9cd9c094f76a 100644 (file)
@@ -226,8 +226,19 @@ class POP3:
         retval = self._shortcmd('STAT')
         rets = retval.split()
         if self._debugging: print('*stat*', repr(rets))
-        numMessages = int(rets[1])
-        sizeMessages = int(rets[2])
+
+        # Check if the response has enough elements
+        # RFC 1939 requires at least 3 elements (+OK, message count, mailbox size)
+        # but allows additional data after the required fields
+        if len(rets) < 3:
+            raise error_proto("Invalid STAT response format")
+
+        try:
+            numMessages = int(rets[1])
+            sizeMessages = int(rets[2])
+        except ValueError:
+            raise error_proto("Invalid STAT response data: non-numeric values")
+
         return (numMessages, sizeMessages)
 
 
index 869f9431b928bb6bdc16e74f8700bc59a42836ee..f1ebbeafe0cfb4d2299409b7b730e8a92c0e388b 100644 (file)
@@ -289,6 +289,37 @@ class TestPOP3Class(TestCase):
     def test_stat(self):
         self.assertEqual(self.client.stat(), (10, 100))
 
+        original_shortcmd = self.client._shortcmd
+        def mock_shortcmd_invalid_format(cmd):
+            if cmd == 'STAT':
+                return b'+OK'
+            return original_shortcmd(cmd)
+
+        self.client._shortcmd = mock_shortcmd_invalid_format
+        with self.assertRaises(poplib.error_proto):
+            self.client.stat()
+
+        def mock_shortcmd_invalid_data(cmd):
+            if cmd == 'STAT':
+                return b'+OK abc def'
+            return original_shortcmd(cmd)
+
+        self.client._shortcmd = mock_shortcmd_invalid_data
+        with self.assertRaises(poplib.error_proto):
+            self.client.stat()
+
+        def mock_shortcmd_extra_fields(cmd):
+            if cmd == 'STAT':
+                return b'+OK 1 2 3 4 5'
+            return original_shortcmd(cmd)
+
+        self.client._shortcmd = mock_shortcmd_extra_fields
+
+        result = self.client.stat()
+        self.assertEqual(result, (1, 2))
+
+        self.client._shortcmd = original_shortcmd
+
     def test_list(self):
         self.assertEqual(self.client.list()[1:],
                          ([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'],
diff --git a/Misc/NEWS.d/next/Library/2025-03-01-02-19-28.gh-issue-130637.swet54w4rs.rst b/Misc/NEWS.d/next/Library/2025-03-01-02-19-28.gh-issue-130637.swet54w4rs.rst
new file mode 100644 (file)
index 0000000..83cd6c6
--- /dev/null
@@ -0,0 +1 @@
+Add validation for numeric response data in poplib.POP3.stat() method