66
77import { expect } from "chai" ;
88import { Container } from "inversify" ;
9+ import express from "express" ;
910import { Config } from "../config" ;
1011import { NonceService } from "./nonce-service" ;
1112
@@ -66,15 +67,15 @@ describe("NonceService", () => {
6667 } ) ;
6768
6869 describe ( "validateOrigin" , ( ) => {
69- it ( "should accept requests from same origin" , ( ) => {
70+ it ( "should accept requests from expected SCM provider origin" , ( ) => {
7071 const req = {
7172 get : ( header : string ) => {
72- if ( header === "Origin" ) return "https://gitpod.io " ;
73+ if ( header === "Origin" ) return "https://github.com " ;
7374 return undefined ;
7475 } ,
75- } as any ;
76+ } as Partial < express . Request > as express . Request ;
7677
77- const isValid = nonceService . validateOrigin ( req ) ;
78+ const isValid = nonceService . validateOrigin ( req , "github.com" ) ;
7879 expect ( isValid ) . to . be . true ;
7980 } ) ;
8081
@@ -84,31 +85,56 @@ describe("NonceService", () => {
8485 if ( header === "Origin" ) return "https://evil.com" ;
8586 return undefined ;
8687 } ,
87- } as any ;
88+ } as Partial < express . Request > as express . Request ;
8889
89- const isValid = nonceService . validateOrigin ( req ) ;
90+ const isValid = nonceService . validateOrigin ( req , "github.com" ) ;
9091 expect ( isValid ) . to . be . false ;
9192 } ) ;
9293
9394 it ( "should reject requests without origin or referer" , ( ) => {
9495 const req = {
9596 get : ( ) => undefined ,
96- } as any ;
97+ } as Partial < express . Request > as express . Request ;
9798
98- const isValid = nonceService . validateOrigin ( req ) ;
99+ const isValid = nonceService . validateOrigin ( req , "github.com" ) ;
99100 expect ( isValid ) . to . be . false ;
100101 } ) ;
101102
102- it ( "should accept requests with valid referer" , ( ) => {
103+ it ( "should accept requests with valid referer from expected host " , ( ) => {
103104 const req = {
104105 get : ( header : string ) => {
105- if ( header === "Referer" ) return "https://gitpod.io/login " ;
106+ if ( header === "Referer" ) return "https://gitlab.com/oauth/authorize " ;
106107 return undefined ;
107108 } ,
108- } as any ;
109+ } as Partial < express . Request > as express . Request ;
109110
110- const isValid = nonceService . validateOrigin ( req ) ;
111+ const isValid = nonceService . validateOrigin ( req , "gitlab.com" ) ;
111112 expect ( isValid ) . to . be . true ;
112113 } ) ;
114+
115+ it ( "should work with different SCM providers" , ( ) => {
116+ const testCases = [
117+ { origin : "https://github.com" , expectedHost : "github.com" , shouldPass : true } ,
118+ { origin : "https://gitlab.com" , expectedHost : "gitlab.com" , shouldPass : true } ,
119+ { origin : "https://bitbucket.org" , expectedHost : "bitbucket.org" , shouldPass : true } ,
120+ { origin : "https://github.com" , expectedHost : "gitlab.com" , shouldPass : false } ,
121+ { origin : "https://evil.com" , expectedHost : "github.com" , shouldPass : false } ,
122+ ] ;
123+
124+ testCases . forEach ( ( { origin, expectedHost, shouldPass } ) => {
125+ const req = {
126+ get : ( header : string ) => {
127+ if ( header === "Origin" ) return origin ;
128+ return undefined ;
129+ } ,
130+ } as Partial < express . Request > as express . Request ;
131+
132+ const isValid = nonceService . validateOrigin ( req , expectedHost ) ;
133+ expect ( isValid ) . to . equal (
134+ shouldPass ,
135+ `${ origin } vs ${ expectedHost } should ${ shouldPass ? "pass" : "fail" } ` ,
136+ ) ;
137+ } ) ;
138+ } ) ;
113139 } ) ;
114140} ) ;
0 commit comments