Skip to content

test: block the network in the Bob plugin test suite - #1126

Merged
amiddavid merged 3 commits into
mainfrom
test/bob-suite-no-network
Nov 17, 2026
Merged

amiddavid merged 3 commits into
mainfrom
test/bob-suite-no-network

Conversation

@vz-ibm

@vz-ibm vz-ibm commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Test only. No behavior change.

Summary

The Bob plugin's Python test suite now blocks every real network call. One test reached GitHub on 2026-11-12 (during #1111): a fake was skipped, and that setup call downloaded a real proxy release into a temp HOME. That test is fixed in #1111. This PR makes the next such test fail loudly, not download.

What changed

  • block_network() in bob-plugin/context-guru/scripts/test_bob_plugin.py, next to TempHome. A setUpModule in each test file turns it on, and a module cleanup undoes it. It covers every test in both files, also the 4 classes that do not use TempHome.
  • It blocks 3 ways out:
    • urllib.request.OpenerDirector.open: the plugin's downloads and its LOOPBACK opener both go through it. It checks the URL before any HTTP_PROXY or DNS step.
    • socket.create_connection: checked by host, before the DNS lookup.
    • socket.socket.connect and connect_ex: checked by address (IPv4 and IPv6; Unix sockets pass).
  • Only 127.0.0.1, ::1 and localhost pass, so the fake release server, the fake proxies and the plugin's loopback calls still work.
  • The error is a BaseException (NetworkBlocked), so the plugin's own except Exception cannot hide it. Its message names only the scheme, host and port: never the user info, the path or the query, so a credential cannot leak.

How it was tested

  • The whole Bob suite with the guard on: 221 tests, OK. No existing test reached the network.
  • 5 new tests (NetworkGuardTest). 4 were red first against an empty guard, on real network errors (a DNS failure, a socket timeout, urllib's parse error, a DNS lookup that must not happen). The 5th checks that loopback still works.

Open point

  • The guard covers the test process only. CanaryTest and HookCommandShellTest start bob_plugin.py as a child process, which the guard does not cover. Covering them needs a change to how those tests start the child. A later PR.

A test once downloaded a real proxy release from GitHub into a temp HOME,
because a fake was skipped. Nothing stopped the next one.

- block_network() patches urllib's OpenerDirector.open (the plugin's
  urlopen and LOOPBACK opener), socket.create_connection and
  socket.socket.connect/connect_ex.
- Only 127.0.0.1, ::1 and localhost pass. Anything else raises
  NetworkBlocked, which names the scheme, host and port, never the user
  info, path or query.
- NetworkBlocked is a BaseException, so the plugin's `except Exception`
  cannot hide it.
- setUpModule in both test files turns it on; a module cleanup undoes it.
- NetworkGuardTest covers a blocked download, a blocked raw socket, a
  blocked create_connection before DNS, no credential in the message,
  and loopback still working.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Vitaly Zabershinsky <[email protected]>

@vz-ibm vz-ibm left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (claude, user of @vz-ibm) Claude Code 2.1.316 offload-review skill, @vz-ibm's session fresh 2 of 2 1 of 2 d80e3521caa2 approve

Verdict: approve

Findings

None.

Non-blocking

  • The guard covers only the test process, not child processes. The module docstring says "A call to any host but 127.0.0.1, ::1 or localhost raises NetworkBlocked". Some tests run bob_plugin.py, hook commands and context-guru-reset through subprocess.run (test_bob_plugin.py:613, :1054, :1060, :1162). Those children have no guard. Today they run only check, signal, ensure, status and reset. None of these calls latest_release_tag or fetch, so nothing leaks now. A future subprocess test of setup or update would not be stopped. Proposal: add one sentence to the docstring, for example "Child processes are not guarded: do not start setup or update in a subprocess." Another option is a sitecustomize on PYTHONPATH for child Pythons, but that is more than this PR needs.
  • One address of a host can stop the address loop in create_connection. socket.create_connection catches only OSError for each resolved address. NetworkBlocked is a BaseException, so the loop stops at the first blocked address and does not try the next one. If a hosts file maps localhost to an address that is not in LOOPBACK_HOSTS, for example fe80::1%lo0 (older macOS /etc/hosts), then test_loopback_calls_still_reach_a_fake_server fails on that machine. A current macOS /etc/hosts maps localhost only to 127.0.0.1 and ::1, so this is a low risk. The failure is fail-closed and clear.
  • socket_check blocks other loopback addresses such as 127.0.0.2 or ::ffff:127.0.0.1. That is fail-closed and correct for a test guard. I note it only so that nobody is surprised later.

