signal-desktop/ts/updater/common.ts

710 lines
19 KiB
TypeScript
Raw Normal View History

2020-10-30 15:34:04 -05:00
// Copyright 2019-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-09-16 12:31:05 -07:00
/* eslint-disable no-console */
2022-02-24 13:01:41 -08:00
import { createWriteStream, statSync } from 'fs';
import { pathExists } from 'fs-extra';
import { readdir, writeFile } from 'fs/promises';
2021-12-03 23:49:15 +01:00
import { promisify } from 'util';
import { execFile } from 'child_process';
2022-02-24 13:01:41 -08:00
import { join, normalize, extname } from 'path';
import { tmpdir } from 'os';
import { throttle } from 'lodash';
import type { ParserConfiguration } from 'dashdash';
import { createParser } from 'dashdash';
import { FAILSAFE_SCHEMA, safeLoad } from 'js-yaml';
import { gt } from 'semver';
2021-08-27 13:21:42 -07:00
import config from 'config';
import got from 'got';
import { v4 as getGuid } from 'uuid';
import pify from 'pify';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';
import type { BrowserWindow } from 'electron';
import { app, ipcMain } from 'electron';
import * as durations from '../util/durations';
2022-02-24 13:01:41 -08:00
import { getTempPath, getUpdateCachePath } from '../util/attachments';
import { DialogType } from '../types/Dialogs';
import * as Errors from '../types/errors';
2021-08-06 14:21:01 -07:00
import { isAlpha, isBeta } from '../util/version';
2022-02-24 13:01:41 -08:00
import { strictAssert } from '../util/assert';
import * as packageJson from '../../package.json';
import {
hexToBinary,
verifySignature,
getSignatureFileName,
} from './signature';
import { isPathInside } from '../util/isPathInside';
import type { SettingsChannel } from '../main/settingsChannel';
import type { LoggerType } from '../types/Logging';
2022-02-24 13:01:41 -08:00
import { getGotOptions } from './got';
import { checkIntegrity } from './util';
import type { PrepareDownloadResultType as DifferentialDownloadDataType } from './differential';
import {
prepareDownload as prepareDifferentialDownload,
download as downloadDifferentialData,
getBlockMapFileName,
} from './differential';
const mkdirpPromise = pify(mkdirp);
const rimrafPromise = pify(rimraf);
const INTERVAL = 30 * durations.MINUTE;
type JSONUpdateSchema = {
version: string;
files: Array<{
url: string;
sha512: string;
size: string;
blockMapSize?: string;
}>;
path: string;
sha512: string;
releaseDate: string;
};
export type UpdateInformationType = {
fileName: string;
size: number;
version: string;
2022-02-24 13:01:41 -08:00
sha512: string;
differentialData: DifferentialDownloadDataType | undefined;
};
export abstract class Updater {
protected fileName: string | undefined;
protected version: string | undefined;
constructor(
protected readonly logger: LoggerType,
private readonly settingsChannel: SettingsChannel,
protected readonly getMainWindow: () => BrowserWindow | undefined
) {}
//
// Public APIs
//
public async force(): Promise<void> {
return this.checkForUpdatesMaybeInstall(true);
}
public async start(): Promise<void> {
this.logger.info('updater/start: starting checks...');
setInterval(async () => {
try {
await this.checkForUpdatesMaybeInstall();
} catch (error) {
this.logger.error(`updater/start: ${Errors.toLogFormat(error)}`);
}
}, INTERVAL);
2019-08-02 14:11:10 -07:00
await this.deletePreviousInstallers();
await this.checkForUpdatesMaybeInstall();
2019-08-02 14:11:10 -07:00
}
//
// Abstract methods
//
protected abstract deletePreviousInstallers(): Promise<void>;
protected abstract installUpdate(updateFilePath: string): Promise<void>;
//
// Protected methods
//
protected setUpdateListener(performUpdateCallback: () => void): void {
ipcMain.removeAllListeners('start-update');
ipcMain.once('start-update', performUpdateCallback);
}
//
// Private methods
//
private async downloadAndInstall(
2022-02-24 13:01:41 -08:00
updateInfo: UpdateInformationType,
updateOnProgress?: boolean
): Promise<void> {
const { logger } = this;
2022-02-24 13:01:41 -08:00
const { fileName: newFileName, version: newVersion } = updateInfo;
try {
const oldVersion = this.version;
this.version = newVersion;
2022-02-24 13:01:41 -08:00
let updateFilePath: string;
try {
2022-02-24 13:01:41 -08:00
updateFilePath = await this.downloadUpdate(
updateInfo,
updateOnProgress
);
} catch (error) {
// Restore state in case of download error
this.version = oldVersion;
throw error;
}
2019-08-02 14:11:10 -07:00
const publicKey = hexToBinary(config.get('updatesPublicKey'));
const verified = await verifySignature(
2022-02-24 13:01:41 -08:00
updateFilePath,
this.version,
publicKey
);
if (!verified) {
// Note: We don't delete the cache here, because we don't want to continually
// re-download the broken release. We will download it only once per launch.
throw new Error(
'Downloaded update did not pass signature verification ' +
2022-02-24 13:01:41 -08:00
`(version: '${this.version}'; fileName: '${newFileName}')`
);
}
2022-02-24 13:01:41 -08:00
await this.installUpdate(updateFilePath);
const mainWindow = this.getMainWindow();
if (mainWindow) {
mainWindow.webContents.send('show-update-dialog', DialogType.Update, {
version: this.version,
});
} else {
logger.warn(
'downloadAndInstall: no mainWindow, cannot show update dialog'
);
}
} catch (error) {
logger.error(`downloadAndInstall: ${Errors.toLogFormat(error)}`);
}
}
private async checkForUpdatesMaybeInstall(force = false): Promise<void> {
const { logger } = this;
logger.info('checkForUpdatesMaybeInstall: checking for update...');
const result = await this.checkForUpdates(force);
if (!result) {
return;
}
2022-02-24 13:01:41 -08:00
const { version: newVersion } = result;
2022-02-24 13:01:41 -08:00
if (force || !this.version || gt(newVersion, this.version)) {
const autoDownloadUpdates = await this.getAutoDownloadUpdateSetting();
if (!autoDownloadUpdates) {
this.setUpdateListener(async () => {
logger.info(
'checkForUpdatesMaybeInstall: have not downloaded update, going to download'
);
2022-02-24 13:01:41 -08:00
await this.downloadAndInstall(result, true);
});
const mainWindow = this.getMainWindow();
if (mainWindow) {
mainWindow.webContents.send(
'show-update-dialog',
DialogType.DownloadReady,
{
downloadSize: result.size,
version: result.version,
}
);
} else {
logger.warn(
'checkForUpdatesMaybeInstall: no mainWindow, cannot show update dialog'
);
}
return;
}
2022-02-24 13:01:41 -08:00
await this.downloadAndInstall(result);
}
}
private async checkForUpdates(
forceUpdate = false
2022-02-24 13:01:41 -08:00
): Promise<UpdateInformationType | undefined> {
const yaml = await getUpdateYaml();
const parsedYaml = parseYaml(yaml);
const version = getVersion(parsedYaml);
if (!version) {
this.logger.warn(
'checkForUpdates: no version extracted from downloaded yaml'
);
2022-02-24 13:01:41 -08:00
return;
}
2022-02-24 13:01:41 -08:00
if (!forceUpdate && !isVersionNewer(version)) {
this.logger.info(
2022-02-24 13:01:41 -08:00
`checkForUpdates: ${version} is not newer than ${packageJson.version}; ` +
'no new update available'
2021-12-03 23:49:15 +01:00
);
2022-02-24 13:01:41 -08:00
return;
}
this.logger.info(
2022-02-24 13:01:41 -08:00
`checkForUpdates: found newer version ${version} ` +
`forceUpdate=${forceUpdate}`
);
const fileName = getUpdateFileName(
parsedYaml,
process.platform,
await this.getArch()
);
const sha512 = getSHA512(parsedYaml, fileName);
strictAssert(sha512 !== undefined, 'Missing required hash');
const latestInstaller = await this.getLatestCachedInstaller(
extname(fileName)
);
2022-02-24 13:01:41 -08:00
let differentialData: DifferentialDownloadDataType | undefined;
if (latestInstaller) {
this.logger.info(
`checkForUpdates: Found local installer ${latestInstaller}`
);
try {
differentialData = await prepareDifferentialDownload({
oldFile: latestInstaller,
newUrl: `${getUpdatesBase()}/${fileName}`,
sha512,
});
this.logger.info(
'checkForUpdates: differential download size',
differentialData.downloadSize
);
} catch (error) {
this.logger.error(
'checkForUpdates: Failed to prepare differential update',
Errors.toLogFormat(error)
);
}
}
return {
fileName,
size: getSize(parsedYaml, fileName),
version,
sha512,
differentialData,
};
}
private async getLatestCachedInstaller(
extension: string
): Promise<string | undefined> {
const cacheDir = await createUpdateCacheDirIfNeeded();
const oldFiles = (await readdir(cacheDir)).map(fileName => {
return join(cacheDir, fileName);
});
return oldFiles.find(fileName => extname(fileName) === extension);
}
private async downloadUpdate(
2022-02-24 13:01:41 -08:00
{ fileName, sha512, differentialData }: UpdateInformationType,
updateOnProgress?: boolean
): Promise<string> {
const baseUrl = getUpdatesBase();
const updateFileUrl = `${baseUrl}/${fileName}`;
const signatureFileName = getSignatureFileName(fileName);
2022-02-24 13:01:41 -08:00
const blockMapFileName = getBlockMapFileName(fileName);
const signatureUrl = `${baseUrl}/${signatureFileName}`;
2022-02-24 13:01:41 -08:00
const blockMapUrl = `${baseUrl}/${blockMapFileName}`;
const cacheDir = await createUpdateCacheDirIfNeeded();
const targetUpdatePath = join(cacheDir, fileName);
const targetSignaturePath = join(cacheDir, signatureFileName);
const targetBlockMapPath = join(cacheDir, blockMapFileName);
const targetPaths = [
targetUpdatePath,
targetSignaturePath,
targetBlockMapPath,
];
// List of files to be deleted on success
const oldFiles = (await readdir(cacheDir))
.map(oldFileName => {
return join(cacheDir, oldFileName);
})
.filter(path => !targetPaths.includes(path));
try {
2022-02-24 13:01:41 -08:00
validatePath(cacheDir, targetUpdatePath);
validatePath(cacheDir, targetSignaturePath);
validatePath(cacheDir, targetBlockMapPath);
this.logger.info(`downloadUpdate: Downloading signature ${signatureUrl}`);
2022-02-24 13:01:41 -08:00
const signature = await got(signatureUrl, getGotOptions()).buffer();
await writeFile(targetSignaturePath, signature);
2022-02-24 13:01:41 -08:00
try {
this.logger.info(`downloadUpdate: Downloading blockmap ${blockMapUrl}`);
const blockMap = await got(blockMapUrl, getGotOptions()).buffer();
await writeFile(targetBlockMapPath, blockMap);
} catch (error) {
this.logger.warn(
'downloadUpdate: Failed to download blockmap, continuing',
Errors.toLogFormat(error)
);
}
2022-02-24 13:01:41 -08:00
let gotUpdate = false;
if (!gotUpdate && (await pathExists(targetUpdatePath))) {
const checkResult = await checkIntegrity(targetUpdatePath, sha512);
if (checkResult.ok) {
this.logger.info(
`downloadUpdate: Not downloading update ${updateFileUrl}, ` +
'local file has the same hash'
);
gotUpdate = true;
} else {
this.logger.error(
'downloadUpdate: integrity check failure',
checkResult.error
);
}
}
if (!gotUpdate && differentialData) {
this.logger.info(
`downloadUpdate: Downloading differential update ${updateFileUrl}`
);
2022-02-24 13:01:41 -08:00
try {
const mainWindow = this.getMainWindow();
const throttledSend = throttle((downloadedSize: number) => {
mainWindow?.webContents.send(
'show-update-dialog',
DialogType.Downloading,
{ downloadedSize }
);
}, 500);
2022-02-24 13:01:41 -08:00
await downloadDifferentialData(
targetUpdatePath,
differentialData,
updateOnProgress ? throttledSend : undefined
);
2022-02-24 13:01:41 -08:00
gotUpdate = true;
} catch (error) {
this.logger.error(
'downloadUpdate: Failed to apply differential update',
Errors.toLogFormat(error)
);
}
}
2022-02-24 13:01:41 -08:00
if (!gotUpdate) {
this.logger.info(
`downloadUpdate: Downloading full update ${updateFileUrl}`
);
await this.downloadAndReport(
updateFileUrl,
targetUpdatePath,
updateOnProgress
);
gotUpdate = true;
}
strictAssert(gotUpdate, 'We should get the update one way or another');
2022-02-24 13:01:41 -08:00
// Now that we successfully downloaded an update - remove old files
await Promise.all(oldFiles.map(path => rimrafPromise(path)));
return targetUpdatePath;
} catch (error) {
2022-02-24 13:01:41 -08:00
try {
await Promise.all([targetPaths.map(path => rimrafPromise(path))]);
} catch (_) {
// Ignore error, this is a cleanup
}
throw error;
}
}
2022-02-24 13:01:41 -08:00
private async downloadAndReport(
updateFileUrl: string,
targetUpdatePath: string,
updateOnProgress = false
): Promise<void> {
const downloadStream = got.stream(updateFileUrl, getGotOptions());
const writeStream = createWriteStream(targetUpdatePath);
await new Promise<void>((resolve, reject) => {
const mainWindow = this.getMainWindow();
if (updateOnProgress && mainWindow) {
let downloadedSize = 0;
const throttledSend = throttle(() => {
mainWindow.webContents.send(
'show-update-dialog',
DialogType.Downloading,
{ downloadedSize }
);
}, 500);
downloadStream.on('data', data => {
downloadedSize += data.length;
throttledSend();
});
}
downloadStream.on('error', error => {
reject(error);
});
downloadStream.on('end', () => {
resolve();
});
writeStream.on('error', error => {
reject(error);
});
downloadStream.pipe(writeStream);
});
}
private async getAutoDownloadUpdateSetting(): Promise<boolean> {
try {
return await this.settingsChannel.getSettingFromMainWindow(
'autoDownloadUpdate'
);
} catch (error) {
this.logger.warn(
'getAutoDownloadUpdateSetting: Failed to fetch, returning false',
Errors.toLogFormat(error)
);
return false;
}
}
2021-12-03 23:49:15 +01:00
private async getArch(): Promise<typeof process.arch> {
if (process.platform !== 'darwin' || process.arch === 'arm64') {
return process.arch;
}
try {
// We might be running under Rosetta
2021-12-06 19:10:15 +01:00
const flag = 'sysctl.proc_translated';
const { stdout } = await promisify(execFile)('sysctl', ['-i', flag]);
if (stdout.includes(`${flag}: 1`)) {
2021-12-03 23:49:15 +01:00
this.logger.info('updater: running under Rosetta');
return 'arm64';
}
} catch (error) {
this.logger.warn(
2021-12-06 19:10:15 +01:00
`updater: Rosetta detection failed with ${Errors.toLogFormat(error)}`
2021-12-03 23:49:15 +01:00
);
}
this.logger.info('updater: not running under Rosetta');
return process.arch;
}
}
export function validatePath(basePath: string, targetPath: string): void {
const normalized = normalize(targetPath);
if (!isPathInside(normalized, basePath)) {
throw new Error(
`validatePath: Path ${normalized} is not under base path ${basePath}`
);
}
}
// Helper functions
export function getUpdateCheckUrl(): string {
return `${getUpdatesBase()}/${getUpdatesFileName()}`;
}
export function getUpdatesBase(): string {
2021-08-27 13:21:42 -07:00
return config.get('updatesUrl');
}
export function getUpdatesFileName(): string {
2021-08-06 14:21:01 -07:00
const prefix = getChannel();
2021-12-03 23:49:15 +01:00
if (process.platform === 'darwin') {
return `${prefix}-mac.yml`;
}
2020-09-16 12:31:05 -07:00
return `${prefix}.yml`;
}
2021-08-06 14:21:01 -07:00
function getChannel(): string {
const { version } = packageJson;
if (isAlpha(version)) {
return 'alpha';
}
if (isBeta(version)) {
return 'beta';
}
return 'latest';
}
function isVersionNewer(newVersion: string): boolean {
const { version } = packageJson;
return gt(newVersion, version);
}
export function getVersion(info: JSONUpdateSchema): string | null {
2020-09-16 12:31:05 -07:00
return info && info.version;
}
2020-09-16 12:31:05 -07:00
const validFile = /^[A-Za-z0-9.-]+$/;
export function isUpdateFileNameValid(name: string): boolean {
2019-08-02 14:11:10 -07:00
return validFile.test(name);
}
2021-12-03 23:49:15 +01:00
export function getUpdateFileName(
info: JSONUpdateSchema,
platform: typeof process.platform,
arch: typeof process.arch
): string {
2019-08-02 14:11:10 -07:00
if (!info || !info.path) {
throw new Error('getUpdateFileName: No path present in YAML file');
}
2021-12-03 23:49:15 +01:00
let path: string | undefined;
if (platform === 'darwin') {
const { files } = info;
const candidates = files.filter(
({ url }) => url.includes(arch) && url.endsWith('.zip')
);
if (candidates.length === 1) {
path = candidates[0].url;
}
}
path = path ?? info.path;
2019-08-02 14:11:10 -07:00
if (!isUpdateFileNameValid(path)) {
throw new Error(
`getUpdateFileName: Path '${path}' contains invalid characters`
);
}
return path;
}
2022-02-24 13:01:41 -08:00
function getSHA512(
info: JSONUpdateSchema,
fileName: string
): string | undefined {
if (!info || !info.files) {
throw new Error('getSHA512: No files present in YAML file');
}
const foundFile = info.files.find(file => file.url === fileName);
return foundFile?.sha512;
}
function getSize(info: JSONUpdateSchema, fileName: string): number {
if (!info || !info.files) {
2022-02-24 13:01:41 -08:00
throw new Error('getSize: No files present in YAML file');
}
const foundFile = info.files.find(file => file.url === fileName);
return Number(foundFile?.size) || 0;
}
export function parseYaml(yaml: string): JSONUpdateSchema {
return safeLoad(yaml, { schema: FAILSAFE_SCHEMA, json: true });
}
async function getUpdateYaml(): Promise<string> {
const targetUrl = getUpdateCheckUrl();
2021-10-06 09:25:22 -07:00
const body = await got(targetUrl, getGotOptions()).text();
if (!body) {
throw new Error('Got unexpected response back from update check');
}
2021-10-06 09:25:22 -07:00
return body;
}
function getBaseTempDir() {
// We only use tmpdir() when this code is run outside of an Electron app (as in: tests)
return app ? getTempPath(app.getPath('userData')) : tmpdir();
}
2020-09-16 12:31:05 -07:00
export async function createTempDir(): Promise<string> {
const baseTempDir = getBaseTempDir();
const uniqueName = getGuid();
const targetDir = join(baseTempDir, uniqueName);
await mkdirpPromise(targetDir);
return targetDir;
}
2022-02-24 13:01:41 -08:00
function getUpdateCacheDir() {
// We only use tmpdir() when this code is run outside of an Electron app (as in: tests)
return app ? getUpdateCachePath(app.getPath('userData')) : tmpdir();
}
export async function createUpdateCacheDirIfNeeded(): Promise<string> {
const targetDir = getUpdateCacheDir();
await mkdirpPromise(targetDir);
return targetDir;
}
2020-09-16 12:31:05 -07:00
export async function deleteTempDir(targetDir: string): Promise<void> {
const pathInfo = statSync(targetDir);
if (!pathInfo.isDirectory()) {
throw new Error(
`deleteTempDir: Cannot delete path '${targetDir}' because it is not a directory`
);
}
const baseTempDir = getBaseTempDir();
if (!isPathInside(targetDir, baseTempDir)) {
throw new Error(
`deleteTempDir: Cannot delete path '${targetDir}' since it is not within base temp dir`
);
}
await rimrafPromise(targetDir);
}
2020-09-16 12:31:05 -07:00
export function getCliOptions<T>(options: ParserConfiguration['options']): T {
const parser = createParser({ options });
const cliOptions = parser.parse(process.argv);
if (cliOptions.help) {
const help = parser.help().trimRight();
console.log(help);
process.exit(0);
}
2021-11-11 16:43:05 -06:00
return cliOptions as unknown as T;
}