@@ -18,16 +18,26 @@ import Proxy from '../src/proxy';
1818const TEST_DEFAULT_REPO = {
1919 url : 'https://github.com/finos/git-proxy.git' ,
2020 name : 'git-proxy' ,
21- project : 'finos/gitproxy ' ,
21+ project : 'finos/git-proxy ' ,
2222 host : 'github.com' ,
23+ proxyUrlPrefix : '/github.com/finos/git-proxy.git' ,
2324} ;
2425
2526const TEST_GITLAB_REPO = {
2627 url : 'https://gitlab.com/gitlab-community/meta.git' ,
2728 name : 'gitlab' ,
2829 project : 'gitlab-community/meta' ,
2930 host : 'gitlab.com' ,
30- proxyUrlPrefix : 'gitlab.com/gitlab-community/meta.git' ,
31+ proxyUrlPrefix : '/gitlab.com/gitlab-community/meta.git' ,
32+ } ;
33+
34+ const TEST_UNKNOWN_REPO = {
35+ url : 'https://github.com/finos/fdc3.git' ,
36+ name : 'fdc3' ,
37+ project : 'finos/fdc3' ,
38+ host : 'github.com' ,
39+ proxyUrlPrefix : '/github.com/finos/fdc3.git' ,
40+ fallbackUrlPrefix : '/finos/fdc3.git' ,
3141} ;
3242
3343describe ( 'proxy route filter middleware' , ( ) => {
@@ -42,6 +52,10 @@ describe('proxy route filter middleware', () => {
4252 sinon . restore ( ) ;
4353 } ) ;
4454
55+ after ( ( ) => {
56+ sinon . restore ( ) ;
57+ } ) ;
58+
4559 it ( 'should reject invalid git requests with 400' , async ( ) => {
4660 const res = await chai
4761 . request ( app )
@@ -50,7 +64,7 @@ describe('proxy route filter middleware', () => {
5064 . set ( 'accept' , 'application/x-git-upload-pack-request' ) ;
5165
5266 expect ( res ) . to . have . status ( 400 ) ;
53- expect ( res . text ) . to . equal ( 'Invalid request received' ) ;
67+ expect ( res . text ) . to . contain ( 'Invalid request received' ) ;
5468 } ) ;
5569
5670 it ( 'should handle blocked requests and return custom packet message' , async ( ) => {
@@ -68,7 +82,7 @@ describe('proxy route filter middleware', () => {
6882 . send ( Buffer . from ( '0000' ) )
6983 . buffer ( ) ;
7084
71- expect ( res . status ) . to . equal ( 200 ) ;
85+ expect ( res . status ) . to . equal ( 403 ) ;
7286 expect ( res . text ) . to . contain ( 'You shall not push!' ) ;
7387 expect ( res . headers [ 'content-type' ] ) . to . include ( 'application/x-git-receive-pack-result' ) ;
7488 expect ( res . headers [ 'x-frame-options' ] ) . to . equal ( 'DENY' ) ;
@@ -321,13 +335,6 @@ describe('proxy express application', async () => {
321335 } ;
322336
323337 before ( async ( ) => {
324- // pass through requests
325- sinon . stub ( chain , 'executeChain' ) . resolves ( {
326- blocked : false ,
327- blockedMessage : '' ,
328- error : false ,
329- } ) ;
330-
331338 // start the API and proxy
332339 proxy = new Proxy ( ) ;
333340 apiApp = await service . start ( proxy ) ;
@@ -364,7 +371,7 @@ describe('proxy express application', async () => {
364371 // proxy a fetch request
365372 const res = await chai
366373 . request ( proxy . getExpressApp ( ) )
367- . get ( '/github.com/finos/git-proxy.git/ info/refs?service=git-upload-pack' )
374+ . get ( ` ${ TEST_DEFAULT_REPO . proxyUrlPrefix } / info/refs?service=git-upload-pack` )
368375 . set ( 'user-agent' , 'git/2.42.0' )
369376 . set ( 'accept' , 'application/x-git-upload-pack-request' )
370377 . buffer ( ) ;
@@ -373,11 +380,11 @@ describe('proxy express application', async () => {
373380 expect ( res . text ) . to . contain ( 'git-upload-pack' ) ;
374381 } ) ;
375382
376- it ( 'should proxy requests for the default GitHub repository using the backwards compatibility URL' , async function ( ) {
383+ it ( 'should proxy requests for the default GitHub repository using the fallback URL' , async function ( ) {
377384 // proxy a fetch request using a fallback URL
378385 const res = await chai
379386 . request ( proxy . getExpressApp ( ) )
380- . get ( '/finos/git-proxy.git /info/refs?service=git-upload-pack' )
387+ . get ( ` ${ TEST_DEFAULT_REPO . proxyUrlPrefix } /info/refs?service=git-upload-pack` )
381388 . set ( 'user-agent' , 'git/2.42.0' )
382389 . set ( 'accept' , 'application/x-git-upload-pack-request' )
383390 . buffer ( ) ;
@@ -415,7 +422,7 @@ describe('proxy express application', async () => {
415422 // proxy a request to the new repo
416423 const res2 = await chai
417424 . request ( proxy . getExpressApp ( ) )
418- . get ( `/ ${ TEST_GITLAB_REPO . proxyUrlPrefix } /info/refs?service=git-upload-pack` )
425+ . get ( `${ TEST_GITLAB_REPO . proxyUrlPrefix } /info/refs?service=git-upload-pack` )
419426 . set ( 'user-agent' , 'git/2.42.0' )
420427 . set ( 'accept' , 'application/x-git-upload-pack-request' )
421428 . buffer ( ) ;
@@ -442,23 +449,53 @@ describe('proxy express application', async () => {
442449 res . should . have . status ( 200 ) ;
443450
444451 // confirm that its gone from the DB
445- repo = await db . getRepoByUrl (
446- TEST_GITLAB_REPO . url ,
452+ repo = await db . getRepoByUrl ( TEST_GITLAB_REPO . url ) ;
453+ expect (
454+ repo ,
447455 'The GitLab repo still existed in the database after it should have been deleted...' ,
448- ) ;
449- expect ( repo ) . to . be . null ;
456+ ) . to . be . null ;
450457
451458 // give the proxy half a second to restart
452459 await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
453460
454461 // try (and fail) to proxy a request to gitlab.com
455462 const res2 = await chai
456463 . request ( proxy . getExpressApp ( ) )
457- . get ( `/ ${ TEST_GITLAB_REPO . proxyUrlPrefix } /info/refs?service=git-upload-pack` )
464+ . get ( `${ TEST_GITLAB_REPO . proxyUrlPrefix } /info/refs?service=git-upload-pack` )
458465 . set ( 'user-agent' , 'git/2.42.0' )
459466 . set ( 'accept' , 'application/x-git-upload-pack-request' )
460467 . buffer ( ) ;
461468
462- res2 . should . have . status ( 404 ) ;
469+ res2 . should . have . status ( 403 ) ;
470+ } ) . timeout ( 5000 ) ;
471+
472+ it ( 'should not proxy requests for an unknown project' , async function ( ) {
473+ // We are testing that the proxy stops proxying requests for a particular origin
474+ // The chain is stubbed and will always passthrough requests, hence, we are only checking what hosts are proxied.
475+
476+ // the gitlab test repo should already exist
477+ const repo = await db . getRepoByUrl ( TEST_UNKNOWN_REPO . url ) ;
478+ expect (
479+ repo ,
480+ 'The unknown (but real) repo existed in the database which is not expected for this test' ,
481+ ) . to . be . null ;
482+
483+ // try (and fail) to proxy a request to the repo directly
484+ const res = await chai
485+ . request ( proxy . getExpressApp ( ) )
486+ . get ( `${ TEST_UNKNOWN_REPO . proxyUrlPrefix } /info/refs?service=git-upload-pack` )
487+ . set ( 'user-agent' , 'git/2.42.0' )
488+ . set ( 'accept' , 'application/x-git-upload-pack-request' )
489+ . buffer ( ) ;
490+ res . should . have . status ( 403 ) ;
491+
492+ // try (and fail) to proxy a request to the repo via the fallback URL directly
493+ const res2 = await chai
494+ . request ( proxy . getExpressApp ( ) )
495+ . get ( `${ TEST_UNKNOWN_REPO . fallbackUrlPrefix } /info/refs?service=git-upload-pack` )
496+ . set ( 'user-agent' , 'git/2.42.0' )
497+ . set ( 'accept' , 'application/x-git-upload-pack-request' )
498+ . buffer ( ) ;
499+ res2 . should . have . status ( 403 ) ;
463500 } ) . timeout ( 5000 ) ;
464501} ) ;
0 commit comments