|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![cfg(feature = "e2e")] |
| 5 | + |
| 6 | +use openshell_e2e::harness::sandbox::SandboxGuard; |
| 7 | + |
| 8 | +fn localhost_bypass_script() -> &'static str { |
| 9 | + r#" |
| 10 | +import json |
| 11 | +import os |
| 12 | +import threading |
| 13 | +import urllib.request |
| 14 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 15 | +
|
| 16 | +expected_no_proxy = '127.0.0.1,localhost,::1' |
| 17 | +assert os.environ['HTTP_PROXY'].startswith('http://') |
| 18 | +assert os.environ['HTTPS_PROXY'].startswith('http://') |
| 19 | +assert os.environ['NO_PROXY'] == expected_no_proxy |
| 20 | +assert os.environ['no_proxy'] == expected_no_proxy |
| 21 | +
|
| 22 | +class Handler(BaseHTTPRequestHandler): |
| 23 | + def log_message(self, format, *args): |
| 24 | + pass |
| 25 | +
|
| 26 | + def do_GET(self): |
| 27 | + self.send_response(200) |
| 28 | + self.send_header('Content-Type', 'application/json') |
| 29 | + self.end_headers() |
| 30 | + self.wfile.write(b'{"message":"hello"}') |
| 31 | +
|
| 32 | +server = HTTPServer(('127.0.0.1', 0), Handler) |
| 33 | +thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 34 | +thread.start() |
| 35 | +
|
| 36 | +try: |
| 37 | + with urllib.request.urlopen(f'http://127.0.0.1:{server.server_port}', timeout=10) as response: |
| 38 | + print(json.dumps({ |
| 39 | + 'no_proxy': os.environ['NO_PROXY'], |
| 40 | + 'payload': json.loads(response.read().decode()), |
| 41 | + }), flush=True) |
| 42 | +finally: |
| 43 | + server.shutdown() |
| 44 | + thread.join(timeout=5) |
| 45 | + server.server_close() |
| 46 | +"# |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::test] |
| 50 | +async fn sandbox_bypasses_proxy_for_localhost_http() { |
| 51 | + let guard = SandboxGuard::create(&["python3", "-c", localhost_bypass_script()]) |
| 52 | + .await |
| 53 | + .expect("sandbox create with localhost proxy bypass check"); |
| 54 | + |
| 55 | + assert!( |
| 56 | + guard.create_output.contains( |
| 57 | + r#"{"no_proxy": "127.0.0.1,localhost,::1", "payload": {"message": "hello"}}"# |
| 58 | + ), |
| 59 | + "expected localhost HTTP request to bypass proxy and succeed:\n{}", |
| 60 | + guard.create_output |
| 61 | + ); |
| 62 | +} |
0 commit comments