@@ -133,4 +133,161 @@ describe('middlewares', () => {
133133 } ) ;
134134 } ) ;
135135 } ) ;
136+
137+ it ( 'should not succeed if the ip does not belong to masterKeyIps list' , ( ) => {
138+ AppCache . put ( fakeReq . body . _ApplicationId , {
139+ masterKey : 'masterKey' ,
140+ masterKeyIps : [ 'ip1' , 'ip2' ]
141+ } ) ;
142+ fakeReq . ip = 'ip3' ;
143+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
144+ middlewares . handleParseHeaders ( fakeReq , fakeRes ) ;
145+ expect ( fakeRes . status ) . toHaveBeenCalledWith ( 403 ) ;
146+ } ) ;
147+
148+ it ( 'should succeed if the ip does belong to masterKeyIps list' , ( done ) => {
149+ AppCache . put ( fakeReq . body . _ApplicationId , {
150+ masterKey : 'masterKey' ,
151+ masterKeyIps : [ 'ip1' , 'ip2' ]
152+ } ) ;
153+ fakeReq . ip = 'ip1' ;
154+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
155+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
156+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
157+ done ( ) ;
158+ } ) ;
159+ } ) ;
160+
161+ it ( 'should not succeed if the connection.remoteAddress does not belong to masterKeyIps list' , ( ) => {
162+ AppCache . put ( fakeReq . body . _ApplicationId , {
163+ masterKey : 'masterKey' ,
164+ masterKeyIps : [ 'ip1' , 'ip2' ]
165+ } ) ;
166+ fakeReq . connection = { remoteAddress : 'ip3' } ;
167+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
168+ middlewares . handleParseHeaders ( fakeReq , fakeRes ) ;
169+ expect ( fakeRes . status ) . toHaveBeenCalledWith ( 403 ) ;
170+ } ) ;
171+
172+ it ( 'should succeed if the connection.remoteAddress does belong to masterKeyIps list' , ( done ) => {
173+ AppCache . put ( fakeReq . body . _ApplicationId , {
174+ masterKey : 'masterKey' ,
175+ masterKeyIps : [ 'ip1' , 'ip2' ]
176+ } ) ;
177+ fakeReq . connection = { remoteAddress : 'ip1' } ;
178+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
179+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
180+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
181+ done ( ) ;
182+ } ) ;
183+ } ) ;
184+
185+ it ( 'should not succeed if the socket.remoteAddress does not belong to masterKeyIps list' , ( ) => {
186+ AppCache . put ( fakeReq . body . _ApplicationId , {
187+ masterKey : 'masterKey' ,
188+ masterKeyIps : [ 'ip1' , 'ip2' ]
189+ } ) ;
190+ fakeReq . socket = { remoteAddress : 'ip3' } ;
191+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
192+ middlewares . handleParseHeaders ( fakeReq , fakeRes ) ;
193+ expect ( fakeRes . status ) . toHaveBeenCalledWith ( 403 ) ;
194+ } ) ;
195+
196+ it ( 'should succeed if the socket.remoteAddress does belong to masterKeyIps list' , ( done ) => {
197+ AppCache . put ( fakeReq . body . _ApplicationId , {
198+ masterKey : 'masterKey' ,
199+ masterKeyIps : [ 'ip1' , 'ip2' ]
200+ } ) ;
201+ fakeReq . socket = { remoteAddress : 'ip1' } ;
202+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
203+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
204+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
205+ done ( ) ;
206+ } ) ;
207+ } ) ;
208+
209+ it ( 'should not succeed if the connection.socket.remoteAddress does not belong to masterKeyIps list' , ( ) => {
210+ AppCache . put ( fakeReq . body . _ApplicationId , {
211+ masterKey : 'masterKey' ,
212+ masterKeyIps : [ 'ip1' , 'ip2' ]
213+ } ) ;
214+ fakeReq . connection = { socket : { remoteAddress : 'ip3' } } ;
215+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
216+ middlewares . handleParseHeaders ( fakeReq , fakeRes ) ;
217+ expect ( fakeRes . status ) . toHaveBeenCalledWith ( 403 ) ;
218+ } ) ;
219+
220+ it ( 'should succeed if the connection.socket.remoteAddress does belong to masterKeyIps list' , ( done ) => {
221+ AppCache . put ( fakeReq . body . _ApplicationId , {
222+ masterKey : 'masterKey' ,
223+ masterKeyIps : [ 'ip1' , 'ip2' ]
224+ } ) ;
225+ fakeReq . connection = { socket : { remoteAddress : 'ip1' } } ;
226+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
227+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
228+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
229+ done ( ) ;
230+ } ) ;
231+ } ) ;
232+
233+ it ( 'should allow any ip to use masterKey if masterKeyIps is empty' , ( done ) => {
234+ AppCache . put ( fakeReq . body . _ApplicationId , {
235+ masterKey : 'masterKey' ,
236+ masterKeyIps : [ ]
237+ } ) ;
238+ fakeReq . ip = 'ip1' ;
239+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
240+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
241+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
242+ done ( ) ;
243+ } ) ;
244+ } ) ;
245+
246+ it ( 'should succeed if xff header does belong to masterKeyIps' , ( done ) => {
247+ AppCache . put ( fakeReq . body . _ApplicationId , {
248+ masterKey : 'masterKey' ,
249+ masterKeyIps : [ 'ip1' ]
250+ } ) ;
251+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
252+ fakeReq . headers [ 'x-forwarded-for' ] = 'ip1, ip2, ip3' ;
253+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
254+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
255+ done ( ) ;
256+ } ) ;
257+ } ) ;
258+
259+ it ( 'should succeed if xff header with one ip does belong to masterKeyIps' , ( done ) => {
260+ AppCache . put ( fakeReq . body . _ApplicationId , {
261+ masterKey : 'masterKey' ,
262+ masterKeyIps : [ 'ip1' ]
263+ } ) ;
264+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
265+ fakeReq . headers [ 'x-forwarded-for' ] = 'ip1' ;
266+ middlewares . handleParseHeaders ( fakeReq , fakeRes , ( ) => {
267+ expect ( fakeRes . status ) . not . toHaveBeenCalled ( ) ;
268+ done ( ) ;
269+ } ) ;
270+ } ) ;
271+
272+ it ( 'should not succeed if xff header does not belong to masterKeyIps' , ( ) => {
273+ AppCache . put ( fakeReq . body . _ApplicationId , {
274+ masterKey : 'masterKey' ,
275+ masterKeyIps : [ 'ip4' ]
276+ } ) ;
277+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
278+ fakeReq . headers [ 'x-forwarded-for' ] = 'ip1, ip2, ip3' ;
279+ middlewares . handleParseHeaders ( fakeReq , fakeRes ) ;
280+ expect ( fakeRes . status ) . toHaveBeenCalledWith ( 403 ) ;
281+ } ) ;
282+
283+ it ( 'should not succeed if xff header is empty and masterKeyIps is set' , ( ) => {
284+ AppCache . put ( fakeReq . body . _ApplicationId , {
285+ masterKey : 'masterKey' ,
286+ masterKeyIps : [ 'ip1' ]
287+ } ) ;
288+ fakeReq . headers [ 'x-parse-master-key' ] = 'masterKey' ;
289+ fakeReq . headers [ 'x-forwarded-for' ] = '' ;
290+ middlewares . handleParseHeaders ( fakeReq , fakeRes ) ;
291+ expect ( fakeRes . status ) . toHaveBeenCalledWith ( 403 ) ;
292+ } ) ;
136293} ) ;
0 commit comments