What I checked

  • I read the full diff cd82e95a..d80e3521 (one commit) and the commit message.
  • Coverage of the plugin's network paths. fetch and latest_release_tag (bob_plugin.py:441-453) use urllib.request.urlopen. It goes through the global OpenerDirector.open, which the guard patches. LOOPBACK (bob_plugin.py:295) is also an OpenerDirector, so it is guarded too. Redirects call self.parent.open again, so each redirect hop is checked. FakeRelease redirects to 127.0.0.1, which passes.
  • Backstops. http.client.HTTPConnection reads socket.create_connection when it creates each connection, so the patch applies. socket.socket.connect and connect_ex are patched on the Python class, and the check runs only for AF_INET and AF_INET6. first_free_port uses only bind, which the guard does not touch.
  • No credential in the error. guarded_open builds the message from the scheme, hostname and port only. test_the_guard_error_names_no_credential proves it with user info and a query that hold SECRET.
  • except Exception cannot hide the error. NetworkBlocked is a BaseException. The plugin has one except BaseException (bob_plugin.py:962). It records the error, restores the files and lets the error go on, so it does not hide it either.
  • Setup and undo. Each test module has its own setUpModule. test_bob_control.py imports only named symbols, so it does not take over the setUpModule of test_bob_plugin.py. unittest runs the module cleanup before it starts the next module, so the two guards never stack. undo restores all four originals.
  • Tests. NetworkGuardTest checks a blocked download, a blocked raw socket to TEST-NET-1, a blocked create_connection before any DNS lookup (getaddrinfo is mocked to fail), and 127.0.0.1 and localhost still working. FakeProxy binds 127.0.0.1. When localhost goes to ::1 first, that connect is refused and the loop goes on to 127.0.0.1. The test expects 3 /healthz calls, which is correct.
  • Rules. Only tests changed. There is no change to a component or a feature, so no data-dependence row is needed, and fail-open behavior does not change.
  • I did not run the tests, because live tests are not approved for this review.

@vz-ibm vz-ibm left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (claude, user of @vz-ibm) Claude Code 2.1.316 offload-review skill, @vz-ibm's session fresh 1 of 2 1 of 2 d80e3521caa2 approve

Verdict: approve

Findings

None.

Non-blocking

  • The guard covers this process only. CanaryTest and HookCommandShellTest start bob_plugin.py and the hook commands as child processes (test_bob_plugin.py:1054, :1060, :613). block_network() does not reach them. Today these subcommands (check, signal, ensure, status) call only 127.0.0.1, so nothing leaks. But the module docstring says "A call to any host but 127.0.0.1 ... raises NetworkBlocked". That is true only in-process. Suggest one sentence: "Child processes are not guarded."
  • A DNS lookup can still leave the machine on the backstop path. http.client.HTTPConnection._create_connection is bound to the real socket.create_connection at import time. So a direct http.client call skips guarded_create, resolves the name, and only then guarded_connect blocks it. The plugin uses only urllib, which guarded_open stops before the lookup. The docstring calls this path a backstop, which is correct. No change needed. It is just a known limit.
  • NetworkGuardTest.test_loopback_calls_still_reach_a_fake_server depends on the developer's proxy settings. PLUGIN.fetch is plain urlopen, so it reads HTTP_PROXY. On a Mac with HTTP_PROXY set and no NO_PROXY (the case test_bob_plugin.py:828 describes), the two fetch calls at :2045-2046 go to the proxy host. The guard then raises NetworkBlocked. The FakeRelease tests already have the same dependence, so this PR does not add a new kind of failure. A mock.patch.dict(os.environ, ...) that removes the proxy variables in this test would make it hold on such machines.
  • real_create can leave a socket open when guarded_connect raises inside it. create_connection closes the socket only on OSError. At most this gives a ResourceWarning in a test that is already failing.

What I checked

  • I read the full diff of both files and the commit message. DCO sign-off and Assisted-By are there. There is no Co-Authored-By.
  • Patch points. urllib.request.urlopen uses a global OpenerDirector, and LOOPBACK (bob_plugin.py:295) is an OpenerDirector instance. Patching OpenerDirector.open on the class therefore covers both. Redirects call self.parent.open, so guarded_open also blocks a redirect away from loopback. ssl.SSLSocket.connect ends in socket.socket.connect, so the backstop covers TLS sockets too.
  • The plugin cannot hide the error. All plugin network calls are in except Exception blocks, or in urllib's except OSError (which wraps errors as URLError). The one except BaseException (bob_plugin.py:962) is in change_and_restart. It wraps load_service/wait_healthy, and these call only healthy() on 127.0.0.1. unittest's bare except reports a BaseException from a test as an error, so a blocked call fails the test in a way you can see. The plugin starts no threads, so no error gets lost in a thread.
  • Setup and teardown. Each module has its own setUpModule, which calls addModuleCleanup(undo). unittest runs doModuleCleanups when it moves to the next module, so the patches never stack. test_bob_control imports only helpers and TempHome (which has no test methods), so it picks up no test classes from the other module.
  • The undo puts back every patched attribute. The restored socket.socket.connect is an explicit class attribute that holds the same C method, so behavior is the same.
  • Credential masking. The message holds the scheme, hostname and port only. test_the_guard_error_names_no_credential checks that the user info and the query are not in it.
  • The new tests can fail. The raw-socket test uses TEST-NET-1. The create_connection test mocks getaddrinfo to raise AssertionError, so a missing guard gives the wrong exception type and the test fails. The loopback test checks that proxy.paths is exactly 3× /healthz.
  • No existing test reaches a non-loopback host in-process. I grepped both files for URLs. The bob.ibm.com/evil.com strings only go to valid_upstream and argument parsing, never to the network. Release tests use FakeRelease on 127.0.0.1 through RELEASE_BASE.
  • reset.sh and bob_route.py make no network calls.
  • Data dependence. This is a test-only change with no new or changed feature, so no row file is needed.
  • I did not run the suite. Live tests are not approved for this review.

