Android: new WebSocket() crashes the app for a URL that OkHttp or java.net.URI rejects
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Đọc com.facebook.react.modules.websocket.WebSocketModule.java và tìm phương thức connect nơi Request.Builder().url(url), getCookie và getDefaultOrigin được gọi. Bọc các lời gọi đó trong một try-catch cho IllegalArgumentException và gửi một sự kiện error/close tới JS như send và close làm. Done nghĩa là các URL không hợp lệ kích hoạt error và close trên WebSocket thay vì làm crash app.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- C++
- Star
- 127k
- Fork
- 25.4k
- Merge trung bình
- 3 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 108
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của react/react-native
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
react/react-native#59722 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
react/react-native#59694 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Image.resolveAssetSource's TypeScript declaration drops the null it returns for an unknown assetCó thể đã có người làm @Hassan1305 đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
react/react-native#59648 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Linking.getInitialURL() returns the task's first link after a link relaunches a reclaimed Android appCó thể đã có người làm @YevheniiKotyrlo đã nhận 21 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
react/react-native#59006 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Fabric TextInput: prepareForRecycle leaves stale inputAccessoryViewButtonLabel (empty Done bar)Có thể đã có người làm @jkasprzyk17 đã nhận 20 ngày trước. Đang mởNeeds: Author Feedback Needs: Repro
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
react/react-native#58952 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của react/react-native
Issue tương tự
-
`BLMPOP` with COUNT 0 or numkeys 0 hangsCó thể đã có người làm @ggyuchive đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
gObjectFullbrightAlphaMaskProgram in newview/llviewershadermgr.cpp is defined but never initializedĐang mởbug triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
secondlife/viewer#6620 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug core p3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
RunanywhereAI/runanywhere-sdks#1258 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ripwire-router skill: stale language list (15 vs 26 supported; Elixir/PHP/Kotlin wrongly listed as unsupported)Có thể đã có người làm @joyful-ii-V-I đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
redhat-et/ripwire#521 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày