From e6870c68f9f55a1cd2104c20f848b7249ef099c4 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 1 May 2025 21:46:36 -0700 Subject: [PATCH] Limit request body size for Network.requestWillBeSent (#51026) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51026 Performance optimisation — truncate request body content (sent as `Request.postData` over CDP) at 1MB. Changelog: [Internal] Reviewed By: vzaidman Differential Revision: D71905868 fbshipit-source-id: ba82b4937902e057d4694bf4f000eb77ee37aa8a --- .../Network/RCTInspectorNetworkReporter.mm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm index a3015eb0128..143acfb5c1c 100644 --- a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm +++ b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm @@ -23,6 +23,22 @@ Headers convertNSDictionaryToHeaders(const NSDictionary return responseHeaders; } +std::string convertRequestBodyToStringTruncated(NSURLRequest *request) +{ + const NSUInteger maxBodySize = 1024 * 1024; // 1MB + auto bodyLength = request.HTTPBody.length; + auto bytesToRead = std::min(bodyLength, maxBodySize); + + auto body = std::string((const char *)request.HTTPBody.bytes, bytesToRead); + + if (bytesToRead < bodyLength) { + body += + "\n... [truncated, showing " + std::to_string(bytesToRead) + " of " + std::to_string(bodyLength) + " bytes]"; + } + + return body; +} + } // namespace #endif @@ -39,7 +55,7 @@ Headers convertNSDictionaryToHeaders(const NSDictionary #ifdef REACT_NATIVE_DEBUGGER_ENABLED // Debug build: Process additional request info for CDP reporting requestInfo.headers = convertNSDictionaryToHeaders(request.allHTTPHeaderFields); - requestInfo.httpBody = std::string((const char *)request.HTTPBody.bytes, request.HTTPBody.length); + requestInfo.httpBody = convertRequestBodyToStringTruncated(request); #endif NetworkReporter::getInstance().reportRequestStart(