Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

email_mirror: Give clear error for IPv6 address passed without port.

Mateusz Manderac341599a328f
diff --git a/zerver/lib/email_mirror_server.py b/zerver/lib/email_mirror_server.py
index afcf8a40ac6..28014c94d23 100644
--- a/zerver/lib/email_mirror_server.py
+++ b/zerver/lib/email_mirror_server.py
@@ -154,13 +154,25 @@ class ZulipMessageHandler(MessageHandler):
def parse_listen_address(listen: str) -> tuple[str, int]:
+ host: str | None
+ port: int | None
if listen.isdigit():
host, port = None, int(listen)
else:
r = SplitResult("", listen, "", "", "")
- if r.port is None:
+ try:
+ port = r.port
+ except ValueError as e:
+ # An unbracketed IPv6 address is ambiguous with
+ # address:port, and parses as a host and a non-numeric
+ # port.
+ raise RuntimeError(
+ f"{listen!r} is not a valid port, or address:port; IPv6 addresses "
+ "must be enclosed in square brackets."
+ ) from e
+ if port is None:
raise RuntimeError(f"{listen!r} does not have a valid port number.")
- host, port = r.hostname, r.port
+ host = r.hostname
if host is None:
# With no address specified, bind the IPv6 wildcard address if
# IPv6 is available, since (with IPV6_V6ONLY disabled, as
diff --git a/zerver/management/commands/email_server.py b/zerver/management/commands/email_server.py
index 20375b841af..940543b770f 100644
--- a/zerver/management/commands/email_server.py
+++ b/zerver/management/commands/email_server.py
@@ -14,7 +14,10 @@ class Command(BaseCommand):
@override
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument(
- "--listen", help="[Port, or address:port, to bind the SMTP server to]", default="25"
+ "--listen",
+ help="[Port, or address:port, to bind the SMTP server to; "
+ "enclose IPv6 addresses in square brackets]",
+ default="25",
)
parser.add_argument(
"--user",
diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.py
index cbc10564d24..46b81c43e0e 100644
--- a/zerver/tests/test_email_mirror.py
+++ b/zerver/tests/test_email_mirror.py
@@ -2188,6 +2188,15 @@ class TestEmailMirrorServer(ZulipTestCase):
RuntimeError, "'mail.example.com' does not have a valid port number."
):
parse_listen_address("mail.example.com")
+ with self.assertRaisesRegex(RuntimeError, r"'\[::\]' does not have a valid port number."):
+ parse_listen_address("[::]")
+ for unbracketed in ("::", "::1", "2001:db8::1"):
+ with self.assertRaisesRegex(
+ RuntimeError, "IPv6 addresses must be enclosed in square brackets"
+ ):
+ parse_listen_address(unbracketed)
+ with self.assertRaisesRegex(RuntimeError, "is not a valid port, or address:port"):
+ parse_listen_address(f"{ipv4_wildcard}:99999")
def test_send_postmaster(self) -> None:
email = EmailMessage()