11import { originCheckMiddleware } from './origin-check.middleware' ;
22
3- // Mock getTrustedOrigins
3+ // Mock isTrustedOrigin (async version)
44jest . mock ( './auth.server' , ( ) => ( {
5- getTrustedOrigins : ( ) => [
6- 'http://localhost:3000' ,
7- 'http://localhost:3002' ,
8- 'https://app.trycomp.ai' ,
9- 'https://portal.trycomp.ai' ,
10- ] ,
5+ isTrustedOrigin : async ( origin : string ) => {
6+ const staticOrigins = [
7+ 'http://localhost:3000' ,
8+ 'http://localhost:3002' ,
9+ 'https://app.trycomp.ai' ,
10+ 'https://portal.trycomp.ai' ,
11+ ] ;
12+ if ( staticOrigins . includes ( origin ) ) return true ;
13+ try {
14+ const url = new URL ( origin ) ;
15+ return (
16+ url . hostname . endsWith ( '.trycomp.ai' ) ||
17+ url . hostname . endsWith ( '.staging.trycomp.ai' ) ||
18+ url . hostname . endsWith ( '.trust.inc' ) ||
19+ url . hostname === 'trust.inc'
20+ ) ;
21+ } catch {
22+ return false ;
23+ }
24+ } ,
1125} ) ) ;
1226
1327function createMockReq (
@@ -22,6 +36,9 @@ function createMockReq(
2236 } ;
2337}
2438
39+ /** Flush the microtask queue so async middleware completes. */
40+ const flushPromises = ( ) => new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
41+
2542function createMockRes ( ) : Record < string , unknown > & { statusCode ?: number ; body ?: unknown } {
2643 const res : Record < string , unknown > & { statusCode ?: number ; body ?: unknown } = { } ;
2744 res . status = jest . fn ( ) . mockImplementation ( ( code : number ) => {
@@ -66,44 +83,48 @@ describe('originCheckMiddleware', () => {
6683 expect ( next ) . toHaveBeenCalled ( ) ;
6784 } ) ;
6885
69- it ( 'should allow POST from trusted origin' , ( ) => {
86+ it ( 'should allow POST from trusted origin' , async ( ) => {
7087 const req = createMockReq ( 'POST' , '/v1/organization/api-keys' , 'http://localhost:3000' ) ;
7188 const res = createMockRes ( ) ;
7289 const next = jest . fn ( ) ;
7390
7491 originCheckMiddleware ( req as any , res as any , next ) ;
92+ await flushPromises ( ) ;
7593
7694 expect ( next ) . toHaveBeenCalled ( ) ;
7795 } ) ;
7896
79- it ( 'should block POST from untrusted origin' , ( ) => {
97+ it ( 'should block POST from untrusted origin' , async ( ) => {
8098 const req = createMockReq ( 'POST' , '/v1/organization/transfer-ownership' , 'http://evil.com' ) ;
8199 const res = createMockRes ( ) ;
82100 const next = jest . fn ( ) ;
83101
84102 originCheckMiddleware ( req as any , res as any , next ) ;
103+ await flushPromises ( ) ;
85104
86105 expect ( next ) . not . toHaveBeenCalled ( ) ;
87106 expect ( res . status ) . toHaveBeenCalledWith ( 403 ) ;
88107 } ) ;
89108
90- it ( 'should block DELETE from untrusted origin' , ( ) => {
109+ it ( 'should block DELETE from untrusted origin' , async ( ) => {
91110 const req = createMockReq ( 'DELETE' , '/v1/organization' , 'http://evil.com' ) ;
92111 const res = createMockRes ( ) ;
93112 const next = jest . fn ( ) ;
94113
95114 originCheckMiddleware ( req as any , res as any , next ) ;
115+ await flushPromises ( ) ;
96116
97117 expect ( next ) . not . toHaveBeenCalled ( ) ;
98118 expect ( res . status ) . toHaveBeenCalledWith ( 403 ) ;
99119 } ) ;
100120
101- it ( 'should block PATCH from untrusted origin' , ( ) => {
121+ it ( 'should block PATCH from untrusted origin' , async ( ) => {
102122 const req = createMockReq ( 'PATCH' , '/v1/members/123/role' , 'http://evil.com' ) ;
103123 const res = createMockRes ( ) ;
104124 const next = jest . fn ( ) ;
105125
106126 originCheckMiddleware ( req as any , res as any , next ) ;
127+ await flushPromises ( ) ;
107128
108129 expect ( next ) . not . toHaveBeenCalled ( ) ;
109130 expect ( res . status ) . toHaveBeenCalledWith ( 403 ) ;
@@ -139,12 +160,13 @@ describe('originCheckMiddleware', () => {
139160 expect ( next ) . toHaveBeenCalled ( ) ;
140161 } ) ;
141162
142- it ( 'should allow production origins' , ( ) => {
163+ it ( 'should allow production origins' , async ( ) => {
143164 const req = createMockReq ( 'POST' , '/v1/organization/api-keys' , 'https://app.trycomp.ai' ) ;
144165 const res = createMockRes ( ) ;
145166 const next = jest . fn ( ) ;
146167
147168 originCheckMiddleware ( req as any , res as any , next ) ;
169+ await flushPromises ( ) ;
148170
149171 expect ( next ) . toHaveBeenCalled ( ) ;
150172 } ) ;
0 commit comments