Skip to content

Commit d9ca98a

Browse files
miss-islingtonMr-Sunglassesericvsmith
authored
[3.12] gh-130637: Add validation for numeric response data in stat() method (GH-130646) (#130764)
gh-130637: Add validation for numeric response data in `stat()` method (GH-130646) (cherry picked from commit a42168d) Co-authored-by: Kanishk Pachauri <[email protected]> Co-authored-by: Eric V. Smith <[email protected]>
1 parent 96e6932 commit d9ca98a

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Lib/poplib.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,19 @@ def stat(self):
226226
retval = self._shortcmd('STAT')
227227
rets = retval.split()
228228
if self._debugging: print('*stat*', repr(rets))
229-
numMessages = int(rets[1])
230-
sizeMessages = int(rets[2])
229+
230+
# Check if the response has enough elements
231+
# RFC 1939 requires at least 3 elements (+OK, message count, mailbox size)
232+
# but allows additional data after the required fields
233+
if len(rets) < 3:
234+
raise error_proto("Invalid STAT response format")
235+
236+
try:
237+
numMessages = int(rets[1])
238+
sizeMessages = int(rets[2])
239+
except ValueError:
240+
raise error_proto("Invalid STAT response data: non-numeric values")
241+
231242
return (numMessages, sizeMessages)
232243

233244

Lib/test/test_poplib.py

+31
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,37 @@ def test_pass_(self):
289289
def test_stat(self):
290290
self.assertEqual(self.client.stat(), (10, 100))
291291

292+
original_shortcmd = self.client._shortcmd
293+
def mock_shortcmd_invalid_format(cmd):
294+
if cmd == 'STAT':
295+
return b'+OK'
296+
return original_shortcmd(cmd)
297+
298+
self.client._shortcmd = mock_shortcmd_invalid_format
299+
with self.assertRaises(poplib.error_proto):
300+
self.client.stat()
301+
302+
def mock_shortcmd_invalid_data(cmd):
303+
if cmd == 'STAT':
304+
return b'+OK abc def'
305+
return original_shortcmd(cmd)
306+
307+
self.client._shortcmd = mock_shortcmd_invalid_data
308+
with self.assertRaises(poplib.error_proto):
309+
self.client.stat()
310+
311+
def mock_shortcmd_extra_fields(cmd):
312+
if cmd == 'STAT':
313+
return b'+OK 1 2 3 4 5'
314+
return original_shortcmd(cmd)
315+
316+
self.client._shortcmd = mock_shortcmd_extra_fields
317+
318+
result = self.client.stat()
319+
self.assertEqual(result, (1, 2))
320+
321+
self.client._shortcmd = original_shortcmd
322+
292323
def test_list(self):
293324
self.assertEqual(self.client.list()[1:],
294325
([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add validation for numeric response data in poplib.POP3.stat() method

0 commit comments

Comments
 (0)