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

Peerjs crashes if you send any Unicode characters above U+FFFF #11

Closed
divec opened this issue Nov 24, 2022 · 7 comments
Closed

Peerjs crashes if you send any Unicode characters above U+FFFF #11

divec opened this issue Nov 24, 2022 · 7 comments
Labels

Comments

@divec
Copy link

divec commented Nov 24, 2022

Steps to reproduce: Essentially, just conn.send( '𨋢' ) using the default binary serialization. See full example below.

Expected behaviour: Data is sent and received correctly.

Actual behaviour: The receiving peer crashes inside util.unpack, because the sender sent garbled data.

Cause
This happens when sending any Unicode codepoint above U+FFFF:

  • In the Javascript string, a single codepoint above U+FFFF is expressed as a UTF-16 surrogate pair made of two code units, e.g. '\uD860\uDEE2'.
  • This is a single Unicode codepoint (even though it has Javascript string length 2).
  • The function utf8Length wrongly assumes each individual surrogate becomes three UTF-8 bytes (i.e. a total of six bytes for a surrogate pair).
  • But in fact a surrogate pair, taken as a whole, becomes four UTF-8 bytes.
  • This length mismatch causes the crash in util.unpack.

Fix: #10

Workaround: Using JSON serialization (instead of the default binary) avoids the crash (but has certain other disadvantages).

Full code to reproduce the error

var peer1 = new Peer();
peer1.on( 'open', function ( id ) {
        var peer2 = new Peer();
        peer2.on( 'open', function() {
                var conn2 = peer2.connect( id );
                // But using JSON serialization, instead of the default binary, avoids the crash
                //var conn2 = peer2.connect( id, { serialization: 'json' } );
                conn2.on( 'open', function () {
                        // Send U+282E2, the Cantonese word for 'elevator'
                        // (but it could be any of the 93,000 Unicode codepoints above U+FFFF)
                        conn2.send( '𨋢' );
                } );
        } );
} );
peer1.on( 'connection', function ( conn1 ) {
        conn1.on( 'data', function ( data ) {
                // This never runs: util.unpack crashes before we get this far
                console.log( 'received: ' + data );
        } );
} );
@divec
Copy link
Author

divec commented Nov 29, 2022

Ping: Any chance someone could review my proposed js-binarypack patch? I'm satisfied it fixes the issue (it has unit tests and comprehensive comments). Thanks!

@jonasgloning jonasgloning transferred this issue from peers/peerjs Dec 5, 2022
@jonasgloning
Copy link
Member

Transferred to the binarypack-repo (issue tracker wasn't enabled before).

Thank you for the bug report and the pull request! Complete with description, analysis, explanation, workaround, fix, and code for reproduction, this is probably the most helpful issue I have seen here!

I could reproduce the crash and confirm your patch works. I'll merge it now, but it will take some time to reach npm. Only the previous maintainer has write-access to the package; I'll ask him to transfer it.

@divec
Copy link
Author

divec commented Dec 5, 2022

Thanks very much!

@divec
Copy link
Author

divec commented Feb 24, 2023

Hi @jonasgloning , is there anything we can do to get this updated in npm? Especially in the package peerjs-js-binarypack used by peerjs. Thanks!

@jonasgloning
Copy link
Member

jonasgloning commented Feb 25, 2023

Released now as v1.0.2!

@jonasgloning
Copy link
Member

Can you explain to me why expected != text in this line https://github.com/divec/js-binarypack/blob/3c7eed63c1da824e7d6fa5a3e0bf8742db72b96f/test/utf8.html#L7 ?

@divec
Copy link
Author

divec commented Feb 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants