2021-07-22 23:04:56 +00:00
|
|
|
import { setDefaultProtocol } from 'insomnia-url';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { parse as urlParse } from 'url';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
import { escapeRegex } from '../common/misc';
|
2021-07-22 23:04:56 +00:00
|
|
|
import certificateUrlParse from './certificate-url-parse';
|
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);
|
2018-10-17 16:42:33 +00:00
|
|
|
const { hostname: cHostname, port: cPort } = certificateUrlParse(cHostWithProtocol);
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION `parseInt(null)` returns `NaN`
|
2017-06-21 20:56:19 +00:00
|
|
|
const assumedPort = parseInt(port) || DEFAULT_PORT;
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION `parseInt(null)` returns `NaN`
|
2017-06-21 20:56:19 +00:00
|
|
|
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
|
|
|
}
|