MCPcopy
hub / github.com/mongodb/node-mongodb-native / checkParentDomainMatch

Function checkParentDomainMatch

src/utils.ts:1160–1190  ·  view source on GitHub ↗
(address: string, srvHost: string)

Source from the content-addressed store, hash-verified

1158 * @returns void
1159 */
1160export 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.

Callers 3

resolveSRVRecordFunction · 0.90
_pollMethod · 0.90
utils.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected