Repository navigation
Conversation
|
This pull request's description says it closes #212, which is on the project roadmap. When it merges, GitHub closes that issue and the roadmap counts it as delivered. Please confirm the change delivers the whole issue before merging. |
c18e82d to
c3e9a0e
Compare
rocklambros
left a comment
There was a problem hiding this comment.
Thanks for this, @almogbhl. The read gates are useful, and much of this PR is solid. Every new test fails on the base branch and passes on your branch, every fail-closed path denies, and CI is green.
I found three things I think need a fix before merge. Details are below, each with a way to check the fix. I also have five questions and suggestions that do not block the merge.
Before merge
adapters/tests/integrations/cursor/test_adapter.py:79: the new replay test passes only once on a developer machine. The second local run fails with-32005 REPLAY_DETECTED. Details are in the inline comment.adapters/src/acs/integrations/cursor/adapter.py:237: the read gates send the full file content to the Guardian. A file larger than about 1 MiB is always denied, and the README does not say that file content leaves the machine. Details are in the inline comment.adapters/docs/cursor/README.md:328: the docs read as if file reads are governed by default. The default wiring does not include the two read gates. Details are in the inline comment.
Questions and suggestions (optional)
- Did you check in live Cursor whether
{"permission": "allow"}frombeforeReadFilecan override Cursor's own read protections, such as.cursorignore? Cursor's docs do not say. I could not check this myself. - When
preToolUseis not wired, I think therequest_id_reflink for a file read is lost. Decision D-016 indocs/decisions/index.mdcovers howrequest_id_refis resolved. Is the missing link intended in that case? adapters/src/acs/integrations/cursor/wire.py:378: the--unwirehelp text does not mention the two new hooks. If you have time, maybe build the unwire list fromHOOK_MAP? Then the two lists always name the same hooks.wire.pysets a hook timeout only forsessionEnd. ThebeforeTabFileReadgate runs while the user types, so a slow Guardian may delay Tab completions. Would a short timeout on that gate help?adapters/docs/cursor/example_payloads.mdshows only the response shapes forbeforeReadFileandbeforeTabFileRead. A stdin example for each hook would help adapter authors.
What I checked
- Commit reviewed:
c3e9a0e9be36137536f9ee9702541c23f1165da6. - Conformance suites: with
nat, 824 passed. Withoutnat, everything passed except the replay test in item 1, on a second run. - Example Guardian suite: 611 passed. The ML-DSA-65 suites and the Go interop check passed.
- Wheel build and wheel check on Python 3.11 and 3.12 passed. The gateway suite passed with 928 tests. The labs pytest jobs passed.
- Repository pytest: 9382 passed. The import-boundary check passed.
- The 23 new tests fail on the base branch and pass on your branch.
- End to end,
acs-cursor-hook beforeReadFilereturned allow, deny, and deny on an unreachable Guardian, each in Cursor's response shape. - The docs' claims about the 19 mapped events match
HOOK_MAP. - A security review of the diff found no other problems.
- CI: 29 checks pass and 1 is skipped.
| self.assertEqual(rc, 0, err) | ||
| self.assertEqual(json.loads(out), {}, "an allow leaves the call to Cursor's own sandbox and approval") | ||
|
|
||
| def test_pre_tool_use_then_before_read_file_sharing_tool_use_id_does_not_replay(self) -> None: |
There was a problem hiding this comment.
I think this test needs a fix before merge, because it passes only once on a developer machine.
The test uses a fixed session_id ("cur-read-seq") and a fixed tool_use_id. So the uuid5 request_id is the same on every run. The CursorAdapter class starts the example Guardian without ACS_GUARDIAN_STATE_DIR. So the Guardian stores the seen request_id values in ~/.cache/acs-guardian-state, and they stay there after the run.
The first local run passes. The second local run fails with -32005 REPLAY_DETECTED. CI passes only because each runner starts empty. CONTRIBUTING asks contributors to run run_conformance.py locally, so after one run that command fails for everyone. The test also writes to the developer's real ~/.cache.
Could you give the Guardian a temporary state directory in setUpClass? adapters/tests/common/test_edge_cases.py:65 does the same thing:
@classmethod
def setUpClass(cls) -> None:
env = {k: v for k, v in os.environ.items() if k != "ACS_HMAC_SECRET_FILE"}
env.update(signing_env())
cls.statedir = tempfile.mkdtemp(prefix="acs-guardian-state-")
env["ACS_GUARDIAN_STATE_DIR"] = cls.statedir
cls.guardian_proc, cls.port = launch_guardian(env=env)Please also remove the directory in tearDownClass with shutil.rmtree(cls.statedir, ignore_errors=True), and add import shutil at the top of the file.
A fresh tool_use_id on each run also works, for example f"toolu_shared_read_{uuid.uuid4().hex}", used on both calls. I ran each fix three times in a row, and the test passed every time.
To check the fix, run this command twice in a row. Both runs should pass:
uv run python -m pytest adapters/tests/integrations/cursor/test_adapter.py -k does_not_replay| def _payload_before_read(event: dict[str, Any], *, tool_name: str = "Read") -> dict[str, Any]: | ||
| args: dict[str, Any] = {"file_path": event.get("file_path", "")} | ||
| if event.get("content") is not None: | ||
| args["content"] = event["content"] | ||
| if isinstance(event.get("attachments"), list): | ||
| args["attachments"] = event["attachments"] | ||
| return {"tool": {"name": tool_name}, "arguments": _wrap_arguments(args)} |
There was a problem hiding this comment.
I think this needs a fix before merge, because large file reads are now always denied.
_payload_before_read puts the full file content into arguments.content. A file over about 1 MiB makes the request larger than the maximum payload size. The same happens with about 525 KB of CJK text, because JSON escapes each character. The adapter then denies the read with request_exceeds_max_payload, even with ACS_DEFAULT_DENY=0. The Guardian receives no request at all. So a legitimate read is denied in every posture, which did not happen before this PR.
Could you cap the content and send its length and a SHA-256 hash above the cap? For example:
_MAX_INLINE_CONTENT_BYTES = 512 * 1024
def _payload_before_read(event: dict[str, Any], *, tool_name: str = "Read") -> dict[str, Any]:
args: dict[str, Any] = {"file_path": event.get("file_path", "")}
content = event.get("content")
if content is not None:
encoded = json.dumps(content).encode("utf-8")
if len(encoded) <= _MAX_INLINE_CONTENT_BYTES:
args["content"] = content
else:
raw = content.encode("utf-8") if isinstance(content, str) else encoded
args["content_length"] = len(raw)
args["content_sha256"] = hashlib.sha256(raw).hexdigest()
if isinstance(event.get("attachments"), list):
args["attachments"] = event["attachments"]
return {"tool": {"name": tool_name}, "arguments": _wrap_arguments(args)}Could you also add a test that reads a 2 MB file through beforeReadFile? The test should check that the Guardian receives the request and that the answer is {"permission": "allow"}.
There is a second point about the same lines. The full content goes to the Guardian before the read, and that includes files such as .env. The other adapters send only the path before a read. Today only mapping.md:23 mentions the content. Could you add one sentence to adapters/docs/cursor/README.md? For example:
`beforeReadFile` and `beforeTabFileRead` send the file's content to the Guardian before Cursor reads the file. Content above 512 KiB is sent as its length and SHA-256 hash.|
@almogbhl thanks, this closes the gap #212 describes. I reproduced Rock's first two blockers here too, and a large file is denied even with
|
Signed-off-by: almogbhl <almogbh.l@gmail.com>
…t state (#212) Signed-off-by: almogbhl <almogbh.l@gmail.com>
35e9d0e to
ab3c670
Compare
|
Thanks @rocklambros and @bar-capsule for the careful reviews, and for reproducing the blockers. The new commit fixes all three blockers. Fixed:
Questions:
Bar's point 1: Yes, for
So a rule that checks read content has to look in a different place for each host. |
What changed
Map Cursor's
beforeReadFileandbeforeTabFileReadhooks tosteps/toolCallRequest(ReadandTabRead) carryingfile_path,content, and optionalattachments, so a Guardian can inspect and block sensitive file reads before Cursor passes them to the model or Tab completions.Key details:
hook.py&adapter.py: Emit each hook's documented output shape ({"permission": "allow"}or{"permission": "deny", "user_message": ...}onbeforeReadFile;{"permission": "allow"}or{"permission": "deny"}onbeforeTabFileRead). Substituteaskanddefertodeny(audited asunenforceable_decision), andmodifytodeny(audited asmodify_unsupported).adapter.py): Real Cursor sends the sametool_use_idon bothpreToolUseandbeforeReadFilefor a singleReadcall. Pin the deterministicuuid5request_idonly onpreToolUse(forpostToolUserequest_id_refcorrelation) and issue a freshuuid4on tool-specific hooks sobeforeReadFilenever collides withpreToolUseas a replay.wire.py: AddbeforeReadFileandbeforeTabFileReadtoGATE_HOOKS(failClosed: trueandACS_DEFAULT_DENY=1) when opted in via--hooks, and clean up optional hooks onacs-cursor-wire --unwirewithout--hooks.mapping.md,README.md,example_payloads.md).Which issue does this implement
Closes #212
meaning, so it needs no issue
Base branch
integration. Every pull request targetsintegration, documentation included.Only a promotion targets
main(see CONTRIBUTING.md)release/0.2, because this changes normative specification text, a schema, or aconformance requirement for 0.2, or carries out such a change in code
Type of change
I tested this
uv run pytest -vpasses on my machineuv run mkdocs build --strictpasses on my machineAlso verified:
lint-imports(2 contracts kept, 0 broken) andadaptersCursor conformance suite (584 passed, 0 failed, plus new replay regression test)acs-example-guardian+ OPA (beforeReadFileandbeforeTabFileReadallow and deny paths)Checklist
git commit -s(required by the DCO)Security
Governs Cursor file reads (
beforeReadFileandbeforeTabFileRead) fail-closed when wired (failClosed: trueandACS_DEFAULT_DENY=1), closing the Cursor read-path gap tracked in #212, and preventstool_use_id-derivedrequest_idcollisions acrosspreToolUseand tool-specific hooks.