Local TLS certificate is not accepted by Node.js unless Auto Encrypt Localhost server is created
To reproduce
Server
const AutoEncryptLocalhost = require('../index')
const server = AutoEncryptLocalhost.https.createServer((request, response) => {
response.end('Hello, world!')
})
server.listen(443, () => {
console.log('\n 🎉 ❨Example❩ Web server is running at https://localhost\n')
})
Separate Node.js client
const https = require('https')
https.get('https://localhost')
What should happen
Client should not throw an error.
What actually happens
Client throws an error:
Error: self signed certificate in certificate chain
at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:932:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'SELF_SIGNED_CERT_IN_CHAIN'
}
Workaround
Import and Auto Encrypt Localhost in the client and create a dummy server.
const AutoEncryptLocalhost = require('@small-tech/auto-encrypt-localhost')
AutoEncryptLocalhost.createServer(null, () => {})
const https = require('https')
https.get('https://localhost')