Raised by both round-1 reviewers of #1126.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Vitaly Zabershinsky <[email protected]>
@vz-ibm

vz-ibm commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Round 1: both reviewers approve. Changes in bc36262:

  1. Child processes are not guarded (both reviewers): documented. The module docstring now says: "Child processes are not guarded: do not start setup or update in a subprocess." Covering children (for example a sitecustomize on PYTHONPATH) is left for a later PR.
  2. The loopback test depends on HTTP_PROXY (reviewer 1): no change. I tried to clear the proxy variables in the test. It does not help: urlopen builds one shared opener on first use, and that opener keeps the proxy settings it saw. A real fix needs urllib's private _opener. The FakeRelease tests have the same limit, so this PR adds no new failure.
  3. http.client skips the pre-DNS check, the create_connection address loop, and other loopback addresses (both reviewers): no change. The plugin uses only urllib; the socket backstop still stops the connection; all cases fail closed, with a clear error.

Checks: the guard tests pass (5). No other change.

main added FAKE_STRATEGIES to the same import line in
test_bob_control.py; the line now has both. The whole Bob suite passes
with the network guard on (230 tests).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Vitaly Zabershinsky <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (claude, user of @amiddavid) Claude Code 2.1.326 offload-review skill, @amiddavid's session fresh 2 of 2 2 of 2 0e2e52487d76 approve

Verdict: approve

Findings

None.

Non-blocking

  • None new. The round-1 non-blocking points stay as the author answered them (see below).

What I checked

  • Last review: round 1, d80e3521caa2, 2026-11-12T09:12:36Z
  • New commits read: 2
    • bc362621 test: say that the network guard does not cover child processes
    • 0e2e5248 chore: merge main into test/bob-suite-no-network (brings main up to a4d43d94)
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
  • New items since my last review:
    • Reviewer 1, round 1 review (09:13:48Z): read. Approve, 4 non-blocking points.
    • Author comment (09:15:36Z): read. I checked each answer below.

Earlier findings

Neither reviewer had a finding in round 1. Status of the non-blocking points:

  1. Child processes are not guarded (both reviewers): fixed. bc362621 adds the docstring line "Child processes are not guarded: do not start setup or update in a subprocess." (test_bob_plugin.py:11).
  2. The loopback test depends on HTTP_PROXY (reviewer 1): rejected by the author. I accept the reason. urllib.request.urlopen builds one global opener on its first call, with the proxy handler it sees then. A later env change in one test does not change it. A real fix needs the private urllib.request._opener. The FakeRelease tests have the same limit.
  3. http.client pre-DNS gap, the create_connection address loop, other loopback addresses (both reviewers): rejected by the author. I accept the reason. The plugin uses only urllib. Each case fails closed with a clear NetworkBlocked.
  4. Socket leak in real_create on a blocked connect (reviewer 1): no reply. It is non-blocking and only affects a test that already fails. No action needed.

The merge of main

The PR diff against base a4d43d94 is the same guard as in round 1, plus the one docstring line. I read main's changes to the Bob plugin since d80e3521 (#1111 and #975: find_binary now probes strategies, too_old_for_preset, SetupSkipsOldProxyTest, FAKE_STRATEGIES):

  • The merge keeps the guard intact. test_bob_control.py imports FAKE_STRATEGIES and block_network and has its own setUpModule. test_bob_plugin.py keeps import time, json, urllib.parse and urllib.request. Both files parse.
  • No new main test reaches a real host in-process. Each setup test in SetupSkipsOldProxyTest either patches RELEASE_BASE to a FakeRelease on 127.0.0.1, or patches install_release_binary to raise. UpdateTest.setUp patches install_release_binary. test_a_release_before_v0_7_1... and test_release_v0_7_1_is_installed call install_release_binary only after serve() patches RELEASE_BASE.
  • The new strategies probe runs a fake binary through subprocess. It makes no network call. The fake prints JSON or "listening", and one fake runs sleep 30.
  • The guard adds safety to main's change. find_binary is stricter now. A future fake without strategies would make setup download a release, and the guard now stops that call with NetworkBlocked.

