Skip to content

Commit 0600eb9

Browse files
committed
Isolate agent app connections and approvals (#13)
Bind remembered approval to a registered direct host and its protected key. Require explicit live-session approval when the host cannot be verified. Keep revocation, receipt and draft retry state scoped to each approved client. Agent: Codex
1 parent ac1fa84 commit 0600eb9

13 files changed

Lines changed: 1227 additions & 458 deletions

‎Helpers/AgentCatalogMCP.swift‎

Lines changed: 152 additions & 74 deletions
Large diffs are not rendered by default.

‎Sources/App/AgentPeerSigning.swift‎

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CoreFoundation
12
import CryptoKit
23
import Darwin
34
import Foundation
@@ -42,21 +43,65 @@ enum AgentPeerSigning {
4243
identifier: identifier, leaf: leaf, expectedHash: expectedHash)
4344
}
4445

45-
/// "identifier:team" of a validly signed, team-identified running program.
46-
/// Looked up by PID, so it narrows who may reuse an approved key but is not
47-
/// by itself proof of identity; the key signature is.
48-
static func teamSignedIdentity(pid: pid_t) -> String? {
46+
/// Only a directly launching app whose signed bundle explicitly declares ownership
47+
/// of the MCP transport can have durable approval. An unregistered launcher can
48+
/// still ask for approval for one live helper.
49+
static func registeredHost(pid: pid_t) -> String? {
50+
guard let child = process(pid), child.pbi_ppid > 1 else { return nil }
51+
let parentPID = pid_t(child.pbi_ppid)
52+
guard let parent = process(parentPID), let runningPath = executablePath(parentPID) else { return nil }
53+
4954
var guest: SecCode?
5055
var code: SecStaticCode?
5156
var info: CFDictionary?
52-
guard SecCodeCopyGuestWithAttributes(nil, [kSecGuestAttributePid as String: pid] as CFDictionary, [], &guest) == errSecSuccess,
53-
let guest, SecCodeCheckValidity(guest, [], nil) == errSecSuccess,
57+
guard SecCodeCopyGuestWithAttributes(nil, [kSecGuestAttributePid as String: parentPID] as CFDictionary,
58+
[], &guest) == errSecSuccess, let guest,
59+
SecCodeCheckValidity(guest, [], nil) == errSecSuccess,
5460
SecCodeCopyStaticCode(guest, [], &code) == errSecSuccess, let code,
5561
SecCodeCopySigningInformation(code, SecCSFlags(rawValue: kSecCSSigningInformation), &info) == errSecSuccess,
56-
let details = info as? [String: Any],
62+
let details = info as? [String: Any], hardened(details),
5763
let identifier = details[kSecCodeInfoIdentifier as String] as? String,
58-
let team = details[kSecCodeInfoTeamIdentifier as String] as? String, !team.isEmpty else { return nil }
59-
return identifier + ":" + team
64+
!identifier.isEmpty, identifier.utf8.count <= 255,
65+
let plist = details[kSecCodeInfoPList as String] as? [String: Any],
66+
plist["CFBundlePackageType"] as? String == "APPL",
67+
plist["CFBundleIdentifier"] as? String == identifier,
68+
let optedIn = plist["CodebasicTTSDirectMCPHost"] as? NSNumber,
69+
CFGetTypeID(optedIn) == CFBooleanGetTypeID(), optedIn.boolValue,
70+
let main = (details[kSecCodeInfoMainExecutable as String] as? URL)?.resolvingSymlinksInPath(),
71+
plist["CFBundleExecutable"] as? String == main.lastPathComponent,
72+
main.path == runningPath,
73+
main.deletingLastPathComponent().lastPathComponent == "MacOS",
74+
main.deletingLastPathComponent().deletingLastPathComponent().lastPathComponent == "Contents",
75+
main.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().pathExtension == "app",
76+
let certificates = details[kSecCodeInfoCertificates as String] as? [SecCertificate],
77+
let certificate = certificates.first,
78+
let currentChild = process(pid), currentChild.pbi_ppid == child.pbi_ppid,
79+
sameProcess(child, currentChild),
80+
let currentParent = process(parentPID), sameProcess(parent, currentParent),
81+
executablePath(parentPID) == runningPath,
82+
SecCodeCheckValidity(guest, [], nil) == errSecSuccess else { return nil }
83+
let signer = AgentBytes.hash(SecCertificateCopyData(certificate) as Data)
84+
return AgentPairing.registeredHostPrefix + "\(signer):\(identifier)"
85+
}
86+
87+
private static func process(_ pid: pid_t) -> proc_bsdinfo? {
88+
var info = proc_bsdinfo()
89+
let size = Int32(MemoryLayout<proc_bsdinfo>.size)
90+
guard proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, size) == size,
91+
info.pbi_uid == geteuid(), info.pbi_ruid == getuid(), info.pbi_start_tvsec > 0 else { return nil }
92+
return info
93+
}
94+
95+
private static func sameProcess(_ lhs: proc_bsdinfo, _ rhs: proc_bsdinfo) -> Bool {
96+
lhs.pbi_pid == rhs.pbi_pid && lhs.pbi_start_tvsec == rhs.pbi_start_tvsec
97+
&& lhs.pbi_start_tvusec == rhs.pbi_start_tvusec
98+
}
99+
100+
private static func executablePath(_ pid: pid_t) -> String? {
101+
var path = [CChar](repeating: 0, count: Int(MAXPATHLEN) * 4)
102+
guard proc_pidpath(pid, &path, UInt32(path.count)) > 0,
103+
let value = String(validatingUTF8: path) else { return nil }
104+
return URL(fileURLWithPath: value).resolvingSymlinksInPath().path
60105
}
61106

62107
private static func signedHash(_ code: SecStaticCode) -> Data? {

0 commit comments

Comments
 (0)