- Notifications
You must be signed in to change notification settings - Fork5
chore: ensure downloaded slim binary version matches server#211
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Foundation | ||
import Subprocess | ||
publicenumValidationError:Error{ | ||
case fileNotFound | ||
@@ -7,7 +8,9 @@ public enum ValidationError: Error { | ||
case unableToRetrieveSignature | ||
case invalidIdentifier(identifier:String?) | ||
case invalidTeamIdentifier(identifier:String?) | ||
case unableToReadVersion(anyError) | ||
case binaryVersionMismatch(binaryVersion:String, serverVersion:String) | ||
case internalError(OSStatus) | ||
publicvardescription:String{ | ||
switchself{ | ||
@@ -21,10 +24,14 @@ public enum ValidationError: Error { | ||
"Unable to retrieve signing information." | ||
caselet.invalidIdentifier(identifier): | ||
"Invalid identifier:\(identifier??"unknown")." | ||
caselet.binaryVersionMismatch(binaryVersion, serverVersion): | ||
"Binaryversion does not match server. Binary:\(binaryVersion), Server:\(serverVersion)." | ||
caselet.invalidTeamIdentifier(identifier): | ||
"Invalid team identifier:\(identifier??"unknown")." | ||
caselet.unableToReadVersion(error): | ||
"Unable to execute the binary to read version:\(error.localizedDescription)" | ||
caselet.internalError(status): | ||
"Internal error with OSStatus code:\(status)." | ||
} | ||
} | ||
@@ -37,22 +44,32 @@ public class Validator { | ||
publicstaticletminimumCoderVersion="2.24.2" | ||
privatestaticletexpectedIdentifier="com.coder.cli" | ||
// The Coder team identifier | ||
privatestaticletexpectedTeamIdentifier="4399GN35BJ" | ||
// Apple-issued certificate chain | ||
publicstaticletanchorRequirement="anchor apple generic" | ||
privatestaticletsignInfoFlags:SecCSFlags=.init(rawValue: kSecCSSigningInformation) | ||
publicstaticfuncvalidateSignature(binaryPath:URL)throws(ValidationError){ | ||
guardFileManager.default.fileExists(atPath:binaryPath.path)else{ | ||
throw.fileNotFound | ||
} | ||
varstaticCode:SecStaticCode? | ||
letstatus=SecStaticCodeCreateWithPath(binaryPathasCFURL,SecCSFlags(),&staticCode) | ||
guard status== errSecSuccess,let code= staticCodeelse{ | ||
throw.unableToCreateStaticCode | ||
} | ||
varrequirement:SecRequirement? | ||
letreqStatus=SecRequirementCreateWithString(anchorRequirementasCFString,SecCSFlags(),&requirement) | ||
guard reqStatus== errSecSuccess,let requirementelse{ | ||
throw.internalError(OSStatus(reqStatus)) | ||
} | ||
letvalidateStatus=SecStaticCodeCheckValidity(code,SecCSFlags(), requirement) | ||
guard validateStatus== errSecSuccesselse{ | ||
throw.invalidSignature | ||
} | ||
@@ -78,6 +95,32 @@ public class Validator { | ||
} | ||
} | ||
// This function executes the binary to read its version, and so it assumes | ||
// the signature has already been validated. | ||
publicstaticfunc validateVersion(binaryPath:URL, serverVersion:String)asyncthrows(ValidationError){ | ||
guardFileManager.default.fileExists(atPath: binaryPath.path)else{ | ||
throw.fileNotFound | ||
} | ||
letversion:String | ||
do{ | ||
trychmodX(at: binaryPath) | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
letversionOutput=tryawaitSubprocess.data(for:[binaryPath.path,"version","--output=json"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Any way to drop privileges for this subprocess? We are root after all... MemberAuthor
| ||
letparsed:VersionOutput=tryJSONDecoder().decode(VersionOutput.self, from: versionOutput) | ||
version= parsed.version | ||
}catch{ | ||
throw.unableToReadVersion(error) | ||
} | ||
guard version== serverVersionelse{ | ||
throw.binaryVersionMismatch(binaryVersion: version, serverVersion: serverVersion) | ||
} | ||
} | ||
structVersionOutput:Codable{ | ||
letversion:String | ||
} | ||
publicstaticletxpcPeerRequirement= anchorRequirement+ | ||
" and certificate leaf[subject.OU] =\""+ expectedTeamIdentifier+"\"" // Signed by the Coder team | ||
} |
Uh oh!
There was an error while loading.Please reload this page.