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