From f230e75b08921c9aac6a2502fbdba9b50b514a90 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 24 Oct 2022 13:33:30 +0200 Subject: [PATCH 1/5] Set the ufrag/pwd according to the spec --- bin/wasm-node/CHANGELOG.md | 4 ++++ bin/wasm-node/javascript/src/index-browser.ts | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index b7426d4432..2c2ad28a49 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -6,6 +6,10 @@ - The parameter of `chainHead_unstable_finalizedDatabase` has been renamed from `max_size_bytes` to `maxSizeBytes`. ([#2923](https://github.com/paritytech/smoldot/pull/2923)) +### Fixed + +- When opening a WebRTC connection, the ufrag and password of SDP requests are now properly set according to the WebRTC libp2p specification. + ## 0.7.3 - 2022-10-19 ### Changed diff --git a/bin/wasm-node/javascript/src/index-browser.ts b/bin/wasm-node/javascript/src/index-browser.ts index b5657b7ea6..f4f4b000a0 100644 --- a/bin/wasm-node/javascript/src/index-browser.ts +++ b/bin/wasm-node/javascript/src/index-browser.ts @@ -267,13 +267,17 @@ export function start(options?: ClientOptions): Client { console.error("Local offer doesn't contain UDP data channel. WebRTC connections will likely fail. Please report this issue."); } // According to the libp2p WebRTC spec, the ufrag and pwd are the same - // randomly-generated string. We modify the local description to ensure that. - const pwd = sdpOffer.match(/^a=ice-pwd:(.+)$/m); - if (pwd != null) { - sdpOffer = sdpOffer.replace(/^a=ice-ufrag.*$/m, 'a=ice-ufrag:' + pwd[1]); - } else { + // randomly-generated string on both sides, and must be prefixed with + // `libp2p-webrtc-v1:`. We modify the local description to ensure that. + // While we could randomly generate a new string, we just grab the one that the + // browser has generated, in order to make sure that it respects the constraints + // of the ICE protocol. + const ufrag_pwd = "libp2p-webrtc-v1:" + sdpOffer.match(/^a=ice-pwd:(.+)$/m)?.at(1); + if (ufrag_pwd === undefined) { console.error("Failed to set ufrag to pwd. WebRTC connections will likely fail. Please report this issue."); } + sdpOffer = sdpOffer.replace(/^a=ice-ufrag.*$/m, 'a=ice-ufrag:' + ufrag_pwd); + sdpOffer = sdpOffer.replace(/^a=ice-pwd.*$/m, 'a=ice-pwd:' + ufrag_pwd); await pc!.setLocalDescription({ type: 'offer', sdp: sdpOffer }); // Transform certificate hash into fingerprint (upper-hex; each byte separated by ":"). @@ -315,10 +319,9 @@ export function start(options?: ClientOptions): Client { "a=ice-options:ice2" + "\n" + // ICE username and password, which are used for establishing and // maintaining the ICE connection. (RFC8839) - // MUST match ones used by the answerer (server). // These values are set according to the libp2p WebRTC specification. - "a=ice-ufrag:" + remoteCertMultibase + "\n" + - "a=ice-pwd:" + remoteCertMultibase + "\n" + + "a=ice-ufrag:" + ufrag_pwd + "\n" + + "a=ice-pwd:" + ufrag_pwd + "\n" + // Fingerprint of the certificate that the server will use during the TLS // handshake. (RFC8122) // MUST be derived from the certificate used by the answerer (server). From be0075df4d8d5bbdade1317523f86a8c99e26dda Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 24 Oct 2022 13:34:36 +0200 Subject: [PATCH 2/5] PR number --- bin/wasm-node/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index 2c2ad28a49..b558c2abb7 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixed -- When opening a WebRTC connection, the ufrag and password of SDP requests are now properly set according to the WebRTC libp2p specification. +- When opening a WebRTC connection, the ufrag and password of SDP requests are now properly set according to the WebRTC libp2p specification. ([#2924](https://github.com/paritytech/smoldot/pull/2924)) ## 0.7.3 - 2022-10-19 From 035b293ee40fa4647936761ec3884bb7773872e0 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 24 Oct 2022 13:35:16 +0200 Subject: [PATCH 3/5] JS uses camelCase --- bin/wasm-node/javascript/src/index-browser.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/wasm-node/javascript/src/index-browser.ts b/bin/wasm-node/javascript/src/index-browser.ts index f4f4b000a0..94d6e5b2e2 100644 --- a/bin/wasm-node/javascript/src/index-browser.ts +++ b/bin/wasm-node/javascript/src/index-browser.ts @@ -272,12 +272,12 @@ export function start(options?: ClientOptions): Client { // While we could randomly generate a new string, we just grab the one that the // browser has generated, in order to make sure that it respects the constraints // of the ICE protocol. - const ufrag_pwd = "libp2p-webrtc-v1:" + sdpOffer.match(/^a=ice-pwd:(.+)$/m)?.at(1); - if (ufrag_pwd === undefined) { + const ufragPwd = "libp2p-webrtc-v1:" + sdpOffer.match(/^a=ice-pwd:(.+)$/m)?.at(1); + if (ufragPwd === undefined) { console.error("Failed to set ufrag to pwd. WebRTC connections will likely fail. Please report this issue."); } - sdpOffer = sdpOffer.replace(/^a=ice-ufrag.*$/m, 'a=ice-ufrag:' + ufrag_pwd); - sdpOffer = sdpOffer.replace(/^a=ice-pwd.*$/m, 'a=ice-pwd:' + ufrag_pwd); + sdpOffer = sdpOffer.replace(/^a=ice-ufrag.*$/m, 'a=ice-ufrag:' + ufragPwd); + sdpOffer = sdpOffer.replace(/^a=ice-pwd.*$/m, 'a=ice-pwd:' + ufragPwd); await pc!.setLocalDescription({ type: 'offer', sdp: sdpOffer }); // Transform certificate hash into fingerprint (upper-hex; each byte separated by ":"). @@ -320,8 +320,8 @@ export function start(options?: ClientOptions): Client { // ICE username and password, which are used for establishing and // maintaining the ICE connection. (RFC8839) // These values are set according to the libp2p WebRTC specification. - "a=ice-ufrag:" + ufrag_pwd + "\n" + - "a=ice-pwd:" + ufrag_pwd + "\n" + + "a=ice-ufrag:" + ufragPwd + "\n" + + "a=ice-pwd:" + ufragPwd + "\n" + // Fingerprint of the certificate that the server will use during the TLS // handshake. (RFC8122) // MUST be derived from the certificate used by the answerer (server). From 7e27e90c36983b936b5e5682efe3457bb365f35c Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 28 Oct 2022 08:56:28 +0200 Subject: [PATCH 4/5] Fix console error never printed --- bin/wasm-node/javascript/src/index-browser.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/wasm-node/javascript/src/index-browser.ts b/bin/wasm-node/javascript/src/index-browser.ts index 592cb46ecd..b1d75fc21a 100644 --- a/bin/wasm-node/javascript/src/index-browser.ts +++ b/bin/wasm-node/javascript/src/index-browser.ts @@ -347,10 +347,11 @@ export function start(options?: ClientOptions): Client { // While we could randomly generate a new string, we just grab the one that the // browser has generated, in order to make sure that it respects the constraints // of the ICE protocol. - const ufragPwd = "libp2p-webrtc-v1:" + sdpOffer.match(/^a=ice-pwd:(.+)$/m)?.at(1); - if (ufragPwd === undefined) { + const browserGeneratedPwd = sdpOffer.match(/^a=ice-pwd:(.+)$/m)?.at(1); + if (browserGeneratedPwd === undefined) { console.error("Failed to set ufrag to pwd. WebRTC connections will likely fail. Please report this issue."); } + const ufragPwd = "libp2p-webrtc-v1:" + browserGeneratedPwd; sdpOffer = sdpOffer.replace(/^a=ice-ufrag.*$/m, 'a=ice-ufrag:' + ufragPwd); sdpOffer = sdpOffer.replace(/^a=ice-pwd.*$/m, 'a=ice-pwd:' + ufragPwd); await pc!.setLocalDescription({ type: 'offer', sdp: sdpOffer }); From 5775c48e640cfbea110822193e2f5e0ade8ef4c2 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 28 Oct 2022 09:52:45 +0200 Subject: [PATCH 5/5] Change prefix --- bin/wasm-node/javascript/src/index-browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/wasm-node/javascript/src/index-browser.ts b/bin/wasm-node/javascript/src/index-browser.ts index b1d75fc21a..d1b31df677 100644 --- a/bin/wasm-node/javascript/src/index-browser.ts +++ b/bin/wasm-node/javascript/src/index-browser.ts @@ -351,7 +351,7 @@ export function start(options?: ClientOptions): Client { if (browserGeneratedPwd === undefined) { console.error("Failed to set ufrag to pwd. WebRTC connections will likely fail. Please report this issue."); } - const ufragPwd = "libp2p-webrtc-v1:" + browserGeneratedPwd; + const ufragPwd = "libp2p+webrtc+v1/" + browserGeneratedPwd; sdpOffer = sdpOffer.replace(/^a=ice-ufrag.*$/m, 'a=ice-ufrag:' + ufragPwd); sdpOffer = sdpOffer.replace(/^a=ice-pwd.*$/m, 'a=ice-pwd:' + ufragPwd); await pc!.setLocalDescription({ type: 'offer', sdp: sdpOffer });