Skip to content

[iOS] DevTools Network cuts response bodies that contain non-ASCII text (RCTStringViewFromNSString uses UTF-16 length) #60443

Done

Description

@mustafaabobakr

Description

In iOS Debug builds, React Native DevTools → Network stores a text response body cut short whenever it contains non-ASCII characters (Arabic, accented Latin, CJK, emoji). Response stops in the middle of the text, and Preview, which can't parse the cut JSON, falls back to numbered plain text instead of the JSON tree. The app itself receives the whole body; only what DevTools stores is cut.

Cause: RCTStringViewFromNSString in Libraries/Network/RCTNetworkConversions.h builds a std::string_view over the UTF-8 bytes but sizes it with NSString.length, which counts UTF-16 units:

inline std::string_view RCTStringViewFromNSString(NSString *string)
{
  return std::string_view{string.UTF8String, string.length};
}

RCTInspectorNetworkReporter passes every text body through it (maybeStoreResponseBody:data:base64Encoded: and the incremental buffer flushed in reportResponseEnd:encodedDataLength:). So DevTools loses (UTF-8 bytes − UTF-16 units) bytes from the end of each body: 1 per Arabic or accented letter, 2 per CJK character or emoji. Android isn't affected: JInspectorNetworkReporter converts the body with toStdString(), which keeps every byte.

Suggested fix: size the view in UTF-8 bytes:

return std::string_view{string.UTF8String, [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]};

This is the same root cause as #57061 (reported on 0.85.3 as a SIGABRT and closed for lack of a reproducer). The line is unchanged in 0.88.1 and on main. A reproducer is linked below.

Steps to reproduce

Reproducer: https://github.com/mustafaabobakr/rn-devtools-utf8-body-repro (a plain @react-native-community/cli init app on 0.88.1; only App.tsx and data/ are changed).

  1. yarn install, then cd ios && bundle install && bundle exec pod install && cd ..
  2. yarn ios (Debug build, iOS simulator)
  3. In the app, press Fetch ascii.json (control), then Fetch arabic.json. Each fetches a JSON file from the repo over raw.githubusercontent.com.
  4. Press j in Metro to open React Native DevTools, then open Network.
  5. Open each request and look at Response and Preview.
File UTF-8 bytes UTF-16 units Expected in DevTools Actual in DevTools
data/ascii.json 9,028 9,028 whole body, ending in "end": "END_OF_BODY" whole body, JSON tree in Preview
data/arabic.json 11,328 9,128 whole body, ending in "end": "END_OF_BODY" only the first 9,128 bytes; Preview shows plain text

The app screen shows the byte count it received, to show the app gets the whole body.

I observed and verified this on 0.88.0 in a production app (iPhone 17 simulator, iOS 27.0, prebuilt React.xcframework), where the users/brands response was cut from 6,272 to 6,050 bytes. The reproducer targets 0.88.1, whose header has the same line; I haven't built and run the reproducer itself yet.

React Native Version

0.88.1

Output of npx @react-native-community/cli info

System:
  OS: macOS 27.2.1
  CPU: (14) arm64 Apple M4 Max
  Memory: 174.08 MB / 36.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 24.22.1
    path: /Users/mostafa/.nvm/current/bin/node
  Yarn:
    version: 1.22.22
    path: /Users/mostafa/.nvm/current/bin/yarn
  npm:
    version: 11.19.1
    path: /Users/mostafa/.nvm/current/bin/npm
  Watchman:
    version: 2026.05.18.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/mostafa/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 27.2
      - iOS 27.2
      - macOS 27.2
      - tvOS 27.2
      - visionOS 27.2
      - watchOS 27.2
  Android SDK:
    API Levels:
      - "33"
      - "34"
      - "35"
      - "36"
      - "37"
    Build Tools:
      - 30.0.3
      - 33.0.1
      - 34.0.0
      - 35.0.0
      - 36.0.0
      - 37.0.0
    Android NDK: Not Found
IDEs:
  Android Studio: 2026.2 AI-262.10968.63.2622.16730706
  Xcode:
    version: 27.2/27B52
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.19
    path: /usr/bin/javac
  Ruby:
    version: 3.4.6
    path: /Users/mostafa/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 20.2.0
    wanted: 20.2.0
  react:
    installed: 19.3.0
    wanted: 19.3.0
  react-native:
    installed: 0.88.1
    wanted: 0.88.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

