Skip to content

Commit 96663e6

Browse files
committed
fix: remove the cli if it is not properly signed
Otherwise, at the next Toolbox restart the signature will no longer be verified, and we run into the risk of running unsigned binaries.
1 parent d5ae289 commit 96663e6

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/main/kotlin/com/coder/toolbox/cli/CoderCLIManager.kt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,36 @@ class CoderCLIManager(
213213
gpgVerifier.verifySignature(cliResult.dst, signatureResult.dst).let { result ->
214214
when {
215215
result.isValid() -> return true
216-
result.isInvalid() -> {
217-
val reason = (result as Invalid).reason
218-
throw UnsignedBinaryExecutionDeniedException(
219-
"Signature of ${cliResult.dst} is invalid." + reason?.let { " Reason: $it" }.orEmpty()
220-
)
216+
else -> {
217+
// remove the cli, otherwise next time the user tries to login the cached cli is picked up,
218+
// and we don't verify cached cli signatures
219+
runCatching { Files.delete(cliResult.dst) }
220+
.onFailure { ex ->
221+
context.logger.warn(ex, "Failed to delete CLI file: ${cliResult.dst}")
222+
}
223+
224+
val exception = when {
225+
result.isInvalid() -> {
226+
val reason = (result as Invalid).reason
227+
UnsignedBinaryExecutionDeniedException(
228+
"Signature of ${cliResult.dst} is invalid." + reason?.let { " Reason: $it" }.orEmpty()
229+
)
230+
}
231+
232+
result.signatureIsNotFound() -> {
233+
UnsignedBinaryExecutionDeniedException(
234+
"Can't verify signature of ${cliResult.dst} because ${signatureResult.dst} does not exist"
235+
)
236+
}
237+
238+
else -> {
239+
UnsignedBinaryExecutionDeniedException((result as Failed).error.message)
240+
}
241+
}
242+
throw exception
221243
}
222-
223-
result.signatureIsNotFound() -> throw UnsignedBinaryExecutionDeniedException("Can't verify signature of ${cliResult.dst} because ${signatureResult.dst} does not exist")
224-
else -> throw UnsignedBinaryExecutionDeniedException((result as Failed).error.message)
225244
}
245+
226246
}
227247
}
228248

0 commit comments

Comments
 (0)