Can't connect to web socket from Electron when using self signed cert

3.4k views Asked by At

I have an Electron app which tries to connect to a device over a web socket. The connection is encrypted (i.e. wss) but the SSL certificate is self signed and thus, untrusted.

Connecting inside Chrome is ok and it works. However inside Electron I run into problems. Without putting any certificate-error handlers on the BrowserWindow or on the app I receive the following error in the console output:

WebSocket connection to 'wss://some_ip:50443/' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID

Then shortly after:

  • User is closing WAMP connection.... unreachable

In my code, to make the connection I run the following.


const connection = new autobahn.Connection({
    realm: 'some_realm',
    url: 'wss://some_ip:50443'
});

connection.onopen = (session, details) => {
    console.log('* User is opening WAMP connection....', session, details);
};

connection.onclose = (reason, details) => {
    console.log('* User is closing WAMP connection....', reason, details);
    return true;
};
connection.open();

// alternatively, this also displays the same error
const socket = new WebSocket(`wss://some_ip:50443`);

socket.onopen = function (event) {
    console.log(event);
};
socket.onclose = function (event) {
    console.log(event);
};

NOTE: Autobahn is a Websocket library for connecting using the WAMP protocol to a socket server of some sort. (in my case, the device) The underlying protocol is wss. Underneath the code above, a native JS new WebSocket() is being called. In other words:

As I mentioned, I've tested this code in the browser window and it works. I've also built a smaller application to try and isolate the issue. Still no luck.

I have tried adding the following code to my main.js process script:

app.commandLine.appendSwitch('ignore-certificate-errors');

and

win.webContents.on('certificate-error', (event, url, error, certificate, callback) => {
    // On certificate error we disable default behaviour (stop loading the page)
    // and we then say "it is all fine - true" to the callback
    event.preventDefault();
    callback(true);
});

and

app.on('certificate-error', (event, webContents, link, error, certificate, callback) => {
    // On certificate error we disable default behaviour (stop loading the page)
    // and we then say "it is all fine - true" to the callback
    event.preventDefault();
    callback(true);
});

This changed the error to:

WebSocket connection to 'wss://some_ip:50443/' failed: WebSocket opening handshake was canceled

My understanding is that the 'certificate-error' handlers above should escape any SSL certificate errors and allow the application to proceed. However, they're not.

I've also tried adding the following to main.js:

win = new BrowserWindow({
    webPreferences: {
        nodeIntegration: true,
        webSecurity: false
    }
});

process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = '1';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

With Election, how do I properly deal with a certificate from an untrusted authority? i.e. a self signed cert.

Any help would be much appreciated.

1

There are 1 answers

0
Roche Olivier On

I had the same problem , all i added was your line:

app.commandLine.appendSwitch('ignore-certificate-errors');

I use socket.io, but i think its the same principal. I do however connect to the https protocol and not wss directly.

This is what my connection looks like on the page:

socket = io.connect(
        'https://yoursocketserver:yourport', {
            'socketpath',
            secure: false,
            transports: ['websocket']
        });

That seems to have done the trick. Thank you for the help :) i hope this answer helps you too.