1

I'm running a node.js websocket server with self signed certificate.

Sample code:

const WebSocket = require("ws").Server;
const HttpsServer = require('https').createServer;
const fs = require("fs");

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

server = HttpsServer({
    cert: fs.readFileSync('cert.pem'),
    key: fs.readFileSync('key.pem')
})
socket = new WebSocket({
    server: server,
});

socket.on("connection", (ws,r) => {console.log(ws, r);})

server.listen(8443);

When I create the client socket using node.js I do this the following way and it works perfectly:

const WebSocket = require('ws');
s = new WebSocket("wss://0.0.0.0:8443", {rejectUnauthorized: false});
s.onopen = function() {console.log("OPENED")};

When I create a WebSocket via pure JS I get an error "code: 'DEPTH_ZERO_SELF_SIGNED_CERT'" telling me I can't connect because certificate is self-signed.

However I'm developing a chrome extension that needs to communicate with my server via websocket and plain JS WebSocket class doesn't seem to support that. Is there any equivalent, or am I forced to use node.js on client side as well, and is it even possible in sandbox environment via plugin?

Thanks!

user1617735
  • 451
  • 5
  • 16
  • Yes you should be using WebSocket class to make client connections from browser extensions - https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications – Amila Senadheera Feb 26 '23 at 13:19
  • @AmilaSenadheera Oh sorry, forgot to mention, I can't connect using pure JS Websocket because I get an error related to self-signed certificate and server considers the client side unauthorized and throws DEPTH_ZERO_SELF_SIGNED_CERT. Edited the question. – user1617735 Feb 26 '23 at 13:59
  • Have you checked this out https://stackoverflow.com/questions/5312311/secure-websockets-with-self-signed-certificate – Amila Senadheera Feb 26 '23 at 14:08
  • @AmilaSenadheera So short answer, it's not possible with native JS code, because chrome simply abandons connection if certificate is self signed. Thanks, I'll have to look for workarounds then. – user1617735 Feb 26 '23 at 16:03

0 Answers0