(address: string, srvHost: string)
| 1158 | * @returns void |
| 1159 | */ |
| 1160 | export function checkParentDomainMatch(address: string, srvHost: string): void { |
| 1161 | // Remove trailing dot if exists on either the resolved address or the srv hostname |
| 1162 | const normalizedAddress = address.endsWith('.') ? address.slice(0, address.length - 1) : address; |
| 1163 | const normalizedSrvHost = srvHost.endsWith('.') ? srvHost.slice(0, srvHost.length - 1) : srvHost; |
| 1164 | |
| 1165 | const allCharacterBeforeFirstDot = /^.*?\./; |
| 1166 | const srvIsLessThanThreeParts = normalizedSrvHost.split('.').length < 3; |
| 1167 | // Remove all characters before first dot |
| 1168 | // Add leading dot back to string so |
| 1169 | // an srvHostDomain = '.trusted.site' |
| 1170 | // will not satisfy an addressDomain that endsWith '.fake-trusted.site' |
| 1171 | const addressDomain = `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`; |
| 1172 | let srvHostDomain = srvIsLessThanThreeParts |
| 1173 | ? normalizedSrvHost |
| 1174 | : `.${normalizedSrvHost.replace(allCharacterBeforeFirstDot, '')}`; |
| 1175 | |
| 1176 | if (!srvHostDomain.startsWith('.')) { |
| 1177 | srvHostDomain = '.' + srvHostDomain; |
| 1178 | } |
| 1179 | if ( |
| 1180 | srvIsLessThanThreeParts && |
| 1181 | normalizedAddress.split('.').length <= normalizedSrvHost.split('.').length |
| 1182 | ) { |
| 1183 | throw new MongoAPIError( |
| 1184 | 'Server record does not have at least one more domain level than parent URI' |
| 1185 | ); |
| 1186 | } |
| 1187 | if (!addressDomain.endsWith(srvHostDomain)) { |
| 1188 | throw new MongoAPIError('Server record does not share hostname with parent URI'); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * Perform a get request that returns status and body. |
no outgoing calls
no test coverage detected