import { createHash, generateKeyPairSync, sign } from 'node:crypto' import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { describe, expect, it } from 'vitest' import { UPDATE_SIGNING_PUBLIC_KEY, readSignedManifest, sha512Base64OfFile, verifyDownloadedUpdate, } from '../../src/main/update-signature' import { versionedManifestUrl } from '../../src/main/update-feed' const { privateKey, publicKey } = generateKeyPairSync('ed25519') const publicKeyPem = publicKey.export({ type: 'spki', format: 'pem' }).toString() function fixture(version = '1.10.0') { const dir = mkdtempSync(join(tmpdir(), 'd3ro-update-sig-')) const installerPath = join(dir, 'setup.exe') writeFileSync(installerPath, `installer bytes ${version}`) const sha512 = createHash('sha512').update(readFileSync(installerPath)).digest('base64') const manifest = [ `version: ${version}`, 'files:', ` - url: D3RO-Voice-Setup-${version}-x64.exe`, ` sha512: ${sha512}`, ' size: 22', `path: D3RO-Voice-Setup-${version}-x64.exe`, `sha512: ${sha512}`, "releaseDate: '2026-10-17T00:00:00.000Z'", '', ].join('\n') const signature = sign(null, Buffer.from(manifest), privateKey).toString('base64') return { installerPath, manifest, signature, sha512 } } function fetcher(files: Record) { return async (url: string): Promise => { const name = url.endsWith('.sig') ? 'sig' : 'manifest' const body = files[name] if (body === undefined) throw new Error(`HTTP 404 ${url}`) return body } } describe('update-signature', () => { it('reads version and sha512 from a correctly signed manifest', () => { const { manifest, signature, sha512 } = fixture() expect(readSignedManifest({ manifest, signature, publicKeyPem })).toEqual({ version: '1.10.0', sha512 }) }) it('rejects a tampered manifest, a foreign key, and a malformed signature', () => { const { manifest, signature } = fixture() expect(readSignedManifest({ manifest: manifest.replace('1.10.0', '9.9.9'), signature, publicKeyPem })).toBeNull() expect(readSignedManifest({ manifest, signature, publicKeyPem: UPDATE_SIGNING_PUBLIC_KEY })).toBeNull() expect(readSignedManifest({ manifest, signature: 'not-a-signature', publicKeyPem })).toBeNull() }) it('accepts the downloaded installer only when manifest, version and hash all match', async () => { const { installerPath, manifest, signature } = fixture() const base = { version: '1.10.0', installerPath, manifestUrl: 'https://feed/1.10.0/latest.yml', publicKeyPem } await expect(verifyDownloadedUpdate({ ...base, fetchText: fetcher({ manifest, sig: signature }) })).resolves.toBeNull() await expect( verifyDownloadedUpdate({ ...base, version: '1.11.0', fetchText: fetcher({ manifest, sig: signature }) }), ).resolves.toMatch(/버전/) writeFileSync(installerPath, 'swapped installer') await expect(verifyDownloadedUpdate({ ...base, fetchText: fetcher({ manifest, sig: signature }) })).resolves.toMatch( /sha512/, ) }) it('fails closed when the signature file is missing', async () => { const { installerPath, manifest } = fixture() await expect( verifyDownloadedUpdate({ version: '1.10.0', installerPath, manifestUrl: 'https://feed/1.10.0/latest.yml', publicKeyPem, fetchText: fetcher({ manifest }), }), ).resolves.toMatch(/받지 못함/) }) it('hashes files as base64 sha512 like electron-builder', async () => { const { installerPath, sha512 } = fixture() await expect(sha512Base64OfFile(installerPath)).resolves.toBe(sha512) }) it('embeds the same public key as release/desktop-update-public.pem', () => { const pem = readFileSync(join(__dirname, '../../../../release/desktop-update-public.pem'), 'utf8') expect(UPDATE_SIGNING_PUBLIC_KEY.trim()).toBe(pem.trim()) }) it('verifies against the immutable versioned path, not the mutable latest alias', () => { expect(versionedManifestUrl('1.10.0')).toBe( 'https://git.chanpaca.net/api/packages/yunchan/generic/d3ro-voice/1.10.0/latest.yml', ) }) it('keeps the electron-updater hook this verifier replaces', async () => { const { NsisUpdater } = await import('electron-updater') expect(typeof (NsisUpdater.prototype as unknown as { verifySignature?: unknown }).verifySignature).toBe('function') }) })