Skip to content

Fix Android WebSocket cookie lookup stripping the URL path - #58365

Closed
lazerg wants to merge 2 commits into
react:mainfrom
lazerg:fix/issue-58358-websocket-cookie-path
Closed

Fix Android WebSocket cookie lookup stripping the URL path#58365
lazerg wants to merge 2 commits into
react:mainfrom
lazerg:fix/issue-58358-websocket-cookie-path

Conversation

@lazerg

@lazerg lazerg commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary:

WebSocketModule.getCookie looks up cookies through getDefaultOrigin(uri), which strips the URL down to scheme://host[:port] before handing it to ForwardingCookieHandler. Android's CookieManager matches cookies against the full URL (domain and path), so any cookie set with a Path other than / gets silently dropped from the WebSocket handshake, breaking auth/session cookies scoped to a sub-path (e.g. /signal-r/hubs/messages).

getDefaultOrigin is also used to build the origin header for the handshake, where stripping the path is correct per the WebSocket protocol, so it can't just be changed in place without affecting that header too. This adds a separate getCookieLookupUri that does the same ws(s):// to http(s):// scheme mapping but keeps the path, query, and fragment, and uses it only for the cookie lookup. It also drops the URI's userinfo from that lookup, since it plays no role in cookie matching and shouldn't be forwarded into the CookieManager call.

Changelog:

[ANDROID] [FIXED] - Fix WebSocket cookie lookup dropping path-scoped cookies

Test Plan:

Added WebSocketModuleTest, exercising getCookieLookupUri through reflection and asserting the path, port, and query survive the ws/wss -> http/https conversion. Couldn't run it through the repo's own Gradle/Robolectric setup in this environment (react-native-gradle-plugin isn't resolvable without the full monorepo build), so I compiled the real companion object with kotlinc standalone and ran the same reflection lookup against it directly, confirming both cases pass and that the private companion method resolves without a NoSuchMethodException.

Fixes #58358

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 6, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 6, 2026
Comment on lines +512 to +513
requestURI.query,
requestURI.fragment,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to include query and fragment?

Can we simplify this method to only re-create the URI if the scheme is ws(s)?

@lazerg lazerg Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified in 476697c: getCookieLookupUri now passes null for query and fragment instead of forwarding them through, so the lookup URI is just scheme://host[:port]/path.

@meta-codesync

meta-codesync Bot commented Sep 7, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D119093059.

@meta-codesync meta-codesync Bot closed this in 0eb9b30 Sep 8, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Sep 8, 2026
@meta-codesync

meta-codesync Bot commented Sep 8, 2026

Copy link
Copy Markdown

@javache merged this pull request in 0eb9b30.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

android websocket implementation is incorrect looking app cookies

2 participants