| 1 | """Regression checks for the Bright Data child-process environment.""" |
| 2 | import json |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import sys |
| 6 | import tempfile |
| 7 | import unittest |
| 8 | from unittest.mock import patch |
| 9 | |
| 10 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "skills" / "last30days" / "scripts")) |
| 11 | from lib import brightdata |
| 12 | |
| 13 | |
| 14 | class ChildEnvironmentTests(unittest.TestCase): |
| 15 | def test_unrelated_secrets_and_runtime_injection_are_excluded(self): |
| 16 | parent = { |
| 17 | "PATH": "runtime-path", "HOME": "home-path", |
| 18 | "OPENAI_API_KEY": "fake-openai", "GITHUB_TOKEN": "fake-github", |
| 19 | "GOOGLE_API_KEY": "fake-google", "CUSTOM_SECRET": "fake-secret", |
| 20 | "NODE_OPTIONS": "--require untrusted.js", "PYTHONPATH": "untrusted", |
| 21 | "BRIGHTDATA_OTHER_TOKEN": "fake-unneeded", |
| 22 | } |
| 23 | with patch.dict(os.environ, parent, clear=True): |
| 24 | child = brightdata._child_env("fake-brightdata") |
| 25 | self.assertEqual(child, {"PATH": "runtime-path", "HOME": "home-path", |
| 26 | "BRIGHTDATA_API_KEY": "fake-brightdata"}) |
| 27 | self.assertNotIn("BRIGHTDATA_API_KEY", os.environ) |
| 28 | |
| 29 | def test_no_key_still_returns_a_filtered_mapping(self): |
| 30 | with patch.dict(os.environ, {"PATH": "runtime-path", "OPENAI_API_KEY": "fake"}, clear=True): |
| 31 | self.assertEqual(brightdata._child_env(""), {"PATH": "runtime-path"}) |
| 32 | with patch.dict(os.environ, {}, clear=True): |
| 33 | self.assertEqual(brightdata._child_env(""), {}) |
| 34 | |
| 35 | def test_explicit_key_overrides_ambient_and_ambient_is_fallback(self): |
| 36 | with patch.dict(os.environ, {"BRIGHTDATA_API_KEY": "fake-ambient"}, clear=True): |
| 37 | self.assertEqual(brightdata._child_env("fake-explicit")["BRIGHTDATA_API_KEY"], "fake-explicit") |
| 38 | self.assertEqual(brightdata._child_env("")["BRIGHTDATA_API_KEY"], "fake-ambient") |
| 39 | self.assertEqual(os.environ["BRIGHTDATA_API_KEY"], "fake-ambient") |
| 40 | |
| 41 | def test_platform_config_and_network_settings_are_preserved(self): |
| 42 | names = ( |
| 43 | "PATH", "HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH", "APPDATA", |
| 44 | "LOCALAPPDATA", "XDG_CONFIG_HOME", "SystemRoot", "WINDIR", |
| 45 | "SystemDrive", "COMSPEC", "PATHEXT", "TMPDIR", "TEMP", "TMP", |
| 46 | "LANG", "LC_ALL", "LC_CTYPE", "NODE_EXTRA_CA_CERTS", "NODE_USE_ENV_PROXY", |
| 47 | "SSL_CERT_FILE", "SSL_CERT_DIR", "HTTP_PROXY", "HTTPS_PROXY", |
| 48 | "ALL_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "all_proxy", "no_proxy", |
| 49 | ) |
| 50 | # A plain mapping also checks case variants independently of Windows' os.environ. |
| 51 | parent = {name: "setting" for name in names} |
| 52 | with patch.object(brightdata.os, "environ", parent): |
| 53 | self.assertEqual(brightdata._child_env(""), parent) |
| 54 | |
| 55 | def test_real_child_receives_only_intended_credentials(self): |
| 56 | probe = ( |
| 57 | "import json,os; print(json.dumps([{'key_ok': " |
| 58 | "os.getenv('BRIGHTDATA_API_KEY') == 'fake-brightdata', " |
| 59 | "'leaked': any(k in os.environ for k in " |
| 60 | "('OPENAI_API_KEY','GITHUB_TOKEN','GOOGLE_API_KEY','CUSTOM_SECRET','NODE_OPTIONS')), " |
| 61 | "'path_ok': 'PATH' in os.environ}]))" |
| 62 | ) |
| 63 | for config in ({"BRIGHTDATA_API_KEY": "fake-brightdata"}, None): |
| 64 | with self.subTest(explicit_key=config is not None): |
| 65 | with patch.dict(os.environ, { |
| 66 | "BRIGHTDATA_API_KEY": "fake-brightdata", "OPENAI_API_KEY": "fake", |
| 67 | "GITHUB_TOKEN": "fake", "GOOGLE_API_KEY": "fake", |
| 68 | "CUSTOM_SECRET": "fake", "NODE_OPTIONS": "--require untrusted.js", |
| 69 | }), patch.object(brightdata, "_build_args", return_value=[sys.executable, "-I", "-c", probe]), patch.object(brightdata, "CLI_BIN", sys.executable): |
| 70 | result = brightdata.run_pipeline("probe", {}, timeout=10, config=config) |
| 71 | self.assertEqual(result, {"records": [{"key_ok": True, "leaked": False, "path_ok": True}]}) |
| 72 | |
| 73 | def test_credentials_file_location_survives_without_env_key(self): |
| 74 | with tempfile.TemporaryDirectory() as temp_dir: |
| 75 | settings = {"APPDATA": temp_dir, "HOME": temp_dir, "USERPROFILE": temp_dir, |
| 76 | "XDG_CONFIG_HOME": temp_dir, "OPENAI_API_KEY": "fake"} |
| 77 | with patch.dict(os.environ, settings): |
| 78 | os.environ.pop("BRIGHTDATA_API_KEY", None) |
| 79 | credential = brightdata._config_dir() / "credentials.json" |
| 80 | credential.parent.mkdir(parents=True, exist_ok=True) |
| 81 | credential.write_text("{}", encoding="utf-8") |
| 82 | self.assertTrue(brightdata.has_credentials()) |
| 83 | probe = ( |
| 84 | "import json,os,pathlib,sys; " |
| 85 | "root=(pathlib.Path(os.environ['APPDATA']) if sys.platform=='win32' " |
| 86 | "else pathlib.Path.home()/'Library'/'Application Support' if sys.platform=='darwin' " |
| 87 | "else pathlib.Path(os.environ['XDG_CONFIG_HOME'])); " |
| 88 | "print(json.dumps({'found':(root/'brightdata-cli'/'credentials.json').exists(), " |
| 89 | "'key_present':'BRIGHTDATA_API_KEY' in os.environ,'leaked':'OPENAI_API_KEY' in os.environ}))" |
| 90 | ) |
| 91 | result = brightdata.subproc.run_with_timeout( |
| 92 | [sys.executable, "-I", "-c", probe], timeout=10, env=brightdata._child_env("")) |
| 93 | self.assertEqual(result.returncode, 0, result.stderr) |
| 94 | self.assertEqual(json.loads(result.stdout), {"found": True, "key_present": False, "leaked": False}) |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | unittest.main() |
| 99 |