2018-06-25 17:42:50 +00:00
|
|
|
import { parse as urlParse } from 'url';
|
2017-06-21 20:56:19 +00:00
|
|
|
import certificateUrlParse from './certificate-url-parse';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { escapeRegex } from '../common/misc';
|
|
|
|
import { setDefaultProtocol } from 'insomnia-url';
|
2017-06-21 20:56:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_PORT = 443;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function urlMatchesCertHost(certificateHost, requestUrl) {
|
2017-06-21 20:56:19 +00:00
|
|
|
const cHostWithProtocol = setDefaultProtocol(certificateHost, 'https:');
|
2018-06-25 17:42:50 +00:00
|
|
|
const { hostname, port } = urlParse(requestUrl);
|
|
|
|
const { hostname: cHostname, port: cPort } = certificateUrlParse(
|
|
|
|
cHostWithProtocol
|
|
|
|
);
|
2017-06-21 20:56:19 +00:00
|
|
|
|
|
|
|
const assumedPort = parseInt(port) || DEFAULT_PORT;
|
|
|
|
const assumedCPort = parseInt(cPort) || DEFAULT_PORT;
|
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
const cHostnameRegex = escapeRegex(cHostname || '').replace(/\\\*/g, '.*');
|
2017-11-04 21:00:16 +00:00
|
|
|
const cPortRegex = escapeRegex(cPort || '').replace(/\\\*/g, '.*');
|
2017-06-21 20:56:19 +00:00
|
|
|
|
2017-11-04 21:00:16 +00:00
|
|
|
// Check ports
|
|
|
|
if ((cPort + '').includes('*')) {
|
|
|
|
if (!(port || '').match(`^${cPortRegex}$`)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (assumedCPort !== assumedPort) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check hostnames
|
|
|
|
if (!(hostname || '').match(`^${cHostnameRegex}$`)) {
|
2017-09-17 14:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-04 21:00:16 +00:00
|
|
|
// Everything matches
|
|
|
|
return true;
|
2017-06-21 20:56:19 +00:00
|
|
|
}
|