-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_installation_confirmation.py
More file actions
503 lines (432 loc) · 18.1 KB
/
Copy pathtest_installation_confirmation.py
File metadata and controls
503 lines (432 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from __future__ import annotations
import asyncio
import os
import threading
from typing import Any, cast
import pytest
from copilot import (
CopilotClient,
InstallationConfirmationContext,
RuntimeConnection,
)
from copilot._jsonrpc import JsonRpcClient, JsonRpcError
from copilot.rpc import InstallationConfirmationRequest, InstallationDecision
class _EndpointProcess:
def __init__(self, stdin, stdout) -> None:
self.stdin = stdin
self.stdout = stdout
self.stderr = None
self.returncode: int | None = None
def poll(self) -> int | None:
return self.returncode
def terminate(self) -> None:
self.returncode = 0
for stream in (self.stdin, self.stdout):
try:
stream.close()
except OSError:
pass
class _Peer:
def __init__(self, client: JsonRpcClient) -> None:
self.client = client
async def request(self, request_id: int | str, method: str, params: dict[str, Any]) -> Any:
assert self.client._loop is not None # noqa: SLF001
future = self.client._loop.create_future() # noqa: SLF001
with self.client._pending_lock: # noqa: SLF001
pending_requests = cast(
dict[int | str, asyncio.Future[Any]], self.client.pending_requests
)
pending_requests[request_id] = future
await self.client._send_message( # noqa: SLF001
{"jsonrpc": "2.0", "id": request_id, "method": method, "params": params}
)
try:
return await asyncio.wait_for(future, timeout=5)
finally:
with self.client._pending_lock: # noqa: SLF001
pending_requests = cast(
dict[int | str, asyncio.Future[Any]], self.client.pending_requests
)
pending_requests.pop(request_id, None)
async def notify(self, method: str, params: dict[str, Any]) -> None:
await self.client.notify(method, params)
class _Harness:
def __init__(
self,
client: CopilotClient,
sdk_rpc: JsonRpcClient,
peer_rpc: JsonRpcClient,
sdk_process: _EndpointProcess,
peer_process: _EndpointProcess,
) -> None:
self.client = client
self.sdk_rpc = sdk_rpc
self.peer_rpc = peer_rpc
self.peer = _Peer(peer_rpc)
self.sdk_process = sdk_process
self.peer_process = peer_process
async def close_peer_connection(self) -> None:
self.peer_process.stdin.close()
await asyncio.sleep(0)
async def close(self) -> None:
# Unblock both readers before closing either read descriptor (Windows CRT).
self.peer_process.stdin.close()
self.sdk_process.stdin.close()
self.peer_process.terminate()
self.sdk_process.terminate()
await self.peer_rpc.stop()
await self.sdk_rpc.stop()
def _pipe_process_pair() -> tuple[_EndpointProcess, _EndpointProcess]:
sdk_to_peer_read, sdk_to_peer_write = os.pipe()
peer_to_sdk_read, peer_to_sdk_write = os.pipe()
sdk_process = _EndpointProcess(
os.fdopen(sdk_to_peer_write, "wb", buffering=0),
os.fdopen(peer_to_sdk_read, "rb", buffering=0),
)
peer_process = _EndpointProcess(
os.fdopen(peer_to_sdk_write, "wb", buffering=0),
os.fdopen(sdk_to_peer_read, "rb", buffering=0),
)
return sdk_process, peer_process
def _request(name: str) -> dict[str, Any]:
return {
"confirmationId": f"challenge-{name}",
"operationId": name,
"policySessionId": "original-session",
"expiresAt": "2026-09-24T03:00:00Z",
"reviewFingerprint": f"fingerprint-{name}",
"review": {
"resource": "mcp",
"review": {
"action": "install",
"identity": {"canonicalName": "io.example/server", "serverName": "example"},
"provenance": {
"authority": "cards.example.test",
"validatedAt": "2026-09-24T02:59:00Z",
"cardDigest": {
"algorithm": "sha256-rfc8785",
"value": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
},
"mediaType": "application/mcp-server-card+json",
},
"target": {"scope": "user", "configKey": "example"},
"policy": {"decision": "allowed", "source": "none"},
"selectedChoice": {
"choiceId": "remote-choice",
"installMethod": "remote",
"endpoint": "https://example.test/mcp",
"transport": "streamable-http",
"requiredValues": [],
"secretPlaceholders": [],
},
"configurationChange": {
"operation": "add",
"scope": "user",
"configKey": "example",
"changedFields": ["type", "url", "headers", "tools"],
"secretReferences": [],
},
"inputs": [],
"suppliedSecrets": [],
"secretStorage": "keychain",
"effectiveConfiguration": {
"transport": "streamable-http",
"url": "https://example.test/mcp",
"headers": {"X-Region": "eu"},
"tools": ["*"],
},
},
},
}
async def _connect(handler=None) -> _Harness:
sdk_process, peer_process = _pipe_process_pair()
sdk_rpc = JsonRpcClient(sdk_process)
peer_rpc = JsonRpcClient(peer_process)
client = CopilotClient(
connection=RuntimeConnection.for_uri("localhost:1234"),
installation_confirmation_handler=handler,
)
client._client = sdk_rpc # noqa: SLF001
sdk_rpc.on_close = client._handle_connection_close # noqa: SLF001
loop = asyncio.get_running_loop()
sdk_rpc.start(loop)
peer_rpc.start(loop)
client._register_client_global_handlers() # noqa: SLF001
return _Harness(client, sdk_rpc, peer_rpc, sdk_process, peer_process)
async def _wait_for(predicate, *, timeout: float = 5.0) -> None:
deadline = asyncio.get_running_loop().time() + timeout
while not predicate():
if asyncio.get_running_loop().time() >= deadline:
raise AssertionError("condition was not met before timeout")
await asyncio.sleep(0)
async def test_closes_both_writers_before_waiting_for_pipe_readers(monkeypatch) -> None:
loop = asyncio.get_running_loop()
reading = asyncio.Event()
sdk_process, peer_process = _pipe_process_pair()
reader_lock = threading.Lock()
original_readline = peer_process.stdout.readline
original_close = peer_process.stdout.close
# Windows' CRT holds the descriptor lock across a blocking read and takes
# that same lock on close. Reproduce that boundary on every platform.
def locked_readline():
with reader_lock:
loop.call_soon_threadsafe(reading.set)
return original_readline()
def locked_close():
with reader_lock:
original_close()
monkeypatch.setattr(peer_process.stdout, "readline", locked_readline)
monkeypatch.setattr(peer_process.stdout, "close", locked_close)
monkeypatch.setattr(f"{__name__}._pipe_process_pair", lambda: (sdk_process, peer_process))
harness = await _connect()
closing = None
try:
await asyncio.wait_for(reading.wait(), timeout=5)
closing = asyncio.create_task(asyncio.to_thread(lambda: asyncio.run(harness.close())))
await asyncio.wait_for(asyncio.shield(closing), timeout=5)
for rpc in (harness.peer_rpc, harness.sdk_rpc):
assert rpc._read_thread is not None # noqa: SLF001
assert not rpc._read_thread.is_alive() # noqa: SLF001
for process in (sdk_process, peer_process):
assert process.stdin.closed
assert process.stdout.closed
finally:
# Release a deadlocked close before awaiting its cleanup on failure.
sdk_process.stdin.close()
peer_process.stdin.close()
if closing is not None:
await closing
else:
await harness.close()
async def test_presents_typed_full_review_and_echoes_challenge_and_fingerprint() -> None:
seen: list[tuple[InstallationConfirmationRequest, InstallationConfirmationContext]] = []
async def handler(
incoming: InstallationConfirmationRequest,
context: InstallationConfirmationContext,
) -> InstallationDecision:
seen.append((incoming, context))
assert not context.cancelled.is_set()
assert incoming.operation_id == "a"
assert incoming.policy_session_id == "original-session"
assert incoming.review.review.action.value == "install"
incoming.confirmation_id = "mutated"
incoming.review_fingerprint = "mutated"
return InstallationDecision.CONFIRM
harness = await _connect(handler)
try:
response = await harness.peer.request(301, "installations.confirm", _request("a"))
assert response == {
"confirmationId": "challenge-a",
"reviewFingerprint": "fingerprint-a",
"decision": "confirm",
}
assert len(seen) == 1
finally:
await harness.close()
async def test_keeps_concurrent_reviews_independent_and_serves_other_rpc() -> None:
decisions = {
"a": asyncio.get_running_loop().create_future(),
"b": asyncio.get_running_loop().create_future(),
}
seen: list[str] = []
def handler(
incoming: InstallationConfirmationRequest,
_context: InstallationConfirmationContext,
):
seen.append(incoming.operation_id)
return decisions[incoming.operation_id]
harness = await _connect(handler)
try:
first = asyncio.create_task(
harness.peer.request(401, "installations.confirm", _request("a"))
)
second = asyncio.create_task(
harness.peer.request(402, "installations.confirm", _request("b"))
)
await _wait_for(lambda: sorted(seen) == ["a", "b"])
with pytest.raises(JsonRpcError, match="No GitHub token provider"):
await harness.peer.request(
403,
"gitHubToken.getToken",
{"registrationId": "unknown", "host": "github.com", "reason": "initial"},
)
decisions["b"].set_result("decline")
assert await second == {
"confirmationId": "challenge-b",
"reviewFingerprint": "fingerprint-b",
"decision": "decline",
}
decisions["a"].set_result("confirm")
assert (await first)["confirmationId"] == "challenge-a"
finally:
await harness.close()
async def test_cancel_request_retires_only_cancelled_review_and_drops_late_decision() -> None:
late_decision = asyncio.get_running_loop().create_future()
contexts: dict[str, InstallationConfirmationContext] = {}
def handler(
incoming: InstallationConfirmationRequest,
context: InstallationConfirmationContext,
):
contexts[incoming.operation_id] = context
if incoming.operation_id == "a":
return late_decision
return "confirm"
harness = await _connect(handler)
try:
first = asyncio.create_task(
harness.peer.request(501, "installations.confirm", _request("a"))
)
await _wait_for(lambda: "a" in contexts)
await harness.peer.notify("$/cancelRequest", {"id": 501})
with pytest.raises(JsonRpcError) as cancelled:
await first
assert cancelled.value.code == -32800
assert contexts["a"].cancelled.is_set()
late_decision.set_result("confirm")
response = await harness.peer.request(502, "installations.confirm", _request("b"))
assert response["confirmationId"] == "challenge-b"
assert not contexts["b"].cancelled.is_set()
finally:
await harness.close()
async def test_cancel_request_and_decision_ready_together_rejects_decision() -> None:
late_decision = asyncio.get_running_loop().create_future()
context: InstallationConfirmationContext | None = None
def handler(
_incoming: InstallationConfirmationRequest,
incoming_context: InstallationConfirmationContext,
):
nonlocal context
context = incoming_context
return late_decision
harness = await _connect(handler)
try:
pending = asyncio.create_task(
harness.peer.request(551, "installations.confirm", _request("a"))
)
await _wait_for(lambda: context is not None)
harness.sdk_rpc._handle_cancel_request({"id": 551}) # noqa: SLF001
late_decision.set_result("confirm")
with pytest.raises(JsonRpcError) as cancelled:
await pending
assert cancelled.value.code == -32800
assert context is not None
assert context.cancelled.is_set()
finally:
await harness.close()
async def test_stale_unknown_and_string_cancel_id_do_not_touch_successor() -> None:
contexts: dict[str, InstallationConfirmationContext] = {}
def handler(
incoming: InstallationConfirmationRequest,
context: InstallationConfirmationContext,
):
contexts[incoming.operation_id] = context
return "decline"
harness = await _connect(handler)
try:
await harness.peer.notify("$/cancelRequest", {"id": 601})
await harness.peer.notify("$/cancelRequest", {"id": 999})
await harness.peer.notify("$/cancelRequest", {"id": "602"})
response = await harness.peer.request(602, "installations.confirm", _request("b"))
assert response["decision"] == "decline"
assert not contexts["b"].cancelled.is_set()
finally:
await harness.close()
async def test_connection_close_cancels_all_reviews_and_isolated_to_original_connection() -> None:
decisions = {
"a": asyncio.get_running_loop().create_future(),
"b": asyncio.get_running_loop().create_future(),
}
originals: dict[str, InstallationConfirmationContext] = {}
def first_handler(
incoming: InstallationConfirmationRequest,
context: InstallationConfirmationContext,
):
originals[incoming.operation_id] = context
return decisions[incoming.operation_id]
first = await _connect(first_handler)
second = await _connect(lambda _request, _context: "decline")
try:
pending_a = asyncio.create_task(
first.peer.request(701, "installations.confirm", _request("a"))
)
pending_b = asyncio.create_task(
first.peer.request(702, "installations.confirm", _request("b"))
)
await _wait_for(lambda: set(originals) == {"a", "b"})
await first.close_peer_connection()
await _wait_for(lambda: all(context.cancelled.is_set() for context in originals.values()))
decisions["a"].set_result("confirm")
decisions["b"].set_result("decline")
pending_a.cancel()
pending_b.cancel()
await asyncio.gather(pending_a, pending_b, return_exceptions=True)
response = await second.peer.request(703, "installations.confirm", _request("c"))
assert response["decision"] == "decline"
finally:
await first.close()
await second.close()
async def test_stopping_client_retires_pending_reviews() -> None:
decision = asyncio.get_running_loop().create_future()
context: InstallationConfirmationContext | None = None
def handler(_incoming: InstallationConfirmationRequest, incoming_context):
nonlocal context
context = incoming_context
return decision
harness = await _connect(handler)
try:
pending = asyncio.create_task(
harness.peer.request(801, "installations.confirm", _request("a"))
)
await _wait_for(lambda: context is not None)
await harness.client.stop()
await _wait_for(lambda: context is not None and context.cancelled.is_set())
assert context is not None
assert context.cancelled.is_set()
decision.set_result("confirm")
pending.cancel()
await asyncio.gather(pending, return_exceptions=True)
finally:
await harness.close()
async def test_refuses_missing_handler_invalid_review_handler_errors_and_unknown_decisions():
missing = await _connect()
try:
with pytest.raises(JsonRpcError, match="No installations client-global handler registered"):
await missing.peer.request(901, "installations.confirm", _request("a"))
finally:
await missing.close()
invalid = await _connect(lambda _request, _context: "confirm")
try:
with pytest.raises(JsonRpcError) as exc:
await invalid.peer.request(902, "installations.confirm", {"confirmationId": "only"})
assert exc.value.code == -32602
finally:
await invalid.close()
failing = await _connect(lambda _request, _context: (_ for _ in ()).throw(RuntimeError("no")))
try:
with pytest.raises(JsonRpcError, match="no"):
await failing.peer.request(903, "installations.confirm", _request("a"))
finally:
await failing.close()
unknown = await _connect(lambda _request, _context: "approve")
try:
with pytest.raises(JsonRpcError, match="Invalid installation confirmation decision"):
await unknown.peer.request(904, "installations.confirm", _request("a"))
finally:
await unknown.close()
async def test_does_not_infer_optional_legacy_session_authority() -> None:
observed: list[str | None] = []
def handler(
incoming: InstallationConfirmationRequest,
_context: InstallationConfirmationContext,
):
observed.append(incoming.policy_session_id)
return "decline"
incoming = _request("legacy")
del incoming["policySessionId"]
harness = await _connect(handler)
try:
response = await harness.peer.request(1001, "installations.confirm", incoming)
assert response["decision"] == "decline"
assert observed == [None]
finally:
await harness.close()