From 1da7ecd6b5f4294dbffd7d723a4c92f66e6ede6f Mon Sep 17 00:00:00 2001 From: Jasper Date: Wed, 16 Sep 2026 13:42:00 +0000 Subject: [PATCH] fix(desktop): create updater staging atomically (#11741) --- .../src/utils/githubUpdater.staging.test.ts | 75 +++++++++++++++++++ ui/desktop/src/utils/githubUpdater.ts | 7 +- 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 ui/desktop/src/utils/githubUpdater.staging.test.ts diff --git a/ui/desktop/src/utils/githubUpdater.staging.test.ts b/ui/desktop/src/utils/githubUpdater.staging.test.ts new file mode 100644 index 000000000..8b9fa7d4e --- /dev/null +++ b/ui/desktop/src/utils/githubUpdater.staging.test.ts @@ -0,0 +1,75 @@ +import * as fs from 'node:fs/promises'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { afterEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('./logger', () => ({ + default: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +import { GitHubUpdater } from './githubUpdater'; + +const cleanupPaths = new Set(); + +afterEach(async () => { + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + await Promise.all( + [...cleanupPaths].map((entry) => fs.rm(entry, { recursive: true, force: true })) + ); + cleanupPaths.clear(); +}); + +describe('GitHubUpdater download staging', () => { + it.skipIf(process.platform === 'win32')( + 'does not adopt a predictable directory or follow a pre-positioned archive symlink', + async () => { + const tempRoot = await fs.realpath(os.tmpdir()); + const workspace = await fs.mkdtemp(path.join(tempRoot, 'goose-updater-race-test-')); + const victimPath = path.join(workspace, 'victim.txt'); + const version = '9.9.9'; + const fixedNow = Date.now() + process.pid; + const predictableDir = path.join(tempRoot, `goose-update-${version}-${fixedNow}`); + const predictableArchive = path.join(predictableDir, `Goose-${version}.zip`); + cleanupPaths.add(workspace); + cleanupPaths.add(predictableDir); + + await fs.writeFile(victimPath, 'do not overwrite'); + await fs.mkdir(predictableDir, { mode: 0o777 }); + await fs.symlink(victimPath, predictableArchive); + vi.spyOn(Date, 'now').mockReturnValue(fixedNow); + vi.stubGlobal( + 'fetch', + vi.fn( + async () => + new Response(new Uint8Array([0x50, 0x4b, 0x03, 0x04]), { + status: 200, + headers: { 'content-length': '4' }, + }) + ) + ); + + const result = await new GitHubUpdater().downloadUpdate( + 'https://example.invalid/Goose.zip', + version + ); + expect(result.success).toBe(true); + expect(result.downloadPath).toBeDefined(); + expect(result.extractedPath).toBeDefined(); + + const stagingDir = result.extractedPath!; + const archivePath = result.downloadPath!; + cleanupPaths.add(stagingDir); + + expect(await fs.readFile(victimPath, 'utf8')).toBe('do not overwrite'); + expect(stagingDir).not.toBe(predictableDir); + expect(path.dirname(archivePath)).toBe(stagingDir); + expect((await fs.stat(stagingDir)).mode & 0o777).toBe(0o700); + + const archiveStat = await fs.lstat(archivePath); + expect(archiveStat.isFile()).toBe(true); + expect(archiveStat.mode & 0o777).toBe(0o600); + expect(await fs.readFile(archivePath)).toEqual(Buffer.from([0x50, 0x4b, 0x03, 0x04])); + } + ); +}); diff --git a/ui/desktop/src/utils/githubUpdater.ts b/ui/desktop/src/utils/githubUpdater.ts index 99c93aa35..025f9acb1 100644 --- a/ui/desktop/src/utils/githubUpdater.ts +++ b/ui/desktop/src/utils/githubUpdater.ts @@ -679,13 +679,14 @@ export class GitHubUpdater { const buffer = Buffer.concat(chunks.map((chunk) => Buffer.from(chunk))); log.info(`GitHubUpdater: Buffer created - ${buffer.length} bytes`); - const stagingDir = path.join(os.tmpdir(), `goose-update-${latestVersion}-${Date.now()}`); - await fs.mkdir(stagingDir, { recursive: true }); + const stagingDir = await fs.mkdtemp( + path.join(await fs.realpath(os.tmpdir()), 'goose-update-') + ); const fileName = `${this.bundleName}-${latestVersion}.zip`; const downloadPath = path.join(stagingDir, fileName); log.info(`GitHubUpdater: Writing file to ${downloadPath}...`); - await fs.writeFile(downloadPath, buffer); + await fs.writeFile(downloadPath, buffer, { flag: 'wx', mode: 0o600 }); const totalDuration = Date.now() - downloadStartTime; log.info(`=== GitHubUpdater: DOWNLOAD COMPLETE in ${totalDuration}ms ===`);