diff --git a/apps/cli/src/commands/update.command.spec.ts b/apps/cli/src/commands/update.command.spec.ts new file mode 100644 index 00000000000..8ce557b9754 --- /dev/null +++ b/apps/cli/src/commands/update.command.spec.ts @@ -0,0 +1,203 @@ +import { mock, MockProxy } from "jest-mock-extended"; + +import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; + +import { MessageResponse } from "../models/response/message.response"; +import { CliLicenseType } from "../platform/cli-license-type"; + +import { UpdateCommand } from "./update.command"; + +const CURRENT_VERSION = "2026.10.0"; +const LATEST_VERSION = "2026.11.0"; +const RELEASE_PAGE_URL = `https://github.com/bitwarden/clients/releases/tag/cli-v${LATEST_VERSION}`; + +const ASSET_NAMES = [ + `bw-bit-linux-${LATEST_VERSION}.zip`, + `bw-bit-linux-arm64-${LATEST_VERSION}.zip`, + `bw-bit-macos-${LATEST_VERSION}.zip`, + `bw-bit-macos-arm64-${LATEST_VERSION}.zip`, + `bw-bit-windows-${LATEST_VERSION}.zip`, + `bw-oss-linux-${LATEST_VERSION}.zip`, + `bw-oss-linux-arm64-${LATEST_VERSION}.zip`, + `bw-oss-macos-${LATEST_VERSION}.zip`, + `bw-oss-macos-arm64-${LATEST_VERSION}.zip`, + `bw-oss-windows-${LATEST_VERSION}.zip`, + `bitwarden-cli.${LATEST_VERSION}.nupkg`, + `bw_${LATEST_VERSION}_amd64.snap`, + `bitwarden-cli-${LATEST_VERSION}-npm-build.zip`, +]; + +const downloadUrl = (name: string) => + `https://github.com/bitwarden/clients/releases/download/cli-v${LATEST_VERSION}/${name}`; + +const toAssets = (names: string[]) => + names.map((name) => ({ name, browser_download_url: downloadUrl(name) })); + +const cliRelease = (assetNames: string[] = ASSET_NAMES) => ({ + tag_name: `cli-v${LATEST_VERSION}`, + html_url: RELEASE_PAGE_URL, + body: "Release notes", + assets: toAssets(assetNames), +}); + +describe("UpdateCommand", () => { + let platformUtilsService: MockProxy; + let apiService: MockProxy; + + const respondWith = (status: number, body: unknown) => + apiService.nativeFetch.mockResolvedValue({ status, json: async () => body } as any); + + const createCommand = ( + licenseType: CliLicenseType, + platform: NodeJS.Platform = "linux", + arch: NodeJS.Architecture = "x64", + ) => new UpdateCommand(platformUtilsService, apiService, licenseType, platform, arch); + + const runAndGetMessage = async (command: UpdateCommand) => { + const response = await command.run(); + expect(response.success).toBe(true); + return response.data as MessageResponse; + }; + + beforeEach(() => { + platformUtilsService = mock(); + apiService = mock(); + platformUtilsService.getApplicationVersion.mockResolvedValue(CURRENT_VERSION); + }); + + describe("asset selection", () => { + it.each([ + [CliLicenseType.Bit, "linux", "x64", "bw-bit-linux"], + [CliLicenseType.Bit, "linux", "arm64", "bw-bit-linux-arm64"], + [CliLicenseType.Bit, "darwin", "x64", "bw-bit-macos"], + [CliLicenseType.Bit, "darwin", "arm64", "bw-bit-macos-arm64"], + [CliLicenseType.Bit, "win32", "x64", "bw-bit-windows"], + [CliLicenseType.Oss, "linux", "x64", "bw-oss-linux"], + [CliLicenseType.Oss, "linux", "arm64", "bw-oss-linux-arm64"], + [CliLicenseType.Oss, "darwin", "x64", "bw-oss-macos"], + [CliLicenseType.Oss, "darwin", "arm64", "bw-oss-macos-arm64"], + [CliLicenseType.Oss, "win32", "x64", "bw-oss-windows"], + ] as [CliLicenseType, NodeJS.Platform, NodeJS.Architecture, string][])( + "returns the %s %s/%s asset", + async (licenseType, platform, arch, assetPrefix) => { + respondWith(200, [cliRelease()]); + + const message = await runAndGetMessage(createCommand(licenseType, platform, arch)); + + expect(message.raw).toBe(downloadUrl(`${assetPrefix}-${LATEST_VERSION}.zip`)); + }, + ); + + it.each([ + ["x64 assets listed first", ASSET_NAMES], + ["arm64 assets listed first", [...ASSET_NAMES].reverse()], + ])("does not depend on asset order (%s)", async (_, assetNames) => { + respondWith(200, [cliRelease(assetNames)]); + + const x64 = await runAndGetMessage(createCommand(CliLicenseType.Bit, "darwin", "x64")); + const arm64 = await runAndGetMessage(createCommand(CliLicenseType.Bit, "darwin", "arm64")); + + expect(x64.raw).toBe(downloadUrl(`bw-bit-macos-${LATEST_VERSION}.zip`)); + expect(arm64.raw).toBe(downloadUrl(`bw-bit-macos-arm64-${LATEST_VERSION}.zip`)); + }); + + it("does not fall back to the other license's asset", async () => { + const ossOnly = ASSET_NAMES.filter((name) => !name.startsWith("bw-bit-")); + respondWith(200, [cliRelease(ossOnly)]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit)); + + expect(message.raw).toBe(RELEASE_PAGE_URL); + }); + + it("ignores legacy unprefixed asset names", async () => { + respondWith(200, [cliRelease([`bw-linux-${LATEST_VERSION}.zip`])]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit)); + + expect(message.raw).toBe(RELEASE_PAGE_URL); + }); + + it("ignores assets for a different version", async () => { + respondWith(200, [cliRelease([`bw-bit-linux-${CURRENT_VERSION}.zip`])]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit)); + + expect(message.raw).toBe(RELEASE_PAGE_URL); + }); + + it.each([ + ["win32", "arm64"], + ["linux", "ia32"], + ["freebsd", "x64"], + ] as [NodeJS.Platform, NodeJS.Architecture][])( + "returns the release page for unsupported %s/%s", + async (platform, arch) => { + respondWith(200, [cliRelease()]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit, platform, arch)); + + expect(message.raw).toBe(RELEASE_PAGE_URL); + }, + ); + + it("returns the releases list when the release has no page URL or assets", async () => { + respondWith(200, [{ tag_name: `cli-v${LATEST_VERSION}`, assets: null }]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit)); + + expect(message.raw).toBe("https://github.com/bitwarden/clients/releases"); + }); + }); + + describe("response", () => { + it("includes the release notes and download URL in the message", async () => { + respondWith(200, [cliRelease()]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit)); + + expect(message.title).toBe(`A new version is available: cli-v${LATEST_VERSION}`); + expect(message.message).toContain("Release notes"); + expect(message.message).toContain( + "You can download this update at " + downloadUrl(`bw-bit-linux-${LATEST_VERSION}.zip`), + ); + }); + + it("uses the first CLI release in the list", async () => { + respondWith(200, [{ tag_name: "desktop-v2026.11.1", assets: [] }, cliRelease()]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Oss)); + + expect(message.raw).toBe(downloadUrl(`bw-oss-linux-${LATEST_VERSION}.zip`)); + }); + + it("reports no update when already on the latest version", async () => { + platformUtilsService.getApplicationVersion.mockResolvedValue(LATEST_VERSION); + respondWith(200, [cliRelease()]); + + const message = await runAndGetMessage(createCommand(CliLicenseType.Bit)); + + expect(message.title).toBe("No update available."); + expect(message.raw).toBeUndefined(); + }); + + it("returns an error when the update API fails", async () => { + respondWith(500, null); + + const response = await createCommand(CliLicenseType.Bit).run(); + + expect(response.success).toBe(false); + expect(response.message).toBe("Error contacting update API: 500"); + }); + + it("returns an error when no CLI release is found", async () => { + respondWith(200, [{ tag_name: "desktop-v2026.11.1", assets: [] }]); + + const response = await createCommand(CliLicenseType.Bit).run(); + + expect(response.success).toBe(false); + expect(response.message).toBe("Could not find latest CLI version."); + }); + }); +}); diff --git a/apps/cli/src/commands/update.command.ts b/apps/cli/src/commands/update.command.ts index 185b272d480..34dd310f7d6 100644 --- a/apps/cli/src/commands/update.command.ts +++ b/apps/cli/src/commands/update.command.ts @@ -1,14 +1,39 @@ -// FIXME: Update this file to be type safe and remove this and next line -// @ts-strict-ignore import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { Response } from "../models/response"; import { MessageResponse } from "../models/response/message.response"; +import { CliLicenseType } from "../platform/cli-license-type"; const CLIENTS_RELEASE_LIST_ENDPOINT = "https://api.github.com/repos/bitwarden/clients/releases"; const DEFAULT_DOWNLOAD_URL = "https://github.com/bitwarden/clients/releases"; const UPDATE_COMMAND = "npm install -g @bitwarden/cli"; +const CLI_TAG_PREFIX = "cli-v"; + +/** The OS segment of the release asset name, keyed by `process.platform`. */ +const ASSET_OS_NAMES: Partial> = { + win32: "windows", + darwin: "macos", + linux: "linux", +}; + +/** The architecture suffix of the release asset name, keyed by `process.arch`. */ +const ASSET_ARCH_SUFFIXES: Partial> = { + x64: "", + arm64: "-arm64", +}; + +interface GitHubReleaseAsset { + name: string; + browser_download_url: string; +} + +interface GitHubRelease { + tag_name: string; + html_url?: string; + body?: string | null; + assets?: GitHubReleaseAsset[] | null; +} export class UpdateCommand { inPkg = false; @@ -16,6 +41,9 @@ export class UpdateCommand { constructor( private platformUtilsService: PlatformUtilsService, protected apiService: ApiService, + private licenseType: CliLicenseType, + private platform: NodeJS.Platform = process.platform, + private arch: NodeJS.Architecture = process.arch, ) { this.inPkg = !!(process as any).pkg; } @@ -26,36 +54,35 @@ export class UpdateCommand { return Response.error("Error contacting update API: " + response.status); } - const responseJson = await response.json(); - const cliRelease = responseJson.find((r: any) => r.tag_name.includes("cli")); - if (cliRelease === undefined || cliRelease === null) { + const releases: GitHubRelease[] = await response.json(); + const cliRelease = releases.find((r) => r.tag_name.includes("cli")); + if (cliRelease == null) { return Response.error("Could not find latest CLI version."); } const currentVersion = await this.platformUtilsService.getApplicationVersion(); - if (cliRelease.tag_name === "cli-v" + currentVersion) { - const response = new MessageResponse(null, null); - response.title = "No update available."; - response.noColor = true; - return Response.success(response); + if (cliRelease.tag_name === CLI_TAG_PREFIX + currentVersion) { + const res = new MessageResponse("No update available.", null); + res.noColor = true; + return Response.success(res); } - const res = this.getFoundUpdateResponse(cliRelease); - return Response.success(res); + return Response.success(this.getFoundUpdateResponse(cliRelease)); } - private getFoundUpdateResponse(release: any) { - const downloadUrl = this.getDownloadUrl(release.assets); + private getFoundUpdateResponse(release: GitHubRelease) { + const downloadUrl = this.getDownloadUrl(release); - const response = new MessageResponse(null, null); - response.title = "A new version is available: " + release.tag_name; + const response = new MessageResponse( + "A new version is available: " + release.tag_name, + this.getMessage(release, downloadUrl), + ); response.raw = downloadUrl; - response.message = this.getMessage(release, downloadUrl); return response; } - private getMessage(release: any, downloadUrl: string) { + private getMessage(release: GitHubRelease, downloadUrl: string) { let message = ""; if (release.body != null && release.body !== "") { @@ -79,35 +106,34 @@ export class UpdateCommand { return message; } - private getDownloadUrl(assets: any) { - if (assets == null) { - return DEFAULT_DOWNLOAD_URL; + /** + * Returns the download URL of the release asset built for this CLI's license, OS and CPU + * architecture. Falls back to the release page when no matching asset exists. + */ + private getDownloadUrl(release: GitHubRelease): string { + const fallbackUrl = release.html_url ?? DEFAULT_DOWNLOAD_URL; + + const assetName = this.getAssetName(release.tag_name); + if (assetName == null) { + return fallbackUrl; } - let downloadUrl: string = DEFAULT_DOWNLOAD_URL; + const asset = release.assets?.find((a) => a.name === assetName); + return asset?.browser_download_url ?? fallbackUrl; + } - for (const a of assets) { - const download: string = a.browser_download_url; - if (download == null) { - continue; - } - - if (download.indexOf(".zip") === -1) { - continue; - } - - if (process.platform === "win32" && download.indexOf("bw-windows") > -1) { - downloadUrl = download; - break; - } else if (process.platform === "darwin" && download.indexOf("bw-macos") > -1) { - downloadUrl = download; - break; - } else if (process.platform === "linux" && download.indexOf("bw-linux") > -1) { - downloadUrl = download; - break; - } + /** + * Builds the release asset name for this CLI, e.g. `bw-bit-macos-arm64-2026.11.0.zip`. + * Returns `null` when no asset is published for this OS or CPU architecture. + */ + private getAssetName(tagName: string): string | null { + const os = ASSET_OS_NAMES[this.platform]; + const archSuffix = ASSET_ARCH_SUFFIXES[this.arch]; + if (os == null || archSuffix == null || !tagName.startsWith(CLI_TAG_PREFIX)) { + return null; } - return downloadUrl; + const version = tagName.slice(CLI_TAG_PREFIX.length); + return `bw-${this.licenseType}-${os}${archSuffix}-${version}.zip`; } } diff --git a/apps/cli/src/platform/cli-license-type.ts b/apps/cli/src/platform/cli-license-type.ts new file mode 100644 index 00000000000..b4722b7dbb0 --- /dev/null +++ b/apps/cli/src/platform/cli-license-type.ts @@ -0,0 +1,11 @@ +/** + * The license the running CLI build was produced under. The values match the `bw-{license}-*` + * prefix of the CLI release assets. + */ +export const CliLicenseType = Object.freeze({ + /** Built from `apps/cli` (GPL). */ + Oss: "oss", + /** Built from `bitwarden_license/bit-cli` (Bitwarden License). */ + Bit: "bit", +} as const); +export type CliLicenseType = (typeof CliLicenseType)[keyof typeof CliLicenseType]; diff --git a/apps/cli/src/program.ts b/apps/cli/src/program.ts index 60c0701fb5b..1db72c15726 100644 --- a/apps/cli/src/program.ts +++ b/apps/cli/src/program.ts @@ -458,6 +458,7 @@ export class Program extends BaseProgram { const command = new UpdateCommand( this.serviceContainer.platformUtilsService, this.serviceContainer.apiService, + this.serviceContainer.licenseType, ); const response = await command.run(); this.processResponse(response); diff --git a/apps/cli/src/service-container/service-container.spec.ts b/apps/cli/src/service-container/service-container.spec.ts index 26258c367c4..f46e8b111eb 100644 --- a/apps/cli/src/service-container/service-container.spec.ts +++ b/apps/cli/src/service-container/service-container.spec.ts @@ -1,7 +1,13 @@ +import { CliLicenseType } from "../platform/cli-license-type"; + import { ServiceContainer } from "./service-container"; describe("ServiceContainer", () => { it("instantiates", async () => { expect(() => new ServiceContainer()).not.toThrow(); }); + + it("reports the oss license type", () => { + expect(new ServiceContainer().licenseType).toBe(CliLicenseType.Oss); + }); }); diff --git a/apps/cli/src/service-container/service-container.ts b/apps/cli/src/service-container/service-container.ts index 6ffc5b2ab5c..5f7d03251a7 100644 --- a/apps/cli/src/service-container/service-container.ts +++ b/apps/cli/src/service-container/service-container.ts @@ -257,6 +257,7 @@ import { CliBiometricsService } from "../key-management/cli-biometrics-service"; import { CliProcessReloadService } from "../key-management/cli-process-reload.service"; import { CliUserKeyRotationService } from "../key-management/cli-user-key-rotation-service"; import { CliSessionTimeoutTypeService } from "../key-management/session-timeout/services/cli-session-timeout-type.service"; +import { CliLicenseType } from "../platform/cli-license-type"; import { devFlagEnabled, devFlagValue, flagEnabled } from "../platform/flags"; import { CliIpcService } from "../platform/services/cli-ipc.service"; import { CliPlatformUtilsService } from "../platform/services/cli-platform-utils.service"; @@ -279,6 +280,9 @@ const packageJson = require("../../package.json"); export class ServiceContainer { private inited = false; + /** The license this build was produced under. The Bitwarden-licensed container overrides it. */ + readonly licenseType: CliLicenseType = CliLicenseType.Oss; + messagingService: MessageSender; storageService: LowdbStorageService; secureStorageService: NodeEnvSecureStorageService; diff --git a/bitwarden_license/bit-cli/src/service-container.spec.ts b/bitwarden_license/bit-cli/src/service-container.spec.ts index 26258c367c4..ed20798c3d2 100644 --- a/bitwarden_license/bit-cli/src/service-container.spec.ts +++ b/bitwarden_license/bit-cli/src/service-container.spec.ts @@ -1,7 +1,13 @@ +import { CliLicenseType } from "@bitwarden/cli/platform/cli-license-type"; + import { ServiceContainer } from "./service-container"; describe("ServiceContainer", () => { it("instantiates", async () => { expect(() => new ServiceContainer()).not.toThrow(); }); + + it("reports the bit license type", () => { + expect(new ServiceContainer().licenseType).toBe(CliLicenseType.Bit); + }); }); diff --git a/bitwarden_license/bit-cli/src/service-container.ts b/bitwarden_license/bit-cli/src/service-container.ts index e139eaa24bf..749657e60a1 100644 --- a/bitwarden_license/bit-cli/src/service-container.ts +++ b/bitwarden_license/bit-cli/src/service-container.ts @@ -4,6 +4,7 @@ import { OrganizationAuthRequestService, OrganizationAuthRequestApiService, } from "@bitwarden/bit-common/admin-console/auth-requests"; +import { CliLicenseType } from "@bitwarden/cli/platform/cli-license-type"; import { ServiceContainer as OssServiceContainer } from "@bitwarden/cli/service-container/service-container"; /** @@ -11,6 +12,8 @@ import { ServiceContainer as OssServiceContainer } from "@bitwarden/cli/service- * Any Bitwarden-licensed services should be registered here. */ export class ServiceContainer extends OssServiceContainer { + override readonly licenseType: CliLicenseType = CliLicenseType.Bit; + organizationAuthRequestApiService: OrganizationAuthRequestApiService; organizationAuthRequestService: OrganizationAuthRequestService;