From 14cb30d969637823f0c643893e74bd5a1517924f Mon Sep 17 00:00:00 2001
From: Seth Michael Larson <seth@python.org>
Date: Tue, 10 Feb 2026 18:02:24 +0800
Subject: [PATCH] gh-143923: Reject control characters in POP3 commands

---
 Lib/poplib.py           | 2 ++
 Lib/test/test_poplib.py | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/Lib/poplib.py b/Lib/poplib.py
index 0f85873..f563030 100644
--- a/Lib/poplib.py
+++ b/Lib/poplib.py
@@ -122,6 +122,8 @@ class POP3:
     def _putcmd(self, line):
         if self._debugging: print('*cmd*', repr(line))
         line = bytes(line, self.encoding)
+        if re.search(b'[\x00-\x1F\x7F]', line):
+            raise ValueError('Control characters not allowed in commands')
         self._putline(line)
 
 
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
index 5ad9202..8feeab0 100644
--- a/Lib/test/test_poplib.py
+++ b/Lib/test/test_poplib.py
@@ -16,6 +16,7 @@ from test.support import hashlib_helper
 from test.support import socket_helper
 from test.support import threading_helper
 from test.support import warnings_helper
+from test.support import control_characters_c0
 
 
 asynchat = warnings_helper.import_deprecated('asynchat')
@@ -367,6 +368,12 @@ class TestPOP3Class(TestCase):
         self.assertIsNone(self.client.sock)
         self.assertIsNone(self.client.file)
 
+    def test_control_characters(self):
+        for c0 in control_characters_c0():
+            with self.assertRaises(ValueError):
+                self.client.user(f'user{c0}')
+            with self.assertRaises(ValueError):
+                self.client.pass_(f'{c0}pass')
     @requires_ssl
     def test_stls_capa(self):
         capa = self.client.capa()
-- 
2.43.0