mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 13:49:40 +08:00
fix(desktop): compare runtime signing keys independently of PEM line endings
This commit is contained in:
10
apps/desktop/scripts/lib/signing.mjs
Normal file
10
apps/desktop/scripts/lib/signing.mjs
Normal 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.');
|
||||
}
|
||||
}
|
||||
@@ -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}`;
|
||||
|
||||
17
apps/desktop/test/signing.test.mjs
Normal file
17
apps/desktop/test/signing.test.mjs
Normal 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/);
|
||||
});
|
||||
Reference in New Issue
Block a user