Skip to content

Commit 3ea03b9

Browse files
committed
Ask again after a revoked agent app reconnects (#43)
Revoking an app's approval kept its key refused for good, and the app offered no way back short of an undocumented --forget-key run. Now the revoked live helper stays blocked, but a new session of that app gets a fresh approval request; allowing it lifts the key's revocation (and any unsaved revocation of it). A failed save leaves the key revoked. Agent: Claude Code (runtime model unverified) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7NX2yks21D5Tau2tYG
1 parent d271b4b commit 3ea03b9

7 files changed

Lines changed: 60 additions & 30 deletions

File tree

‎Helpers/AgentCatalogMCP.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ import Security
184184
clientOverride = AgentPairing.clientName(arguments[flag + 1])
185185
}
186186
if arguments.contains("--forget-key") {
187-
// Explicit reset from the same registered host. A revoked credential
188-
// stays revoked until the host requests a reset and the user approves again.
187+
// Explicit reset from the same registered host. Not needed after a
188+
// revocation: a new session of the host asks the user again.
189189
guard let registeredHost else { exit(1) }
190190
exit(ClientKey.delete(host: registeredHost) ? 0 : 1)
191191
}
@@ -421,7 +421,7 @@ import Security
421421
("unproven", "The selected instruction changed or was not read by this helper. Read it again with \(reader), then resend with the returned profile and revision."),
422422
("approval_required", "Ask the user to approve this pending connection in the app. Do not retry in a loop."),
423423
("disabled", "Draft receipt is off for this app or globally. Ask the user to turn it on."),
424-
("revoked", "This app's approval was revoked. It requires a credential reset and new approval."),
424+
("revoked", "The user revoked this app's approval for this session. Stop. A new agent session asks the user for approval again."),
425425
("locked", "The Mac is locked. Retry after the user unlocks it."),
426426
("too_large", "A field is over its limit. Nothing was opened or shortened. Shorten it, or split the body into separate drafts with their own keys."),
427427
("invalid", "A field is missing, misnamed or malformed. Fix the arguments; resending unchanged will not help."),
@@ -517,7 +517,7 @@ import Security
517517
case "approval_required": return "Ask the user to approve this specific pending connection in \(TTSRuntime.displayName) → Agent (에이전트), then check again."
518518
case "disabled": return "Draft receipt is off globally or for this agent app. Ask the user to enable it in \(TTSRuntime.displayName) → Agent (에이전트)."
519519
case "locked": return "The Mac's screen is locked, so nothing is received. Retry after the user unlocks it; no new approval is needed."
520-
case "revoked": return "The user removed this client in \(TTSRuntime.displayName). An explicit host credential reset and a new approval are required for this registered agent."
520+
case "revoked": return "The user revoked this app's approval in \(TTSRuntime.displayName). This session stays disconnected; start a new agent session to ask the user for approval again."
521521
case "upgrade_required": return "The running app and this helper are different versions. Use the helper bundled with the running app."
522522
case "reading_only": return "Connected. One instruction revision is approved. Draft receipt is off."
523523
case "receiving_only": return "Connected. Send with submit_tts_draft; there is no selected instruction."