Rules

  • Test-only change. No component or feature changes, so no data-dependence row is needed. No change to fail-open behavior, and no behavior change for users.
  • bc362621 has a DCO sign-off and Assisted-By, with no Co-Authored-By.
  • I did not run the tests. Live tests are not approved for this review.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (claude, user of @amiddavid) Claude Code 2.1.326 offload-review skill, @amiddavid's session fresh 1 of 2 2 of 2 0e2e52487d76 approve

Verdict: approve

Findings

None.

Non-blocking

  • No CI job runs the Bob Python suite. I found no unittest or test_bob step in .github/workflows/ or the Makefile. The green checks on 0e2e5248 therefore do not prove that the merged suite passes under the guard. The author's round-1 reply says "the guard tests pass (5)". That covers NetworkGuardTest only, and it was before the merge of main. Proposal: run the full suite one time on the merge head before merge (python3 -m unittest discover -s bob-plugin/context-guru/scripts -p 'test_*.py'). My code reading (below) finds no test that the guard would now stop.

What I checked

  • Last review: round 1, d80e3521, 2026-11-12T09:13:48Z (posted by @vz-ibm's session, verdict approve).
  • New commits read: 2.
    • bc362621 test: say that the network guard does not cover child processes.
    • 0e2e5248 chore: merge main into test/bob-suite-no-network (brings 13 main commits).
  • Read in full: 2 reviews, 1 comment, 0 inline comments.
  • New items since my last review:
    • Author comment of 09:15:36Z (round-1 replies). I checked each point:
      1. Child processes: fixed. bc362621 adds the docstring sentence at test_bob_plugin.py:11.
      2. HTTP_PROXY in the loopback test (my point): rejected with a reason. The reason is right. urlopen caches one global opener (urllib.request._opener) with the proxy handler it built on first use, so a later os.environ patch has no effect. I accept it.
      3. http.client, the create_connection address loop and other loopback addresses: no change. I accept it. All three fail closed, and the plugin uses urllib only.
    • Reviewer 2 round 1: approve, no findings. Its non-blocking points match mine or are covered by the reply above.
  • Merge result. git diff a4d43d94..HEAD shows only this PR's change: the guard, NetworkGuardTest, the docstring, and the setUpModule in test_bob_control.py. The import line merges cleanly with main's new FAKE_STRATEGIES import.
  • Main's Bob changes under the guard. I read git diff cd82e95a..a4d43d94 -- bob-plugin/ (#1111):
    • find_binary now also calls load_strategy_table, which runs the binary as a subprocess. No network.
    • SetupSkipsOldProxyTest.setup_downloading uses FakeRelease through RELEASE_BASE on 127.0.0.1. The guard lets it pass.
    • setup_without_download, the offline test, UpdateTest.setUp and test_setup_says_why_it_downloads mock install_release_binary. So a binary that find_binary now skips does not fall through to a real download.
    • Most other setup tests mock find_binary (for example test_bob_plugin.py:662).
  • Rules: test-only change. No component or feature changes, so no data-dependence row. No behavior change for users.
  • I did not run the tests. Live tests are not approved for this review.

@amiddavid
amiddavid merged commit 2f2b030 into main Nov 17, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 17, 2026
amiddavid added a commit that referenced this pull request Nov 19, 2026
Plugin versions for v0.7.4:
- Claude Code plugin 0.7.3 -> 0.7.4 (#1096, #1135, #1174, #1177, #1199,
  #1216).
- Codex plugin 0.2.12 -> 0.2.13 (#1115, #1135).
- Pi package 0.1.1 -> 0.1.2 (#1113).
- Bob plugin 0.1.1 -> 0.1.2 (#1093, #1111, #1118, #1124, #1126, #1127).
  The owner's Bob hold (#1221) covers release tests only.
- OpenCode: PINNED_VERSION v0.7.3 -> v0.7.4 in opencode-plugin/index.js,
  its README, docs/how-to/install-opencode-plugin.md and 3 tests.

Behavior change: the version numbers; the OpenCode plugin's first start
downloads proxy v0.7.4. Release bumps are approved by the owner ("I
approve version bumps").

Data dependence: none.

Reviews: 2 offload review rounds (GLM): both approve at ab79001. After
it: 1422093, a merge of main with no hand resolution (remerge-diff 0;
approval-still-valid: SAME CHANGE).

Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants