2019-03-28 01:46:17 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2023-08-15 10:34:05 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
|
|
const https = require('https');
|
|
|
|
|
const fs = require('fs');
|
2019-03-28 01:46:17 -05:00
|
|
|
const express = require('../support/express');
|
|
|
|
|
const request = require('../support/client');
|
2017-10-19 09:12:31 +01:00
|
|
|
|
|
|
|
|
const app = express();
|
2019-03-28 01:46:17 -05:00
|
|
|
|
2017-10-19 09:12:31 +01:00
|
|
|
const ca = fs.readFileSync(`${__dirname}/fixtures/ca.cert.pem`);
|
|
|
|
|
const key = fs.readFileSync(`${__dirname}/fixtures/key.pem`);
|
|
|
|
|
const pfx = fs.readFileSync(`${__dirname}/fixtures/cert.pfx`);
|
|
|
|
|
const cert = fs.readFileSync(`${__dirname}/fixtures/cert.pem`);
|
|
|
|
|
const passpfx = fs.readFileSync(`${__dirname}/fixtures/passcert.pfx`);
|
2018-12-02 16:16:48 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
openssl genrsa -out ca.key.pem 2048
|
|
|
|
|
openssl req -x509 -new -nodes -key ca.key.pem -sha256 -days 5000 -out ca.cert.pem # specify CN = CA
|
|
|
|
|
|
|
|
|
|
openssl genrsa -out key.pem 2048
|
|
|
|
|
openssl req -new -key key.pem -out cert.csr # specify CN = localhost
|
|
|
|
|
|
|
|
|
|
openssl x509 -req -in cert.csr -CA ca.cert.pem -CAkey ca.key.pem -CAcreateserial -out cert.pem -days 5000 -sha256
|
|
|
|
|
openssl pkcs12 -export -in cert.pem -inkey key.pem -out cert.pfx # empty password
|
|
|
|
|
|
|
|
|
|
openssl pkcs12 -export -in cert.pem -inkey key.pem -out passcert.pfx # password test
|
|
|
|
|
|
|
|
|
|
*/
|
2018-07-30 02:40:34 +09:00
|
|
|
let http2;
|
2019-03-28 01:46:17 -05:00
|
|
|
if (process.env.HTTP2_TEST) {
|
2023-08-15 10:34:05 -05:00
|
|
|
http2 = require('http2');
|
2018-07-30 02:40:34 +09:00
|
|
|
}
|
2019-03-28 01:46:17 -05:00
|
|
|
|
2017-10-19 09:12:31 +01:00
|
|
|
let server;
|
|
|
|
|
|
2021-11-30 19:42:15 -05:00
|
|
|
app.get('/', (request_, res) => {
|
2019-03-28 01:46:17 -05:00
|
|
|
res.send('Safe and secure!');
|
2013-03-28 21:45:59 -04:00
|
|
|
});
|
|
|
|
|
|
2016-01-27 11:17:15 -07:00
|
|
|
// WARNING: this .listen() boilerplate is slightly different from most tests
|
|
|
|
|
// due to HTTPS. Do not copy/paste without examination.
|
2019-03-28 01:46:17 -05:00
|
|
|
const base = 'https://localhost';
|
2017-10-19 09:12:31 +01:00
|
|
|
let testEndpoint;
|
2013-03-28 21:45:59 -04:00
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
describe('https', () => {
|
|
|
|
|
describe('certificate authority', () => {
|
2016-08-31 18:45:38 +02:00
|
|
|
before(function listen(done) {
|
2021-02-15 11:51:59 -06:00
|
|
|
server = process.env.HTTP2_TEST
|
|
|
|
|
? http2.createSecureServer(
|
|
|
|
|
{
|
|
|
|
|
key,
|
|
|
|
|
cert
|
|
|
|
|
},
|
|
|
|
|
app
|
|
|
|
|
)
|
|
|
|
|
: https.createServer(
|
|
|
|
|
{
|
|
|
|
|
key,
|
|
|
|
|
cert
|
|
|
|
|
},
|
|
|
|
|
app
|
|
|
|
|
);
|
2019-03-28 01:46:17 -05:00
|
|
|
|
2016-08-31 18:45:38 +02:00
|
|
|
server.listen(0, function listening() {
|
2017-10-19 09:12:31 +01:00
|
|
|
testEndpoint = `${base}:${server.address().port}`;
|
2013-03-28 21:45:59 -04:00
|
|
|
done();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
2016-08-31 18:45:38 +02:00
|
|
|
|
2017-10-19 09:12:31 +01:00
|
|
|
after(() => {
|
2018-12-02 16:16:48 +00:00
|
|
|
if (server) server.close();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
2016-08-31 18:45:38 +02:00
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
describe('request', () => {
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should give a good response', (done) => {
|
2016-08-31 18:45:38 +02:00
|
|
|
request
|
2017-10-19 09:12:31 +01:00
|
|
|
.get(testEndpoint)
|
|
|
|
|
.ca(ca)
|
2021-11-30 19:42:15 -05:00
|
|
|
.end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2017-10-19 09:12:31 +01:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2017-10-19 09:12:31 +01:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-12-02 16:16:48 +00:00
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
it('should reject unauthorized response', () => {
|
2018-12-02 16:16:48 +00:00
|
|
|
return request
|
|
|
|
|
.get(testEndpoint)
|
2018-12-02 13:33:17 +00:00
|
|
|
.trustLocalhost(false)
|
2019-03-28 01:46:17 -05:00
|
|
|
.then(
|
|
|
|
|
() => {
|
|
|
|
|
throw new Error('Allows MITM');
|
|
|
|
|
},
|
|
|
|
|
() => {}
|
|
|
|
|
);
|
2018-12-02 16:16:48 +00:00
|
|
|
});
|
2018-12-02 13:33:17 +00:00
|
|
|
|
2019-11-21 21:24:36 +01:00
|
|
|
it('should not reject unauthorized response', () => {
|
|
|
|
|
return request
|
|
|
|
|
.get(testEndpoint)
|
|
|
|
|
.disableTLSCerts()
|
|
|
|
|
.then(({ status }) => {
|
|
|
|
|
assert.strictEqual(status, 200);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
it('should trust localhost unauthorized response', () => {
|
|
|
|
|
return request.get(testEndpoint).trustLocalhost(true);
|
2018-12-02 13:33:17 +00:00
|
|
|
});
|
|
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
it('should trust overriden localhost unauthorized response', () => {
|
2018-12-02 13:33:17 +00:00
|
|
|
return request
|
|
|
|
|
.get(`https://example.com:${server.address().port}`)
|
2019-03-28 01:46:17 -05:00
|
|
|
.connect('127.0.0.1')
|
|
|
|
|
.trustLocalhost();
|
2018-12-02 13:33:17 +00:00
|
|
|
});
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
|
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
describe('.agent', () => {
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should be able to make multiple requests without redefining the certificate', (done) => {
|
2017-10-19 09:12:31 +01:00
|
|
|
const agent = request.agent({ ca });
|
2021-11-30 19:42:15 -05:00
|
|
|
agent.get(testEndpoint).end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2016-08-31 18:45:38 +02:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2024-04-25 22:48:35 +02:00
|
|
|
agent.get(new URL(testEndpoint)).end((error, res) => {
|
2021-11-30 19:42:15 -05:00
|
|
|
assert.ifError(error);
|
2016-08-31 18:45:38 +02:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2016-08-31 18:45:38 +02:00
|
|
|
done();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-08-31 18:45:38 +02:00
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
describe.skip('client certificates', () => {
|
2016-08-31 18:45:38 +02:00
|
|
|
before(function listen(done) {
|
2021-02-15 11:51:59 -06:00
|
|
|
server = process.env.HTTP2_TEST
|
|
|
|
|
? http2.createSecureServer(
|
|
|
|
|
{
|
|
|
|
|
ca,
|
|
|
|
|
key,
|
|
|
|
|
cert,
|
|
|
|
|
requestCert: true,
|
|
|
|
|
rejectUnauthorized: true
|
|
|
|
|
},
|
|
|
|
|
app
|
|
|
|
|
)
|
|
|
|
|
: https.createServer(
|
|
|
|
|
{
|
|
|
|
|
ca,
|
|
|
|
|
key,
|
|
|
|
|
cert,
|
|
|
|
|
requestCert: true,
|
|
|
|
|
rejectUnauthorized: true
|
|
|
|
|
},
|
|
|
|
|
app
|
|
|
|
|
);
|
2019-03-28 01:46:17 -05:00
|
|
|
|
2016-08-31 18:45:38 +02:00
|
|
|
server.listen(0, function listening() {
|
2017-10-19 09:12:31 +01:00
|
|
|
testEndpoint = `${base}:${server.address().port}`;
|
2016-08-31 18:45:38 +02:00
|
|
|
done();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
2016-08-31 18:45:38 +02:00
|
|
|
|
2017-10-19 09:12:31 +01:00
|
|
|
after(() => {
|
2018-12-02 16:16:48 +00:00
|
|
|
if (server) server.close();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
2016-08-31 18:45:38 +02:00
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
describe('request', () => {
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should give a good response with client certificates and CA', (done) => {
|
2016-08-31 18:45:38 +02:00
|
|
|
request
|
2017-10-19 09:12:31 +01:00
|
|
|
.get(testEndpoint)
|
|
|
|
|
.ca(ca)
|
|
|
|
|
.key(key)
|
|
|
|
|
.cert(cert)
|
2021-11-30 19:42:15 -05:00
|
|
|
.end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2017-10-19 09:12:31 +01:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2017-10-19 09:12:31 +01:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should give a good response with client pfx', (done) => {
|
2016-11-07 13:14:29 -08:00
|
|
|
request
|
2017-10-19 09:12:31 +01:00
|
|
|
.get(testEndpoint)
|
|
|
|
|
.pfx(pfx)
|
2021-11-30 19:42:15 -05:00
|
|
|
.end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2017-10-19 09:12:31 +01:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2017-10-19 09:12:31 +01:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should give a good response with client pfx with passphrase', (done) => {
|
2017-05-18 12:14:38 +01:00
|
|
|
request
|
2017-10-19 09:12:31 +01:00
|
|
|
.get(testEndpoint)
|
|
|
|
|
.pfx({
|
|
|
|
|
pfx: passpfx,
|
2019-03-28 01:46:17 -05:00
|
|
|
passphrase: 'test'
|
2017-10-19 09:12:31 +01:00
|
|
|
})
|
2021-11-30 19:42:15 -05:00
|
|
|
.end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2017-10-19 09:12:31 +01:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2017-10-19 09:12:31 +01:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-28 01:46:17 -05:00
|
|
|
describe('.agent', () => {
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should be able to make multiple requests without redefining the certificates', (done) => {
|
2017-10-19 09:12:31 +01:00
|
|
|
const agent = request.agent({ ca, key, cert });
|
2021-11-30 19:42:15 -05:00
|
|
|
agent.get(testEndpoint).end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2016-08-31 18:45:38 +02:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2024-04-25 22:48:35 +02:00
|
|
|
agent.get(new URL(testEndpoint)).end((error, res) => {
|
2021-11-30 19:42:15 -05:00
|
|
|
assert.ifError(error);
|
2016-08-31 18:45:38 +02:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2016-08-31 18:45:38 +02:00
|
|
|
done();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-08-08 18:45:37 -05:00
|
|
|
it('should be able to make multiple requests without redefining pfx', (done) => {
|
2017-10-19 09:12:31 +01:00
|
|
|
const agent = request.agent({ pfx });
|
2021-11-30 19:42:15 -05:00
|
|
|
agent.get(testEndpoint).end((error, res) => {
|
|
|
|
|
assert.ifError(error);
|
2016-11-07 13:14:29 -08:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2024-04-25 22:48:35 +02:00
|
|
|
agent.get(new URL(testEndpoint)).end((error, res) => {
|
2021-11-30 19:42:15 -05:00
|
|
|
assert.ifError(error);
|
2016-11-07 13:14:29 -08:00
|
|
|
assert(res.ok);
|
2019-03-28 01:46:17 -05:00
|
|
|
assert.strictEqual('Safe and secure!', res.text);
|
2016-11-07 13:14:29 -08:00
|
|
|
done();
|
2017-10-19 09:12:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|