@@ -2,20 +2,14 @@ import fs from "node:fs/promises";
22import https from "node:https" ;
33import tls from "node:tls" ;
44import { ProxyAgent } from "proxy-agent" ;
5- import {
6- afterAll ,
7- beforeAll ,
8- describe ,
9- expect ,
10- it ,
11- onTestFinished ,
12- vi ,
13- } from "vitest" ;
5+ import { describe , expect , it , onTestFinished , vi } from "vitest" ;
146import { WebSocketServer } from "ws" ;
157
168import { CoderApi } from "@/api/coderApi" ;
179import { createHttpAgent } from "@/api/httpAgent" ;
10+ import { CrossOriginRedirectError } from "@/error/crossOriginRedirectError" ;
1811
12+ import { startRecordingServer } from "../../mocks/recordingServer" ;
1913import {
2014 clearProxyEnv ,
2115 config ,
@@ -46,71 +40,90 @@ vi.mock("@/api/httpAgent", async (importActual) => ({
4640
4741describe ( "TLS key exchange" , ( ) => {
4842 const certPath = getFixturePath ( "tls" , "self-signed.crt" ) ;
49- let ca : Buffer ;
50- let origin : string ;
51- let server : https . Server ;
5243
53- beforeAll ( async ( ) => {
54- ca = await fs . readFile ( certPath ) ;
44+ /**
45+ * Serves HTTPS and WebSocket with only the test groups. `redirects` maps a
46+ * path to a 302 location; every other path answers 200.
47+ */
48+ async function setup ( redirects : Record < string , string > = { } ) {
49+ const ca = await fs . readFile ( certPath ) ;
5550 vi . mocked ( createHttpAgent ) . mockResolvedValue (
5651 new ProxyAgent ( { ca, getProxyForUrl : ( ) => "" } ) ,
5752 ) ;
58- server = https . createServer (
53+ const server = https . createServer (
5954 {
6055 key : await fs . readFile ( getFixturePath ( "tls" , "self-signed.key" ) ) ,
6156 cert : ca ,
6257 ecdhCurve : TEST_ECDH_CURVE ,
6358 } ,
6459 ( req , res ) => {
65- if ( req . url === "/old" ) {
66- res . writeHead ( 302 , { location : "/new" } ) ;
60+ const location = redirects [ req . url ?? "" ] ;
61+ if ( location ) {
62+ res . writeHead ( 302 , { location } ) ;
6763 }
6864 res . end ( ) ;
6965 } ,
7066 ) ;
7167 new WebSocketServer ( { server } ) . on ( "connection" , ( ws ) => ws . close ( ) ) ;
68+ onTestFinished (
69+ ( ) => new Promise < void > ( ( resolve ) => server . close ( ( ) => resolve ( ) ) ) ,
70+ ) ;
7271 // The fixture certificate is issued for localhost.
7372 await new Promise < void > ( ( resolve ) =>
7473 server . listen ( 0 , "localhost" , resolve ) ,
7574 ) ;
76- origin = `https://localhost:${ ( server . address ( ) as AddressInfo ) . port } ` ;
77- } ) ;
78-
79- afterAll ( ( ) => server . close ( ) ) ;
80-
81- function get ( options : https . RequestOptions ) : Promise < number | undefined > {
82- return new Promise ( ( resolve , reject ) =>
83- https
84- . get ( `${ origin } /` , options , ( res ) => resolve ( res . statusCode ) )
85- . on ( "error" , reject ) ,
86- ) ;
87- }
75+ const origin = `https://localhost:${ ( server . address ( ) as AddressInfo ) . port } ` ;
8876
89- function createApi ( ) : CoderApi {
90- new MockConfigurationProvider ( ) ;
91- const api = CoderApi . create ( origin , "token" , createMockLogger ( ) ) ;
92- onTestFinished ( ( ) => api . dispose ( ) ) ;
93- return api ;
77+ const get = ( options : https . RequestOptions ) =>
78+ new Promise < number | undefined > ( ( resolve , reject ) =>
79+ https
80+ . get ( `${ origin } /` , options , ( res ) => resolve ( res . statusCode ) )
81+ . on ( "error" , reject ) ,
82+ ) ;
83+ const createApi = ( ) => {
84+ new MockConfigurationProvider ( ) ;
85+ const api = CoderApi . create ( origin , "token" , createMockLogger ( ) ) ;
86+ onTestFinished ( ( ) => api . dispose ( ) ) ;
87+ return api ;
88+ } ;
89+ return { ca, get, createApi } ;
9490 }
9591
9692 it ( "fails the handshake without the requested groups" , async ( ) => {
93+ const { ca, get } = await setup ( ) ;
94+
9795 await expect ( get ( { ca } ) ) . rejects . toThrow ( / H A N D S H A K E _ F A I L U R E / ) ;
9896 } ) ;
9997
10098 it ( "offers the groups on REST requests, across redirects" , async ( ) => {
99+ const { createApi } = await setup ( { "/old" : "/new" } ) ;
100+
101101 const response = await createApi ( ) . getAxiosInstance ( ) . get ( "/old" ) ;
102102
103103 expect ( response . status ) . toBe ( 200 ) ;
104104 } ) ;
105105
106+ it ( "refuses REST redirects to another origin" , async ( ) => {
107+ const elsewhere = await startRecordingServer ( ) ;
108+ const { createApi } = await setup ( { "/moved" : `${ elsewhere . origin } /api` } ) ;
109+
110+ await expect (
111+ createApi ( ) . getAxiosInstance ( ) . get ( "/moved" ) ,
112+ ) . rejects . toBeInstanceOf ( CrossOriginRedirectError ) ;
113+ expect ( elsewhere . requests ) . toHaveLength ( 0 ) ;
114+ } ) ;
115+
106116 it ( "offers the groups on WebSocket connections" , async ( ) => {
117+ const { createApi } = await setup ( ) ;
118+
107119 const socket = await createApi ( ) . watchBuildLogsByBuildId ( "build" , [ ] ) ;
108120
109121 await expect ( socket . opened ) . resolves . toBeUndefined ( ) ;
110122 } ) ;
111123
112124 it ( "offers the groups from our own agent" , async ( ) => {
113125 clearProxyEnv ( ) ;
126+ const { get } = await setup ( ) ;
114127 const { createHttpAgent : createRealAgent } =
115128 await vi . importActual < typeof import ( "@/api/httpAgent" ) > (
116129 "@/api/httpAgent" ,
0 commit comments