Android: new WebSocket() crashes the app for a URL that OkHttp or java.net.URI rejects
維護者通常 2 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 72/100
研究方向
閱讀 com.facebook.react.modules.websocket.WebSocketModule.java 並找到呼叫了 Request.Builder().url(url)、getCookie 和 getDefaultOrigin 的 connect 方法。將這些呼叫包裹在針對 IllegalArgumentException 的 try-catch 中,並像 send 和 close 那樣向 JS 分發一個 error/close 事件。Done 表示無效的 URL 會在 WebSocket 上觸發 error 和 close,而不是使應用程式崩潰。
由索引模型根據 Issue 內容生成。
描述
Description
On Android, new WebSocket(url) crashes the app when OkHttp or java.net.URI rejects url, such as ws://localhost:0/ (port 0) or ws://localhost:4000/a|b (a character RFC 2396 doesn't allow in a path). JavaScript gets no exception and no error or close event, and a release build dies with a fatal exception on the native modules thread.
Expected: the socket fires error and close, as it does when it can't connect, or the constructor throws in JavaScript.
Likely cause: WebSocketModule.connect calls Request.Builder().url(url), getCookie, and getDefaultOrigin outside a try, and they throw IllegalArgumentException for such URLs, while send and close catch their exceptions. #24427 reported the same crash in 0.59.
We found this in Evolu, a local-first platform, whose relay URL type now rejects such URLs.
Steps to reproduce
- Replace
App.tsxof an app made from the reproducer template with the code below. - Build it in release for Android and run it.
- Tap either button. The app crashes instead of showing
errorandclose.
import { useState } from 'react';
import { Button, Text, View } from 'react-native';
// Each URL should report an error and close to JS. On Android, connect throws
// natively instead.
const urls = ['ws://localhost:0/', 'ws://localhost:4000/a|b'];
function App() {
const [log, setLog] = useState<ReadonlyArray<string>>([]);
const append = (line: string) => setLog(lines => [...lines, line]);
return (
<View style={{ padding: 48, gap: 16 }}>
{urls.map(url => (
<Button
key={url}
title={url}
onPress={() => {
const socket = new WebSocket(url);
socket.onerror = () => append(`error ${url}`);
socket.onclose = event => append(`close ${url} ${event.code}`);
}}
/>
))}
{log.map((line, index) => (
<Text key={index}>{line}</Text>
))}
</View>
);
}
export default App;
React Native Version
0.88.1
Affected Platforms
Runtime - Android, Build - MacOS
Output of npx @react-native-community/cli info
System:
OS: macOS 26.6.2
CPU: (10) arm64 Apple M5
Binaries:
Node: 24.22.0
npmPackages:
react-native: 0.88.1
Android:
hermesEnabled: true
newArchEnabled: true
Device: Android 16 (API 36) arm64 emulator
Stacktrace or Logs
E AndroidRuntime: FATAL EXCEPTION: mqt_v_native
E AndroidRuntime: java.lang.IllegalArgumentException: Invalid URL port: "0"
E AndroidRuntime: at t6.m(r8-map-id-50fb0331…:24)
E AndroidRuntime: at vv.c(r8-map-id-50fb0331…:513)
E AndroidRuntime: at xi.h(r8-map-id-50fb0331…:50)
E AndroidRuntime: at com.facebook.react.modules.websocket.WebSocketModule.connect(r8-map-id-50fb0331…:118)
E AndroidRuntime: at com.facebook.jni.NativeRunnable.run(Native Method)
E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(…)
E AndroidRuntime: FATAL EXCEPTION: mqt_v_native
E AndroidRuntime: java.lang.IllegalArgumentException: Unable to set ws://localhost:4000/a|b as default origin header
E AndroidRuntime: at com.facebook.react.modules.websocket.WebSocketModule.getCookie(r8-map-id-50fb0331…:8)
E AndroidRuntime: at com.facebook.react.modules.websocket.WebSocketModule.connect(r8-map-id-50fb0331…:121)
MANDATORY Reproducer
No repository: the App.tsx in Steps to reproduce is the whole reproducer.
Screenshots and Videos
No response
- 主要語言
- C++
- 星號
- 127k
- 分支
- 25.4k
- 平均合併
- 2 天 9 小時
- 30 天內合併 PR
- 91
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
react/react-native 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 70/100
react/react-native#59722 ·
維護者通常 2 天內回覆
-
難度 1/5 1 小時以內 新手友好度 88/100
react/react-native#59694 ·
維護者通常 2 天內回覆
-
Android autolinking keeps a stale autolinking.json when only bun.lock changes, since bun.lock isn't in the default lockFiles可能已有人在做 @lazerg 於 2 天前認領。 未關閉
難度 1/5 1 小時以內 新手友好度 85/100
react/react-native#59693 ·
維護者通常 2 天內回覆
-
Image.resolveAssetSource's TypeScript declaration drops the null it returns for an unknown asset可能已有人在做 @Hassan1305 於 2 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
react/react-native#59648 · 1 則留言 ·
維護者通常 2 天內回覆
-
Linking.getInitialURL() returns the task's first link after a link relaunches a reclaimed Android app可能已有人在做 @YevheniiKotyrlo 於 19 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
react/react-native#59006 · 2 則留言 ·
維護者通常 2 天內回覆
查看 react/react-native 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
bug needs-triage
難度 2/5 1-3 小時 新手友好度 68/100
microsoft/microsoft-ui-xaml#12515 · 1 則留言 · 1 個 reaction ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 66/100
FujiNetWIFI/fujinet-firmware#2261 ·
維護者通常 1 天內回覆
-
bug difficulty: easy good first issue help wanted
難度 2/5 1-3 小時 新手友好度 78/100
nagisa-kamishiro/rosehip#73 ·
維護者通常 1 天內回覆
-
bazel bug odb
難度 1/5 1 小時以內 新手友好度 80/100
The-OpenROAD-Project/OpenROAD#12095 · 1 則留言 ·
維護者通常 1 天內回覆