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

Treat non-localhost non-secure WebSocket as not supported in browsers #1360

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Changed

- Addresses that are not supported by the host platform are now ignored during the discovery process. For example, TCP/IP connections are ignored while in a browser. This avoids populating the address book with peers that we know we can't connect to anyway. ([#1359](https://github.com/smol-dot/smoldot/pull/1359))
- Addresses that are not supported by the host platform are now ignored during the discovery process. For example, TCP/IP connections are ignored while in a browser. This avoids populating the address book with peers that we know we can't connect to anyway. ([#1359](https://github.com/smol-dot/smoldot/pull/1359), [#1360](https://github.com/smol-dot/smoldot/pull/1360))

## 2.0.10 - 2023-11-17

Expand Down
14 changes: 14 additions & 0 deletions wasm-node/javascript/src/no-auto-bytecode-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ export {
export function startWithBytecode(options: ClientOptionsWithBytecode): Client {
options.forbidTcp = true;

// When in a secure context, browsers refuse to open non-secure WebSocket connections to
// non-localhost. There is an exception if the page is localhost, in which case all connections
// are allowed.
// Detecting this ahead of time is better for the overall health of the client, as it will
// avoid storing in memory addresses that it knows it can't connect to.
// The condition below is a hint, and false-positives or false-negatives are not fundamentally
// an issue.
if ((typeof isSecureContext === 'boolean' && isSecureContext) && typeof location !== undefined) {
const loc = location.toString();
if (loc.indexOf('localhost') !== -1 && loc.indexOf('127.0.0.1') !== -1 && loc.indexOf('::1') !== -1) {
options.forbidNonLocalWs = true;
}
}

return innerStart(options, options.bytecode, {
performanceNow: () => {
return performance.now()
Expand Down
Loading