Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: add code signing requirements to xpc connections#206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanndickson merged 1 commit intomainfromethan/xpc-validation
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsCoder-Desktop/Coder-Desktop/AppHelperXPCClient.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,6 +37,7 @@ import VPNLib
_=self.connect()
}
logger.info("connecting to\(helperAppMachServiceName)")
connection.setCodeSigningRequirement(Validator.xpcPeerRequirement)
connection.resume()
self.connection= connection
return connection
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,7 @@ class HelperNEXPCServer: NSObject, NSXPCListenerDelegate, @unchecked Sendable {
conns.removeAll { $0 == newConnection }
logger.debug("connection interrupted")
}
newConnection.setCodeSigningRequirement(Validator.xpcPeerRequirement)
newConnection.resume()
conns.append(newConnection)
return true
Expand DownExpand Up@@ -145,6 +146,7 @@ class HelperAppXPCServer: NSObject, NSXPCListenerDelegate, @unchecked Sendable {
conns.removeAll { $0 == newConnection }
logger.debug("app connection invalidated")
}
newConnection.setCodeSigningRequirement(Validator.xpcPeerRequirement)
newConnection.resume()
conns.append(newConnection)
return true
Expand Down
1 change: 1 addition & 0 deletionsCoder-Desktop/VPN/NEHelperXPCClient.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@ final class HelperXPCClient: @unchecked Sendable {
connection.interruptionHandler={[weak self]in
self?.connection=nil
}
connection.setCodeSigningRequirement(Validator.xpcPeerRequirement)
connection.resume()
self.connection= connection
return connection
Expand Down
124 changes: 0 additions & 124 deletionsCoder-Desktop/VPNLib/Download.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,6 @@
import CryptoKit
import Foundation

public enum ValidationError: Error {
case fileNotFound
case unableToCreateStaticCode
case invalidSignature
case unableToRetrieveInfo
case invalidIdentifier(identifier: String?)
case invalidTeamIdentifier(identifier: String?)
case missingInfoPList
case invalidVersion(version: String?)
case belowMinimumCoderVersion

public var description: String {
switch self {
case .fileNotFound:
"The file does not exist."
case .unableToCreateStaticCode:
"Unable to create a static code object."
case .invalidSignature:
"The file's signature is invalid."
case .unableToRetrieveInfo:
"Unable to retrieve signing information."
case let .invalidIdentifier(identifier):
"Invalid identifier: \(identifier ?? "unknown")."
case let .invalidVersion(version):
"Invalid runtime version: \(version ?? "unknown")."
case let .invalidTeamIdentifier(identifier):
"Invalid team identifier: \(identifier ?? "unknown")."
case .missingInfoPList:
"Info.plist is not embedded within the dylib."
case .belowMinimumCoderVersion:
"""
The Coder deployment must be version \(Validator.minimumCoderVersion)
or higher to use Coder Desktop.
"""
}
}

public var localizedDescription: String { description }
}

public class Validator {
// Whilst older dylibs exist, this app assumes v2.20 or later.
public static let minimumCoderVersion = "2.20.0"

private static let expectedName = "CoderVPN"
private static let expectedIdentifier = "com.coder.Coder-Desktop.VPN.dylib"
private static let expectedTeamIdentifier = "4399GN35BJ"

private static let infoIdentifierKey = "CFBundleIdentifier"
private static let infoNameKey = "CFBundleName"
private static let infoShortVersionKey = "CFBundleShortVersionString"

private static let signInfoFlags: SecCSFlags = .init(rawValue: kSecCSSigningInformation)

// `expectedVersion` must be of the form `[0-9]+.[0-9]+.[0-9]+`
public static func validate(path: URL, expectedVersion: String) throws(ValidationError) {
guard FileManager.default.fileExists(atPath: path.path) else {
throw .fileNotFound
}

var staticCode: SecStaticCode?
let status = SecStaticCodeCreateWithPath(path as CFURL, SecCSFlags(), &staticCode)
guard status == errSecSuccess, let code = staticCode else {
throw .unableToCreateStaticCode
}

let validateStatus = SecStaticCodeCheckValidity(code, SecCSFlags(), nil)
guard validateStatus == errSecSuccess else {
throw .invalidSignature
}

var information: CFDictionary?
let infoStatus = SecCodeCopySigningInformation(code, signInfoFlags, &information)
guard infoStatus == errSecSuccess, let info = information as? [String: Any] else {
throw .unableToRetrieveInfo
}

guard let identifier = info[kSecCodeInfoIdentifier as String] as? String,
identifier == expectedIdentifier
else {
throw .invalidIdentifier(identifier: info[kSecCodeInfoIdentifier as String] as? String)
}

guard let teamIdentifier = info[kSecCodeInfoTeamIdentifier as String] as? String,
teamIdentifier == expectedTeamIdentifier
else {
throw .invalidTeamIdentifier(
identifier: info[kSecCodeInfoTeamIdentifier as String] as? String
)
}

guard let infoPlist = info[kSecCodeInfoPList as String] as? [String: AnyObject] else {
throw .missingInfoPList
}

try validateInfo(infoPlist: infoPlist, expectedVersion: expectedVersion)
}

private static func validateInfo(infoPlist: [String: AnyObject], expectedVersion: String) throws(ValidationError) {
guard let plistIdent = infoPlist[infoIdentifierKey] as? String, plistIdent == expectedIdentifier else {
throw .invalidIdentifier(identifier: infoPlist[infoIdentifierKey] as? String)
}

guard let plistName = infoPlist[infoNameKey] as? String, plistName == expectedName else {
throw .invalidIdentifier(identifier: infoPlist[infoNameKey] as? String)
}

// Downloaded dylib must match the version of the server
guard let dylibVersion = infoPlist[infoShortVersionKey] as? String,
expectedVersion == dylibVersion
else {
throw .invalidVersion(version: infoPlist[infoShortVersionKey] as? String)
}

// Downloaded dylib must be at least the minimum Coder server version
guard let dylibVersion = infoPlist[infoShortVersionKey] as? String,
// x.compare(y) is .orderedDescending if x > y
minimumCoderVersion.compare(dylibVersion, options: .numeric) != .orderedDescending
else {
throw .belowMinimumCoderVersion
}
}
}

public func download(
src: URL,
dest: URL,
Expand Down
128 changes: 128 additions & 0 deletionsCoder-Desktop/VPNLib/Validate.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
import Foundation

publicenumValidationError:Error{
case fileNotFound
case unableToCreateStaticCode
case invalidSignature
case unableToRetrieveInfo
case invalidIdentifier(identifier:String?)
case invalidTeamIdentifier(identifier:String?)
case missingInfoPList
case invalidVersion(version:String?)
case belowMinimumCoderVersion

publicvardescription:String{
switchself{
case.fileNotFound:
"The file does not exist."
case.unableToCreateStaticCode:
"Unable to create a static code object."
case.invalidSignature:
"The file's signature is invalid."
case.unableToRetrieveInfo:
"Unable to retrieve signing information."
caselet.invalidIdentifier(identifier):
"Invalid identifier:\(identifier??"unknown")."
caselet.invalidVersion(version):
"Invalid runtime version:\(version??"unknown")."
caselet.invalidTeamIdentifier(identifier):
"Invalid team identifier:\(identifier??"unknown")."
case.missingInfoPList:
"Info.plist is not embedded within the dylib."
case.belowMinimumCoderVersion:
"""
The Coder deployment must be version\(Validator.minimumCoderVersion)
or higher to use Coder Desktop.
"""
}
}

publicvarlocalizedDescription:String{ description}
}

publicclassValidator{
// Whilst older dylibs exist, this app assumes v2.20 or later.
publicstaticletminimumCoderVersion="2.20.0"

privatestaticletexpectedName="CoderVPN"
privatestaticletexpectedIdentifier="com.coder.Coder-Desktop.VPN.dylib"
privatestaticletexpectedTeamIdentifier="4399GN35BJ"

privatestaticletinfoIdentifierKey="CFBundleIdentifier"
privatestaticletinfoNameKey="CFBundleName"
privatestaticletinfoShortVersionKey="CFBundleShortVersionString"

privatestaticletsignInfoFlags:SecCSFlags=.init(rawValue: kSecCSSigningInformation)

// `expectedVersion` must be of the form `[0-9]+.[0-9]+.[0-9]+`
publicstaticfunc validate(path:URL, expectedVersion:String)throws(ValidationError){
guardFileManager.default.fileExists(atPath: path.path)else{
throw.fileNotFound
}

varstaticCode:SecStaticCode?
letstatus=SecStaticCodeCreateWithPath(pathasCFURL,SecCSFlags(),&staticCode)
guard status== errSecSuccess,let code= staticCodeelse{
throw.unableToCreateStaticCode
}

letvalidateStatus=SecStaticCodeCheckValidity(code,SecCSFlags(),nil)
guard validateStatus== errSecSuccesselse{
throw.invalidSignature
}

varinformation:CFDictionary?
letinfoStatus=SecCodeCopySigningInformation(code, signInfoFlags,&information)
guard infoStatus== errSecSuccess,let info= informationas?[String:Any]else{
throw.unableToRetrieveInfo
}

guardlet identifier=info[kSecCodeInfoIdentifierasString]as?String,
identifier== expectedIdentifier
else{
throw.invalidIdentifier(identifier:info[kSecCodeInfoIdentifierasString]as?String)
}

guardlet teamIdentifier=info[kSecCodeInfoTeamIdentifierasString]as?String,
teamIdentifier== expectedTeamIdentifier
else{
throw.invalidTeamIdentifier(
identifier:info[kSecCodeInfoTeamIdentifierasString]as?String
)
}

guardlet infoPlist=info[kSecCodeInfoPListasString]as?[String:AnyObject]else{
throw.missingInfoPList
}

tryvalidateInfo(infoPlist: infoPlist, expectedVersion: expectedVersion)
}

publicstaticletxpcPeerRequirement="anchor apple generic"+ // Apple-issued certificate chain
" and certificate leaf[subject.OU] =\""+ expectedTeamIdentifier+"\"" // Signed by the Coder team

privatestaticfunc validateInfo(infoPlist:[String:AnyObject], expectedVersion:String)throws(ValidationError){
guardlet plistIdent=infoPlist[infoIdentifierKey]as?String, plistIdent== expectedIdentifierelse{
throw.invalidIdentifier(identifier:infoPlist[infoIdentifierKey]as?String)
}

guardlet plistName=infoPlist[infoNameKey]as?String, plistName== expectedNameelse{
throw.invalidIdentifier(identifier:infoPlist[infoNameKey]as?String)
Copy link
Preview

CopilotAIAug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The error type should be a name-specific validation error, notinvalidIdentifier. This validation is checking the bundle name, not the identifier, so it should throw a different error type or the existinginvalidIdentifier case should be renamed to be more generic.

Suggested change
throw.invalidIdentifier(identifier:infoPlist[infoNameKey]as?String)
throw.invalidName(name:infoPlist[infoNameKey]as?String)

Copilot uses AI. Check for mistakes.

}

// Downloaded dylib must match the version of the server
guardlet dylibVersion=infoPlist[infoShortVersionKey]as?String,
expectedVersion== dylibVersion
else{
throw.invalidVersion(version:infoPlist[infoShortVersionKey]as?String)
}

// Downloaded dylib must be at least the minimum Coder server version
guardlet dylibVersion=infoPlist[infoShortVersionKey]as?String,
// x.compare(y) is .orderedDescending if x > y
minimumCoderVersion.compare(dylibVersion, options:.numeric)!=.orderedDescending
else{
throw.belowMinimumCoderVersion
}
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp