-
Notifications
You must be signed in to change notification settings - Fork 52.6k
fix: harden subprocess env handling (scrub secrets, isolate browser sessions) #59840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ai-ag2026
wants to merge
5
commits into
NousResearch:main
from
ai-ag2026:upstream/pr-a1-subprocess-hardening
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5938442
fix: scrub secrets from terminal env snapshots
ai-ag2026 3e02bf8
fix: isolate browser tool subprocess sessions
ai-ag2026 2a094c1
fix(env): keep secret-scrub snapshot atomic — brace-group the redirect
ai-ag2026 6353702
fix: preserve explicit env across docker commands
ai-ag2026 ff99152
fix: preserve snapshots when export fails
ai-ag2026 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import json | ||
| import os | ||
|
|
||
| import tools.browser_tool as browser_tool | ||
| import tools.interrupt as interrupt | ||
|
|
||
|
|
||
| def test_browser_popen_extra_starts_new_session_on_posix(monkeypatch): | ||
| monkeypatch.setattr(browser_tool.os, "name", "posix") | ||
|
|
||
| assert browser_tool._browser_popen_extra() == {"start_new_session": True} | ||
|
|
||
|
|
||
| def test_browser_popen_extra_keeps_windows_no_console_without_new_group(monkeypatch): | ||
| class FakeStartupInfo: | ||
| def __init__(self): | ||
| self.dwFlags = 0 | ||
|
|
||
| monkeypatch.setattr(browser_tool.os, "name", "nt") | ||
| monkeypatch.setattr(browser_tool.subprocess, "STARTUPINFO", FakeStartupInfo, raising=False) | ||
| monkeypatch.setattr(browser_tool.subprocess, "STARTF_USESTDHANDLES", 0x100, raising=False) | ||
|
|
||
| extra = browser_tool._browser_popen_extra() | ||
|
|
||
| assert extra["creationflags"] == 0x08000000 | ||
| assert extra["close_fds"] is True | ||
| assert isinstance(extra["startupinfo"], FakeStartupInfo) | ||
| assert extra["startupinfo"].dwFlags == 0x100 | ||
| assert "start_new_session" not in extra | ||
|
|
||
|
|
||
| def test_browser_process_isolation_kwargs_cover_both_popen_paths(monkeypatch, tmp_path): | ||
| """Both production launch paths must pass the shared isolation kwargs.""" | ||
| popen_calls = [] | ||
| isolation = {"start_new_session": "sentinel"} | ||
|
|
||
| class FakeProcess: | ||
| returncode = 0 | ||
|
|
||
| def __init__(self, argv, **kwargs): | ||
| popen_calls.append((argv, kwargs)) | ||
| os.write(kwargs["stdout"], json.dumps({"success": True}).encode()) | ||
|
|
||
| def wait(self, timeout=None): | ||
| return self.returncode | ||
|
|
||
| def kill(self): | ||
| self.returncode = -9 | ||
|
|
||
| monkeypatch.setattr(browser_tool, "_browser_popen_extra", lambda: isolation) | ||
| monkeypatch.setattr(browser_tool.subprocess, "Popen", FakeProcess) | ||
| monkeypatch.setattr(browser_tool, "_find_agent_browser", lambda: "/bin/agent-browser") | ||
| monkeypatch.setattr(browser_tool, "_requires_real_termux_browser_install", lambda command: False) | ||
| monkeypatch.setattr(browser_tool, "_is_local_mode", lambda: False) | ||
| monkeypatch.setattr(browser_tool, "_get_browser_engine", lambda: "auto") | ||
| monkeypatch.setattr(browser_tool, "_get_session_info", lambda task_id: { | ||
| "session_name": "normal", "cdp_url": "ws://example.invalid" | ||
| }) | ||
| monkeypatch.setattr(browser_tool, "_socket_safe_tmpdir", lambda: str(tmp_path)) | ||
| monkeypatch.setattr(browser_tool, "_write_owner_pid", lambda *args: None) | ||
| monkeypatch.setattr(browser_tool, "_build_browser_env", lambda: {}) | ||
| monkeypatch.setattr(browser_tool, "_merge_browser_path", lambda value: value) | ||
| monkeypatch.setattr(interrupt, "is_interrupted", lambda: False) | ||
|
|
||
| result = browser_tool._run_browser_command("task", "snapshot") | ||
| assert result["success"] is True | ||
| assert len(popen_calls) == 1 | ||
| assert popen_calls[0][1]["start_new_session"] == "sentinel" | ||
|
|
||
| popen_calls.clear() | ||
| monkeypatch.setattr( | ||
| browser_tool, | ||
| "_run_browser_command", | ||
| lambda *args, **kwargs: {"success": True, "data": {"result": "https://example.com"}}, | ||
| ) | ||
| monkeypatch.setattr(browser_tool, "_chromium_installed", lambda: True) | ||
|
|
||
| result = browser_tool._run_chrome_fallback_command("task", "snapshot", [], 5) | ||
| assert result["success"] is True | ||
| assert len(popen_calls) == 3 # open, requested command, close | ||
| assert all(call[1]["start_new_session"] == "sentinel" for call in popen_calls) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please replace this source-count assertion with behavioral coverage that intercepts
subprocess.Popenat both execution paths and verifies the kwargs. AGENTS.md prohibits tests that read source text because they do not validate runtime wiring.