@@ -119,3 +119,130 @@ describe('allow takes precedence over deny', () => {
119119 }
120120 )
121121} )
122+
123+ describe ( 'allowPublicTraffic=false' , ( ) => {
124+ sandboxTest . scoped ( {
125+ sandboxOpts : {
126+ network : {
127+ allowPublicTraffic : false ,
128+ } ,
129+ } ,
130+ } )
131+
132+ sandboxTest . skipIf ( isDebug ) (
133+ 'sandbox requires traffic access token' ,
134+ async ( { sandbox } ) => {
135+ // Verify the sandbox was created successfully and has a traffic access token
136+ assert ( sandbox . trafficAccessToken )
137+
138+ // Start a simple HTTP server in the sandbox
139+ const port = 8080
140+ sandbox . commands . run ( `python3 -m http.server ${ port } ` , {
141+ background : true ,
142+ } )
143+
144+ // Wait for server to start
145+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
146+
147+ // Get the public URL for the sandbox
148+ const sandboxUrl = `https://${ sandbox . getHost ( port ) } `
149+
150+ // Test 1: Request without traffic access token should fail with 403
151+ const response1 = await fetch ( sandboxUrl )
152+ assert . equal ( response1 . status , 403 )
153+
154+ // Test 2: Request with valid traffic access token should succeed
155+ const response2 = await fetch ( sandboxUrl , {
156+ headers : {
157+ 'e2b-traffic-access-token' : sandbox . trafficAccessToken ,
158+ } ,
159+ } )
160+ assert . equal ( response2 . status , 200 )
161+ }
162+ )
163+ } )
164+
165+ describe ( 'allowPublicTraffic=true' , ( ) => {
166+ sandboxTest . scoped ( {
167+ sandboxOpts : {
168+ network : {
169+ allowPublicTraffic : true ,
170+ } ,
171+ } ,
172+ } )
173+
174+ sandboxTest . skipIf ( isDebug ) (
175+ 'sandbox works without token' ,
176+ async ( { sandbox } ) => {
177+ // Start a simple HTTP server in the sandbox
178+ const port = 8080
179+ sandbox . commands . run ( `python3 -m http.server ${ port } ` , {
180+ background : true ,
181+ } )
182+
183+ // Wait for server to start
184+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
185+
186+ // Get the public URL for the sandbox
187+ const sandboxUrl = `https://${ sandbox . getHost ( port ) } `
188+
189+ // Request without traffic access token should succeed (public access enabled)
190+ const response = await fetch ( sandboxUrl )
191+ assert . equal ( response . status , 200 )
192+ }
193+ )
194+ } )
195+
196+ describe ( 'maskRequestHost option' , ( ) => {
197+ sandboxTest . scoped ( {
198+ sandboxOpts : {
199+ network : {
200+ maskRequestHost : 'custom-host.example.com:${PORT}' ,
201+ } ,
202+ } ,
203+ } )
204+
205+ sandboxTest . skipIf ( isDebug ) (
206+ 'verify maskRequestHost modifies Host header correctly' ,
207+ async ( { sandbox } ) => {
208+ // Install netcat for testing
209+ await sandbox . commands . run ( 'apt-get update' , { user : 'root' } )
210+ await sandbox . commands . run ( 'apt-get install -y netcat-traditional' , {
211+ user : 'root' ,
212+ } )
213+
214+ const port = 8080
215+ const outputFile = '/tmp/nc_output.txt'
216+
217+ // Start netcat listener in background to capture request headers
218+ sandbox . commands . run ( `nc -l -p ${ port } > ${ outputFile } ` , {
219+ background : true ,
220+ user : 'root' ,
221+ } )
222+
223+ // Wait for netcat to start
224+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
225+
226+ // Get the public URL for the sandbox
227+ const sandboxUrl = `https://${ sandbox . getHost ( port ) } `
228+
229+ // Make a request from OUTSIDE the sandbox through the proxy
230+ // The Host header should be modified according to maskRequestHost
231+ try {
232+ await fetch ( sandboxUrl , { signal : AbortSignal . timeout ( 5000 ) } )
233+ } catch ( error ) {
234+ // Request may fail since netcat doesn't respond properly, but headers are captured
235+ }
236+
237+ // Read the captured output from inside the sandbox
238+ const result = await sandbox . commands . run ( `cat ${ outputFile } ` , {
239+ user : 'root' ,
240+ } )
241+
242+ // Verify the Host header was modified according to maskRequestHost
243+ assert . include ( result . stdout , 'Host:' )
244+ assert . include ( result . stdout , 'custom-host.example.com' )
245+ assert . include ( result . stdout , `${ port } ` )
246+ }
247+ )
248+ } )
0 commit comments