diff --git a/packages/js-sdk/tests/sandbox/network.test.ts b/packages/js-sdk/tests/sandbox/network.test.ts index c728b3f58c..b6d2ebe23c 100644 --- a/packages/js-sdk/tests/sandbox/network.test.ts +++ b/packages/js-sdk/tests/sandbox/network.test.ts @@ -205,23 +205,27 @@ describe('maskRequestHost option', () => { sandboxTest.skipIf(isDebug)( 'verify maskRequestHost modifies Host header correctly', async ({ sandbox }) => { - // Install netcat for testing - await sandbox.commands.run('apt-get update', { user: 'root' }) - await sandbox.commands.run('apt-get install -y netcat-traditional', { - user: 'root', - }) - const port = 8080 - const outputFile = '/tmp/nc_output.txt' - - // Start netcat listener in background to capture request headers - sandbox.commands.run(`nc -l -p ${port} > ${outputFile}`, { - background: true, - user: 'root', - }) + const outputFile = '/tmp/headers.txt' + + // Start a Python HTTP server that captures request headers and writes them to a file + sandbox.commands.run( + `python3 -c " +import http.server +class H(http.server.BaseHTTPRequestHandler): + def do_GET(self): + with open('${outputFile}', 'w') as f: + for k, v in self.headers.items(): + f.write(k + ': ' + v + chr(10)) + self.send_response(200) + self.end_headers() + def log_message(self, *a): pass +http.server.HTTPServer(('', ${port}), H).handle_request() +"`, + { background: true } + ) - // Wait for netcat to start - await new Promise((resolve) => setTimeout(resolve, 3000)) + await new Promise((resolve) => setTimeout(resolve, 2000)) // Get the public URL for the sandbox const sandboxUrl = `https://${sandbox.getHost(port)}` @@ -231,13 +235,13 @@ describe('maskRequestHost option', () => { try { await fetch(sandboxUrl, { signal: AbortSignal.timeout(5000) }) } catch (error) { - // Request may fail since netcat doesn't respond properly, but headers are captured + // Request may timeout, but headers are captured by the server } - // Read the captured output from inside the sandbox - const result = await sandbox.commands.run(`cat ${outputFile}`, { - user: 'root', - }) + await new Promise((resolve) => setTimeout(resolve, 1000)) + + // Read the captured headers from inside the sandbox + const result = await sandbox.commands.run(`cat ${outputFile}`) // Verify the Host header was modified according to maskRequestHost assert.include(result.stdout, 'Host:') diff --git a/packages/python-sdk/tests/async/sandbox_async/test_network.py b/packages/python-sdk/tests/async/sandbox_async/test_network.py index 1a7dd794a0..8c008371cf 100644 --- a/packages/python-sdk/tests/async/sandbox_async/test_network.py +++ b/packages/python-sdk/tests/async/sandbox_async/test_network.py @@ -171,25 +171,27 @@ async def test_mask_request_host(async_sandbox_factory): import httpx - # Install netcat for testing - await async_sandbox.commands.run("apt-get update", user="root") - await async_sandbox.commands.run( - "apt-get install -y netcat-traditional", user="root" - ) - port = 8080 - output_file = "/tmp/nc_output.txt" + output_file = "/tmp/headers.txt" - # Start netcat listener in background to capture request headers + # Start a Python HTTP server that captures request headers and writes them to a file await async_sandbox.commands.run( - f"nc -l -p {port} > {output_file}", + f"""python3 -c " +import http.server, json +class H(http.server.BaseHTTPRequestHandler): + def do_GET(self): + with open('{output_file}', 'w') as f: + for k, v in self.headers.items(): + f.write(k + ': ' + v + chr(10)) + self.send_response(200) + self.end_headers() + def log_message(self, *a): pass +http.server.HTTPServer(('', {port}), H).handle_request() +" """, background=True, - timeout=0, - user="root", ) - # Wait for netcat to start - await asyncio.sleep(3) + await asyncio.sleep(2) # Get the public URL for the sandbox sandbox_url = f"https://{async_sandbox.get_host(port)}" @@ -200,11 +202,12 @@ async def test_mask_request_host(async_sandbox_factory): try: await client.get(sandbox_url, timeout=5.0) except Exception: - # Request may fail since netcat doesn't respond properly, but headers are captured pass - # Read the captured output from inside the sandbox - result = await async_sandbox.commands.run(f"cat {output_file}", user="root") + await asyncio.sleep(1) + + # Read the captured headers from inside the sandbox + result = await async_sandbox.commands.run(f"cat {output_file}") # Verify the Host header was modified according to mask_request_host assert "Host:" in result.stdout diff --git a/packages/python-sdk/tests/sync/sandbox_sync/test_network.py b/packages/python-sdk/tests/sync/sandbox_sync/test_network.py index 6e93f13689..0cbe32d972 100644 --- a/packages/python-sdk/tests/sync/sandbox_sync/test_network.py +++ b/packages/python-sdk/tests/sync/sandbox_sync/test_network.py @@ -169,22 +169,27 @@ def test_mask_request_host(sandbox_factory): import httpx - # Install netcat for testing - sandbox.commands.run("apt-get update", user="root") - sandbox.commands.run("apt-get install -y netcat-traditional", user="root") - port = 8080 - output_file = "/tmp/nc_output.txt" + output_file = "/tmp/headers.txt" - # Start netcat listener in background to capture request headers + # Start a Python HTTP server that captures request headers and writes them to a file sandbox.commands.run( - f"nc -l -p {port} > {output_file}", + f"""python3 -c " +import http.server, json +class H(http.server.BaseHTTPRequestHandler): + def do_GET(self): + with open('{output_file}', 'w') as f: + for k, v in self.headers.items(): + f.write(k + ': ' + v + chr(10)) + self.send_response(200) + self.end_headers() + def log_message(self, *a): pass +http.server.HTTPServer(('', {port}), H).handle_request() +" """, background=True, - user="root", ) - # Wait for netcat to start - time.sleep(3) + time.sleep(2) # Get the public URL for the sandbox sandbox_url = f"https://{sandbox.get_host(port)}" @@ -195,11 +200,12 @@ def test_mask_request_host(sandbox_factory): try: client.get(sandbox_url, timeout=5.0) except Exception: - # Request may fail since netcat doesn't respond properly, but headers are captured pass - # Read the captured output from inside the sandbox - result = sandbox.commands.run(f"cat {output_file}", user="root") + time.sleep(1) + + # Read the captured headers from inside the sandbox + result = sandbox.commands.run(f"cat {output_file}") # Verify the Host header was modified according to mask_request_host assert "Host:" in result.stdout