From d5b2827401d205e58d55ddb7f72b2a19de285874 Mon Sep 17 00:00:00 2001 From: Vitali Zaidman Date: Mon, 2 Mar 2026 23:26:02 -0800 Subject: [PATCH] Add URL validation check to prevent crash (#55748) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55748 Add defensive check to validate the WebSocket URL before attempting to connect. This prevents potential crashes when an invalid or nil URL is passed to the WebSocket module, which could cause XPC serialization failures deep in the network stack. The crash was observed in during XPC serialization when SocketRocket attempted to open a connection with invalid URL data. Changelog: [Internal] Reviewed By: javache Differential Revision: D94375527 fbshipit-source-id: e82876c075a3daa9fbe6a06dc27d91bd809fecf7 --- .../react-native/React/CoreModules/RCTWebSocketModule.mm | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/react-native/React/CoreModules/RCTWebSocketModule.mm b/packages/react-native/React/CoreModules/RCTWebSocketModule.mm index e29d76a76a2..22fef87badd 100644 --- a/packages/react-native/React/CoreModules/RCTWebSocketModule.mm +++ b/packages/react-native/React/CoreModules/RCTWebSocketModule.mm @@ -10,6 +10,7 @@ #import #import +#import #import #import #import @@ -66,6 +67,12 @@ RCT_EXPORT_METHOD( connect : (NSURL *)URL protocols : (NSArray *)protocols options : (JS::NativeWebSocketModule::SpecConnectOptions &) options socketID : (double)socketID) { + if (URL == nil || URL.absoluteString.length == 0u) { + RCTAssert(NO, @"RCTWebSocketModule: Invalid WebSocket URL passed to connect"); + [self sendEventWithName:@"websocketFailed" body:@{@"message" : @"Invalid WebSocket URL", @"id" : @(socketID)}]; + return; + } + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; // We load cookies from sharedHTTPCookieStorage (shared with XHR and