1- import {
2- inputValidationMock ,
3- inputValidationMockFns ,
4- } from '@sim/testing/mocks/input-validation.mock'
1+ import { MongoClient } from 'mongodb'
52import { beforeEach , describe , expect , it , vi } from 'vitest'
63
7- const { mockClient, mockMongoClient } = vi . hoisted ( ( ) => {
8- const client = {
9- close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
10- connect : vi . fn ( ) ,
11- }
12- return {
13- mockClient : client ,
14- mockMongoClient : vi . fn ( function MockMongoClient ( ) {
15- return client
16- } ) ,
17- }
18- } )
19-
20- vi . mock ( 'mongodb' , ( ) => ( { MongoClient : mockMongoClient } ) )
4+ const { mockResolve } = vi . hoisted ( ( ) => ( {
5+ mockResolve : vi . fn ( ) ,
6+ } ) )
217
22- vi . mock ( '@/lib/core/security/input-validation.server' , ( ) => inputValidationMock )
8+ vi . mock ( '@sim/security/dns' , ( ) => ( {
9+ resolveHostAddresses : mockResolve ,
10+ preferIpv4 : ( addresses : string [ ] ) =>
11+ addresses . find ( ( address ) => address . includes ( '.' ) ) ?? addresses [ 0 ] ,
12+ } ) )
2313
2414import { createMongodbClient , type MongodbConnectionConfig } from '@/lib/internal/mongodb/client'
2515
26- const { mockCreatePinnedLookup, mockValidateDatabaseHost } = inputValidationMockFns
27-
2816const CONNECTION_CONFIG : MongodbConnectionConfig = {
2917 host : 'db.example.com' ,
3018 port : 27017 ,
@@ -36,33 +24,79 @@ const CONNECTION_CONFIG: MongodbConnectionConfig = {
3624}
3725
3826describe ( 'MongoDB client' , ( ) => {
27+ let connect : ReturnType < typeof vi . spyOn >
28+ let close : ReturnType < typeof vi . spyOn >
29+
3930 beforeEach ( ( ) => {
40- mockClient . close . mockResolvedValue ( undefined )
41- mockClient . connect . mockResolvedValue ( mockClient )
42- mockValidateDatabaseHost . mockResolvedValue ( {
43- isValid : true ,
44- resolvedIP : '93.184.216.34' ,
45- originalHostname : 'db.example.com' ,
31+ // A wildcard DNS zone answers any name, including one shaped like a seed list.
32+ mockResolve . mockResolvedValue ( { addresses : [ '93.184.216.34' ] , preferred : '93.184.216.34' } )
33+ connect = vi . spyOn ( MongoClient . prototype , 'connect' ) . mockImplementation ( async function (
34+ this : MongoClient
35+ ) {
36+ return this
4637 } )
47- mockCreatePinnedLookup . mockReturnValue ( 'pinned-lookup' )
38+ close = vi . spyOn ( MongoClient . prototype , 'close' ) . mockResolvedValue ( undefined )
4839 } )
4940
50- it ( 'does not construct a client when DNS validation fails' , async ( ) => {
51- mockValidateDatabaseHost . mockResolvedValue ( {
52- isValid : false ,
53- error : 'host resolves to a blocked IP address' ,
54- } )
41+ it ( 'connects to exactly the validated host, without following advertised members' , async ( ) => {
42+ const client = await createMongodbClient ( CONNECTION_CONFIG )
43+
44+ expect ( client . options . hosts . map ( String ) ) . toEqual ( [ 'db.example.com:27017' ] )
45+ expect ( client . options . directConnection ) . toBe ( true )
46+ expect ( client . options . proxyHost ) . toBeUndefined ( )
47+ expect ( client . options . loadBalanced ) . toBe ( false )
48+ expect ( client . options . tls ) . toBe ( true )
49+ expect ( client . options . credentials ?. username ) . toBe ( 'user@example.com' )
50+ expect ( client . options . credentials ?. password ) . toBe ( 'p@ss word' )
51+ expect ( client . options . credentials ?. source ) . toBe ( 'admin' )
52+ } )
53+
54+ it ( 'authenticates against the database when no auth source is given' , async ( ) => {
55+ const client = await createMongodbClient ( { ...CONNECTION_CONFIG , authSource : '' } )
56+
57+ expect ( client . options . credentials ?. source ) . toBe ( 'application' )
58+ } )
5559
56- await expect ( createMongodbClient ( CONNECTION_CONFIG ) ) . rejects . toThrow (
57- 'host resolves to a blocked IP address'
60+ it . each ( [
61+ [ 'a seed list naming an internal address' , '127.0.0.1:6379,db.example.com' ] ,
62+ [ 'a path and options after the host' , 'db.example.com/admin?directConnection=false' ] ,
63+ ] ) ( 'refuses %s as the host' , async ( _case , host ) => {
64+ await expect ( createMongodbClient ( { ...CONNECTION_CONFIG , host } ) ) . rejects . toThrow (
65+ 'host must be a single hostname or IP address'
5866 )
59- expect ( mockMongoClient ) . not . toHaveBeenCalled ( )
67+ expect ( connect ) . not . toHaveBeenCalled ( )
68+ } )
69+
70+ it . each ( [
71+ [ 'connection-string options' , 'db?directConnection=false&proxyHost=127.0.0.1&proxyPort=1080' ] ,
72+ [ 'a path separator' , 'app/other' ] ,
73+ [ 'a dot' , 'app.other' ] ,
74+ [ 'more than 63 bytes in fewer characters' , 'é' . repeat ( 32 ) ] ,
75+ ] ) ( 'refuses a database name carrying %s' , async ( _case , database ) => {
76+ await expect ( createMongodbClient ( { ...CONNECTION_CONFIG , database } ) ) . rejects . toThrow (
77+ 'database must be'
78+ )
79+ expect ( connect ) . not . toHaveBeenCalled ( )
80+ } )
81+
82+ it . each ( [ 0 , 27017.5 , 70000 ] ) ( 'refuses port %s' , async ( port ) => {
83+ await expect ( createMongodbClient ( { ...CONNECTION_CONFIG , port } ) ) . rejects . toThrow (
84+ 'port must be an integer'
85+ )
86+ expect ( connect ) . not . toHaveBeenCalled ( )
87+ } )
88+
89+ it ( 'does not connect when the host resolves to a private address' , async ( ) => {
90+ mockResolve . mockResolvedValue ( { addresses : [ '10.0.0.5' ] , preferred : '10.0.0.5' } )
91+
92+ await expect ( createMongodbClient ( CONNECTION_CONFIG ) ) . rejects . toThrow ( 'private or reserved' )
93+ expect ( connect ) . not . toHaveBeenCalled ( )
6094 } )
6195
6296 it ( 'closes a partially connected client when connection fails' , async ( ) => {
63- mockClient . connect . mockRejectedValue ( new Error ( 'handshake failed' ) )
97+ connect . mockRejectedValue ( new Error ( 'handshake failed' ) )
6498
6599 await expect ( createMongodbClient ( CONNECTION_CONFIG ) ) . rejects . toThrow ( 'handshake failed' )
66- expect ( mockClient . close ) . toHaveBeenCalledOnce ( )
100+ expect ( close ) . toHaveBeenCalledOnce ( )
67101 } )
68102} )
0 commit comments