From 6155ad04383320bdbdc701ee55c45928752d3a63 Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Thu, 22 Apr 2021 08:14:05 -0700 Subject: [PATCH] iOS: allow disabling packager access at runtime Summary: Allow disabling packager access based on the app runtime environment, e.g. running tests. Changelog: [Internal] Differential Revision: D27903019 fbshipit-source-id: cafa25e93efab3cf8e96d60a8fc03de8abb833f4 --- React/Base/RCTBundleURLProvider.h | 19 +++++++++++-------- React/Base/RCTBundleURLProvider.mm | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.h b/React/Base/RCTBundleURLProvider.h index 6e1dea3a66a..890f8c3819a 100644 --- a/React/Base/RCTBundleURLProvider.h +++ b/React/Base/RCTBundleURLProvider.h @@ -7,16 +7,19 @@ #import -#if defined(__cplusplus) -extern "C" { -#endif +#import "RCTDefines.h" -extern NSString *const RCTBundleURLProviderUpdatedNotification; +RCT_EXTERN NSString *const RCTBundleURLProviderUpdatedNotification; +RCT_EXTERN const NSUInteger kRCTBundleURLProviderDefaultPort; -extern const NSUInteger kRCTBundleURLProviderDefaultPort; - -#if defined(__cplusplus) -} +#if RCT_DEV_MENU +/** + * Allow/disallow accessing the packager server for various runtime scenario. + * For instance, if a test run should never access the packager, disable it + * by calling this function before initializing React Native (RCTBridge etc). + * By default the access is enabled. + */ +RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed); #endif @interface RCTBundleURLProvider : NSObject diff --git a/React/Base/RCTBundleURLProvider.mm b/React/Base/RCTBundleURLProvider.mm index cda955fe9f0..393ca563102 100644 --- a/React/Base/RCTBundleURLProvider.mm +++ b/React/Base/RCTBundleURLProvider.mm @@ -9,11 +9,20 @@ #import "RCTConvert.h" #import "RCTDefines.h" +#import "RCTLog.h" NSString *const RCTBundleURLProviderUpdatedNotification = @"RCTBundleURLProviderUpdatedNotification"; const NSUInteger kRCTBundleURLProviderDefaultPort = RCT_METRO_PORT; +#if RCT_DEV_MENU +static BOOL kRCTAllowPackagerAccess = YES; +void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed) +{ + kRCTAllowPackagerAccess = allowed; +} +#endif + static NSString *const kRCTJsLocationKey = @"RCT_jsLocation"; static NSString *const kRCTEnableDevKey = @"RCT_enableDev"; static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification"; @@ -127,6 +136,12 @@ static NSURL *serverRootWithHostPort(NSString *hostPort) - (NSString *)packagerServerHostPort { +#if RCT_DEV_MENU + if (!kRCTAllowPackagerAccess) { + RCTLogInfo(@"Packager server access is disabled in this environment"); + return nil; + } +#endif NSString *location = [self jsLocation]; #if RCT_DEV_MENU if ([location length] && ![RCTBundleURLProvider isPackagerRunning:location]) {