Skip to content

Commit

Permalink
fix(websocket-websys): Unsubscribe from websocket events on drop
Browse files Browse the repository at this point in the history
Avoid use-after-free handler invocation from JS side.

Fixes #5490

Pull-Request: #5521.
  • Loading branch information
oblique authored Aug 2, 2024
1 parent 5c11f4a commit d733dfe
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ libp2p-webrtc = { version = "0.7.1-alpha", path = "transports/webrtc" }
libp2p-webrtc-utils = { version = "0.2.1", path = "misc/webrtc-utils" }
libp2p-webrtc-websys = { version = "0.3.0-alpha", path = "transports/webrtc-websys" }
libp2p-websocket = { version = "0.43.2", path = "transports/websocket" }
libp2p-websocket-websys = { version = "0.3.2", path = "transports/websocket-websys" }
libp2p-websocket-websys = { version = "0.3.3", path = "transports/websocket-websys" }
libp2p-webtransport-websys = { version = "0.3.0", path = "transports/webtransport-websys" }
libp2p-yamux = { version = "0.45.2", path = "muxers/yamux" }
multiaddr = "0.18.1"
Expand Down
5 changes: 5 additions & 0 deletions transports/websocket-websys/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.3

- Fix use-after-free handler invocation from JS side.
See [PR 5521](https://github.com/libp2p/rust-libp2p/pull/5521).

## 0.3.2

- Change close code in drop implementation to `1000` given that in browsers only
Expand Down
2 changes: 1 addition & 1 deletion transports/websocket-websys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-websocket-websys"
edition = "2021"
rust-version = "1.60.0"
description = "WebSocket for libp2p under WASM environment"
version = "0.3.2"
version = "0.3.3"
authors = ["Vince Vasta <vince.vasta@gmail.com>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
6 changes: 6 additions & 0 deletions transports/websocket-websys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ impl AsyncWrite for Connection {

impl Drop for Connection {
fn drop(&mut self) {
// Unset event listeners, as otherwise they will be called by JS after the handlers have already been dropped.
self.inner.socket.set_onclose(None);
self.inner.socket.set_onerror(None);
self.inner.socket.set_onopen(None);
self.inner.socket.set_onmessage(None);

// In browsers, userland code is not allowed to use any other status code than 1000: https://websockets.spec.whatwg.org/#dom-websocket-close
const REGULAR_CLOSE: u16 = 1000; // See https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1.

Expand Down

0 comments on commit d733dfe

Please sign in to comment.