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
This commit is contained in:
Vitali Zaidman authored and meta-codesync[bot] committed 2026-03-02 23:26:02 -08:00
1 parent 9450489a44
commit d5b2827401
1 file changed
+7
@@ -10,6 +10,7 @@
#import <objc/runtime.h>
#import <FBReactNativeSpec/FBReactNativeSpec.h>
#import <React/RCTAssert.h>
#import <React/RCTConvert.h>
#import <React/RCTUtils.h>
#import <SocketRocket/SRWebSocket.h>
@@ -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