(The iOS values read "Not found" / false because pod install hadn't run in the reproducer when this was collected.)

Screenshots and Videos

No screenshots: the screenshots I have come from a production app and show private data. The reproducer shows the same cut body in DevTools → Network → Response.

A workaround that I verified on 0.88.0 is in the first comment.

Activity

  1. mustafaabobakr commented on Dec 1, 2026

    @mustafaabobakr
    Author

    Workaround until this is fixed (Debug only)

    Patching RCTNetworkConversions.h in node_modules does nothing for most apps, because they link the prebuilt React.xcframework (the default), which already has the reporter compiled in. So the workaround lives in the app's own native code: one Objective-C++ file that swaps four RCTInspectorNetworkReporter class methods at load.

    • It hands the reporter each text body as UTF-8 bytes with base64Encoded:YES. The reporter stores their base64 text, which is ASCII (UTF-16 units == UTF-8 bytes), so the length bug no longer cuts it, and DevTools decodes it back.
    • Incremental (streamed) text bodies are buffered in the file and stored the same way when the response ends; a failed request drops its buffer.
    • It installs all four methods or none. If a method is renamed in a later version it logs [DevTools] RCTInspectorNetworkReporter changed; UTF-8 response body fix not installed and leaves React Native unchanged.
    • It is wrapped in #if DEBUG, so Release builds contain nothing.

    How to use it: add workaround/RCTInspectorNetworkReporter+UTF8Body.mm to your iOS app target (Xcode → drag it into the project, tick every app target), rebuild a Debug build, and reopen DevTools → Network. Delete it once RCTStringViewFromNSString sizes the view in UTF-8 bytes.

    Verified on 0.88.0 (iPhone 17 simulator, iOS 27.0, prebuilt React.xcframework), reading bodies back over CDP Network.getResponseBody:

    • a 6,272-byte JSON with Arabic text: 6,050 bytes before, 6,272 after, and Preview shows the JSON tree again;
    • every other response matched its Content-Length;
    • a 2.5 MB Arabic JSON came back identical to the text the app received.

    Limits: base64 makes a stored body about a third larger, so the largest single body DevTools keeps drops from about 100 MB to about 75 MB (the reporter's buffer is REQUEST_BUFFER_MAX_SIZE_BYTES, 100 MB). It relies on four method names of RCTInspectorNetworkReporter, so recheck it on every upgrade (the log line above tells you).

    RCTInspectorNetworkReporter+UTF8Body.mm
    /**
     * Workaround (Debug only): makes React Native DevTools' Network panel show whole iOS
     * response bodies that contain non-ASCII text.
     *
     * `RCTStringViewFromNSString` (`Libraries/Network/RCTNetworkConversions.h`) sizes the
     * UTF-8 bytes with `NSString.length`, a count of UTF-16 units, so every non-ASCII
     * character shortens the body DevTools stores. Patching that header in `node_modules`
     * has no effect when the app links the prebuilt `React.xcframework`, so this file
     * swaps four `RCTInspectorNetworkReporter` class methods at load instead.
     *
     * It hands the reporter the body as UTF-8 bytes with `base64Encoded:YES`. The reporter
     * stores their base64 text, which is ASCII (UTF-16 units == UTF-8 bytes), and DevTools
     * decodes it back. Incremental (streamed) text bodies are buffered here and stored the
     * same way when the response ends.
     *
     * Add this file to the app target. Delete it once `RCTStringViewFromNSString` uses the
     * UTF-8 byte length.
     */
    
    #if DEBUG
    
    #import <Foundation/Foundation.h>
    #import <React/RCTInspectorNetworkReporter.h>
    #import <objc/runtime.h>
    
    typedef void (*RNWStoreBodyIMP)(id, SEL, NSString *, id, bool);
    typedef void (*RNWResponseEndIMP)(id, SEL, NSString *, int);
    typedef void (*RNWRequestFailedIMP)(id, SEL, NSString *, BOOL);
    
    static RNWStoreBodyIMP originalStoreBody;
    static RNWResponseEndIMP originalResponseEnd;
    static RNWRequestFailedIMP originalRequestFailed;
    
    /** Streamed text bodies by DevTools request id, until the response ends or fails. */
    static NSMutableDictionary<NSString *, NSMutableString *> *incrementalBodies;
    
    static NSMutableString *takeIncrementalBody(NSString *requestId)
    {
      @synchronized(incrementalBodies) {
        NSMutableString *body = incrementalBodies[requestId];
        [incrementalBodies removeObjectForKey:requestId];
        return body;
      }
    }
    
    static void storeBody(id self, SEL _cmd, NSString *requestId, id data, bool base64Encoded)
    {
      if (!base64Encoded && [data isKindOfClass:[NSString class]]) {
        NSData *utf8 = [(NSString *)data dataUsingEncoding:NSUTF8StringEncoding];
        originalStoreBody(self, _cmd, requestId, utf8, true);
        return;
      }
      originalStoreBody(self, _cmd, requestId, data, base64Encoded);
    }
    
    static void storeBodyIncremental(id self, SEL _cmd, NSString *requestId, NSString *data)
    {
      if (requestId == nil || data.length == 0) {
        return;
      }
      @synchronized(incrementalBodies) {
        NSMutableString *body = incrementalBodies[requestId];
        if (body == nil) {
          body = [NSMutableString string];
          incrementalBodies[requestId] = body;
        }
        [body appendString:data];
      }
    }
    
    static void responseEnd(id self, SEL _cmd, NSString *requestId, int encodedDataLength)
    {
      originalResponseEnd(self, _cmd, requestId, encodedDataLength);
      NSMutableString *body = requestId == nil ? nil : takeIncrementalBody(requestId);
      if (body.length > 0) {
        storeBody(self, @selector(maybeStoreResponseBody:data:base64Encoded:), requestId, body, false);
      }
    }
    
    static void requestFailed(id self, SEL _cmd, NSString *requestId, BOOL cancelled)
    {
      if (requestId != nil) {
        takeIncrementalBody(requestId);
      }
      originalRequestFailed(self, _cmd, requestId, cancelled);
    }
    
    @interface RNWInspectorNetworkBodyFix : NSObject
    @end
    
    @implementation RNWInspectorNetworkBodyFix
    
    + (void)load
    {
      Class reporter = [RCTInspectorNetworkReporter class];
      Method store = class_getClassMethod(reporter, @selector(maybeStoreResponseBody:data:base64Encoded:));
      Method incremental = class_getClassMethod(reporter, @selector(maybeStoreResponseBodyIncremental:data:));
      Method end = class_getClassMethod(reporter, @selector(reportResponseEnd:encodedDataLength:));
      Method failed = class_getClassMethod(reporter, @selector(reportRequestFailed:cancelled:));
      // All four or none: a half-swapped reporter would drop or double-store bodies.
      if (store == NULL || incremental == NULL || end == NULL || failed == NULL) {
        NSLog(@"[DevTools] RCTInspectorNetworkReporter changed; UTF-8 response body fix not installed");
        return;
      }
    
      incrementalBodies = [NSMutableDictionary dictionary];
      originalStoreBody = (RNWStoreBodyIMP)method_setImplementation(store, (IMP)storeBody);
      originalResponseEnd = (RNWResponseEndIMP)method_setImplementation(end, (IMP)responseEnd);
      originalRequestFailed = (RNWRequestFailedIMP)method_setImplementation(failed, (IMP)requestFailed);
      method_setImplementation(incremental, (IMP)storeBodyIncremental);
    }
    
    @end
    
    #endif
  2. gabrieldonadel commented on Dec 1, 2026

    @gabrieldonadel
    Collaborator

    Fixed on main by #60322 (fa1dc61). RCTStringViewFromNSString now measures the view in UTF-8 bytes (lengthOfBytesUsingEncoding:NSUTF8StringEncoding), not UTF-16 units, so DevTools gets the whole text body. This also covers the crash in #57061.

    Thanks for the detailed report and reproducer! Until the fix is picked to a stable release, the workaround above still applies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions