Repository navigation
fix(computer): guard Unix-only pwd import in site_proxy - #519
saiyam-jain-dev wants to merge 3 commits into
Conversation
|
Added a fresh-import test (test_the_proxy_imports_when_pwd_is_unavailable) that blocks pwd in sys.modules and reloads site_proxy to verify it imports cleanly without the module. |
andrewpark-scaleai
left a comment
There was a problem hiding this comment.
Requesting changes, mainly because this doesn't achieve its stated goal.
It still doesn't import on non-Unix. I blocked every Unix-only module (pwd, fcntl, grp, termios, resource, pty, tty) and imported agent_env.env.envs.computer.utils.sites and computer.env at this head. Both still fail at src/agent_env/store/object_store/local/store.py:27, which does an unconditional import fcntl. The PR only gets past pwd, so the same import chain still breaks on Windows. bundle/_fs.py:18 also says agent-env doesn't target Windows. Which platform hit this? If there is a real non-Unix caller, fcntl needs a guard too. If there isn't one, I'd close this.
Keep the change to the import itself. serve, drop_privileges, install and trust_nss only run as root inside the Linux computer, on the base's own python3. The only host-side import of this module is sites.py:36, which takes is_local and matches. A try/except around import pwd, or moving those two functions into a module with no pwd dependency, covers the host case. That makes the runtime guards unnecessary. Details are inline.
| os.setgroups([]) | ||
| os.setgid(entry.pw_gid) | ||
| os.setuid(entry.pw_uid) | ||
| if hasattr(os, "setgroups"): |
There was a problem hiding this comment.
These hasattr checks can't be false. This branch is only reachable after os.geteuid() == 0, which only happens on a Unix host where setgroups, setgid and setuid all exist. And if one of them ever were missing, the privilege drop would be silently skipped while the code carried on as root. A privilege drop shouldn't fail that way. Please revert to the unconditional calls.
| return [] | ||
| try: | ||
| home = Path(pwd.getpwnam(user).pw_dir) | ||
| except KeyError: |
There was a problem hiding this comment.
This changes behavior on Linux, and the description doesn't mention it. Before, an unknown session user raised KeyError and install failed loudly. Now it returns [], so a misnamed user silently skips NSS trust, and the first symptom is a certificate error in Chromium much later. The pwd is None check above is enough for the import case. Please drop this except KeyError.
| monkeypatch.setattr(site_proxy, "pwd", None) | ||
| assert site_proxy.serve_user("agent") == "agent" | ||
| assert site_proxy.trust_nss("agent", "/fake/ca.pem") == [] | ||
| site_proxy.drop_privileges("agent") |
There was a problem hiding this comment.
Nothing is asserted after this call, so the test only shows that it doesn't raise. If the hasattr guards are removed as suggested above, this line goes too.
| site_proxy.drop_privileges("agent") | ||
|
|
||
|
|
||
| def test_the_proxy_imports_when_pwd_is_unavailable(monkeypatch): |
There was a problem hiding this comment.
Reloading site_proxy partway through the suite swaps out the module's classes. After the reload, site_proxy.Config is not the Config this file imported at the top. I saw no failures locally (the file and the rest of tst/unit/env/envs pass), but this is fragile. If the import guard is the only change left, running python -c in a subprocess with sys.modules['pwd'] = None tests the fresh import without reloading the module in the test process.
…oxy import in subprocess
|
Thanks for the detailed review and guidance @andrewpark-scaleai. Updated the branch with the following changes:
|
What and why
site_proxy.py imported pwd unconditionally at module load. Because pwd is a Unix-specific module, importing site_proxy or anything importing it transitively (such as computer.utils.sites and computer.env) fails with ModuleNotFoundError on non-Unix platforms.
This change:
How it was tested


Confidence Score: 5/5
The PR appears safe to merge; no new actionable issues were found.Summary
The PR lets
site_proxyload whenpwdis unavailable and adds a fresh-process regression test.pwdmodule.fcntl.Reviews (3) · Last reviewed commit: "fix(computer): revert runtime privilege ..." · Reviewed by Greptile