Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ It follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Security

- Verify the Coder CLI with Coder's new release signing key. The previous key is no longer trusted. The plugin ships
with new signatures for Coder 2.29.0 to 2.39.4.
- Restrict HTTP redirects and deployment credentials to their original destination. Disable redirects in CLI v2.38.0 and newer.

- Ask for confirmation before a workspace link opens an unknown Coder deployment. Links select the deployment by
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,23 @@ running potentially malicious or tampered binaries.
1. **Binary Download**: When connecting to a Coder deployment, the plugin downloads the appropriate Coder CLI binary for
the user's operating system and architecture from the deployment's `/bin/` endpoint.

2. **Signature Download**: After downloading the binary, the plugin attempts to download the corresponding `.asc`
2. **Signature Download**: After downloading the binary, the plugin attempts to download the corresponding `.v2.asc`
signature file from the same location. The signature file is named according to the binary (e.g.,
`coder-linux-amd64.asc` for `coder-linux-amd64`).
`coder-linux-amd64.v2.asc` for `coder-linux-amd64`).

3. **Fallback Signature Sources**: If the signature is not available from the deployment, the plugin can optionally fall
back to downloading signatures from `releases.coder.com`. This is controlled by the `fallbackOnCoderForSignatures`
3. **Bundled Signatures**: Deployments released before Coder's current release signing key have no `.v2.asc` signature.
If the signature is not available from the deployment, the plugin uses the signature that ships with the plugin for
the release of the deployment's version. Coder made these signatures with the current key for the releases from
2.29.0 to 2.39.4. They are the signatures that `releases.coder.com` serves for those releases.

4. **Fallback Signature Sources**: If the plugin has no signature for the release, it can optionally fall back to
downloading signatures from `releases.coder.com`. This is controlled by the `fallbackOnCoderForSignatures`
setting.

4. **GPG Verification**: The plugin uses the BouncyCastle library to verify the detached GPG signature against the
5. **GPG Verification**: The plugin uses the BouncyCastle library to verify the detached GPG signature against the
downloaded binary using Coder's trusted public key.

5. **User Interaction**: If signature verification fails or signatures are unavailable, the plugin presents security
6. **User Interaction**: If signature verification fails or signatures are unavailable, the plugin presents security
warnings to users, allowing them to accept the risk and continue or abort the operation.

### Verification Process
Expand All @@ -266,6 +271,7 @@ Users can control signature verification behavior through plugin settings:
### Security Considerations

- The plugin embeds Coder's trusted public key in the plugin resources
- The plugin embeds the signatures that Coder made with that key for the releases published before it
- Verification uses detached signatures, which are more secure than attached signatures
- Users are warned about security risks when verification fails
- The system gracefully handles cases where signatures are unavailable
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/com/coder/toolbox/cli/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ class CoderCLIManager(
downloader.downloadSignature(showTextProgress)
}

if (signatureResult.isNotDownloaded()) {
signatureResult = withContext(Dispatchers.IO) {
downloader.bundledSignature(buildVersion)
}
}

if (signatureResult.isNotDownloaded()) {
if (context.settingsStore.fallbackOnCoderForSignatures == ALLOW) {
context.logger.info("Trying to download signature file from releases.coder.com")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.coder.toolbox.cli.downloader
import com.coder.toolbox.CoderToolboxContext
import com.coder.toolbox.cli.ex.ResponseException
import com.coder.toolbox.util.OS
import com.coder.toolbox.util.SemVer
import com.coder.toolbox.util.hasSameOrigin
import com.coder.toolbox.util.getHeaders
import com.coder.toolbox.util.getOS
import com.coder.toolbox.util.sha1
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.ResponseBody
Expand All @@ -25,6 +26,7 @@ import java.nio.file.StandardOpenOption
import java.util.zip.GZIPInputStream
import kotlin.io.path.name
import kotlin.io.path.notExists
import kotlin.io.path.writeText

private val SUPPORTED_BIN_MIME_TYPES = listOf(
"application/octet-stream",
Expand All @@ -40,6 +42,9 @@ private val SUPPORTED_BIN_MIME_TYPES = listOf(
"application/vnd.microsoft.portable-executable"
)

private const val BUNDLED_SIGNATURES = "/META-INF/release-signatures.json"
private val RELEASE_BUILD = Regex("""v?(\d+\.\d+\.\d+)(\+.*)?""")

/**
* Handles the download steps of Coder CLI
*/
Expand Down Expand Up @@ -245,10 +250,41 @@ class CoderDownloadService(
buildVersion: String,
showTextProgress: (String) -> Unit
): DownloadResult {
val semVer = SemVer.parse(buildVersion)
val release = releaseVersion(buildVersion)
if (release == null) {
context.logger.info("No signature is published on releases.coder.com for $buildVersion")
return DownloadResult.NotFound
}
return downloadSignature(
URI.create("https://releases.coder.com/coder-cli/${semVer.major}.${semVer.minor}.${semVer.patch}/").toURL(),
URI.create("https://releases.coder.com/coder-cli/$release/").toURL(),
showTextProgress
)
}

fun bundledSignature(buildVersion: String): DownloadResult {
val source = javaClass.getResource(BUNDLED_SIGNATURES) ?: return DownloadResult.NotFound
val signature = releaseVersion(buildVersion)
?.let { bundledSignatures(source)[it] }
?.get(context.settingsStore.defaultCliBinaryNameByOsAndArch)
if (signature == null) {
context.logger.info("No signature is bundled with the plugin for $buildVersion")
return DownloadResult.NotFound
}
val localSignaturePath = cliFinalDst.parent.resolve(context.settingsStore.defaultSignatureNameByOsAndArch)
context.logger.info("Using the signature bundled with the plugin for the $buildVersion release")
localSignaturePath.writeText(signature)
return DownloadResult.Downloaded(source, localSignaturePath)
}
}

private fun releaseVersion(buildVersion: String): String? =
RELEASE_BUILD.matchEntire(buildVersion)?.groupValues?.get(1)

internal fun bundledSignatures(source: URL): Map<String, Map<String, String>> {
val type = Types.newParameterizedType(
Map::class.java,
String::class.java,
Types.newParameterizedType(Map::class.java, String::class.java, String::class.java),
)
return Moshi.Builder().build().adapter<Map<String, Map<String, String>>>(type).fromJson(source.readText()).orEmpty()
}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/coder/toolbox/store/CoderSettingsStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,11 @@ class CoderSettingsStore(
}

/**
* Return the name of the signature file (.asc) for the provided OS and architecture.
* Return the name of the signature file (.v2.asc) for the provided OS and architecture.
*/
private fun getCoderSignatureForOS(os: OS?, arch: Arch?): String {
logger.debug("Resolving signature for $os $arch")
return "${getCoderCLIForOS(os, arch)}.asc"
return "${getCoderCLIForOS(os, arch)}.v2.asc"
}

/**
Expand Down
Loading
Loading