Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions packages/js-sdk/tests/sandbox/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
Expand All @@ -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:')
Expand Down
35 changes: 19 additions & 16 deletions packages/python-sdk/tests/async/sandbox_async/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand All @@ -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
Expand Down
32 changes: 19 additions & 13 deletions packages/python-sdk/tests/sync/sandbox_sync/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand All @@ -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
Expand Down