2017-06-21 20:56:19 +00:00
|
|
|
import {parse as urlParse} from 'url';
|
|
|
|
import certificateUrlParse from './certificate-url-parse';
|
2017-11-26 20:45:40 +00:00
|
|
|
import {escapeRegex} from '../common/misc';
|
|
|
|
import {setDefaultProtocol} from 'insomnia-url';
|
2017-06-21 20:56:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_PORT = 443;
|
|
|
|
|
2017-09-17 14:04:56 +00:00
|
|
|
export function urlMatchesCertHost (certificateHost, requestUrl) {
|
2017-06-21 20:56:19 +00:00
|
|
|
const cHostWithProtocol = setDefaultProtocol(certificateHost, 'https:');
|
|
|
|
const {hostname, port} = urlParse(requestUrl);
|
|
|
|
const {hostname: cHostname, port: cPort} = certificateUrlParse(cHostWithProtocol);
|
|
|
|
|
|
|
|
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
|
|
|
}
|