|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as dns from 'dns'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | + |
| 5 | +import { MongoAPIError, Server, ServerDescription, Topology } from '../../mongodb'; |
| 6 | +import { topologyWithPlaceholderClient } from '../../tools/utils'; |
| 7 | + |
| 8 | +describe('Initial DNS Seedlist Discovery (Prose Tests)', () => { |
| 9 | + describe('1. Allow SRVs with fewer than 3 . separated parts', function () { |
| 10 | + context('when running validation on an SRV string before DNS resolution', function () { |
| 11 | + /** |
| 12 | + * When running validation on an SRV string before DNS resolution, do not throw a error due to number of SRV parts. |
| 13 | + * - mongodb+srv://localhost |
| 14 | + * - mongodb+srv://mongo.localhost |
| 15 | + */ |
| 16 | + |
| 17 | + let client; |
| 18 | + |
| 19 | + beforeEach(async function () { |
| 20 | + // this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation |
| 21 | + |
| 22 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 23 | + return [ |
| 24 | + { |
| 25 | + name: 'resolved.mongo.localhost', |
| 26 | + port: 27017, |
| 27 | + weight: 0, |
| 28 | + priority: 0 |
| 29 | + } |
| 30 | + ]; |
| 31 | + }); |
| 32 | + |
| 33 | + sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => { |
| 34 | + throw { code: 'ENODATA' }; |
| 35 | + }); |
| 36 | + |
| 37 | + sinon.stub(Topology.prototype, 'selectServer').callsFake(async () => { |
| 38 | + return new Server( |
| 39 | + topologyWithPlaceholderClient([], {} as any), |
| 40 | + new ServerDescription('a:1'), |
| 41 | + {} as any |
| 42 | + ); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async function () { |
| 47 | + sinon.restore(); |
| 48 | + await client.close(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not error on an SRV because it has one domain level', async function () { |
| 52 | + client = await this.configuration.newClient('mongodb+srv://localhost', {}); |
| 53 | + await client.connect(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does not error on an SRV because it has two domain levels', async function () { |
| 57 | + client = await this.configuration.newClient('mongodb+srv://mongo.localhost', {}); |
| 58 | + await client.connect(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('2. Throw when return address does not end with SRV domain', function () { |
| 64 | + context( |
| 65 | + 'when given a host from DNS resolution that does NOT end with the original SRVs domain name', |
| 66 | + function () { |
| 67 | + /** |
| 68 | + * When given a returned address that does NOT end with the original SRV's domain name, throw a runtime error. |
| 69 | + * For this test, run each of the following cases: |
| 70 | + * - the SRV mongodb+srv://localhost resolving to localhost.mongodb |
| 71 | + * - the SRV mongodb+srv://mongo.local resolving to test_1.evil.local |
| 72 | + * - the SRV mongodb+srv://blogs.mongodb.com resolving to blogs.evil.com |
| 73 | + * Remember, the domain of an SRV with one or two . separated parts is the SRVs entire hostname. |
| 74 | + */ |
| 75 | + |
| 76 | + beforeEach(async function () { |
| 77 | + sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => { |
| 78 | + throw { code: 'ENODATA' }; |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + afterEach(async function () { |
| 83 | + sinon.restore(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('an SRV with one domain level causes a runtime error', async function () { |
| 87 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 88 | + return [ |
| 89 | + { |
| 90 | + name: 'localhost.mongodb', |
| 91 | + port: 27017, |
| 92 | + weight: 0, |
| 93 | + priority: 0 |
| 94 | + } |
| 95 | + ]; |
| 96 | + }); |
| 97 | + const err = await this.configuration |
| 98 | + .newClient('mongodb+srv://localhost', {}) |
| 99 | + .connect() |
| 100 | + .catch((e: any) => e); |
| 101 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 102 | + expect(err.message).to.equal('Server record does not share hostname with parent URI'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('an SRV with two domain levels causes a runtime error', async function () { |
| 106 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 107 | + return [ |
| 108 | + { |
| 109 | + name: 'test_1.evil.local', // this string only ends with part of the domain, not all of it! |
| 110 | + port: 27017, |
| 111 | + weight: 0, |
| 112 | + priority: 0 |
| 113 | + } |
| 114 | + ]; |
| 115 | + }); |
| 116 | + const err = await this.configuration |
| 117 | + .newClient('mongodb+srv://mongo.local', {}) |
| 118 | + .connect() |
| 119 | + .catch(e => e); |
| 120 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 121 | + expect(err.message).to.equal('Server record does not share hostname with parent URI'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('an SRV with three or more domain levels causes a runtime error', async function () { |
| 125 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 126 | + return [ |
| 127 | + { |
| 128 | + name: 'blogs.evil.com', |
| 129 | + port: 27017, |
| 130 | + weight: 0, |
| 131 | + priority: 0 |
| 132 | + } |
| 133 | + ]; |
| 134 | + }); |
| 135 | + const err = await this.configuration |
| 136 | + .newClient('mongodb+srv://blogs.mongodb.com', {}) |
| 137 | + .connect() |
| 138 | + .catch(e => e); |
| 139 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 140 | + expect(err.message).to.equal('Server record does not share hostname with parent URI'); |
| 141 | + }); |
| 142 | + } |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('3. Throw when return address is identical to SRV hostname', function () { |
| 147 | + /** |
| 148 | + * When given a returned address that is identical to the SRV hostname and the SRV hostname has fewer than three . separated parts, throw a runtime error. |
| 149 | + * For this test, run each of the following cases: |
| 150 | + * - the SRV mongodb+srv://localhost resolving to localhost |
| 151 | + * - the SRV mongodb+srv://mongo.local resolving to mongo.local |
| 152 | + */ |
| 153 | + |
| 154 | + context( |
| 155 | + 'when given a host from DNS resolution that is identical to the original SRVs hostname', |
| 156 | + function () { |
| 157 | + beforeEach(async function () { |
| 158 | + sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => { |
| 159 | + throw { code: 'ENODATA' }; |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + afterEach(async function () { |
| 164 | + sinon.restore(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('an SRV with one domain level causes a runtime error', async function () { |
| 168 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 169 | + return [ |
| 170 | + { |
| 171 | + name: 'localhost', |
| 172 | + port: 27017, |
| 173 | + weight: 0, |
| 174 | + priority: 0 |
| 175 | + } |
| 176 | + ]; |
| 177 | + }); |
| 178 | + const err = await this.configuration |
| 179 | + .newClient('mongodb+srv://localhost', {}) |
| 180 | + .connect() |
| 181 | + .catch(e => e); |
| 182 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 183 | + expect(err.message).to.equal( |
| 184 | + 'Server record does not have at least one more domain level than parent URI' |
| 185 | + ); |
| 186 | + }); |
| 187 | + |
| 188 | + it('an SRV with two domain levels causes a runtime error', async function () { |
| 189 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 190 | + return [ |
| 191 | + { |
| 192 | + name: 'mongo.local', |
| 193 | + port: 27017, |
| 194 | + weight: 0, |
| 195 | + priority: 0 |
| 196 | + } |
| 197 | + ]; |
| 198 | + }); |
| 199 | + const err = await this.configuration |
| 200 | + .newClient('mongodb+srv://mongo.local', {}) |
| 201 | + .connect() |
| 202 | + .catch(e => e); |
| 203 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 204 | + expect(err.message).to.equal( |
| 205 | + 'Server record does not have at least one more domain level than parent URI' |
| 206 | + ); |
| 207 | + }); |
| 208 | + } |
| 209 | + ); |
| 210 | + }); |
| 211 | + |
| 212 | + describe('4. Throw when return address does not contain . separating shared part of domain', function () { |
| 213 | + /** |
| 214 | + * When given a returned address that does NOT share the domain name of the SRV record because it's missing a ., throw a runtime error. |
| 215 | + * For this test, run each of the following cases: |
| 216 | + * - the SRV mongodb+srv://localhost resolving to test_1.cluster_1localhost |
| 217 | + * - the SRV mongodb+srv://mongo.local resolving to test_1.my_hostmongo.local |
| 218 | + * - the SRV mongodb+srv://blogs.mongodb.com resolving to cluster.testmongodb.com |
| 219 | + */ |
| 220 | + |
| 221 | + context( |
| 222 | + 'when given a returned address that does NOT share the domain name of the SRV record because its missing a `.`', |
| 223 | + function () { |
| 224 | + beforeEach(async function () { |
| 225 | + sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => { |
| 226 | + throw { code: 'ENODATA' }; |
| 227 | + }); |
| 228 | + }); |
| 229 | + |
| 230 | + afterEach(async function () { |
| 231 | + sinon.restore(); |
| 232 | + }); |
| 233 | + |
| 234 | + it('an SRV with one domain level causes a runtime error', async function () { |
| 235 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 236 | + return [ |
| 237 | + { |
| 238 | + name: 'test_1.cluster_1localhost', |
| 239 | + port: 27017, |
| 240 | + weight: 0, |
| 241 | + priority: 0 |
| 242 | + } |
| 243 | + ]; |
| 244 | + }); |
| 245 | + const err = await this.configuration |
| 246 | + .newClient('mongodb+srv://localhost', {}) |
| 247 | + .connect() |
| 248 | + .catch(e => e); |
| 249 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 250 | + expect(err.message).to.equal('Server record does not share hostname with parent URI'); |
| 251 | + }); |
| 252 | + |
| 253 | + it('an SRV with two domain levels causes a runtime error', async function () { |
| 254 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 255 | + return [ |
| 256 | + { |
| 257 | + name: 'test_1.my_hostmongo.local', |
| 258 | + port: 27017, |
| 259 | + weight: 0, |
| 260 | + priority: 0 |
| 261 | + } |
| 262 | + ]; |
| 263 | + }); |
| 264 | + const err = await this.configuration |
| 265 | + .newClient('mongodb+srv://mongo.local', {}) |
| 266 | + .connect() |
| 267 | + .catch(e => e); |
| 268 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 269 | + expect(err.message).to.equal('Server record does not share hostname with parent URI'); |
| 270 | + }); |
| 271 | + |
| 272 | + it('an SRV with three domain levels causes a runtime error', async function () { |
| 273 | + sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { |
| 274 | + return [ |
| 275 | + { |
| 276 | + name: 'cluster.testmongodb.com', |
| 277 | + port: 27017, |
| 278 | + weight: 0, |
| 279 | + priority: 0 |
| 280 | + } |
| 281 | + ]; |
| 282 | + }); |
| 283 | + const err = await this.configuration |
| 284 | + .newClient('mongodb+srv://blogs.mongodb.com', {}) |
| 285 | + .connect() |
| 286 | + .catch(e => e); |
| 287 | + expect(err).to.be.instanceOf(MongoAPIError); |
| 288 | + expect(err.message).to.equal('Server record does not share hostname with parent URI'); |
| 289 | + }); |
| 290 | + } |
| 291 | + ); |
| 292 | + }); |
| 293 | +}); |
0 commit comments