‎Sources/App/AgentSessionController.swift‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ import Security
371371
if blockedPeers.contains(peer) { return ["status": "revoked"] }
372372
guard let keyID = AgentPairingProof.keyID(publicKey) else { return ["status": "invalid"] }
373373
let host = persistent ? AgentPeerSigning.registeredHost(pid: peer.pid) : nil
374-
if trust.lookup(keyID: keyID, host: host ?? "") == .revoked { return ["status": "revoked"] }
374+
// A revoked key gets a challenge like an unknown one: a new helper
375+
// process may ask the user again (the revoked live helper stays blocked).
375376
var bytes = [UInt8](repeating: 0, count: 32)
376377
guard SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes) == errSecSuccess else { return ["status": "unavailable"] }
377378
let nonce = Data(bytes).base64EncodedString()
@@ -400,7 +401,6 @@ import Security
400401
let lookup = trust.lookup(keyID: claim.keyID, host: claim.host ?? "")
401402
let outcome: [String: String]
402403
switch lookup {
403-
case .revoked: outcome = ["status": "revoked"]
404404
case .trusted(let record) where claim.host != nil:
405405
if !autoReceive || !record.receiptEnabled { outcome = ["status": "disabled"] }
406406
else if bindings[record.id] != nil { outcome = ["status": "other_agent"] }
@@ -483,8 +483,7 @@ import Security
483483
removeApproval(id)
484484
let claim = request.claim
485485
guard ContinuousClock.now < request.deadline, Self.sameHost(as: claim),
486-
!blockedPeers.contains(claim.peer), binding(for: claim.peer) == nil,
487-
trust.lookup(keyID: claim.keyID, host: claim.host ?? "") != .revoked else { return false }
486+
!blockedPeers.contains(claim.peer), binding(for: claim.peer) == nil else { return false }
488487
var client = UUID()
489488
if let host = claim.host {
490489
switch trust.lookup(keyID: claim.keyID, host: host) {

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ final class AgentTrustStore {
177177
lastGood = loaded
178178
savedRevoked.formUnion(loaded.revoked)
179179
needsAdopt = false
180-
// Revocations are never undone (a revoked key cannot be approved again),
180+
// Revocations are undone only by the user's new approval of that key,
181181
// so every revocation this run knows is kept, also over a restored older
182182
// copy that predates it. Receipt choices are not merged this way: the user
183183
// may have turned receipt back on through another instance in between.
@@ -330,10 +330,15 @@ final class AgentTrustStore {
330330

331331
/// Explicit approval can renew or rotate the credential for one registered
332332
/// host. Other hosts keep their records even when the labels match.
333+
/// A revoked key can be approved again only by the user's new approval
334+
/// (#33 manual check 6/7): the approval lifts its revocation.
333335
func trust(publicKey: Data, label: String, host: String) throws -> AgentTrustedClient {
334336
guard let keyID = AgentPairingProof.keyID(publicKey) else { throw Failure.invalid }
335-
guard !file.revoked.contains(keyID) else { throw Failure.revoked }
336337
var next = file
338+
next.revoked.removeAll { $0 == keyID }
339+
// An unsaved revocation of this key must not undo the new approval later.
340+
let hadPendingRevoke = pending.contains(.revoke(keyID))
341+
pending.removeAll { $0 == .revoke(keyID) }
337342
let time = now()
338343
let client: AgentTrustedClient
339344
var replaced: String?
@@ -358,6 +363,8 @@ final class AgentTrustStore {
358363
}
359364
if let replaced, !pending.contains(.revoke(replaced)) { pending.append(.revoke(replaced)) }
360365
guard grant(next, byUser: false) else {
366+
// Not approved: the key stays revoked as before.
367+
if hadPendingRevoke, !pending.contains(.revoke(keyID)) { pending.append(.revoke(keyID)) }
361368
// The new key is not approved, but the replaced one stays revoked.
362369
if let replaced {
363370
Self.apply(.revoke(replaced), to: &file)
@@ -397,9 +404,9 @@ final class AgentTrustStore {
397404
_ = commit(next, byUser: false, denial: false)
398405
}
399406

400-
/// Forgetting tombstones the key, so the same credential cannot come back
401-
/// even inside a later approval window. Fails closed in memory if the disk
402-
/// write fails.
407+
/// Forgetting revokes the key: it is refused until the user approves a new
408+
/// request from it, and a restored older copy does not bring it back. Fails
409+
/// closed in memory if the disk write fails.
403410
@discardableResult
404411
func forget(_ id: UUID) -> Bool {
405412
guard let client = file.clients.first(where: { $0.id == id }) else { return false }

‎Tests/AgentCatalogTests.swift‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,15 @@ enum CodeExplanation { static let noChange = "변경 없음" }
441441
precondition(store.forget(renewed.id))
442442
store = open()
443443
precondition(store.lookup(keyID: keyID, host: "h") == .revoked && store.clients.count == AgentTrustStore.maxClients - 1)
444-
do {
445-
_ = try store.trust(publicKey: publicKey, label: "claude", host: "h")
446-
preconditionFailure("a forgotten key must not be approved again")
447-
} catch AgentTrustStore.Failure.revoked {}
444+
// The user's new approval of a forgotten key lifts its revocation (#33 manual
445+
// check 6/7), also after a restart; forgetting it again revokes it again.
446+
let reapproved = try store.trust(publicKey: publicKey, label: "claude", host: "h")
447+
precondition(store.lookup(keyID: keyID, host: "h") == .trusted(reapproved) && !store.file.revoked.contains(keyID))
448+
store = open()
449+
precondition(store.lookup(keyID: keyID, host: "h") == .trusted(reapproved))
450+
precondition(store.forget(reapproved.id))
451+
store = open()
452+
precondition(store.lookup(keyID: keyID, host: "h") == .revoked)
448453

449454
let retryURL = directory.appendingPathComponent("agent-denial.json")
450455
var writesFail = false
@@ -464,6 +469,22 @@ enum CodeExplanation { static let noChange = "변경 없음" }
464469
precondition(retry.setReceiptEnabled(false, for: rotated.id))
465470
let restarted = AgentTrustStore(url: retryURL)
466471
precondition(!restarted.autoReceive && restarted.clients.count == 1 && !restarted.clients[0].receiptEnabled)
472+
// A revocation not saved yet does not undo a later approval of the same key,
473+
// and a failed approval leaves it revoked and pending.
474+
writesFail = true
475+
precondition(!retry.forget(rotated.id) && retry.hasUnsavedDenials)
476+
let otherKey = other.publicKey.x963Representation
477+
let otherID = AgentPairingProof.keyID(otherKey)!
478+
do {
479+
_ = try retry.trust(publicKey: otherKey, label: "same", host: registeredHost)
480+
preconditionFailure("an approval that cannot be saved must fail")
481+
} catch AgentTrustStore.Failure.unwritable {}
482+
precondition(retry.lookup(keyID: otherID, host: registeredHost) == .revoked && retry.hasUnsavedDenials)
483+
writesFail = false
484+
let back = try retry.trust(publicKey: otherKey, label: "same", host: registeredHost)
485+
retry.adoptDisk()
486+
precondition(retry.lookup(keyID: otherID, host: registeredHost) == .trusted(back) && !retry.hasUnsavedDenials)
487+
precondition(AgentTrustStore(url: retryURL).lookup(keyID: otherID, host: registeredHost) == .trusted(back))
467488

468489
// Anything but our own private, intact file is no trust at all.
469490
precondition(chmod(url.path, 0o644) == 0 && open().clients.isEmpty && !open().autoReceive)

‎Tests/Fixtures/run_agent_app_connections.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,15 @@ def connect(host, helper="helper", key_dir=keys):
295295
expect_status(a, "revoked")
296296
assert a.send("after-revoke", "철회 뒤 차단되는 원고")["outcome"] == "rejected"
297297
a.close()
298+
# A new session of the revoked app asks the user again (#33 manual check 6/7).
298299
a = connect("A")
299-
expect_status(a, "revoked")
300+
expect_status(a, "approval_required")
301+
request_again, = requests(server)
300302
expect_status(b, "receiving_only")
301303
assert b.send("after-other-revoke", "다른 앱 철회 뒤의 B 원고")["status"] == "accepted"
302-
check("master off survives restart; revocation clears A's pending approvals and blocks its old key")
304+
approve(server, a, request_again)
305+
check("master off survives restart; revocation clears A's pending approvals and blocks its live helper; "
306+
"a new A session asks again")
303307

304308
other = connect("B", helper="other-helper")
305309
expect_status(other, "app_unavailable")

‎Tests/Fixtures/run_persistent_pairing.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,20 @@ def client_id():
304304
assert server.snapshot()["trusted"] == [slot_a]
305305
ok("lost or replaced client key: blocked until a new approval, which supersedes the old record")
306306

307-
# forget <id> revokes the app's key (#13): a late request and a restart with the
308-
# same key are refused for good; only a key reset and a new approval connect again.
307+
# forget <id> revokes the app's key (#13): a late request is refused and that
308+
# helper stays revoked. A new session with the same key asks the user again and
309+
# connects only after a new approval (#33 manual check 6/7).
309310
assert server("forget " + client_id()) == "ok"
310311
late = a.send("late", "잊은 뒤 늦은 요청")
311312
assert late["outcome"] == "rejected" and server.documents() == []
312313
assert a.status() == "revoked" and server.snapshot()["trusted"] == []
313314
a.close()
314315
a = client()
315-
assert a.status() == "revoked" and not server.snapshot()["paired"]
316-
a.close()
317-
assert forget_key(slot_a) == 0
318-
a = client()
319-
assert a.status() == "approval_required"
316+
assert a.status() == "approval_required" and not server.snapshot()["paired"]
317+
assert server.snapshot()["trusted"] == []
320318
assert fs.approve(server, a) == "receiving_only"; clicks += 1
321-
ok("forget: late request and restart are refused; only a new approval with a new key connects again")
319+
assert server.snapshot()["trusted"] == [slot_a]
320+
ok("forget: late request refused, that helper stays revoked; a new session asks again and connects after approval")
322321

323322
# disconnect <id> is this app's receipt off (#13): it keeps the approval and stays
324323
# off across client and app restarts; resume turns it back on with no new pairing.

0 commit comments

Comments
 (0)