From 3c4517ffb63d6adfdac5f9f7d51d834a22ac2cd9 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 13 Dec 2023 09:47:52 -0800 Subject: [PATCH] Add safe guard when receive network response data too long (#41717) Summary: Fixes https://github.com/facebook/react-native/issues/41651 ## Changelog: [IOS] [FIXED] - Add safe guard when receive network response data too long Pull Request resolved: https://github.com/facebook/react-native/pull/41717 Test Plan: None. Reviewed By: cortinico Differential Revision: D51981597 Pulled By: dmytrorykun fbshipit-source-id: fabaa1490684da6725270e8ca617e3da97c56978 --- .../Libraries/Network/RCTNetworkTask.mm | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Network/RCTNetworkTask.mm b/packages/react-native/Libraries/Network/RCTNetworkTask.mm index fc2c6dcad9f..47a2e92d0d5 100644 --- a/packages/react-native/Libraries/Network/RCTNetworkTask.mm +++ b/packages/react-native/Libraries/Network/RCTNetworkTask.mm @@ -164,7 +164,21 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init) if (!_data) { _data = [NSMutableData new]; } - [_data appendData:data]; + @try { + [_data appendData:data]; + } @catch (NSException *exception) { + _status = RCTNetworkTaskFinished; + if (_completionBlock) { + RCTURLRequestCompletionBlock completionBlock = _completionBlock; + [self dispatchCallback:^{ + completionBlock( + self->_response, nil, RCTErrorWithMessage(exception.reason ?: @"Request's received data too long.")); + }]; + } + [self invalidate]; + return; + } + length = _data.length; }