Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript strings with NULL character are not handled properly when passed to Native Modules #12731

Closed
nickspacek opened this issue Mar 6, 2017 · 4 comments
Labels
Help Wanted :octocat: Issues ideal for external contributors. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@nickspacek
Copy link

Description

We are using STOMP over WebSockets to communicate with our API. On iOS everything is working as expected, on Android messages are not arriving intact. As we dug it, we discovered the trailing NULL (\0) character in the strings was not arriving.

As we sought to narrow down the cause we were able to confirm (using a sample Native Module based on the ToastAndroid module) that on Android a JavaScript string containing any NULL characters is not processed as expected; when the parameter arrives in the Java code, it has been truncated to the first occurrence of the NULL character, and the NULL character is also dropped.

For example "Hello World\0" arrives as "Hello World". "Hello\0World\0" arrives as "Hello".

Because the STOMP protocol is expecting a NULL-terminated string, it discards the WebSocket message as invalid.

Reproduction

Managed to reproduce in a fairly straightforward manner here: https://rnplay.org/apps/tkNMOQ

On Android, the element shows "Hello", presumably because the ws.send('Hello\0World\0') has been truncated. On iOS, the element shows "HelloWorld"

Solution

I'd love to understand where things are being translated from JavaScript to Java in React Native, and did start digging a bit. Ideally, NULL characters are escaped/allowed to be passed through to the Java side of things untouched, as they are on iOS.

Additional Information

  • React Native version: 0.42
  • Platform: Android
  • Operating System: Linux
@janicduplessis janicduplessis added Android Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Help Wanted :octocat: Issues ideal for external contributors. labels Mar 8, 2017
@janicduplessis
Copy link
Contributor

Could it be because the c++ bridge uses NULL terminated strings?

cc @mhorowitz

@binlaniua
Copy link

we have the same proble, did you resolve it? @nickspacek

@binlaniua
Copy link

if you are use spring websocket, you can try this

public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        if (message instanceof TextMessage) {
            TextMessage msg = (TextMessage) message;
            String payload = msg.getPayload();
            message = new TextMessage(payload + "\u0000");
        }
        super.handleMessage(session, message);
    }

@stale
Copy link

stale bot commented Nov 17, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 17, 2017
@stale stale bot closed this as completed Nov 24, 2017
@facebook facebook locked and limited conversation to collaborators May 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Help Wanted :octocat: Issues ideal for external contributors. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants