From 6bb5b20670012802b35b50f972b61b08e4965635 Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:51:32 -0500 Subject: [PATCH] remote(server): fix crash when invalid port is specified Fixes #6584 --- Platform/macOS/SettingsView.swift | 5 ++++- Remote/UTMRemoteServer.swift | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Platform/macOS/SettingsView.swift b/Platform/macOS/SettingsView.swift index f97779d4..9225a95e 100644 --- a/Platform/macOS/SettingsView.swift +++ b/Platform/macOS/SettingsView.swift @@ -223,9 +223,12 @@ struct ServerSettingsView: View { .multilineTextAlignment(.trailing) .help("Specify a port number to listen on. This is required if external clients are permitted.") .onChange(of: serverPort) { newValue in - if serverPort == 0 { + if newValue == 0 { isServerExternal = false } + if newValue < 0 || newValue >= UInt16.max { + serverPort = defaultPort + } } } Section(header: Text("Authentication")) { diff --git a/Remote/UTMRemoteServer.swift b/Remote/UTMRemoteServer.swift index b9fea5f7..0dd01716 100644 --- a/Remote/UTMRemoteServer.swift +++ b/Remote/UTMRemoteServer.swift @@ -131,7 +131,7 @@ actor UTMRemoteServer { registerNotifications() listener = Task { await withErrorNotification { - if isServerExternal && serverPort > 0 { + if isServerExternal && serverPort > 0 && serverPort <= UInt16.max { natPort = Port.TCP(internalPort: UInt16(serverPort)) natPort!.mappingChangedHandler = { port in Task { @@ -146,7 +146,7 @@ actor UTMRemoteServer { } } } - let port = serverPort > 0 ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any + let port = serverPort > 0 && serverPort <= UInt16.max ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any for try await connection in Connection.advertise(on: port, forServiceType: service, txtRecord: metadata, connectionQueue: connectionQueue, identity: keyManager.identity) { let connection = try? await Connection(connection: connection, connectionQueue: connectionQueue) { connection, error in Task {