mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-01 23:30:02 +08:00
Protect desktop release credentials and rotate runtime trust key
This commit is contained in:
@@ -4,16 +4,14 @@ name: Desktop Notarization Diagnostics
|
||||
# publishing. Only DeepTutor submissions are included in the saved reports.
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches: [dev]
|
||||
paths:
|
||||
- .github/workflows/desktop-notarization-diagnostics.yml
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
inspect:
|
||||
if: github.ref_type == 'tag' && startsWith(github.ref_name, 'v')
|
||||
environment: desktop-release
|
||||
runs-on: macos-15
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
|
||||
52
.github/workflows/desktop-release.yml
vendored
52
.github/workflows/desktop-release.yml
vendored
@@ -7,18 +7,16 @@ name: Desktop Release
|
||||
#
|
||||
# Pull requests that touch apps/desktop build and smoke-test all three without
|
||||
# publishing; they keep the downloads as workflow artifacts for 7 days.
|
||||
# Pushes to dev and manual validation runs exercise configured signing without
|
||||
# publishing. Pull requests always use disposable runtime signing keys.
|
||||
# Branch and pull request builds use disposable keys. Version tags use the
|
||||
# protected desktop-release environment for signed validation and publishing.
|
||||
#
|
||||
# Secrets:
|
||||
# desktop-release environment secrets:
|
||||
# DESKTOP_RUNTIME_SIGNING_KEY (required for releases) Ed25519 PEM matching
|
||||
# apps/desktop/assets/runtime-public.pem.
|
||||
# Pull requests always seal with a throwaway key.
|
||||
# APPLE_CERTIFICATE_P12, APPLE_CERTIFICATE_PASSWORD, APPLE_SIGNING_IDENTITY,
|
||||
# APPLE_ID, APPLE_TEAM_ID, APPLE_APP_PASSWORD
|
||||
# (optional) Developer ID signing and
|
||||
# notarization. Without them the Mac apps are
|
||||
# signed ad hoc and Gatekeeper warns on first open.
|
||||
# Required for signed and notarized Mac tag builds.
|
||||
# The Windows build is unsigned; SmartScreen warns on first open.
|
||||
|
||||
on:
|
||||
@@ -45,7 +43,7 @@ on:
|
||||
default: false
|
||||
|
||||
env:
|
||||
RELEASE_TAG: ${{ inputs.tag || github.event.release.tag_name }}
|
||||
RELEASE_TAG: ${{ github.ref_type == 'tag' && github.ref_name || '' }}
|
||||
PUBLISH: ${{ github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && inputs.publish) }}
|
||||
|
||||
permissions:
|
||||
@@ -71,21 +69,34 @@ jobs:
|
||||
- target: windows-x64
|
||||
name: Open DeepTutor for Windows
|
||||
runner: windows-2025
|
||||
environment: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && 'desktop-release' || 'desktop-validation' }}
|
||||
runs-on: ${{ matrix.runner }}
|
||||
timeout-minutes: 120
|
||||
env:
|
||||
HAS_DEVELOPER_ID: ${{ secrets.APPLE_CERTIFICATE_P12 != '' }}
|
||||
HAS_NOTARY: ${{ secrets.APPLE_ID != '' }}
|
||||
HAS_RUNTIME_KEY: ${{ github.event_name != 'pull_request' && secrets.DESKTOP_RUNTIME_SIGNING_KEY != '' }}
|
||||
HAS_DEVELOPER_ID: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && secrets.APPLE_CERTIFICATE_P12 != '' }}
|
||||
HAS_NOTARY: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && secrets.APPLE_ID != '' }}
|
||||
HAS_RUNTIME_KEY: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && secrets.DESKTOP_RUNTIME_SIGNING_KEY != '' }}
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
working-directory: apps/desktop
|
||||
steps:
|
||||
- name: Check publishing ref
|
||||
if: env.PUBLISH == 'true'
|
||||
working-directory: ${{ runner.temp }}
|
||||
env:
|
||||
REQUESTED_TAG: ${{ inputs.tag || github.event.release.tag_name }}
|
||||
run: |
|
||||
[[ "$RELEASE_TAG" == v* && "$RELEASE_TAG" == "$REQUESTED_TAG" ]] || {
|
||||
echo "Select the same version tag as the workflow ref and release tag." >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ env.PUBLISH == 'true' && env.RELEASE_TAG || '' }}
|
||||
ref: ${{ github.sha }}
|
||||
persist-credentials: false
|
||||
|
||||
- name: Verify release tag matches deeptutor/__version__.py
|
||||
if: env.PUBLISH == 'true'
|
||||
@@ -98,7 +109,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Verify signing configuration
|
||||
if: github.event_name != 'pull_request'
|
||||
if: github.ref_type == 'tag' && startsWith(github.ref_name, 'v')
|
||||
env:
|
||||
HAS_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD != '' }}
|
||||
HAS_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY != '' }}
|
||||
@@ -106,14 +117,11 @@ jobs:
|
||||
HAS_NOTARY_PASSWORD: ${{ secrets.APPLE_APP_PASSWORD != '' }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
[ "$HAS_RUNTIME_KEY" = "true" ] || { echo "DESKTOP_RUNTIME_SIGNING_KEY is required for signed validation and releases." >&2; exit 1; }
|
||||
[ "$HAS_RUNTIME_KEY" = "true" ] || { echo "Missing release runtime signing key." >&2; exit 1; }
|
||||
if [ "$RUNNER_OS" = "macOS" ]; then
|
||||
if [ "$HAS_DEVELOPER_ID" = "true" ] || [ "$HAS_SIGNING_IDENTITY" = "true" ] || [ "$HAS_CERTIFICATE_PASSWORD" = "true" ]; then
|
||||
[ "$HAS_DEVELOPER_ID" = "true" ] && [ "$HAS_SIGNING_IDENTITY" = "true" ] && [ "$HAS_CERTIFICATE_PASSWORD" = "true" ] || { echo "Developer ID signing configuration is incomplete." >&2; exit 1; }
|
||||
fi
|
||||
if [ "$HAS_NOTARY" = "true" ] || [ "$HAS_NOTARY_PASSWORD" = "true" ]; then
|
||||
[ "$HAS_DEVELOPER_ID" = "true" ] && [ "$HAS_NOTARY" = "true" ] && [ "$HAS_TEAM_ID" = "true" ] && [ "$HAS_NOTARY_PASSWORD" = "true" ] || { echo "Apple notarization configuration is incomplete." >&2; exit 1; }
|
||||
fi
|
||||
for configured in "$HAS_DEVELOPER_ID" "$HAS_SIGNING_IDENTITY" "$HAS_CERTIFICATE_PASSWORD" "$HAS_NOTARY" "$HAS_TEAM_ID" "$HAS_NOTARY_PASSWORD"; do
|
||||
[ "$configured" = "true" ] || { echo "Missing release signing or notarization credential." >&2; exit 1; }
|
||||
done
|
||||
fi
|
||||
|
||||
- uses: actions/setup-node@v4
|
||||
@@ -154,8 +162,8 @@ jobs:
|
||||
|
||||
- name: Build the bundled runtime
|
||||
env:
|
||||
OPEN_DEEPTUTOR_SIGNING_KEY: ${{ github.event_name != 'pull_request' && secrets.DESKTOP_RUNTIME_SIGNING_KEY || '' }}
|
||||
APPLE_SIGNING_IDENTITY: ${{ github.event_name != 'pull_request' && secrets.APPLE_SIGNING_IDENTITY || '' }}
|
||||
OPEN_DEEPTUTOR_SIGNING_KEY: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && secrets.DESKTOP_RUNTIME_SIGNING_KEY || '' }}
|
||||
APPLE_SIGNING_IDENTITY: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && secrets.APPLE_SIGNING_IDENTITY || '' }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if [ "$HAS_RUNTIME_KEY" = "true" ]; then
|
||||
@@ -172,7 +180,7 @@ jobs:
|
||||
|
||||
- name: Package
|
||||
env:
|
||||
APPLE_SIGNING_IDENTITY: ${{ github.event_name != 'pull_request' && secrets.APPLE_SIGNING_IDENTITY || '' }}
|
||||
APPLE_SIGNING_IDENTITY: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && secrets.APPLE_SIGNING_IDENTITY || '' }}
|
||||
run: npm run package -- --target ${{ matrix.target }}
|
||||
|
||||
- name: Verify Windows installation and app lifecycle
|
||||
|
||||
@@ -121,20 +121,15 @@ the release as complete.
|
||||
`.github/workflows/desktop-release.yml` runs when a DeepTutor GitHub Release is
|
||||
published. For each target it verifies the tag matches
|
||||
`deeptutor/__version__.py`, builds and smoke-tests the runtime, packages the
|
||||
app and attaches the downloads to the release. It needs the
|
||||
`DESKTOP_RUNTIME_SIGNING_KEY` secret; Developer ID signing and notarization run
|
||||
when the Apple secrets listed in the workflow are present. Pull requests that
|
||||
touch `apps/desktop` run the same builds with a throwaway key and keep the
|
||||
downloads as workflow artifacts instead of publishing them.
|
||||
app and attaches the downloads to the release. Signed builds read the runtime
|
||||
key and Apple signing and notarization credentials from the protected
|
||||
`desktop-release` environment after deployment approval.
|
||||
|
||||
Pushes to `dev` that change the desktop app also build all three targets with
|
||||
the configured runtime key and Apple signing credentials, keeping the results
|
||||
as workflow artifacts without publishing a release. Manual runs default to the
|
||||
same validation mode on the selected branch. To attach downloads to an existing
|
||||
release, explicitly enable `publish` and supply its tag. GitHub only exposes the
|
||||
manual entry point after the workflow reaches the default branch. Apple
|
||||
notarization additionally needs `APPLE_ID` and `APPLE_APP_PASSWORD`; a Developer
|
||||
ID signature alone does not mean the app is notarized. Windows Authenticode
|
||||
Pull requests, pushes to `dev` and manual branch validations use disposable
|
||||
runtime keys. To validate signing, dispatch the workflow on a version tag.
|
||||
To attach downloads to an existing release, select that same tag as the
|
||||
workflow ref and the tag input, and enable `publish`. The notarization
|
||||
diagnostics workflow also runs manually on version tags. Windows Authenticode
|
||||
signing is not configured by this workflow.
|
||||
|
||||
## Core updates
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MCowBQYDK2VwAyEAk7aCNtMExO4fy9WktsQU/cyhioYTSC6f9QOjtAk2IF0=
|
||||
MCowBQYDK2VwAyEAWr7LxwSX7g8M/wUrkuDILFGpKDuiijEhXFKK6tmDJu0=
|
||||
-----END PUBLIC KEY-----
|
||||
|
||||
@@ -12,6 +12,8 @@ export function canonicalManifest(manifest) {
|
||||
return JSON.stringify(Object.fromEntries(Object.entries(manifest).filter(([key]) => key !== 'signature').sort(([a], [b]) => a.localeCompare(b))));
|
||||
}
|
||||
|
||||
class InvalidRuntimeSignature extends Error {}
|
||||
|
||||
export function verifyManifest(manifest, publicKey, architecture = process.arch, platform = process.platform) {
|
||||
if (!manifest || manifest.formatVersion !== 1 || manifest.protocolVersion !== 1
|
||||
|| manifest.platform !== platform || manifest.arch !== architecture
|
||||
@@ -19,7 +21,7 @@ export function verifyManifest(manifest, publicKey, architecture = process.arch,
|
||||
|| !/^[a-zA-Z0-9][a-zA-Z0-9.+_-]*\.tar\.gz$/.test(manifest.archive)
|
||||
|| !/^[a-f0-9]{64}$/.test(manifest.sha256)) throw new Error('This core bundle is not compatible with this app or this computer');
|
||||
if (!verify(null, Buffer.from(canonicalManifest(manifest)), publicKey, Buffer.from(manifest.signature ?? '', 'base64'))) {
|
||||
throw new Error('The core bundle signature is invalid');
|
||||
throw new InvalidRuntimeSignature('The core bundle signature is invalid');
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
@@ -53,7 +55,14 @@ export function newerRuntime(candidate, current) {
|
||||
*/
|
||||
export function startupRuntime({ saved, seed, transaction, publicKey }) {
|
||||
if (!saved) return {};
|
||||
const current = verifyManifest(saved, publicKey);
|
||||
let current;
|
||||
try { current = verifyManifest(saved, publicKey); }
|
||||
catch (error) {
|
||||
// A shell with a rotated key can recover only from its own trusted seed.
|
||||
if (!(error instanceof InvalidRuntimeSignature) || !seed) throw error;
|
||||
verifyManifest(seed, publicKey);
|
||||
return { replaceUntrusted: true };
|
||||
}
|
||||
let bundled = null;
|
||||
let seedError;
|
||||
if (seed) {
|
||||
|
||||
@@ -228,9 +228,10 @@ async function startupManifest() {
|
||||
if (!saved) return { manifestFile: seed };
|
||||
// A missing or unreadable seed must not stop the installed core from starting.
|
||||
const bundled = await readJSON(seed).catch(error => { console.error(error); return null; });
|
||||
const { current, upgrade, seedError } = startupRuntime({
|
||||
const { current, upgrade, seedError, replaceUntrusted } = startupRuntime({
|
||||
saved, seed: bundled, transaction: await readJSON(transactionFile), publicKey,
|
||||
});
|
||||
if (replaceUntrusted) return { manifestFile: seed, replaceUntrusted: true };
|
||||
if (seedError) console.error(seedError);
|
||||
const installed = path.join(userRoot, 'runtimes', current.runtimeId, 'installed.json');
|
||||
return upgrade ? { manifestFile: seed, previous: current, installed } : { manifestFile: installed };
|
||||
@@ -244,7 +245,7 @@ async function boot() {
|
||||
status({ phase: 'starting', step: 'starting' });
|
||||
await stopWorker();
|
||||
await recoverInterruptedUpgrade();
|
||||
const { manifestFile, previous, installed } = await startupManifest();
|
||||
const { manifestFile, previous, installed, replaceUntrusted } = await startupManifest();
|
||||
try { active = await installBundle(manifestFile, userRoot, publicKey, step => status({ step })); }
|
||||
catch (error) {
|
||||
if (!previous) throw error;
|
||||
@@ -253,7 +254,7 @@ async function boot() {
|
||||
active = await installBundle(installed, userRoot, publicKey, step => status({ step }));
|
||||
}
|
||||
if (quitting) return;
|
||||
if (previous && active.manifest.runtimeId !== previous.runtimeId) {
|
||||
if (replaceUntrusted || (previous && active.manifest.runtimeId !== previous.runtimeId)) {
|
||||
// The app was installed over an older core: switch to the bundled one with the
|
||||
// same backup and rollback as a manual update.
|
||||
status({ phase: 'starting', step: 'updating' });
|
||||
@@ -265,7 +266,7 @@ async function boot() {
|
||||
if (error.code !== 'ENOENT') throw error;
|
||||
backup = undefined;
|
||||
}
|
||||
transaction = { phase: 'testing', previous, candidate: active.manifest, backup };
|
||||
transaction = { phase: 'testing', previous: previous ?? null, candidate: active.manifest, backup };
|
||||
await writeJSON(transactionFile, transaction);
|
||||
}
|
||||
const ready = await launchRuntime(active);
|
||||
|
||||
@@ -64,6 +64,11 @@ test('boot keeps the saved core unless the app ships a newer signed one that has
|
||||
assert.deepEqual(startupRuntime({ saved, seed, publicKey }), { current: saved, upgrade: true, seedError: undefined });
|
||||
assert.equal(startupRuntime({ saved: seed, seed: saved, publicKey }).upgrade, false);
|
||||
assert.equal(startupRuntime({ saved, seed: null, publicKey }).upgrade, false);
|
||||
const retired = generateKeyPairSync('ed25519');
|
||||
const old = { ...saved, signature: sign(null, Buffer.from(canonicalManifest(saved)), retired.privateKey).toString('base64') };
|
||||
assert.deepEqual(startupRuntime({ saved: old, seed, publicKey }), { replaceUntrusted: true });
|
||||
assert.throws(() => startupRuntime({ saved: old, seed: old, publicKey }), /signature/);
|
||||
assert.throws(() => startupRuntime({ saved: old, seed: null, publicKey }), /signature/);
|
||||
const tampered = startupRuntime({ saved, seed: { ...seed, coreVersion: '9.0.0' }, publicKey });
|
||||
assert.equal(tampered.upgrade, false);
|
||||
assert.match(tampered.seedError.message, /signature/);
|
||||
|
||||
Reference in New Issue
Block a user