fix(desktop): compare runtime signing keys independently of PEM line endings

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-30 22:14:57 +08:00
parent ed2416a4a4
commit 40c051b731
3 changed files with 30 additions and 5 deletions

View File

@@ -0,0 +1,10 @@
import { createPublicKey } from 'node:crypto';
/** Compare key material, independent of PEM whitespace and checkout line endings. */
export function assertSigningKey(privateKey, publicKey) {
const derived = createPublicKey(privateKey).export({ type: 'spki', format: 'der' });
const expected = createPublicKey(publicKey).export({ type: 'spki', format: 'der' });
if (!derived.equals(expected)) {
throw new Error('The signing key does not match assets/runtime-public.pem; the shipped app would reject this core.');
}
}

View File

@@ -1,10 +1,11 @@
import { createPrivateKey, createPublicKey, generateKeyPairSync, sign } from 'node:crypto';
import { createPrivateKey, generateKeyPairSync, sign } from 'node:crypto';
import { mkdir, readFile, rm, writeFile, access } from 'node:fs/promises';
import { execFileSync } from 'node:child_process';
import path from 'node:path';
import { canonicalManifest, sha256 } from '../src/bundles.mjs';
import { systemTar } from '../src/platform.mjs';
import { appRoot, loadTarget } from './lib/target.mjs';
import { assertSigningKey } from './lib/signing.mjs';
// One signing identity covers every platform; the manifest binds platform and architecture.
const target = await loadTarget();
@@ -26,10 +27,7 @@ catch {
await writeFile(publicKeyFile, keys.publicKey.export({ type: 'spki', format: 'pem' }));
}
const privateKey = createPrivateKey(await readFile(keyFile));
const derived = createPublicKey(privateKey).export({ type: 'spki', format: 'pem' }).trim();
if (derived !== (await readFile(publicKeyFile, 'utf8')).trim()) {
throw new Error('The signing key does not match assets/runtime-public.pem; the shipped app would reject this core.');
}
assertSigningKey(privateKey, await readFile(publicKeyFile));
const coreVersion = /__version__\s*=\s*['"]([^'"]+)/.exec(await readFile(path.join(target.runtime, 'core/deeptutor/__version__.py'), 'utf8'))[1];
const commit = (await readFile(path.join(target.runtime, 'BUILD_COMMIT'), 'utf8')).trim();
const runtimeId = `${coreVersion}-${commit.slice(0, 9)}-${target.arch}`;

View File

@@ -0,0 +1,17 @@
import assert from 'node:assert/strict';
import { generateKeyPairSync } from 'node:crypto';
import test from 'node:test';
import { assertSigningKey } from '../scripts/lib/signing.mjs';
test('release signing accepts the same public key checked out with Windows line endings', () => {
const { privateKey, publicKey } = generateKeyPairSync('ed25519');
const pem = publicKey.export({ type: 'spki', format: 'pem' });
assert.doesNotThrow(() => assertSigningKey(privateKey, pem));
assert.doesNotThrow(() => assertSigningKey(privateKey, pem.replaceAll('\n', '\r\n')));
});
test('release signing rejects a different public key', () => {
const { privateKey } = generateKeyPairSync('ed25519');
const { publicKey } = generateKeyPairSync('ed25519');
assert.throws(() => assertSigningKey(privateKey, publicKey.export({ type: 'spki', format: 'pem' })), /signing key does not match/);
});