Backport of:

From b234a2b67539f787e191d2ef19a7cbdce32874e7 Mon Sep 17 00:00:00 2001
From: Seth Michael Larson <seth@python.org>
Date: Tue, 20 Jan 2026 14:46:32 -0600
Subject: [PATCH] gh-143923: Reject control characters in POP3 commands

---
 Lib/poplib.py                                             | 2 ++
 Lib/test/test_poplib.py                                   | 8 ++++++++
 .../2026-01-16-11-43-47.gh-issue-143923.DuytMe.rst        | 1 +
 3 files changed, 11 insertions(+)
 create mode 100644 Misc/NEWS.d/next/Security/2026-01-16-11-43-47.gh-issue-143923.DuytMe.rst

--- python3.10-3.10.12.orig/Lib/poplib.py
+++ python3.10-3.10.12/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)
 
 
--- python3.10-3.10.12.orig/Lib/test/test_poplib.py
+++ python3.10-3.10.12/Lib/test/test_poplib.py
@@ -15,6 +15,7 @@ from test import support as test_support
 from test.support import hashlib_helper
 from test.support import socket_helper
 from test.support import threading_helper
+from test.support import control_characters_c0
 
 import warnings
 with warnings.catch_warnings():
@@ -365,6 +366,13 @@ 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()
